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. 245cabbc6bSPrashanth Sreenivasa * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 2514372834SHans Rosenfeld * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. 26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2745818ee1SMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved. 28c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 29c8811bd3SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com> 30ce1577b0SDave Eddy * Copyright 2017 Joyent, Inc. 311702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 320fb055e8SAndy Fiddaman * Copyright 2018 OmniOS Community Edition (OmniOSce) Association. 335aeb9474SGarrett D'Amore */ 34fa9e4066Sahrens 35fa9e4066Sahrens /* 363e30c24aSWill Andrews * SPA: Storage Pool Allocator 373e30c24aSWill Andrews * 38fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 39fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 40fa9e4066Sahrens * pool. 41fa9e4066Sahrens */ 42fa9e4066Sahrens 43fa9e4066Sahrens #include <sys/zfs_context.h> 44ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 45fa9e4066Sahrens #include <sys/spa_impl.h> 46fa9e4066Sahrens #include <sys/zio.h> 47fa9e4066Sahrens #include <sys/zio_checksum.h> 48fa9e4066Sahrens #include <sys/dmu.h> 49fa9e4066Sahrens #include <sys/dmu_tx.h> 50fa9e4066Sahrens #include <sys/zap.h> 51fa9e4066Sahrens #include <sys/zil.h> 52b24ab676SJeff Bonwick #include <sys/ddt.h> 53fa9e4066Sahrens #include <sys/vdev_impl.h> 545cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h> 555cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_mapping.h> 565cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_births.h> 57fa9e4066Sahrens #include <sys/metaslab.h> 5888ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 59fa9e4066Sahrens #include <sys/uberblock_impl.h> 60fa9e4066Sahrens #include <sys/txg.h> 61fa9e4066Sahrens #include <sys/avl.h> 625cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h> 63fa9e4066Sahrens #include <sys/dmu_traverse.h> 64b1b8ab34Slling #include <sys/dmu_objset.h> 65fa9e4066Sahrens #include <sys/unique.h> 66fa9e4066Sahrens #include <sys/dsl_pool.h> 67b1b8ab34Slling #include <sys/dsl_dataset.h> 68fa9e4066Sahrens #include <sys/dsl_dir.h> 69fa9e4066Sahrens #include <sys/dsl_prop.h> 70b1b8ab34Slling #include <sys/dsl_synctask.h> 71fa9e4066Sahrens #include <sys/fs/zfs.h> 72fa94a07fSbrendan #include <sys/arc.h> 73fa9e4066Sahrens #include <sys/callb.h> 7495173954Sek #include <sys/systeminfo.h> 75e7cbe64fSgw #include <sys/spa_boot.h> 76573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 773f9d6ad7SLin Ling #include <sys/dsl_scan.h> 78ad135b5dSChristopher Siden #include <sys/zfeature.h> 793b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 80770499e1SDan Kimmel #include <sys/abd.h> 81fa9e4066Sahrens 825679c89fSjv #ifdef _KERNEL 83dedec472SJack Meng #include <sys/bootprops.h> 8435a5a358SJonathan Adams #include <sys/callb.h> 8535a5a358SJonathan Adams #include <sys/cpupart.h> 8635a5a358SJonathan Adams #include <sys/pool.h> 8735a5a358SJonathan Adams #include <sys/sysdc.h> 8835a5a358SJonathan Adams #include <sys/zone.h> 895679c89fSjv #endif /* _KERNEL */ 905679c89fSjv 91990b4856Slling #include "zfs_prop.h" 92b7b97454Sperrin #include "zfs_comutil.h" 93990b4856Slling 943cb69f73SWill Andrews /* 953cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 963cb69f73SWill Andrews * should be retried. 973cb69f73SWill Andrews */ 985cabbc6bSPrashanth Sreenivasa int zfs_ccw_retry_interval = 300; 993cb69f73SWill Andrews 10035a5a358SJonathan Adams typedef enum zti_modes { 101ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 102ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 103ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 104ec94d322SAdam Leventhal ZTI_NMODES 10535a5a358SJonathan Adams } zti_modes_t; 106416e0cd8Sek 107ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 108ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 109ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1102e0c549eSJonathan Adams 111ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 112ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1132e0c549eSJonathan Adams 1142e0c549eSJonathan Adams typedef struct zio_taskq_info { 115ec94d322SAdam Leventhal zti_modes_t zti_mode; 11680eb36f2SGeorge Wilson uint_t zti_value; 117ec94d322SAdam Leventhal uint_t zti_count; 1182e0c549eSJonathan Adams } zio_taskq_info_t; 1192e0c549eSJonathan Adams 1202e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 12135a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1222e0c549eSJonathan Adams }; 1232e0c549eSJonathan Adams 12480eb36f2SGeorge Wilson /* 125ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 126ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 127ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 128ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 129ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 130ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 131ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 132ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 133ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 134ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 135ec94d322SAdam Leventhal * particular taskq is chosen at random. 136ec94d322SAdam Leventhal * 137ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 138ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 139ec94d322SAdam Leventhal * need to be handled with minimum delay. 14080eb36f2SGeorge Wilson */ 14180eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 14280eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 143ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1441b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 145ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 146ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 147ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 148ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1492e0c549eSJonathan Adams }; 1502e0c549eSJonathan Adams 1513b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1523b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 15389a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 154*86714001SSerapheim Dimitropoulos static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport); 155cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 156990b4856Slling 15769962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15835a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 15935a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 16035a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 16135a5a358SJonathan Adams 16235a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 16301f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16435a5a358SJonathan Adams 165e144c4e6SPavel Zakharov /* 166e144c4e6SPavel Zakharov * Report any spa_load_verify errors found, but do not fail spa_load. 167e144c4e6SPavel Zakharov * This is used by zdb to analyze non-idle pools. 168e144c4e6SPavel Zakharov */ 169e144c4e6SPavel Zakharov boolean_t spa_load_verify_dryrun = B_FALSE; 170e144c4e6SPavel Zakharov 17135a5a358SJonathan Adams /* 17235a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 17335a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 17435a5a358SJonathan Adams */ 17535a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 17635a5a358SJonathan Adams 1776f793812SPavel Zakharov /* 1786f793812SPavel Zakharov * For debugging purposes: print out vdev tree during pool import. 1796f793812SPavel Zakharov */ 1806f793812SPavel Zakharov boolean_t spa_load_print_vdev_tree = B_FALSE; 1816f793812SPavel Zakharov 1826f793812SPavel Zakharov /* 1836f793812SPavel Zakharov * A non-zero value for zfs_max_missing_tvds means that we allow importing 1846f793812SPavel Zakharov * pools with missing top-level vdevs. This is strictly intended for advanced 1856f793812SPavel Zakharov * pool recovery cases since missing data is almost inevitable. Pools with 1866f793812SPavel Zakharov * missing devices can only be imported read-only for safety reasons, and their 1876f793812SPavel Zakharov * fail-mode will be automatically set to "continue". 1886f793812SPavel Zakharov * 1896f793812SPavel Zakharov * With 1 missing vdev we should be able to import the pool and mount all 1906f793812SPavel Zakharov * datasets. User data that was not modified after the missing device has been 1916f793812SPavel Zakharov * added should be recoverable. This means that snapshots created prior to the 1926f793812SPavel Zakharov * addition of that device should be completely intact. 1936f793812SPavel Zakharov * 1946f793812SPavel Zakharov * With 2 missing vdevs, some datasets may fail to mount since there are 1956f793812SPavel Zakharov * dataset statistics that are stored as regular metadata. Some data might be 1966f793812SPavel Zakharov * recoverable if those vdevs were added recently. 1976f793812SPavel Zakharov * 1986f793812SPavel Zakharov * With 3 or more missing vdevs, the pool is severely damaged and MOS entries 1996f793812SPavel Zakharov * may be missing entirely. Chances of data recovery are very low. Note that 2006f793812SPavel Zakharov * there are also risks of performing an inadvertent rewind as we might be 2016f793812SPavel Zakharov * missing all the vdevs with the latest uberblocks. 2026f793812SPavel Zakharov */ 2036f793812SPavel Zakharov uint64_t zfs_max_missing_tvds = 0; 2046f793812SPavel Zakharov 2056f793812SPavel Zakharov /* 2066f793812SPavel Zakharov * The parameters below are similar to zfs_max_missing_tvds but are only 2076f793812SPavel Zakharov * intended for a preliminary open of the pool with an untrusted config which 2086f793812SPavel Zakharov * might be incomplete or out-dated. 2096f793812SPavel Zakharov * 2106f793812SPavel Zakharov * We are more tolerant for pools opened from a cachefile since we could have 2116f793812SPavel Zakharov * an out-dated cachefile where a device removal was not registered. 2126f793812SPavel Zakharov * We could have set the limit arbitrarily high but in the case where devices 2136f793812SPavel Zakharov * are really missing we would want to return the proper error codes; we chose 2146f793812SPavel Zakharov * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available 2156f793812SPavel Zakharov * and we get a chance to retrieve the trusted config. 2166f793812SPavel Zakharov */ 2176f793812SPavel Zakharov uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; 218*86714001SSerapheim Dimitropoulos 2196f793812SPavel Zakharov /* 2206f793812SPavel Zakharov * In the case where config was assembled by scanning device paths (/dev/dsks 2216f793812SPavel Zakharov * by default) we are less tolerant since all the existing devices should have 2226f793812SPavel Zakharov * been detected and we want spa_load to return the right error codes. 2236f793812SPavel Zakharov */ 2246f793812SPavel Zakharov uint64_t zfs_max_missing_tvds_scan = 0; 2256f793812SPavel Zakharov 226*86714001SSerapheim Dimitropoulos /* 227*86714001SSerapheim Dimitropoulos * Debugging aid that pauses spa_sync() towards the end. 228*86714001SSerapheim Dimitropoulos */ 229*86714001SSerapheim Dimitropoulos boolean_t zfs_pause_spa_sync = B_FALSE; 230*86714001SSerapheim Dimitropoulos 231990b4856Slling /* 232990b4856Slling * ========================================================================== 233990b4856Slling * SPA properties routines 234990b4856Slling * ========================================================================== 235990b4856Slling */ 236990b4856Slling 237990b4856Slling /* 238990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 239990b4856Slling */ 2409d82f4f6Slling static void 241990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 242990b4856Slling uint64_t intval, zprop_source_t src) 243990b4856Slling { 244990b4856Slling const char *propname = zpool_prop_to_name(prop); 245990b4856Slling nvlist_t *propval; 246990b4856Slling 2479d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2489d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 249990b4856Slling 2509d82f4f6Slling if (strval != NULL) 2519d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 2529d82f4f6Slling else 2539d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 254990b4856Slling 2559d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 256990b4856Slling nvlist_free(propval); 257990b4856Slling } 258990b4856Slling 259990b4856Slling /* 260990b4856Slling * Get property values from the spa configuration. 261990b4856Slling */ 2629d82f4f6Slling static void 263990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 264990b4856Slling { 2654263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 266ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2672e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 268990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 269c5904d13Seschrock spa_config_dirent_t *dp; 2702e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 271990b4856Slling 272e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 273e14bb325SJeff Bonwick 2744263d13fSGeorge Wilson if (rvd != NULL) { 275485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 276b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 277379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 278379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 279485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 280485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 281485bbbf5SGeorge Wilson size - alloc, src); 282*86714001SSerapheim Dimitropoulos spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, 283*86714001SSerapheim Dimitropoulos spa->spa_checkpoint_info.sci_dspace, src); 2844263d13fSGeorge Wilson 2852e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2862e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2872e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2882e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 289f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 290f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 291379c004dSEric Schrock 292485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 293379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 294379c004dSEric Schrock 295b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 296b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 297b24ab676SJeff Bonwick 298379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2994263d13fSGeorge Wilson rvd->vdev_state, src); 300379c004dSEric Schrock 301379c004dSEric Schrock version = spa_version(spa); 302379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 303379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 304379c004dSEric Schrock else 305379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 306379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 307379c004dSEric Schrock } 308990b4856Slling 309ad135b5dSChristopher Siden if (pool != NULL) { 310ad135b5dSChristopher Siden /* 311ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 312ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 313ad135b5dSChristopher Siden */ 3147fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 315ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 316c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 317c1379625SJustin T. Gibbs src); 318ad135b5dSChristopher Siden } else { 319ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 320ad135b5dSChristopher Siden NULL, 0, src); 321ad135b5dSChristopher Siden } 3227fd05ac4SMatthew Ahrens 3237fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 3247fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 325c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 326c1379625SJustin T. Gibbs src); 3277fd05ac4SMatthew Ahrens } else { 3287fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 3297fd05ac4SMatthew Ahrens NULL, 0, src); 3307fd05ac4SMatthew Ahrens } 331ad135b5dSChristopher Siden } 332ad135b5dSChristopher Siden 3339d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 334990b4856Slling 3358704186eSDan McDonald if (spa->spa_comment != NULL) { 3368704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 3378704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 3388704186eSDan McDonald } 3398704186eSDan McDonald 3409d82f4f6Slling if (spa->spa_root != NULL) 3419d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 3429d82f4f6Slling 0, ZPROP_SRC_LOCAL); 343990b4856Slling 344b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 345b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 346b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 347b5152584SMatthew Ahrens } else { 348b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 349b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 350b5152584SMatthew Ahrens } 351b5152584SMatthew Ahrens 352c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 353c5904d13Seschrock if (dp->scd_path == NULL) { 3549d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 355c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 356c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 3579d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 358c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 3592f8aaab3Seschrock } 3602f8aaab3Seschrock } 361990b4856Slling } 362990b4856Slling 363990b4856Slling /* 364990b4856Slling * Get zpool property values. 365990b4856Slling */ 366990b4856Slling int 367990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 368990b4856Slling { 369b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 370990b4856Slling zap_cursor_t zc; 371990b4856Slling zap_attribute_t za; 372990b4856Slling int err; 373990b4856Slling 3749d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 375990b4856Slling 376e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 377e14bb325SJeff Bonwick 378990b4856Slling /* 379990b4856Slling * Get properties from the spa config. 380990b4856Slling */ 3819d82f4f6Slling spa_prop_get_config(spa, nvp); 382990b4856Slling 383990b4856Slling /* If no pool property object, no more prop to get. */ 384afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 385990b4856Slling mutex_exit(&spa->spa_props_lock); 386990b4856Slling return (0); 387990b4856Slling } 388990b4856Slling 389990b4856Slling /* 390990b4856Slling * Get properties from the MOS pool property object. 391990b4856Slling */ 392990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 393990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 394990b4856Slling zap_cursor_advance(&zc)) { 395990b4856Slling uint64_t intval = 0; 396990b4856Slling char *strval = NULL; 397990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 398990b4856Slling zpool_prop_t prop; 399990b4856Slling 4004ae5f5f0SAlan Somers if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) 401990b4856Slling continue; 402990b4856Slling 403990b4856Slling switch (za.za_integer_length) { 404990b4856Slling case 8: 405990b4856Slling /* integer property */ 406990b4856Slling if (za.za_first_integer != 407990b4856Slling zpool_prop_default_numeric(prop)) 408990b4856Slling src = ZPROP_SRC_LOCAL; 409990b4856Slling 410990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 411990b4856Slling dsl_pool_t *dp; 412990b4856Slling dsl_dataset_t *ds = NULL; 413990b4856Slling 414990b4856Slling dp = spa_get_dsl(spa); 4153b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 416745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 417745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 4183b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 419990b4856Slling break; 420990b4856Slling } 421990b4856Slling 4229adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 423990b4856Slling KM_SLEEP); 424990b4856Slling dsl_dataset_name(ds, strval); 425745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 4263b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 427990b4856Slling } else { 428990b4856Slling strval = NULL; 429990b4856Slling intval = za.za_first_integer; 430990b4856Slling } 431990b4856Slling 4329d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 433990b4856Slling 434990b4856Slling if (strval != NULL) 4359adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 436990b4856Slling 437990b4856Slling break; 438990b4856Slling 439990b4856Slling case 1: 440990b4856Slling /* string property */ 441990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 442990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 443990b4856Slling za.za_name, 1, za.za_num_integers, strval); 444990b4856Slling if (err) { 445990b4856Slling kmem_free(strval, za.za_num_integers); 446990b4856Slling break; 447990b4856Slling } 4489d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 449990b4856Slling kmem_free(strval, za.za_num_integers); 450990b4856Slling break; 451990b4856Slling 452990b4856Slling default: 453990b4856Slling break; 454990b4856Slling } 455990b4856Slling } 456990b4856Slling zap_cursor_fini(&zc); 457990b4856Slling mutex_exit(&spa->spa_props_lock); 458990b4856Slling out: 459990b4856Slling if (err && err != ENOENT) { 460990b4856Slling nvlist_free(*nvp); 4619d82f4f6Slling *nvp = NULL; 462990b4856Slling return (err); 463990b4856Slling } 464990b4856Slling 465990b4856Slling return (0); 466990b4856Slling } 467990b4856Slling 468990b4856Slling /* 469990b4856Slling * Validate the given pool properties nvlist and modify the list 470990b4856Slling * for the property values to be set. 471990b4856Slling */ 472990b4856Slling static int 473990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 474990b4856Slling { 475990b4856Slling nvpair_t *elem; 476990b4856Slling int error = 0, reset_bootfs = 0; 477d5285caeSGeorge Wilson uint64_t objnum = 0; 478ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 479990b4856Slling 480990b4856Slling elem = NULL; 481990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 482990b4856Slling uint64_t intval; 483ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 484ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 485ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 486ad135b5dSChristopher Siden 487ad135b5dSChristopher Siden switch (prop) { 4884ae5f5f0SAlan Somers case ZPOOL_PROP_INVAL: 489ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 490be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 491ad135b5dSChristopher Siden break; 492ad135b5dSChristopher Siden } 493990b4856Slling 494ad135b5dSChristopher Siden /* 495ad135b5dSChristopher Siden * Sanitize the input. 496ad135b5dSChristopher Siden */ 497ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 498be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 499ad135b5dSChristopher Siden break; 500ad135b5dSChristopher Siden } 501990b4856Slling 502ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 503be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 504ad135b5dSChristopher Siden break; 505ad135b5dSChristopher Siden } 506ad135b5dSChristopher Siden 507ad135b5dSChristopher Siden if (intval != 0) { 508be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 509ad135b5dSChristopher Siden break; 510ad135b5dSChristopher Siden } 511ad135b5dSChristopher Siden 512ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 513ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 514be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 515ad135b5dSChristopher Siden break; 516ad135b5dSChristopher Siden } 517ad135b5dSChristopher Siden 518ad135b5dSChristopher Siden has_feature = B_TRUE; 519ad135b5dSChristopher Siden break; 520990b4856Slling 521990b4856Slling case ZPOOL_PROP_VERSION: 522990b4856Slling error = nvpair_value_uint64(elem, &intval); 523990b4856Slling if (!error && 524ad135b5dSChristopher Siden (intval < spa_version(spa) || 525ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 526ad135b5dSChristopher Siden has_feature)) 527be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 528990b4856Slling break; 529990b4856Slling 530990b4856Slling case ZPOOL_PROP_DELEGATION: 531990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 532d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 533573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 534990b4856Slling error = nvpair_value_uint64(elem, &intval); 535990b4856Slling if (!error && intval > 1) 536be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 537990b4856Slling break; 538990b4856Slling 539990b4856Slling case ZPOOL_PROP_BOOTFS: 54025f89ee2SJeff Bonwick /* 54125f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 54225f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 54325f89ee2SJeff Bonwick * the bootfs property cannot be set. 54425f89ee2SJeff Bonwick */ 545990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 546be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 547990b4856Slling break; 548990b4856Slling } 549990b4856Slling 550990b4856Slling /* 55115e6edf1Sgw * Make sure the vdev config is bootable 552990b4856Slling */ 55315e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 554be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 555990b4856Slling break; 556990b4856Slling } 557990b4856Slling 558990b4856Slling reset_bootfs = 1; 559990b4856Slling 560990b4856Slling error = nvpair_value_string(elem, &strval); 561990b4856Slling 562990b4856Slling if (!error) { 563ad135b5dSChristopher Siden objset_t *os; 564b5152584SMatthew Ahrens uint64_t propval; 56515e6edf1Sgw 566990b4856Slling if (strval == NULL || strval[0] == '\0') { 567990b4856Slling objnum = zpool_prop_default_numeric( 568990b4856Slling ZPOOL_PROP_BOOTFS); 569990b4856Slling break; 570990b4856Slling } 571990b4856Slling 572503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 573990b4856Slling break; 57415e6edf1Sgw 575b5152584SMatthew Ahrens /* 576b5152584SMatthew Ahrens * Must be ZPL, and its property settings 577b5152584SMatthew Ahrens * must be supported by GRUB (compression 578b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 579b5152584SMatthew Ahrens */ 580503ad85cSMatthew Ahrens 581503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 582be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5833b2aab18SMatthew Ahrens } else if ((error = 5843b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 58515e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 586b5152584SMatthew Ahrens &propval)) == 0 && 587b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 588b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 58915e6edf1Sgw } else { 59015e6edf1Sgw objnum = dmu_objset_id(os); 59115e6edf1Sgw } 592503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 593990b4856Slling } 594990b4856Slling break; 595e14bb325SJeff Bonwick 5960a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5970a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5980a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5990a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 600be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6010a4e9518Sgw 6020a4e9518Sgw /* 6030a4e9518Sgw * This is a special case which only occurs when 6040a4e9518Sgw * the pool has completely failed. This allows 6050a4e9518Sgw * the user to change the in-core failmode property 6060a4e9518Sgw * without syncing it out to disk (I/Os might 6070a4e9518Sgw * currently be blocked). We do this by returning 6080a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 6090a4e9518Sgw * into thinking we encountered a property validation 6100a4e9518Sgw * error. 6110a4e9518Sgw */ 612e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 6130a4e9518Sgw spa->spa_failmode = intval; 614be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 6150a4e9518Sgw } 6160a4e9518Sgw break; 6172f8aaab3Seschrock 6182f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6192f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 6202f8aaab3Seschrock break; 6212f8aaab3Seschrock 6222f8aaab3Seschrock if (strval[0] == '\0') 6232f8aaab3Seschrock break; 6242f8aaab3Seschrock 6252f8aaab3Seschrock if (strcmp(strval, "none") == 0) 6262f8aaab3Seschrock break; 6272f8aaab3Seschrock 6282f8aaab3Seschrock if (strval[0] != '/') { 629be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6302f8aaab3Seschrock break; 6312f8aaab3Seschrock } 6322f8aaab3Seschrock 6332f8aaab3Seschrock slash = strrchr(strval, '/'); 6342f8aaab3Seschrock ASSERT(slash != NULL); 6352f8aaab3Seschrock 6362f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6372f8aaab3Seschrock strcmp(slash, "/..") == 0) 638be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6392f8aaab3Seschrock break; 640b24ab676SJeff Bonwick 6418704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6428704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 6438704186eSDan McDonald break; 6448704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6458704186eSDan McDonald /* 6468704186eSDan McDonald * The kernel doesn't have an easy isprint() 6478704186eSDan McDonald * check. For this kernel check, we merely 6488704186eSDan McDonald * check ASCII apart from DEL. Fix this if 6498704186eSDan McDonald * there is an easy-to-use kernel isprint(). 6508704186eSDan McDonald */ 6518704186eSDan McDonald if (*check >= 0x7f) { 652be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6538704186eSDan McDonald break; 6548704186eSDan McDonald } 6558704186eSDan McDonald } 6568704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 6578704186eSDan McDonald error = E2BIG; 6588704186eSDan McDonald break; 6598704186eSDan McDonald 660b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 661b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 662be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 663b24ab676SJeff Bonwick else 664b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 665b24ab676SJeff Bonwick if (error == 0 && 666b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 667be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 668b24ab676SJeff Bonwick break; 669990b4856Slling } 670990b4856Slling 671990b4856Slling if (error) 672990b4856Slling break; 673990b4856Slling } 674990b4856Slling 675990b4856Slling if (!error && reset_bootfs) { 676990b4856Slling error = nvlist_remove(props, 677990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 678990b4856Slling 679990b4856Slling if (!error) { 680990b4856Slling error = nvlist_add_uint64(props, 681990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 682990b4856Slling } 683990b4856Slling } 684990b4856Slling 685990b4856Slling return (error); 686990b4856Slling } 687990b4856Slling 688379c004dSEric Schrock void 689379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 690379c004dSEric Schrock { 691379c004dSEric Schrock char *cachefile; 692379c004dSEric Schrock spa_config_dirent_t *dp; 693379c004dSEric Schrock 694379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 695379c004dSEric Schrock &cachefile) != 0) 696379c004dSEric Schrock return; 697379c004dSEric Schrock 698379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 699379c004dSEric Schrock KM_SLEEP); 700379c004dSEric Schrock 701379c004dSEric Schrock if (cachefile[0] == '\0') 702379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 703379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 704379c004dSEric Schrock dp->scd_path = NULL; 705379c004dSEric Schrock else 706379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 707379c004dSEric Schrock 708379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 709379c004dSEric Schrock if (need_sync) 710379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 711379c004dSEric Schrock } 712379c004dSEric Schrock 713990b4856Slling int 714990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 715990b4856Slling { 716990b4856Slling int error; 717ad135b5dSChristopher Siden nvpair_t *elem = NULL; 718379c004dSEric Schrock boolean_t need_sync = B_FALSE; 719990b4856Slling 720990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 721990b4856Slling return (error); 722990b4856Slling 723379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 724ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 725379c004dSEric Schrock 726f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 727f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 728f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 729379c004dSEric Schrock continue; 730379c004dSEric Schrock 7314ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { 732ad135b5dSChristopher Siden uint64_t ver; 733ad135b5dSChristopher Siden 734ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 735ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 736ad135b5dSChristopher Siden } else { 737ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 738ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 739ad135b5dSChristopher Siden need_sync = B_TRUE; 740ad135b5dSChristopher Siden } 741ad135b5dSChristopher Siden 742ad135b5dSChristopher Siden /* Save time if the version is already set. */ 743ad135b5dSChristopher Siden if (ver == spa_version(spa)) 744ad135b5dSChristopher Siden continue; 745ad135b5dSChristopher Siden 746ad135b5dSChristopher Siden /* 747ad135b5dSChristopher Siden * In addition to the pool directory object, we might 748ad135b5dSChristopher Siden * create the pool properties object, the features for 749ad135b5dSChristopher Siden * read object, the features for write object, or the 750ad135b5dSChristopher Siden * feature descriptions object. 751ad135b5dSChristopher Siden */ 7523b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 7537d46dc6cSMatthew Ahrens spa_sync_version, &ver, 7547d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 755ad135b5dSChristopher Siden if (error) 756ad135b5dSChristopher Siden return (error); 757ad135b5dSChristopher Siden continue; 758ad135b5dSChristopher Siden } 759ad135b5dSChristopher Siden 760379c004dSEric Schrock need_sync = B_TRUE; 761379c004dSEric Schrock break; 762379c004dSEric Schrock } 763379c004dSEric Schrock 764ad135b5dSChristopher Siden if (need_sync) { 7653b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7667d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 767ad135b5dSChristopher Siden } 768ad135b5dSChristopher Siden 769ad135b5dSChristopher Siden return (0); 770990b4856Slling } 771990b4856Slling 772990b4856Slling /* 773990b4856Slling * If the bootfs property value is dsobj, clear it. 774990b4856Slling */ 775990b4856Slling void 776990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 777990b4856Slling { 778990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 779990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 780990b4856Slling spa->spa_pool_props_object, 781990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 782990b4856Slling spa->spa_bootfs = 0; 783990b4856Slling } 784990b4856Slling } 785990b4856Slling 786dfbb9432SGeorge Wilson /*ARGSUSED*/ 787dfbb9432SGeorge Wilson static int 7883b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 789dfbb9432SGeorge Wilson { 7903b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7913b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 792dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 793dfbb9432SGeorge Wilson uint64_t vdev_state; 794dfbb9432SGeorge Wilson 795*86714001SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 796*86714001SSerapheim Dimitropoulos int error = (spa_has_checkpoint(spa)) ? 797*86714001SSerapheim Dimitropoulos ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 798*86714001SSerapheim Dimitropoulos return (SET_ERROR(error)); 799*86714001SSerapheim Dimitropoulos } 800*86714001SSerapheim Dimitropoulos 801dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 802dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 803dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 804dfbb9432SGeorge Wilson 805dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 806be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 807dfbb9432SGeorge Wilson 808dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 809dfbb9432SGeorge Wilson 810dfbb9432SGeorge Wilson return (0); 811dfbb9432SGeorge Wilson } 812dfbb9432SGeorge Wilson 813dfbb9432SGeorge Wilson static void 8143b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 815dfbb9432SGeorge Wilson { 8163b2aab18SMatthew Ahrens uint64_t *newguid = arg; 8173b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 818dfbb9432SGeorge Wilson uint64_t oldguid; 819dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 820dfbb9432SGeorge Wilson 821dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 822dfbb9432SGeorge Wilson 823dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 824dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 825dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 826dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 827dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 828dfbb9432SGeorge Wilson 82920128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 830dfbb9432SGeorge Wilson oldguid, *newguid); 831dfbb9432SGeorge Wilson } 832dfbb9432SGeorge Wilson 833e9103aaeSGarrett D'Amore /* 834e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 835e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 836e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 837e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 838e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 839e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 840e9103aaeSGarrett D'Amore * sysevent to update any watchers. 841e9103aaeSGarrett D'Amore */ 842e9103aaeSGarrett D'Amore int 843e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 844e9103aaeSGarrett D'Amore { 845dfbb9432SGeorge Wilson int error; 846dfbb9432SGeorge Wilson uint64_t guid; 847e9103aaeSGarrett D'Amore 8482c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 849dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 850dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 851e9103aaeSGarrett D'Amore 8523b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 8537d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 854e9103aaeSGarrett D'Amore 855dfbb9432SGeorge Wilson if (error == 0) { 8565cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 857ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 858dfbb9432SGeorge Wilson } 859e9103aaeSGarrett D'Amore 860dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 8612c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 862e9103aaeSGarrett D'Amore 863dfbb9432SGeorge Wilson return (error); 864e9103aaeSGarrett D'Amore } 865e9103aaeSGarrett D'Amore 866fa9e4066Sahrens /* 867fa9e4066Sahrens * ========================================================================== 868fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 869fa9e4066Sahrens * ========================================================================== 870fa9e4066Sahrens */ 871fa9e4066Sahrens 872ea8dc4b6Seschrock static int 873ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 874ea8dc4b6Seschrock { 875ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 876ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 877ea8dc4b6Seschrock int ret; 878ea8dc4b6Seschrock 879ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8807802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 881ea8dc4b6Seschrock 882ea8dc4b6Seschrock if (ret < 0) 883ea8dc4b6Seschrock return (-1); 884ea8dc4b6Seschrock else if (ret > 0) 885ea8dc4b6Seschrock return (1); 886ea8dc4b6Seschrock else 887ea8dc4b6Seschrock return (0); 888ea8dc4b6Seschrock } 889ea8dc4b6Seschrock 890ea8dc4b6Seschrock /* 891ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 892ea8dc4b6Seschrock * re-initializes them in the process. 893ea8dc4b6Seschrock */ 894ea8dc4b6Seschrock void 895ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 896ea8dc4b6Seschrock { 897ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 898ea8dc4b6Seschrock 899ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 900ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 901ea8dc4b6Seschrock 902ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 903ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 904ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 905ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 906ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 907ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 908ea8dc4b6Seschrock } 909ea8dc4b6Seschrock 910ec94d322SAdam Leventhal static void 911ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 912fa9e4066Sahrens { 913ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 914ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 915ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 916ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 917ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 918ec94d322SAdam Leventhal char name[32]; 9195aeb9474SGarrett D'Amore uint_t flags = 0; 92035a5a358SJonathan Adams boolean_t batch = B_FALSE; 921fa9e4066Sahrens 922ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 923ec94d322SAdam Leventhal tqs->stqs_count = 0; 924ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 925ec94d322SAdam Leventhal return; 926ec94d322SAdam Leventhal } 927fa9e4066Sahrens 928ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 929fa9e4066Sahrens 930ec94d322SAdam Leventhal tqs->stqs_count = count; 931ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 93235a5a358SJonathan Adams 93369962b56SMatthew Ahrens switch (mode) { 93469962b56SMatthew Ahrens case ZTI_MODE_FIXED: 93569962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 93669962b56SMatthew Ahrens value = MAX(value, 1); 93769962b56SMatthew Ahrens break; 938ec94d322SAdam Leventhal 93969962b56SMatthew Ahrens case ZTI_MODE_BATCH: 94069962b56SMatthew Ahrens batch = B_TRUE; 94169962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 94269962b56SMatthew Ahrens value = zio_taskq_batch_pct; 94369962b56SMatthew Ahrens break; 944ec94d322SAdam Leventhal 94569962b56SMatthew Ahrens default: 94669962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 94769962b56SMatthew Ahrens "spa_activate()", 94869962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 94969962b56SMatthew Ahrens break; 95069962b56SMatthew Ahrens } 951ec94d322SAdam Leventhal 95269962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 95369962b56SMatthew Ahrens taskq_t *tq; 954ec94d322SAdam Leventhal 955ec94d322SAdam Leventhal if (count > 1) { 956ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 957ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 958ec94d322SAdam Leventhal } else { 959ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 960ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 961ec94d322SAdam Leventhal } 962ec94d322SAdam Leventhal 963ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 964ec94d322SAdam Leventhal if (batch) 965ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 966ec94d322SAdam Leventhal 967ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 968ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 969ec94d322SAdam Leventhal } else { 97069962b56SMatthew Ahrens pri_t pri = maxclsyspri; 97169962b56SMatthew Ahrens /* 97269962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 97369962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 97469962b56SMatthew Ahrens * than the other taskqs. 97569962b56SMatthew Ahrens */ 97669962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 97769962b56SMatthew Ahrens pri--; 97869962b56SMatthew Ahrens 97969962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 980ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 981ec94d322SAdam Leventhal } 982ec94d322SAdam Leventhal 983ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 984ec94d322SAdam Leventhal } 985ec94d322SAdam Leventhal } 986ec94d322SAdam Leventhal 987ec94d322SAdam Leventhal static void 988ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 989ec94d322SAdam Leventhal { 990ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 991ec94d322SAdam Leventhal 992ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 993ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 994ec94d322SAdam Leventhal return; 995ec94d322SAdam Leventhal } 996ec94d322SAdam Leventhal 997ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 998ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 999ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 100035a5a358SJonathan Adams } 100135a5a358SJonathan Adams 1002ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 1003ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 1004ec94d322SAdam Leventhal } 100535a5a358SJonathan Adams 1006ec94d322SAdam Leventhal /* 1007ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 1008ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 1009ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 1010ec94d322SAdam Leventhal * the low bits of gethrtime(). 1011ec94d322SAdam Leventhal */ 1012ec94d322SAdam Leventhal void 1013ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 1014ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 1015ec94d322SAdam Leventhal { 1016ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1017ec94d322SAdam Leventhal taskq_t *tq; 1018ec94d322SAdam Leventhal 1019ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 1020ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 1021ec94d322SAdam Leventhal 1022ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 1023ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 1024ec94d322SAdam Leventhal } else { 1025ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 102635a5a358SJonathan Adams } 1027ec94d322SAdam Leventhal 1028ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 102935a5a358SJonathan Adams } 103035a5a358SJonathan Adams 103135a5a358SJonathan Adams static void 103235a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 103335a5a358SJonathan Adams { 1034e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1035e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1036ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 103735a5a358SJonathan Adams } 103835a5a358SJonathan Adams } 103935a5a358SJonathan Adams } 104035a5a358SJonathan Adams 104135a5a358SJonathan Adams #ifdef _KERNEL 104235a5a358SJonathan Adams static void 104335a5a358SJonathan Adams spa_thread(void *arg) 104435a5a358SJonathan Adams { 104535a5a358SJonathan Adams callb_cpr_t cprinfo; 10462e0c549eSJonathan Adams 104735a5a358SJonathan Adams spa_t *spa = arg; 104835a5a358SJonathan Adams user_t *pu = PTOU(curproc); 10492e0c549eSJonathan Adams 105035a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 105135a5a358SJonathan Adams spa->spa_name); 10522e0c549eSJonathan Adams 105335a5a358SJonathan Adams ASSERT(curproc != &p0); 105435a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 105535a5a358SJonathan Adams "zpool-%s", spa->spa_name); 105635a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 10572e0c549eSJonathan Adams 105835a5a358SJonathan Adams /* bind this thread to the requested psrset */ 105935a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 106035a5a358SJonathan Adams pool_lock(); 106135a5a358SJonathan Adams mutex_enter(&cpu_lock); 106235a5a358SJonathan Adams mutex_enter(&pidlock); 106335a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 106480eb36f2SGeorge Wilson 106535a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 106635a5a358SJonathan Adams 0, NULL, NULL) == 0) { 106735a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 106835a5a358SJonathan Adams } else { 106935a5a358SJonathan Adams cmn_err(CE_WARN, 107035a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 107135a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 107235a5a358SJonathan Adams } 107335a5a358SJonathan Adams 107435a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 107535a5a358SJonathan Adams mutex_exit(&pidlock); 107635a5a358SJonathan Adams mutex_exit(&cpu_lock); 107735a5a358SJonathan Adams pool_unlock(); 107835a5a358SJonathan Adams } 107935a5a358SJonathan Adams 108035a5a358SJonathan Adams if (zio_taskq_sysdc) { 108135a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 108235a5a358SJonathan Adams } 108335a5a358SJonathan Adams 108435a5a358SJonathan Adams spa->spa_proc = curproc; 108535a5a358SJonathan Adams spa->spa_did = curthread->t_did; 108635a5a358SJonathan Adams 108735a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108835a5a358SJonathan Adams 108935a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 109035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 109135a5a358SJonathan Adams 109235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 109335a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 109435a5a358SJonathan Adams 109535a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 109635a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 109735a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 109835a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 109935a5a358SJonathan Adams 110035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 110135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 110235a5a358SJonathan Adams spa->spa_proc = &p0; 110335a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 110435a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 110535a5a358SJonathan Adams 110635a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 110735a5a358SJonathan Adams lwp_exit(); 110835a5a358SJonathan Adams } 110935a5a358SJonathan Adams #endif 111035a5a358SJonathan Adams 111135a5a358SJonathan Adams /* 111235a5a358SJonathan Adams * Activate an uninitialized pool. 111335a5a358SJonathan Adams */ 111435a5a358SJonathan Adams static void 111535a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 111635a5a358SJonathan Adams { 111735a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 111835a5a358SJonathan Adams 111935a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 112035a5a358SJonathan Adams spa->spa_mode = mode; 112135a5a358SJonathan Adams 112235a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 112335a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 112435a5a358SJonathan Adams 112535a5a358SJonathan Adams /* Try to create a covering process */ 112635a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 112735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 112835a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 112935a5a358SJonathan Adams spa->spa_did = 0; 113035a5a358SJonathan Adams 113135a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 113235a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 113335a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 113435a5a358SJonathan Adams NULL, 0) == 0) { 113535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 113635a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 113735a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 113835a5a358SJonathan Adams &spa->spa_proc_lock); 11392e0c549eSJonathan Adams } 114035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 114135a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 114235a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 114335a5a358SJonathan Adams } else { 114435a5a358SJonathan Adams #ifdef _KERNEL 114535a5a358SJonathan Adams cmn_err(CE_WARN, 114635a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 114735a5a358SJonathan Adams spa->spa_name); 114835a5a358SJonathan Adams #endif 1149e14bb325SJeff Bonwick } 1150fa9e4066Sahrens } 115135a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 115235a5a358SJonathan Adams 115335a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 115435a5a358SJonathan Adams if (spa->spa_proc == &p0) { 115535a5a358SJonathan Adams spa_create_zio_taskqs(spa); 115635a5a358SJonathan Adams } 1157fa9e4066Sahrens 11585cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) 11595cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, 0); 11605cabbc6bSPrashanth Sreenivasa 1161e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1162e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1163bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1164bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1165e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1166e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1167fa9e4066Sahrens 1168b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1169fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1170ea8dc4b6Seschrock 1171ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1172ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1173ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1174ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1175ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1176ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1177fa9e4066Sahrens } 1178fa9e4066Sahrens 1179fa9e4066Sahrens /* 1180fa9e4066Sahrens * Opposite of spa_activate(). 1181fa9e4066Sahrens */ 1182fa9e4066Sahrens static void 1183fa9e4066Sahrens spa_deactivate(spa_t *spa) 1184fa9e4066Sahrens { 1185fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1186fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1187fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 118825f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1189fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1190fa9e4066Sahrens 1191bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1192bc9014e6SJustin Gibbs 1193fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1194fa9e4066Sahrens 1195e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1196bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1197e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1198fa9e4066Sahrens 1199e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1200e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1201ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1202e14bb325SJeff Bonwick } 1203fa9e4066Sahrens } 1204fa9e4066Sahrens 12055cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) { 12065cabbc6bSPrashanth Sreenivasa ASSERT3P(spa->spa_txg_zio[i], !=, NULL); 12075cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[i])); 12085cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = NULL; 12095cabbc6bSPrashanth Sreenivasa } 12105cabbc6bSPrashanth Sreenivasa 1211fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1212fa9e4066Sahrens spa->spa_normal_class = NULL; 1213fa9e4066Sahrens 12148654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 12158654d025Sperrin spa->spa_log_class = NULL; 12168654d025Sperrin 1217ea8dc4b6Seschrock /* 1218ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1219ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1220ea8dc4b6Seschrock */ 1221ea8dc4b6Seschrock spa_errlog_drain(spa); 1222ea8dc4b6Seschrock 1223ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1224ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1225ea8dc4b6Seschrock 1226fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 122735a5a358SJonathan Adams 122835a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 122935a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 123035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 123135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 123235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 123335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 123435a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 123535a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 123635a5a358SJonathan Adams } 123735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 123835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 123935a5a358SJonathan Adams } 124035a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 124135a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 124235a5a358SJonathan Adams 124335a5a358SJonathan Adams /* 124435a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 124535a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 124635a5a358SJonathan Adams * it. 124735a5a358SJonathan Adams */ 124835a5a358SJonathan Adams if (spa->spa_did != 0) { 124935a5a358SJonathan Adams thread_join(spa->spa_did); 125035a5a358SJonathan Adams spa->spa_did = 0; 125135a5a358SJonathan Adams } 1252fa9e4066Sahrens } 1253fa9e4066Sahrens 1254fa9e4066Sahrens /* 1255fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1256fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1257fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1258fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1259fa9e4066Sahrens */ 126099653d4eSeschrock static int 126199653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 126299653d4eSeschrock uint_t id, int atype) 1263fa9e4066Sahrens { 1264fa9e4066Sahrens nvlist_t **child; 1265573ca77eSGeorge Wilson uint_t children; 126699653d4eSeschrock int error; 1267fa9e4066Sahrens 126899653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 126999653d4eSeschrock return (error); 1270fa9e4066Sahrens 127199653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 127299653d4eSeschrock return (0); 1273fa9e4066Sahrens 1274e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1275e14bb325SJeff Bonwick &child, &children); 1276e14bb325SJeff Bonwick 1277e14bb325SJeff Bonwick if (error == ENOENT) 1278e14bb325SJeff Bonwick return (0); 1279e14bb325SJeff Bonwick 1280e14bb325SJeff Bonwick if (error) { 128199653d4eSeschrock vdev_free(*vdp); 128299653d4eSeschrock *vdp = NULL; 1283be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1284fa9e4066Sahrens } 1285fa9e4066Sahrens 1286573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 128799653d4eSeschrock vdev_t *vd; 128899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 128999653d4eSeschrock atype)) != 0) { 129099653d4eSeschrock vdev_free(*vdp); 129199653d4eSeschrock *vdp = NULL; 129299653d4eSeschrock return (error); 1293fa9e4066Sahrens } 1294fa9e4066Sahrens } 1295fa9e4066Sahrens 129699653d4eSeschrock ASSERT(*vdp != NULL); 129799653d4eSeschrock 129899653d4eSeschrock return (0); 1299fa9e4066Sahrens } 1300fa9e4066Sahrens 1301fa9e4066Sahrens /* 1302fa9e4066Sahrens * Opposite of spa_load(). 1303fa9e4066Sahrens */ 1304fa9e4066Sahrens static void 1305fa9e4066Sahrens spa_unload(spa_t *spa) 1306fa9e4066Sahrens { 130799653d4eSeschrock int i; 130899653d4eSeschrock 1309e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1310e14bb325SJeff Bonwick 13113ee8c80cSPavel Zakharov spa_load_note(spa, "UNLOADING"); 13123ee8c80cSPavel Zakharov 1313ea8dc4b6Seschrock /* 1314ea8dc4b6Seschrock * Stop async tasks. 1315ea8dc4b6Seschrock */ 1316ea8dc4b6Seschrock spa_async_suspend(spa); 1317ea8dc4b6Seschrock 1318fa9e4066Sahrens /* 1319fa9e4066Sahrens * Stop syncing. 1320fa9e4066Sahrens */ 1321fa9e4066Sahrens if (spa->spa_sync_on) { 1322fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1323fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1324fa9e4066Sahrens } 1325fa9e4066Sahrens 13268363e80aSGeorge Wilson /* 13278363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 13288363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 13298363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 13308363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 13318363e80aSGeorge Wilson */ 13328363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 13338363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13348363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 13358363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 13368363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 13378363e80aSGeorge Wilson } 13388363e80aSGeorge Wilson 1339fa9e4066Sahrens /* 1340e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1341fa9e4066Sahrens */ 134254d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 13436f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 13446f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 13456f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 134654d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 134754d692b7SGeorge Wilson } 1348fa9e4066Sahrens 13495cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 13505cabbc6bSPrashanth Sreenivasa spa_vdev_removal_destroy(spa->spa_vdev_removal); 13515cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal = NULL; 13525cabbc6bSPrashanth Sreenivasa } 13535cabbc6bSPrashanth Sreenivasa 1354667ec66fSSerapheim Dimitropoulos if (spa->spa_condense_zthr != NULL) { 1355667ec66fSSerapheim Dimitropoulos ASSERT(!zthr_isrunning(spa->spa_condense_zthr)); 1356667ec66fSSerapheim Dimitropoulos zthr_destroy(spa->spa_condense_zthr); 1357667ec66fSSerapheim Dimitropoulos spa->spa_condense_zthr = NULL; 1358667ec66fSSerapheim Dimitropoulos } 1359667ec66fSSerapheim Dimitropoulos 1360*86714001SSerapheim Dimitropoulos if (spa->spa_checkpoint_discard_zthr != NULL) { 1361*86714001SSerapheim Dimitropoulos ASSERT(!zthr_isrunning(spa->spa_checkpoint_discard_zthr)); 1362*86714001SSerapheim Dimitropoulos zthr_destroy(spa->spa_checkpoint_discard_zthr); 1363*86714001SSerapheim Dimitropoulos spa->spa_checkpoint_discard_zthr = NULL; 1364*86714001SSerapheim Dimitropoulos } 1365*86714001SSerapheim Dimitropoulos 13665cabbc6bSPrashanth Sreenivasa spa_condense_fini(spa); 13675cabbc6bSPrashanth Sreenivasa 1368cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1369cde58dbcSMatthew Ahrens 13700713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13710713e232SGeorge Wilson 13720713e232SGeorge Wilson /* 13730713e232SGeorge Wilson * Close all vdevs. 13740713e232SGeorge Wilson */ 13750713e232SGeorge Wilson if (spa->spa_root_vdev) 13760713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 13770713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 13780713e232SGeorge Wilson 1379fa9e4066Sahrens /* 1380fa9e4066Sahrens * Close the dsl pool. 1381fa9e4066Sahrens */ 1382fa9e4066Sahrens if (spa->spa_dsl_pool) { 1383fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1384fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1385afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1386fa9e4066Sahrens } 1387fa9e4066Sahrens 1388b24ab676SJeff Bonwick ddt_unload(spa); 1389b24ab676SJeff Bonwick 13908ad4d6ddSJeff Bonwick /* 13918ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 13928ad4d6ddSJeff Bonwick */ 13938ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 13948ad4d6ddSJeff Bonwick 1395fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1396fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1397fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1398fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1399fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1400fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 140199653d4eSeschrock } 1402fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1403fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1404fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1405fa94a07fSbrendan } 14062ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1407fa94a07fSbrendan 1408cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1409cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1410fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1411cd0837ccSGeorge Wilson } 1412fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1413fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1414fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1415fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1416fa94a07fSbrendan } 1417fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1418fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1419fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 142099653d4eSeschrock } 14212ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 142299653d4eSeschrock 1423ea8dc4b6Seschrock spa->spa_async_suspended = 0; 14248ad4d6ddSJeff Bonwick 14255cabbc6bSPrashanth Sreenivasa spa->spa_indirect_vdevs_loaded = B_FALSE; 14265cabbc6bSPrashanth Sreenivasa 14278704186eSDan McDonald if (spa->spa_comment != NULL) { 14288704186eSDan McDonald spa_strfree(spa->spa_comment); 14298704186eSDan McDonald spa->spa_comment = NULL; 14308704186eSDan McDonald } 14318704186eSDan McDonald 14328ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1433fa9e4066Sahrens } 1434fa9e4066Sahrens 143599653d4eSeschrock /* 143699653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 143799653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1438fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1439fa94a07fSbrendan * then re-generate a more complete list including status information. 144099653d4eSeschrock */ 14415cabbc6bSPrashanth Sreenivasa void 144299653d4eSeschrock spa_load_spares(spa_t *spa) 144399653d4eSeschrock { 144499653d4eSeschrock nvlist_t **spares; 144599653d4eSeschrock uint_t nspares; 144699653d4eSeschrock int i; 144739c23413Seschrock vdev_t *vd, *tvd; 144899653d4eSeschrock 1449*86714001SSerapheim Dimitropoulos #ifndef _KERNEL 1450*86714001SSerapheim Dimitropoulos /* 1451*86714001SSerapheim Dimitropoulos * zdb opens both the current state of the pool and the 1452*86714001SSerapheim Dimitropoulos * checkpointed state (if present), with a different spa_t. 1453*86714001SSerapheim Dimitropoulos * 1454*86714001SSerapheim Dimitropoulos * As spare vdevs are shared among open pools, we skip loading 1455*86714001SSerapheim Dimitropoulos * them when we load the checkpointed state of the pool. 1456*86714001SSerapheim Dimitropoulos */ 1457*86714001SSerapheim Dimitropoulos if (!spa_writeable(spa)) 1458*86714001SSerapheim Dimitropoulos return; 1459*86714001SSerapheim Dimitropoulos #endif 1460*86714001SSerapheim Dimitropoulos 1461e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1462e14bb325SJeff Bonwick 146399653d4eSeschrock /* 146499653d4eSeschrock * First, close and free any existing spare vdevs. 146599653d4eSeschrock */ 1466fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1467fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 146839c23413Seschrock 146939c23413Seschrock /* Undo the call to spa_activate() below */ 1470c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1471c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 147239c23413Seschrock spa_spare_remove(tvd); 147339c23413Seschrock vdev_close(vd); 147439c23413Seschrock vdev_free(vd); 147599653d4eSeschrock } 147639c23413Seschrock 1477fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1478fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1479fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 148099653d4eSeschrock 1481fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 148299653d4eSeschrock nspares = 0; 148399653d4eSeschrock else 1484fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 148599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 148699653d4eSeschrock 1487fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1488fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 148999653d4eSeschrock 149099653d4eSeschrock if (nspares == 0) 149199653d4eSeschrock return; 149299653d4eSeschrock 149399653d4eSeschrock /* 149499653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 149539c23413Seschrock * process. For each spare, there is potentially two different vdev_t 149639c23413Seschrock * structures associated with it: one in the list of spares (used only 149739c23413Seschrock * for basic validation purposes) and one in the active vdev 149839c23413Seschrock * configuration (if it's spared in). During this phase we open and 149939c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 150039c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 150199653d4eSeschrock */ 1502fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1503fa94a07fSbrendan KM_SLEEP); 1504fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 150599653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 150699653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 150799653d4eSeschrock ASSERT(vd != NULL); 150899653d4eSeschrock 1509fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 151099653d4eSeschrock 1511c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1512c5904d13Seschrock B_FALSE)) != NULL) { 151339c23413Seschrock if (!tvd->vdev_isspare) 151439c23413Seschrock spa_spare_add(tvd); 151539c23413Seschrock 151639c23413Seschrock /* 151739c23413Seschrock * We only mark the spare active if we were successfully 151839c23413Seschrock * able to load the vdev. Otherwise, importing a pool 151939c23413Seschrock * with a bad active spare would result in strange 152039c23413Seschrock * behavior, because multiple pool would think the spare 152139c23413Seschrock * is actively in use. 152239c23413Seschrock * 152339c23413Seschrock * There is a vulnerability here to an equally bizarre 152439c23413Seschrock * circumstance, where a dead active spare is later 152539c23413Seschrock * brought back to life (onlined or otherwise). Given 152639c23413Seschrock * the rarity of this scenario, and the extra complexity 152739c23413Seschrock * it adds, we ignore the possibility. 152839c23413Seschrock */ 152939c23413Seschrock if (!vdev_is_dead(tvd)) 153039c23413Seschrock spa_spare_activate(tvd); 153139c23413Seschrock } 153239c23413Seschrock 1533e14bb325SJeff Bonwick vd->vdev_top = vd; 15346809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1535e14bb325SJeff Bonwick 153699653d4eSeschrock if (vdev_open(vd) != 0) 153799653d4eSeschrock continue; 153899653d4eSeschrock 1539fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1540fa94a07fSbrendan spa_spare_add(vd); 154199653d4eSeschrock } 154299653d4eSeschrock 154399653d4eSeschrock /* 154499653d4eSeschrock * Recompute the stashed list of spares, with status information 154599653d4eSeschrock * this time. 154699653d4eSeschrock */ 1547fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 154899653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 154999653d4eSeschrock 1550fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1551fa94a07fSbrendan KM_SLEEP); 1552fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1553fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 15543f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1555fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1556fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1557fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 155899653d4eSeschrock nvlist_free(spares[i]); 1559fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1560fa94a07fSbrendan } 1561fa94a07fSbrendan 1562fa94a07fSbrendan /* 1563fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1564fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1565fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1566fa94a07fSbrendan * then re-generate a more complete list including status information. 1567fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1568fa94a07fSbrendan * not re-opened. 1569fa94a07fSbrendan */ 15705cabbc6bSPrashanth Sreenivasa void 1571fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1572fa94a07fSbrendan { 1573fa94a07fSbrendan nvlist_t **l2cache; 1574fa94a07fSbrendan uint_t nl2cache; 1575fa94a07fSbrendan int i, j, oldnvdevs; 1576573ca77eSGeorge Wilson uint64_t guid; 1577fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1578fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1579fa94a07fSbrendan 1580*86714001SSerapheim Dimitropoulos #ifndef _KERNEL 1581*86714001SSerapheim Dimitropoulos /* 1582*86714001SSerapheim Dimitropoulos * zdb opens both the current state of the pool and the 1583*86714001SSerapheim Dimitropoulos * checkpointed state (if present), with a different spa_t. 1584*86714001SSerapheim Dimitropoulos * 1585*86714001SSerapheim Dimitropoulos * As L2 caches are part of the ARC which is shared among open 1586*86714001SSerapheim Dimitropoulos * pools, we skip loading them when we load the checkpointed 1587*86714001SSerapheim Dimitropoulos * state of the pool. 1588*86714001SSerapheim Dimitropoulos */ 1589*86714001SSerapheim Dimitropoulos if (!spa_writeable(spa)) 1590*86714001SSerapheim Dimitropoulos return; 1591*86714001SSerapheim Dimitropoulos #endif 1592*86714001SSerapheim Dimitropoulos 1593e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1594e14bb325SJeff Bonwick 1595fa94a07fSbrendan if (sav->sav_config != NULL) { 1596fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1597fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1598fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1599fa94a07fSbrendan } else { 1600fa94a07fSbrendan nl2cache = 0; 1601d5285caeSGeorge Wilson newvdevs = NULL; 1602fa94a07fSbrendan } 1603fa94a07fSbrendan 1604fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1605fa94a07fSbrendan oldnvdevs = sav->sav_count; 1606fa94a07fSbrendan sav->sav_vdevs = NULL; 1607fa94a07fSbrendan sav->sav_count = 0; 1608fa94a07fSbrendan 1609fa94a07fSbrendan /* 1610fa94a07fSbrendan * Process new nvlist of vdevs. 1611fa94a07fSbrendan */ 1612fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1613fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1614fa94a07fSbrendan &guid) == 0); 1615fa94a07fSbrendan 1616fa94a07fSbrendan newvdevs[i] = NULL; 1617fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1618fa94a07fSbrendan vd = oldvdevs[j]; 1619fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1620fa94a07fSbrendan /* 1621fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1622fa94a07fSbrendan */ 1623fa94a07fSbrendan newvdevs[i] = vd; 1624fa94a07fSbrendan oldvdevs[j] = NULL; 1625fa94a07fSbrendan break; 1626fa94a07fSbrendan } 1627fa94a07fSbrendan } 1628fa94a07fSbrendan 1629fa94a07fSbrendan if (newvdevs[i] == NULL) { 1630fa94a07fSbrendan /* 1631fa94a07fSbrendan * Create new vdev 1632fa94a07fSbrendan */ 1633fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1634fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1635fa94a07fSbrendan ASSERT(vd != NULL); 1636fa94a07fSbrendan newvdevs[i] = vd; 1637fa94a07fSbrendan 1638fa94a07fSbrendan /* 1639fa94a07fSbrendan * Commit this vdev as an l2cache device, 1640fa94a07fSbrendan * even if it fails to open. 1641fa94a07fSbrendan */ 1642fa94a07fSbrendan spa_l2cache_add(vd); 1643fa94a07fSbrendan 1644c5904d13Seschrock vd->vdev_top = vd; 1645c5904d13Seschrock vd->vdev_aux = sav; 1646c5904d13Seschrock 1647c5904d13Seschrock spa_l2cache_activate(vd); 1648c5904d13Seschrock 1649fa94a07fSbrendan if (vdev_open(vd) != 0) 1650fa94a07fSbrendan continue; 1651fa94a07fSbrendan 1652fa94a07fSbrendan (void) vdev_validate_aux(vd); 1653fa94a07fSbrendan 1654573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1655573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1656fa94a07fSbrendan } 1657fa94a07fSbrendan } 1658fa94a07fSbrendan 1659fa94a07fSbrendan /* 1660fa94a07fSbrendan * Purge vdevs that were dropped 1661fa94a07fSbrendan */ 1662fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1663fa94a07fSbrendan uint64_t pool; 1664fa94a07fSbrendan 1665fa94a07fSbrendan vd = oldvdevs[i]; 1666fa94a07fSbrendan if (vd != NULL) { 1667cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1668cd0837ccSGeorge Wilson 16698ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 16708ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1671fa94a07fSbrendan l2arc_remove_vdev(vd); 1672cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1673cd0837ccSGeorge Wilson vdev_free(vd); 1674fa94a07fSbrendan } 1675fa94a07fSbrendan } 1676fa94a07fSbrendan 1677fa94a07fSbrendan if (oldvdevs) 1678fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1679fa94a07fSbrendan 1680fa94a07fSbrendan if (sav->sav_config == NULL) 1681fa94a07fSbrendan goto out; 1682fa94a07fSbrendan 1683fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1684fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1685fa94a07fSbrendan 1686fa94a07fSbrendan /* 1687fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1688fa94a07fSbrendan * information this time. 1689fa94a07fSbrendan */ 1690fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1691fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1692fa94a07fSbrendan 1693fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1694fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1695fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 16963f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1697fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1698fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1699fa94a07fSbrendan out: 1700fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1701fa94a07fSbrendan nvlist_free(l2cache[i]); 1702fa94a07fSbrendan if (sav->sav_count) 1703fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 170499653d4eSeschrock } 170599653d4eSeschrock 170699653d4eSeschrock static int 170799653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 170899653d4eSeschrock { 170999653d4eSeschrock dmu_buf_t *db; 171099653d4eSeschrock char *packed = NULL; 171199653d4eSeschrock size_t nvsize = 0; 171299653d4eSeschrock int error; 171399653d4eSeschrock *value = NULL; 171499653d4eSeschrock 1715a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1716a45f1c3cSBrian Behlendorf if (error != 0) 1717a45f1c3cSBrian Behlendorf return (error); 1718a45f1c3cSBrian Behlendorf 171999653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 172099653d4eSeschrock dmu_buf_rele(db, FTAG); 172199653d4eSeschrock 172299653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 17237bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 17247bfdf011SNeil Perrin DMU_READ_PREFETCH); 172599653d4eSeschrock if (error == 0) 172699653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 172799653d4eSeschrock kmem_free(packed, nvsize); 172899653d4eSeschrock 172999653d4eSeschrock return (error); 173099653d4eSeschrock } 173199653d4eSeschrock 17326f793812SPavel Zakharov /* 17336f793812SPavel Zakharov * Concrete top-level vdevs that are not missing and are not logs. At every 17346f793812SPavel Zakharov * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. 17356f793812SPavel Zakharov */ 17366f793812SPavel Zakharov static uint64_t 17376f793812SPavel Zakharov spa_healthy_core_tvds(spa_t *spa) 17386f793812SPavel Zakharov { 17396f793812SPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 17406f793812SPavel Zakharov uint64_t tvds = 0; 17416f793812SPavel Zakharov 17426f793812SPavel Zakharov for (uint64_t i = 0; i < rvd->vdev_children; i++) { 17436f793812SPavel Zakharov vdev_t *vd = rvd->vdev_child[i]; 17446f793812SPavel Zakharov if (vd->vdev_islog) 17456f793812SPavel Zakharov continue; 17466f793812SPavel Zakharov if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) 17476f793812SPavel Zakharov tvds++; 17486f793812SPavel Zakharov } 17496f793812SPavel Zakharov 17506f793812SPavel Zakharov return (tvds); 17516f793812SPavel Zakharov } 17526f793812SPavel Zakharov 17533d7072f8Seschrock /* 17543d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 17553d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 17563d7072f8Seschrock */ 17573d7072f8Seschrock static void 17583d7072f8Seschrock spa_check_removed(vdev_t *vd) 17593d7072f8Seschrock { 17606f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 17613d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 17623d7072f8Seschrock 1763efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 17645cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 17653d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 1766ce1577b0SDave Eddy spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 17673d7072f8Seschrock } 17683d7072f8Seschrock } 17693d7072f8Seschrock 17706f793812SPavel Zakharov static int 17716f793812SPavel Zakharov spa_check_for_missing_logs(spa_t *spa) 1772e6ca193dSGeorge Wilson { 17736f793812SPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 1774e6ca193dSGeorge Wilson 177588ecc943SGeorge Wilson /* 17764b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 17776f793812SPavel Zakharov * diagnostic information about missing log devices. 17784b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 177988ecc943SGeorge Wilson */ 17804b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 17814b964adaSGeorge Wilson nvlist_t **child, *nv; 17824b964adaSGeorge Wilson uint64_t idx = 0; 17834b964adaSGeorge Wilson 17844b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 17854b964adaSGeorge Wilson KM_SLEEP); 17864b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1787e6ca193dSGeorge Wilson 17886f793812SPavel Zakharov for (uint64_t c = 0; c < rvd->vdev_children; c++) { 17894b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 17904b964adaSGeorge Wilson 17916f793812SPavel Zakharov /* 17926f793812SPavel Zakharov * We consider a device as missing only if it failed 17936f793812SPavel Zakharov * to open (i.e. offline or faulted is not considered 17946f793812SPavel Zakharov * as missing). 17956f793812SPavel Zakharov */ 17966f793812SPavel Zakharov if (tvd->vdev_islog && 17976f793812SPavel Zakharov tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 17986f793812SPavel Zakharov child[idx++] = vdev_config_generate(spa, tvd, 17996f793812SPavel Zakharov B_FALSE, VDEV_CONFIG_MISSING); 18006f793812SPavel Zakharov } 18014b964adaSGeorge Wilson } 18024b964adaSGeorge Wilson 18036f793812SPavel Zakharov if (idx > 0) { 18046f793812SPavel Zakharov fnvlist_add_nvlist_array(nv, 18056f793812SPavel Zakharov ZPOOL_CONFIG_CHILDREN, child, idx); 18066f793812SPavel Zakharov fnvlist_add_nvlist(spa->spa_load_info, 18076f793812SPavel Zakharov ZPOOL_CONFIG_MISSING_DEVICES, nv); 18084b964adaSGeorge Wilson 18096f793812SPavel Zakharov for (uint64_t i = 0; i < idx; i++) 18104b964adaSGeorge Wilson nvlist_free(child[i]); 18114b964adaSGeorge Wilson } 18124b964adaSGeorge Wilson nvlist_free(nv); 18134b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 18144b964adaSGeorge Wilson 18156f793812SPavel Zakharov if (idx > 0) { 18166f793812SPavel Zakharov spa_load_failed(spa, "some log devices are missing"); 18176f793812SPavel Zakharov return (SET_ERROR(ENXIO)); 18186f793812SPavel Zakharov } 18196f793812SPavel Zakharov } else { 18206f793812SPavel Zakharov for (uint64_t c = 0; c < rvd->vdev_children; c++) { 18216f793812SPavel Zakharov vdev_t *tvd = rvd->vdev_child[c]; 18225cabbc6bSPrashanth Sreenivasa 18236f793812SPavel Zakharov if (tvd->vdev_islog && 18246f793812SPavel Zakharov tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 18254b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 18266f793812SPavel Zakharov spa_load_note(spa, "some log devices are " 18276f793812SPavel Zakharov "missing, ZIL is dropped."); 18286f793812SPavel Zakharov break; 1829215198a6SJoe Stein } 18304b964adaSGeorge Wilson } 1831e6ca193dSGeorge Wilson } 1832215198a6SJoe Stein 18336f793812SPavel Zakharov return (0); 1834e6ca193dSGeorge Wilson } 1835e6ca193dSGeorge Wilson 1836b87f3af3Sperrin /* 1837b87f3af3Sperrin * Check for missing log devices 1838b87f3af3Sperrin */ 18393b2aab18SMatthew Ahrens static boolean_t 1840b87f3af3Sperrin spa_check_logs(spa_t *spa) 1841b87f3af3Sperrin { 18423b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 184312380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 18443b2aab18SMatthew Ahrens 1845b87f3af3Sperrin switch (spa->spa_log_state) { 1846b87f3af3Sperrin case SPA_LOG_MISSING: 1847b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1848b87f3af3Sperrin case SPA_LOG_UNKNOWN: 184912380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 185012380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 18513b2aab18SMatthew Ahrens if (rv) 18521195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1853b87f3af3Sperrin break; 1854b87f3af3Sperrin } 18553b2aab18SMatthew Ahrens return (rv); 1856b87f3af3Sperrin } 1857b87f3af3Sperrin 18581195e687SMark J Musante static boolean_t 18591195e687SMark J Musante spa_passivate_log(spa_t *spa) 18601195e687SMark J Musante { 18611195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18621195e687SMark J Musante boolean_t slog_found = B_FALSE; 18631195e687SMark J Musante 18641195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18651195e687SMark J Musante 18661195e687SMark J Musante if (!spa_has_slogs(spa)) 18671195e687SMark J Musante return (B_FALSE); 18681195e687SMark J Musante 18691195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18701195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18711195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18721195e687SMark J Musante 18731195e687SMark J Musante if (tvd->vdev_islog) { 18741195e687SMark J Musante metaslab_group_passivate(mg); 18751195e687SMark J Musante slog_found = B_TRUE; 18761195e687SMark J Musante } 18771195e687SMark J Musante } 18781195e687SMark J Musante 18791195e687SMark J Musante return (slog_found); 18801195e687SMark J Musante } 18811195e687SMark J Musante 18821195e687SMark J Musante static void 18831195e687SMark J Musante spa_activate_log(spa_t *spa) 18841195e687SMark J Musante { 18851195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18861195e687SMark J Musante 18871195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18881195e687SMark J Musante 18891195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18901195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18911195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18921195e687SMark J Musante 18931195e687SMark J Musante if (tvd->vdev_islog) 18941195e687SMark J Musante metaslab_group_activate(mg); 18951195e687SMark J Musante } 18961195e687SMark J Musante } 18971195e687SMark J Musante 18981195e687SMark J Musante int 18995cabbc6bSPrashanth Sreenivasa spa_reset_logs(spa_t *spa) 19001195e687SMark J Musante { 19013b2aab18SMatthew Ahrens int error; 19021195e687SMark J Musante 19035cabbc6bSPrashanth Sreenivasa error = dmu_objset_find(spa_name(spa), zil_reset, 19043b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 19053b2aab18SMatthew Ahrens if (error == 0) { 19061195e687SMark J Musante /* 19071195e687SMark J Musante * We successfully offlined the log device, sync out the 19081195e687SMark J Musante * current txg so that the "stubby" block can be removed 19091195e687SMark J Musante * by zil_sync(). 19101195e687SMark J Musante */ 19111195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 19121195e687SMark J Musante } 19131195e687SMark J Musante return (error); 19141195e687SMark J Musante } 19151195e687SMark J Musante 1916b693757aSEric Schrock static void 1917b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1918b693757aSEric Schrock { 1919b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1920b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1921b693757aSEric Schrock } 1922b693757aSEric Schrock 1923b24ab676SJeff Bonwick void 1924b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1925b24ab676SJeff Bonwick { 1926b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1927b24ab676SJeff Bonwick 1928b24ab676SJeff Bonwick if (zio->io_error) 1929b24ab676SJeff Bonwick return; 1930b24ab676SJeff Bonwick 1931b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1932b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1933b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1934b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1935b24ab676SJeff Bonwick } 1936b24ab676SJeff Bonwick 1937468c413aSTim Haley typedef struct spa_load_error { 1938c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1939468c413aSTim Haley uint64_t sle_data_count; 1940468c413aSTim Haley } spa_load_error_t; 1941468c413aSTim Haley 1942468c413aSTim Haley static void 1943468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1944468c413aSTim Haley { 1945468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1946468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1947468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1948468c413aSTim Haley int error = zio->io_error; 1949e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1950468c413aSTim Haley 1951770499e1SDan Kimmel abd_free(zio->io_abd); 1952468c413aSTim Haley if (error) { 1953ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1954468c413aSTim Haley type != DMU_OT_INTENT_LOG) 19551a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1956468c413aSTim Haley else 19571a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1958468c413aSTim Haley } 1959e42d2059SMatthew Ahrens 1960e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1961e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1962e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1963e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1964468c413aSTim Haley } 1965468c413aSTim Haley 1966e42d2059SMatthew Ahrens /* 1967e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1968e42d2059SMatthew Ahrens * a pool while importing it. 1969e42d2059SMatthew Ahrens */ 1970e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1971e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1972e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1973e42d2059SMatthew Ahrens 1974468c413aSTim Haley /*ARGSUSED*/ 1975468c413aSTim Haley static int 1976b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19777802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1978468c413aSTim Haley { 1979a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1980e42d2059SMatthew Ahrens return (0); 1981e42d2059SMatthew Ahrens /* 1982e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1983e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1984e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1985e42d2059SMatthew Ahrens */ 1986e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1987e42d2059SMatthew Ahrens return (0); 1988770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1989e42d2059SMatthew Ahrens return (0); 1990468c413aSTim Haley 1991e42d2059SMatthew Ahrens zio_t *rio = arg; 1992e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1993e42d2059SMatthew Ahrens 1994e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1995e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1996e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1997e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1998e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1999e42d2059SMatthew Ahrens 2000770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 2001e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 2002e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 2003e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 2004468c413aSTim Haley return (0); 2005468c413aSTim Haley } 2006468c413aSTim Haley 2007c971037bSPaul Dagnelie /* ARGSUSED */ 2008c971037bSPaul Dagnelie int 2009c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 2010c971037bSPaul Dagnelie { 20119adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 2012c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 2013c971037bSPaul Dagnelie 2014c971037bSPaul Dagnelie return (0); 2015c971037bSPaul Dagnelie } 2016c971037bSPaul Dagnelie 2017468c413aSTim Haley static int 2018468c413aSTim Haley spa_load_verify(spa_t *spa) 2019468c413aSTim Haley { 2020468c413aSTim Haley zio_t *rio; 2021468c413aSTim Haley spa_load_error_t sle = { 0 }; 2022468c413aSTim Haley zpool_rewind_policy_t policy; 2023468c413aSTim Haley boolean_t verify_ok = B_FALSE; 2024e42d2059SMatthew Ahrens int error = 0; 2025468c413aSTim Haley 2026c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 2027c8ee1847SVictor Latushkin 2028c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 2029c8ee1847SVictor Latushkin return (0); 2030c8ee1847SVictor Latushkin 2031c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 2032c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 2033c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 2034c971037bSPaul Dagnelie DS_FIND_CHILDREN); 2035c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 2036c971037bSPaul Dagnelie if (error != 0) 2037c971037bSPaul Dagnelie return (error); 2038c971037bSPaul Dagnelie 2039468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 2040468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 2041468c413aSTim Haley 2042e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 20433ee8c80cSPavel Zakharov if (spa->spa_extreme_rewind) { 20443ee8c80cSPavel Zakharov spa_load_note(spa, "performing a complete scan of the " 20453ee8c80cSPavel Zakharov "pool since extreme rewind is on. This may take " 20463ee8c80cSPavel Zakharov "a very long time.\n (spa_load_verify_data=%u, " 20473ee8c80cSPavel Zakharov "spa_load_verify_metadata=%u)", 20483ee8c80cSPavel Zakharov spa_load_verify_data, spa_load_verify_metadata); 20493ee8c80cSPavel Zakharov } 2050e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 2051e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 2052e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 2053e42d2059SMatthew Ahrens } 2054468c413aSTim Haley 2055468c413aSTim Haley (void) zio_wait(rio); 2056468c413aSTim Haley 2057c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 2058468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 2059468c413aSTim Haley 2060e144c4e6SPavel Zakharov if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { 2061e144c4e6SPavel Zakharov spa_load_note(spa, "spa_load_verify found %llu metadata errors " 2062e144c4e6SPavel Zakharov "and %llu data errors", (u_longlong_t)sle.sle_meta_count, 2063e144c4e6SPavel Zakharov (u_longlong_t)sle.sle_data_count); 2064e144c4e6SPavel Zakharov } 2065e144c4e6SPavel Zakharov 2066e144c4e6SPavel Zakharov if (spa_load_verify_dryrun || 2067e144c4e6SPavel Zakharov (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 2068e144c4e6SPavel Zakharov sle.sle_data_count <= policy.zrp_maxdata)) { 20694b964adaSGeorge Wilson int64_t loss = 0; 20704b964adaSGeorge Wilson 2071468c413aSTim Haley verify_ok = B_TRUE; 2072468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 2073468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 20744b964adaSGeorge Wilson 20754b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 20764b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20774b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 20784b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 20794b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 20804b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20814b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 2082a33cae98STim Haley } else { 2083a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2084468c413aSTim Haley } 2085468c413aSTim Haley 2086e144c4e6SPavel Zakharov if (spa_load_verify_dryrun) 2087e144c4e6SPavel Zakharov return (0); 2088e144c4e6SPavel Zakharov 2089468c413aSTim Haley if (error) { 2090468c413aSTim Haley if (error != ENXIO && error != EIO) 2091be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2092468c413aSTim Haley return (error); 2093468c413aSTim Haley } 2094468c413aSTim Haley 2095468c413aSTim Haley return (verify_ok ? 0 : EIO); 2096468c413aSTim Haley } 2097468c413aSTim Haley 20981195e687SMark J Musante /* 20991195e687SMark J Musante * Find a value in the pool props object. 21001195e687SMark J Musante */ 21011195e687SMark J Musante static void 21021195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 21031195e687SMark J Musante { 21041195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 21051195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 21061195e687SMark J Musante } 21071195e687SMark J Musante 21081195e687SMark J Musante /* 21091195e687SMark J Musante * Find a value in the pool directory object. 21101195e687SMark J Musante */ 21111195e687SMark J Musante static int 21123ee8c80cSPavel Zakharov spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) 21131195e687SMark J Musante { 21143ee8c80cSPavel Zakharov int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 21153ee8c80cSPavel Zakharov name, sizeof (uint64_t), 1, val); 21163ee8c80cSPavel Zakharov 21173ee8c80cSPavel Zakharov if (error != 0 && (error != ENOENT || log_enoent)) { 21183ee8c80cSPavel Zakharov spa_load_failed(spa, "couldn't get '%s' value in MOS directory " 21193ee8c80cSPavel Zakharov "[error=%d]", name, error); 21203ee8c80cSPavel Zakharov } 21213ee8c80cSPavel Zakharov 21223ee8c80cSPavel Zakharov return (error); 21231195e687SMark J Musante } 21241195e687SMark J Musante 21251195e687SMark J Musante static int 21261195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 21271195e687SMark J Musante { 21281195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 21295cabbc6bSPrashanth Sreenivasa return (SET_ERROR(err)); 21301195e687SMark J Musante } 21311195e687SMark J Musante 2132667ec66fSSerapheim Dimitropoulos static void 2133667ec66fSSerapheim Dimitropoulos spa_spawn_aux_threads(spa_t *spa) 2134667ec66fSSerapheim Dimitropoulos { 2135667ec66fSSerapheim Dimitropoulos ASSERT(spa_writeable(spa)); 2136667ec66fSSerapheim Dimitropoulos 2137667ec66fSSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2138667ec66fSSerapheim Dimitropoulos 2139667ec66fSSerapheim Dimitropoulos spa_start_indirect_condensing_thread(spa); 2140*86714001SSerapheim Dimitropoulos 2141*86714001SSerapheim Dimitropoulos ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); 2142*86714001SSerapheim Dimitropoulos spa->spa_checkpoint_discard_zthr = 2143*86714001SSerapheim Dimitropoulos zthr_create(spa_checkpoint_discard_thread_check, 2144*86714001SSerapheim Dimitropoulos spa_checkpoint_discard_thread, spa); 2145667ec66fSSerapheim Dimitropoulos } 2146667ec66fSSerapheim Dimitropoulos 21471195e687SMark J Musante /* 21481195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 21491195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 21501195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 21511195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 21521195e687SMark J Musante * 21531195e687SMark J Musante * This function determines what to do with that list: either rejoin 21541195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 21551195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 21561195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 21571195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 21581195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 21591195e687SMark J Musante * 2160d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2161d41c4376SMark J Musante * the original pool. 21621195e687SMark J Musante */ 21631195e687SMark J Musante static void 21641195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 21651195e687SMark J Musante { 21661195e687SMark J Musante uint_t extracted; 21671195e687SMark J Musante uint64_t *glist; 21681195e687SMark J Musante uint_t i, gcount; 21691195e687SMark J Musante nvlist_t *nvl; 21701195e687SMark J Musante vdev_t **vd; 21711195e687SMark J Musante boolean_t attempt_reopen; 21721195e687SMark J Musante 21731195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 21741195e687SMark J Musante return; 21751195e687SMark J Musante 21761195e687SMark J Musante /* check that the config is complete */ 21771195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 21781195e687SMark J Musante &glist, &gcount) != 0) 21791195e687SMark J Musante return; 21801195e687SMark J Musante 21811195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 21821195e687SMark J Musante 21831195e687SMark J Musante /* attempt to online all the vdevs & validate */ 21841195e687SMark J Musante attempt_reopen = B_TRUE; 21851195e687SMark J Musante for (i = 0; i < gcount; i++) { 21861195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 21871195e687SMark J Musante continue; 21881195e687SMark J Musante 21891195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 21901195e687SMark J Musante if (vd[i] == NULL) { 21911195e687SMark J Musante /* 21921195e687SMark J Musante * Don't bother attempting to reopen the disks; 21931195e687SMark J Musante * just do the split. 21941195e687SMark J Musante */ 21951195e687SMark J Musante attempt_reopen = B_FALSE; 21961195e687SMark J Musante } else { 21971195e687SMark J Musante /* attempt to re-online it */ 21981195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 21991195e687SMark J Musante } 22001195e687SMark J Musante } 22011195e687SMark J Musante 22021195e687SMark J Musante if (attempt_reopen) { 22031195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 22041195e687SMark J Musante 22051195e687SMark J Musante /* check each device to see what state it's in */ 22061195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 22071195e687SMark J Musante if (vd[i] != NULL && 22081195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 22091195e687SMark J Musante break; 22101195e687SMark J Musante ++extracted; 22111195e687SMark J Musante } 22121195e687SMark J Musante } 22131195e687SMark J Musante 22141195e687SMark J Musante /* 22151195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 22161195e687SMark J Musante * even attempted to look at them, then we split them off for 22171195e687SMark J Musante * good. 22181195e687SMark J Musante */ 22191195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 22201195e687SMark J Musante for (i = 0; i < gcount; i++) 22211195e687SMark J Musante if (vd[i] != NULL) 22221195e687SMark J Musante vdev_split(vd[i]); 22231195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 22241195e687SMark J Musante } 22251195e687SMark J Musante 22261195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 22271195e687SMark J Musante } 22281195e687SMark J Musante 22291195e687SMark J Musante static int 22306f793812SPavel Zakharov spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) 22311195e687SMark J Musante { 22321195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 22331195e687SMark J Musante int error; 22341195e687SMark J Musante 22356f793812SPavel Zakharov spa->spa_load_state = state; 2236ad135b5dSChristopher Siden 22376f793812SPavel Zakharov gethrestime(&spa->spa_loaded_ts); 2238*86714001SSerapheim Dimitropoulos error = spa_load_impl(spa, type, &ereport); 22391195e687SMark J Musante 2240bc9014e6SJustin Gibbs /* 2241bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2242bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2243bc9014e6SJustin Gibbs */ 2244bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 22451195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 224611027bc7STim Haley if (error) { 224711027bc7STim Haley if (error != EEXIST) { 224811027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 224911027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 225011027bc7STim Haley } 225111027bc7STim Haley if (error != EBADF) { 225211027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 225311027bc7STim Haley } 225411027bc7STim Haley } 22551195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 22561195e687SMark J Musante spa->spa_ena = 0; 22571195e687SMark J Musante 22581195e687SMark J Musante return (error); 22591195e687SMark J Musante } 22601195e687SMark J Musante 2261215198a6SJoe Stein /* 2262215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2263215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2264215198a6SJoe Stein * spa's per-vdev ZAP list. 2265215198a6SJoe Stein */ 2266215198a6SJoe Stein static uint64_t 2267215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2268215198a6SJoe Stein { 2269215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2270215198a6SJoe Stein uint64_t total = 0; 2271215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2272215198a6SJoe Stein total++; 2273215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2274215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2275215198a6SJoe Stein } 2276215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2277215198a6SJoe Stein total++; 2278215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2279215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2280215198a6SJoe Stein } 2281215198a6SJoe Stein 2282215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2283215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2284215198a6SJoe Stein } 2285215198a6SJoe Stein 2286215198a6SJoe Stein return (total); 2287215198a6SJoe Stein } 2288215198a6SJoe Stein 2289fa9e4066Sahrens static int 22906f793812SPavel Zakharov spa_verify_host(spa_t *spa, nvlist_t *mos_config) 22916f793812SPavel Zakharov { 22926f793812SPavel Zakharov uint64_t hostid; 22936f793812SPavel Zakharov char *hostname; 22946f793812SPavel Zakharov uint64_t myhostid = 0; 22956f793812SPavel Zakharov 22966f793812SPavel Zakharov if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, 22976f793812SPavel Zakharov ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 22986f793812SPavel Zakharov hostname = fnvlist_lookup_string(mos_config, 22996f793812SPavel Zakharov ZPOOL_CONFIG_HOSTNAME); 23006f793812SPavel Zakharov 23016f793812SPavel Zakharov myhostid = zone_get_hostid(NULL); 23026f793812SPavel Zakharov 23036f793812SPavel Zakharov if (hostid != 0 && myhostid != 0 && hostid != myhostid) { 23046f793812SPavel Zakharov cmn_err(CE_WARN, "pool '%s' could not be " 23056f793812SPavel Zakharov "loaded as it was last accessed by " 23066f793812SPavel Zakharov "another system (host: %s hostid: 0x%llx). " 23076f793812SPavel Zakharov "See: http://illumos.org/msg/ZFS-8000-EY", 23086f793812SPavel Zakharov spa_name(spa), hostname, (u_longlong_t)hostid); 23096f793812SPavel Zakharov spa_load_failed(spa, "hostid verification failed: pool " 23106f793812SPavel Zakharov "last accessed by host: %s (hostid: 0x%llx)", 23116f793812SPavel Zakharov hostname, (u_longlong_t)hostid); 23126f793812SPavel Zakharov return (SET_ERROR(EBADF)); 23136f793812SPavel Zakharov } 23146f793812SPavel Zakharov } 23156f793812SPavel Zakharov 23166f793812SPavel Zakharov return (0); 23176f793812SPavel Zakharov } 23186f793812SPavel Zakharov 23196f793812SPavel Zakharov static int 23206f793812SPavel Zakharov spa_ld_parse_config(spa_t *spa, spa_import_type_t type) 2321fa9e4066Sahrens { 2322fa9e4066Sahrens int error = 0; 23236f793812SPavel Zakharov nvlist_t *nvtree, *nvl, *config = spa->spa_config; 23241195e687SMark J Musante int parse; 23251fd3785fSPavel Zakharov vdev_t *rvd; 23266f793812SPavel Zakharov uint64_t pool_guid; 23276f793812SPavel Zakharov char *comment; 23286f793812SPavel Zakharov 23296f793812SPavel Zakharov /* 23306f793812SPavel Zakharov * Versioning wasn't explicitly added to the label until later, so if 23316f793812SPavel Zakharov * it's not present treat it as the initial version. 23326f793812SPavel Zakharov */ 23336f793812SPavel Zakharov if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 23346f793812SPavel Zakharov &spa->spa_ubsync.ub_version) != 0) 23356f793812SPavel Zakharov spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 23366f793812SPavel Zakharov 23376f793812SPavel Zakharov if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 23386f793812SPavel Zakharov spa_load_failed(spa, "invalid config provided: '%s' missing", 23396f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 23406f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 23416f793812SPavel Zakharov } 23426f793812SPavel Zakharov 2343*86714001SSerapheim Dimitropoulos /* 2344*86714001SSerapheim Dimitropoulos * If we are doing an import, ensure that the pool is not already 2345*86714001SSerapheim Dimitropoulos * imported by checking if its pool guid already exists in the 2346*86714001SSerapheim Dimitropoulos * spa namespace. 2347*86714001SSerapheim Dimitropoulos * 2348*86714001SSerapheim Dimitropoulos * The only case that we allow an already imported pool to be 2349*86714001SSerapheim Dimitropoulos * imported again, is when the pool is checkpointed and we want to 2350*86714001SSerapheim Dimitropoulos * look at its checkpointed state from userland tools like zdb. 2351*86714001SSerapheim Dimitropoulos */ 2352*86714001SSerapheim Dimitropoulos #ifdef _KERNEL 2353*86714001SSerapheim Dimitropoulos if ((spa->spa_load_state == SPA_LOAD_IMPORT || 2354*86714001SSerapheim Dimitropoulos spa->spa_load_state == SPA_LOAD_TRYIMPORT) && 2355*86714001SSerapheim Dimitropoulos spa_guid_exists(pool_guid, 0)) { 2356*86714001SSerapheim Dimitropoulos #else 2357*86714001SSerapheim Dimitropoulos if ((spa->spa_load_state == SPA_LOAD_IMPORT || 2358*86714001SSerapheim Dimitropoulos spa->spa_load_state == SPA_LOAD_TRYIMPORT) && 2359*86714001SSerapheim Dimitropoulos spa_guid_exists(pool_guid, 0) && 2360*86714001SSerapheim Dimitropoulos !spa_importing_readonly_checkpoint(spa)) { 2361*86714001SSerapheim Dimitropoulos #endif 23626f793812SPavel Zakharov spa_load_failed(spa, "a pool with guid %llu is already open", 23636f793812SPavel Zakharov (u_longlong_t)pool_guid); 23646f793812SPavel Zakharov return (SET_ERROR(EEXIST)); 23656f793812SPavel Zakharov } 23666f793812SPavel Zakharov 23676f793812SPavel Zakharov spa->spa_config_guid = pool_guid; 23686f793812SPavel Zakharov 23696f793812SPavel Zakharov nvlist_free(spa->spa_load_info); 23706f793812SPavel Zakharov spa->spa_load_info = fnvlist_alloc(); 23716f793812SPavel Zakharov 23726f793812SPavel Zakharov ASSERT(spa->spa_comment == NULL); 23736f793812SPavel Zakharov if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 23746f793812SPavel Zakharov spa->spa_comment = spa_strdup(comment); 23756f793812SPavel Zakharov 23766f793812SPavel Zakharov (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 23776f793812SPavel Zakharov &spa->spa_config_txg); 23786f793812SPavel Zakharov 23796f793812SPavel Zakharov if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) 23806f793812SPavel Zakharov spa->spa_config_splitting = fnvlist_dup(nvl); 23818ad4d6ddSJeff Bonwick 23823ee8c80cSPavel Zakharov if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { 23833ee8c80cSPavel Zakharov spa_load_failed(spa, "invalid config provided: '%s' missing", 23843ee8c80cSPavel Zakharov ZPOOL_CONFIG_VDEV_TREE); 2385be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 23863ee8c80cSPavel Zakharov } 2387fa9e4066Sahrens 238854d692b7SGeorge Wilson /* 238954d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 239054d692b7SGeorge Wilson */ 23916f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 23926f834bc1SMatthew Ahrens KM_SLEEP); 23936f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 23946f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 23956f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 23966f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 23976f834bc1SMatthew Ahrens } 239854d692b7SGeorge Wilson 2399fa9e4066Sahrens /* 240099653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 240199653d4eSeschrock * value that will be returned by spa_version() since parsing the 240299653d4eSeschrock * configuration requires knowing the version number. 2403fa9e4066Sahrens */ 2404e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24056f793812SPavel Zakharov parse = (type == SPA_IMPORT_EXISTING ? 24066f793812SPavel Zakharov VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 24071fd3785fSPavel Zakharov error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); 2408e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2409fa9e4066Sahrens 24103ee8c80cSPavel Zakharov if (error != 0) { 24113ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to parse config [error=%d]", 24123ee8c80cSPavel Zakharov error); 24131195e687SMark J Musante return (error); 24143ee8c80cSPavel Zakharov } 2415fa9e4066Sahrens 24160e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 241781cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 241881cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 24191195e687SMark J Musante 24201195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 24211195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 24221195e687SMark J Musante } 2423fa9e4066Sahrens 24241fd3785fSPavel Zakharov return (0); 24251fd3785fSPavel Zakharov } 24261fd3785fSPavel Zakharov 24276f793812SPavel Zakharov /* 24286f793812SPavel Zakharov * Recursively open all vdevs in the vdev tree. This function is called twice: 24296f793812SPavel Zakharov * first with the untrusted config, then with the trusted config. 24306f793812SPavel Zakharov */ 24311fd3785fSPavel Zakharov static int 24321fd3785fSPavel Zakharov spa_ld_open_vdevs(spa_t *spa) 24331fd3785fSPavel Zakharov { 24341fd3785fSPavel Zakharov int error = 0; 24351fd3785fSPavel Zakharov 24366f793812SPavel Zakharov /* 24376f793812SPavel Zakharov * spa_missing_tvds_allowed defines how many top-level vdevs can be 24386f793812SPavel Zakharov * missing/unopenable for the root vdev to be still considered openable. 24396f793812SPavel Zakharov */ 24406f793812SPavel Zakharov if (spa->spa_trust_config) { 24416f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; 24426f793812SPavel Zakharov } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { 24436f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; 24446f793812SPavel Zakharov } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { 24456f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; 24466f793812SPavel Zakharov } else { 24476f793812SPavel Zakharov spa->spa_missing_tvds_allowed = 0; 24486f793812SPavel Zakharov } 24496f793812SPavel Zakharov 24506f793812SPavel Zakharov spa->spa_missing_tvds_allowed = 24516f793812SPavel Zakharov MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); 24526f793812SPavel Zakharov 2453e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24541fd3785fSPavel Zakharov error = vdev_open(spa->spa_root_vdev); 2455e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24566f793812SPavel Zakharov 24576f793812SPavel Zakharov if (spa->spa_missing_tvds != 0) { 24586f793812SPavel Zakharov spa_load_note(spa, "vdev tree has %lld missing top-level " 24596f793812SPavel Zakharov "vdevs.", (u_longlong_t)spa->spa_missing_tvds); 24606f793812SPavel Zakharov if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { 24616f793812SPavel Zakharov /* 24626f793812SPavel Zakharov * Although theoretically we could allow users to open 24636f793812SPavel Zakharov * incomplete pools in RW mode, we'd need to add a lot 24646f793812SPavel Zakharov * of extra logic (e.g. adjust pool space to account 24656f793812SPavel Zakharov * for missing vdevs). 24666f793812SPavel Zakharov * This limitation also prevents users from accidentally 24676f793812SPavel Zakharov * opening the pool in RW mode during data recovery and 24686f793812SPavel Zakharov * damaging it further. 24696f793812SPavel Zakharov */ 24706f793812SPavel Zakharov spa_load_note(spa, "pools with missing top-level " 24716f793812SPavel Zakharov "vdevs can only be opened in read-only mode."); 24726f793812SPavel Zakharov error = SET_ERROR(ENXIO); 24736f793812SPavel Zakharov } else { 24746f793812SPavel Zakharov spa_load_note(spa, "current settings allow for maximum " 24756f793812SPavel Zakharov "%lld missing top-level vdevs at this stage.", 24766f793812SPavel Zakharov (u_longlong_t)spa->spa_missing_tvds_allowed); 24776f793812SPavel Zakharov } 24786f793812SPavel Zakharov } 24793ee8c80cSPavel Zakharov if (error != 0) { 24803ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to open vdev tree [error=%d]", 24813ee8c80cSPavel Zakharov error); 24823ee8c80cSPavel Zakharov } 24836f793812SPavel Zakharov if (spa->spa_missing_tvds != 0 || error != 0) 24846f793812SPavel Zakharov vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); 24851fd3785fSPavel Zakharov 24861fd3785fSPavel Zakharov return (error); 24871fd3785fSPavel Zakharov } 24881fd3785fSPavel Zakharov 24896f793812SPavel Zakharov /* 24906f793812SPavel Zakharov * We need to validate the vdev labels against the configuration that 24916f793812SPavel Zakharov * we have in hand. This function is called twice: first with an untrusted 24926f793812SPavel Zakharov * config, then with a trusted config. The validation is more strict when the 24936f793812SPavel Zakharov * config is trusted. 24946f793812SPavel Zakharov */ 24951fd3785fSPavel Zakharov static int 24966f793812SPavel Zakharov spa_ld_validate_vdevs(spa_t *spa) 24971fd3785fSPavel Zakharov { 24981fd3785fSPavel Zakharov int error = 0; 24991fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 2500fa9e4066Sahrens 25016f793812SPavel Zakharov spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 25026f793812SPavel Zakharov error = vdev_validate(rvd); 25036f793812SPavel Zakharov spa_config_exit(spa, SCL_ALL, FTAG); 2504560e6e96Seschrock 25056f793812SPavel Zakharov if (error != 0) { 25066f793812SPavel Zakharov spa_load_failed(spa, "vdev_validate failed [error=%d]", error); 25076f793812SPavel Zakharov return (error); 25086f793812SPavel Zakharov } 25091195e687SMark J Musante 25106f793812SPavel Zakharov if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 25116f793812SPavel Zakharov spa_load_failed(spa, "cannot open vdev tree after invalidating " 25126f793812SPavel Zakharov "some vdevs"); 25136f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 25146f793812SPavel Zakharov return (SET_ERROR(ENXIO)); 2515560e6e96Seschrock } 2516560e6e96Seschrock 25171fd3785fSPavel Zakharov return (0); 25181fd3785fSPavel Zakharov } 25191fd3785fSPavel Zakharov 2520*86714001SSerapheim Dimitropoulos static void 2521*86714001SSerapheim Dimitropoulos spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) 2522*86714001SSerapheim Dimitropoulos { 2523*86714001SSerapheim Dimitropoulos spa->spa_state = POOL_STATE_ACTIVE; 2524*86714001SSerapheim Dimitropoulos spa->spa_ubsync = spa->spa_uberblock; 2525*86714001SSerapheim Dimitropoulos spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2526*86714001SSerapheim Dimitropoulos TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2527*86714001SSerapheim Dimitropoulos spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2528*86714001SSerapheim Dimitropoulos spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2529*86714001SSerapheim Dimitropoulos spa->spa_claim_max_txg = spa->spa_first_txg; 2530*86714001SSerapheim Dimitropoulos spa->spa_prev_software_version = ub->ub_software_version; 2531*86714001SSerapheim Dimitropoulos } 2532*86714001SSerapheim Dimitropoulos 25331fd3785fSPavel Zakharov static int 25346f793812SPavel Zakharov spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) 25351fd3785fSPavel Zakharov { 25361fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 25371fd3785fSPavel Zakharov nvlist_t *label; 25381fd3785fSPavel Zakharov uberblock_t *ub = &spa->spa_uberblock; 25391fd3785fSPavel Zakharov 2540*86714001SSerapheim Dimitropoulos /* 2541*86714001SSerapheim Dimitropoulos * If we are opening the checkpointed state of the pool by 2542*86714001SSerapheim Dimitropoulos * rewinding to it, at this point we will have written the 2543*86714001SSerapheim Dimitropoulos * checkpointed uberblock to the vdev labels, so searching 2544*86714001SSerapheim Dimitropoulos * the labels will find the right uberblock. However, if 2545*86714001SSerapheim Dimitropoulos * we are opening the checkpointed state read-only, we have 2546*86714001SSerapheim Dimitropoulos * not modified the labels. Therefore, we must ignore the 2547*86714001SSerapheim Dimitropoulos * labels and continue using the spa_uberblock that was set 2548*86714001SSerapheim Dimitropoulos * by spa_ld_checkpoint_rewind. 2549*86714001SSerapheim Dimitropoulos * 2550*86714001SSerapheim Dimitropoulos * Note that it would be fine to ignore the labels when 2551*86714001SSerapheim Dimitropoulos * rewinding (opening writeable) as well. However, if we 2552*86714001SSerapheim Dimitropoulos * crash just after writing the labels, we will end up 2553*86714001SSerapheim Dimitropoulos * searching the labels. Doing so in the common case means 2554*86714001SSerapheim Dimitropoulos * that this code path gets exercised normally, rather than 2555*86714001SSerapheim Dimitropoulos * just in the edge case. 2556*86714001SSerapheim Dimitropoulos */ 2557*86714001SSerapheim Dimitropoulos if (ub->ub_checkpoint_txg != 0 && 2558*86714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa)) { 2559*86714001SSerapheim Dimitropoulos spa_ld_select_uberblock_done(spa, ub); 2560*86714001SSerapheim Dimitropoulos return (0); 2561*86714001SSerapheim Dimitropoulos } 2562*86714001SSerapheim Dimitropoulos 2563fa9e4066Sahrens /* 2564fa9e4066Sahrens * Find the best uberblock. 2565fa9e4066Sahrens */ 2566ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2567fa9e4066Sahrens 2568fa9e4066Sahrens /* 2569fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2570fa9e4066Sahrens */ 2571ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2572ad135b5dSChristopher Siden nvlist_free(label); 25733ee8c80cSPavel Zakharov spa_load_failed(spa, "no valid uberblock found"); 25741195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2575ad135b5dSChristopher Siden } 2576ea8dc4b6Seschrock 25773ee8c80cSPavel Zakharov spa_load_note(spa, "using uberblock with txg=%llu", 25783ee8c80cSPavel Zakharov (u_longlong_t)ub->ub_txg); 25793ee8c80cSPavel Zakharov 2580ea8dc4b6Seschrock /* 2581ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2582ea8dc4b6Seschrock */ 2583ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2584ad135b5dSChristopher Siden nvlist_free(label); 25853ee8c80cSPavel Zakharov spa_load_failed(spa, "version %llu is not supported", 25863ee8c80cSPavel Zakharov (u_longlong_t)ub->ub_version); 25871195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2588ad135b5dSChristopher Siden } 2589ad135b5dSChristopher Siden 2590ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2591ad135b5dSChristopher Siden nvlist_t *features; 2592ad135b5dSChristopher Siden 2593ad135b5dSChristopher Siden /* 2594ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2595ad135b5dSChristopher Siden * MOS in the label, return failure. 2596ad135b5dSChristopher Siden */ 25973ee8c80cSPavel Zakharov if (label == NULL) { 25983ee8c80cSPavel Zakharov spa_load_failed(spa, "label config unavailable"); 25993ee8c80cSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 26003ee8c80cSPavel Zakharov ENXIO)); 26013ee8c80cSPavel Zakharov } 26023ee8c80cSPavel Zakharov 26033ee8c80cSPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, 26043ee8c80cSPavel Zakharov &features) != 0) { 2605ad135b5dSChristopher Siden nvlist_free(label); 26063ee8c80cSPavel Zakharov spa_load_failed(spa, "invalid label: '%s' missing", 26073ee8c80cSPavel Zakharov ZPOOL_CONFIG_FEATURES_FOR_READ); 2608ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2609ad135b5dSChristopher Siden ENXIO)); 2610ad135b5dSChristopher Siden } 2611ad135b5dSChristopher Siden 2612ad135b5dSChristopher Siden /* 2613ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2614ad135b5dSChristopher Siden * from the label. 2615ad135b5dSChristopher Siden */ 2616ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2617ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2618ad135b5dSChristopher Siden } 2619ad135b5dSChristopher Siden 2620ad135b5dSChristopher Siden nvlist_free(label); 2621ad135b5dSChristopher Siden 2622ad135b5dSChristopher Siden /* 2623ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2624ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2625ad135b5dSChristopher Siden * cannot open a pool. 2626ad135b5dSChristopher Siden */ 2627ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2628ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2629ad135b5dSChristopher Siden 2630ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2631ad135b5dSChristopher Siden 0); 2632ad135b5dSChristopher Siden 2633ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2634ad135b5dSChristopher Siden NULL); nvp != NULL; 2635ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2636ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2637ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2638ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2639ad135b5dSChristopher Siden } 2640ad135b5dSChristopher Siden } 2641ad135b5dSChristopher Siden 2642ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2643ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2644ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2645ad135b5dSChristopher Siden nvlist_free(unsup_feat); 26463ee8c80cSPavel Zakharov spa_load_failed(spa, "some features are unsupported"); 2647ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2648ad135b5dSChristopher Siden ENOTSUP)); 2649ad135b5dSChristopher Siden } 2650ad135b5dSChristopher Siden 2651ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2652ad135b5dSChristopher Siden } 2653fa9e4066Sahrens 26541195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 26551195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26566f793812SPavel Zakharov spa_try_repair(spa, spa->spa_config); 26571195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 26581195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 26591195e687SMark J Musante spa->spa_config_splitting = NULL; 2660fa9e4066Sahrens } 2661fa9e4066Sahrens 2662fa9e4066Sahrens /* 2663fa9e4066Sahrens * Initialize internal SPA structures. 2664fa9e4066Sahrens */ 2665*86714001SSerapheim Dimitropoulos spa_ld_select_uberblock_done(spa, ub); 2666b24ab676SJeff Bonwick 26671fd3785fSPavel Zakharov return (0); 26681fd3785fSPavel Zakharov } 26691fd3785fSPavel Zakharov 26701fd3785fSPavel Zakharov static int 26711fd3785fSPavel Zakharov spa_ld_open_rootbp(spa_t *spa) 26721fd3785fSPavel Zakharov { 26731fd3785fSPavel Zakharov int error = 0; 26741fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 26755cabbc6bSPrashanth Sreenivasa 2676ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 26773ee8c80cSPavel Zakharov if (error != 0) { 26783ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " 26793ee8c80cSPavel Zakharov "[error=%d]", error); 26801195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26813ee8c80cSPavel Zakharov } 2682fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2683fa9e4066Sahrens 26841fd3785fSPavel Zakharov return (0); 26851fd3785fSPavel Zakharov } 26861fd3785fSPavel Zakharov 26871fd3785fSPavel Zakharov static int 2688*86714001SSerapheim Dimitropoulos spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, 26896f793812SPavel Zakharov boolean_t reloading) 26901fd3785fSPavel Zakharov { 26916f793812SPavel Zakharov vdev_t *mrvd, *rvd = spa->spa_root_vdev; 26926f793812SPavel Zakharov nvlist_t *nv, *mos_config, *policy; 26936f793812SPavel Zakharov int error = 0, copy_error; 26946f793812SPavel Zakharov uint64_t healthy_tvds, healthy_tvds_mos; 26956f793812SPavel Zakharov uint64_t mos_config_txg; 26961fd3785fSPavel Zakharov 26973ee8c80cSPavel Zakharov if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) 26983ee8c80cSPavel Zakharov != 0) 26991195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2700fa9e4066Sahrens 27015cabbc6bSPrashanth Sreenivasa /* 27026f793812SPavel Zakharov * If we're assembling a pool from a split, the config provided is 27036f793812SPavel Zakharov * already trusted so there is nothing to do. 27045cabbc6bSPrashanth Sreenivasa */ 27056f793812SPavel Zakharov if (type == SPA_IMPORT_ASSEMBLE) 27066f793812SPavel Zakharov return (0); 27075cabbc6bSPrashanth Sreenivasa 27086f793812SPavel Zakharov healthy_tvds = spa_healthy_core_tvds(spa); 27096f793812SPavel Zakharov 27106f793812SPavel Zakharov if (load_nvlist(spa, spa->spa_config_object, &mos_config) 27116f793812SPavel Zakharov != 0) { 27126f793812SPavel Zakharov spa_load_failed(spa, "unable to retrieve MOS config"); 27136f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27146f793812SPavel Zakharov } 27156f793812SPavel Zakharov 27166f793812SPavel Zakharov /* 27176f793812SPavel Zakharov * If we are doing an open, pool owner wasn't verified yet, thus do 27186f793812SPavel Zakharov * the verification here. 27196f793812SPavel Zakharov */ 27206f793812SPavel Zakharov if (spa->spa_load_state == SPA_LOAD_OPEN) { 27216f793812SPavel Zakharov error = spa_verify_host(spa, mos_config); 27226f793812SPavel Zakharov if (error != 0) { 27235cabbc6bSPrashanth Sreenivasa nvlist_free(mos_config); 27246f793812SPavel Zakharov return (error); 27255cabbc6bSPrashanth Sreenivasa } 27266f793812SPavel Zakharov } 27276f793812SPavel Zakharov 27286f793812SPavel Zakharov nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); 27296f793812SPavel Zakharov 27306f793812SPavel Zakharov spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 27315cabbc6bSPrashanth Sreenivasa 27326f793812SPavel Zakharov /* 27336f793812SPavel Zakharov * Build a new vdev tree from the trusted config 27346f793812SPavel Zakharov */ 27356f793812SPavel Zakharov VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 27366f793812SPavel Zakharov 27376f793812SPavel Zakharov /* 27386f793812SPavel Zakharov * Vdev paths in the MOS may be obsolete. If the untrusted config was 27396f793812SPavel Zakharov * obtained by scanning /dev/dsk, then it will have the right vdev 27406f793812SPavel Zakharov * paths. We update the trusted MOS config with this information. 27416f793812SPavel Zakharov * We first try to copy the paths with vdev_copy_path_strict, which 27426f793812SPavel Zakharov * succeeds only when both configs have exactly the same vdev tree. 27436f793812SPavel Zakharov * If that fails, we fall back to a more flexible method that has a 27446f793812SPavel Zakharov * best effort policy. 27456f793812SPavel Zakharov */ 27466f793812SPavel Zakharov copy_error = vdev_copy_path_strict(rvd, mrvd); 27476f793812SPavel Zakharov if (copy_error != 0 || spa_load_print_vdev_tree) { 27486f793812SPavel Zakharov spa_load_note(spa, "provided vdev tree:"); 27496f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 27506f793812SPavel Zakharov spa_load_note(spa, "MOS vdev tree:"); 27516f793812SPavel Zakharov vdev_dbgmsg_print_tree(mrvd, 2); 27526f793812SPavel Zakharov } 27536f793812SPavel Zakharov if (copy_error != 0) { 27546f793812SPavel Zakharov spa_load_note(spa, "vdev_copy_path_strict failed, falling " 27556f793812SPavel Zakharov "back to vdev_copy_path_relaxed"); 27566f793812SPavel Zakharov vdev_copy_path_relaxed(rvd, mrvd); 27576f793812SPavel Zakharov } 27586f793812SPavel Zakharov 27596f793812SPavel Zakharov vdev_close(rvd); 27606f793812SPavel Zakharov vdev_free(rvd); 27616f793812SPavel Zakharov spa->spa_root_vdev = mrvd; 27626f793812SPavel Zakharov rvd = mrvd; 27636f793812SPavel Zakharov spa_config_exit(spa, SCL_ALL, FTAG); 27646f793812SPavel Zakharov 27656f793812SPavel Zakharov /* 27666f793812SPavel Zakharov * We will use spa_config if we decide to reload the spa or if spa_load 27676f793812SPavel Zakharov * fails and we rewind. We must thus regenerate the config using the 27686f793812SPavel Zakharov * MOS information with the updated paths. Rewind policy is an import 27696f793812SPavel Zakharov * setting and is not in the MOS. We copy it over to our new, trusted 27706f793812SPavel Zakharov * config. 27716f793812SPavel Zakharov */ 27726f793812SPavel Zakharov mos_config_txg = fnvlist_lookup_uint64(mos_config, 27736f793812SPavel Zakharov ZPOOL_CONFIG_POOL_TXG); 27746f793812SPavel Zakharov nvlist_free(mos_config); 27756f793812SPavel Zakharov mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); 27766f793812SPavel Zakharov if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_REWIND_POLICY, 27776f793812SPavel Zakharov &policy) == 0) 27786f793812SPavel Zakharov fnvlist_add_nvlist(mos_config, ZPOOL_REWIND_POLICY, policy); 27796f793812SPavel Zakharov spa_config_set(spa, mos_config); 27806f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_MOS; 27816f793812SPavel Zakharov 27826f793812SPavel Zakharov /* 27836f793812SPavel Zakharov * Now that we got the config from the MOS, we should be more strict 27846f793812SPavel Zakharov * in checking blkptrs and can make assumptions about the consistency 27856f793812SPavel Zakharov * of the vdev tree. spa_trust_config must be set to true before opening 27866f793812SPavel Zakharov * vdevs in order for them to be writeable. 27876f793812SPavel Zakharov */ 27886f793812SPavel Zakharov spa->spa_trust_config = B_TRUE; 27896f793812SPavel Zakharov 27906f793812SPavel Zakharov /* 27916f793812SPavel Zakharov * Open and validate the new vdev tree 27926f793812SPavel Zakharov */ 27936f793812SPavel Zakharov error = spa_ld_open_vdevs(spa); 27946f793812SPavel Zakharov if (error != 0) 27956f793812SPavel Zakharov return (error); 27966f793812SPavel Zakharov 27976f793812SPavel Zakharov error = spa_ld_validate_vdevs(spa); 27986f793812SPavel Zakharov if (error != 0) 27996f793812SPavel Zakharov return (error); 28006f793812SPavel Zakharov 28016f793812SPavel Zakharov if (copy_error != 0 || spa_load_print_vdev_tree) { 28026f793812SPavel Zakharov spa_load_note(spa, "final vdev tree:"); 28036f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 28046f793812SPavel Zakharov } 28056f793812SPavel Zakharov 28066f793812SPavel Zakharov if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && 28076f793812SPavel Zakharov !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { 28085cabbc6bSPrashanth Sreenivasa /* 28096f793812SPavel Zakharov * Sanity check to make sure that we are indeed loading the 28106f793812SPavel Zakharov * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds 28116f793812SPavel Zakharov * in the config provided and they happened to be the only ones 28126f793812SPavel Zakharov * to have the latest uberblock, we could involuntarily perform 28136f793812SPavel Zakharov * an extreme rewind. 28145cabbc6bSPrashanth Sreenivasa */ 28156f793812SPavel Zakharov healthy_tvds_mos = spa_healthy_core_tvds(spa); 28166f793812SPavel Zakharov if (healthy_tvds_mos - healthy_tvds >= 28176f793812SPavel Zakharov SPA_SYNC_MIN_VDEVS) { 28186f793812SPavel Zakharov spa_load_note(spa, "config provided misses too many " 28196f793812SPavel Zakharov "top-level vdevs compared to MOS (%lld vs %lld). ", 28206f793812SPavel Zakharov (u_longlong_t)healthy_tvds, 28216f793812SPavel Zakharov (u_longlong_t)healthy_tvds_mos); 28226f793812SPavel Zakharov spa_load_note(spa, "vdev tree:"); 28236f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 28246f793812SPavel Zakharov if (reloading) { 28256f793812SPavel Zakharov spa_load_failed(spa, "config was already " 28266f793812SPavel Zakharov "provided from MOS. Aborting."); 28276f793812SPavel Zakharov return (spa_vdev_err(rvd, 28286f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA, EIO)); 28296f793812SPavel Zakharov } 28306f793812SPavel Zakharov spa_load_note(spa, "spa must be reloaded using MOS " 28316f793812SPavel Zakharov "config"); 28326f793812SPavel Zakharov return (SET_ERROR(EAGAIN)); 28333ee8c80cSPavel Zakharov } 28345cabbc6bSPrashanth Sreenivasa } 28355cabbc6bSPrashanth Sreenivasa 28366f793812SPavel Zakharov error = spa_check_for_missing_logs(spa); 28376f793812SPavel Zakharov if (error != 0) 28386f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 28396f793812SPavel Zakharov 28406f793812SPavel Zakharov if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { 28416f793812SPavel Zakharov spa_load_failed(spa, "uberblock guid sum doesn't match MOS " 28426f793812SPavel Zakharov "guid sum (%llu != %llu)", 28436f793812SPavel Zakharov (u_longlong_t)spa->spa_uberblock.ub_guid_sum, 28446f793812SPavel Zakharov (u_longlong_t)rvd->vdev_guid_sum); 28456f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 28466f793812SPavel Zakharov ENXIO)); 28476f793812SPavel Zakharov } 28486f793812SPavel Zakharov 28491fd3785fSPavel Zakharov return (0); 28501fd3785fSPavel Zakharov } 28511fd3785fSPavel Zakharov 28521fd3785fSPavel Zakharov static int 28531fd3785fSPavel Zakharov spa_ld_open_indirect_vdev_metadata(spa_t *spa) 28541fd3785fSPavel Zakharov { 28551fd3785fSPavel Zakharov int error = 0; 28561fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 28571fd3785fSPavel Zakharov 28585cabbc6bSPrashanth Sreenivasa /* 28595cabbc6bSPrashanth Sreenivasa * Everything that we read before spa_remove_init() must be stored 28605cabbc6bSPrashanth Sreenivasa * on concreted vdevs. Therefore we do this as early as possible. 28615cabbc6bSPrashanth Sreenivasa */ 28623ee8c80cSPavel Zakharov error = spa_remove_init(spa); 28633ee8c80cSPavel Zakharov if (error != 0) { 28643ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_remove_init failed [error=%d]", 28653ee8c80cSPavel Zakharov error); 28665cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 28673ee8c80cSPavel Zakharov } 28685cabbc6bSPrashanth Sreenivasa 28691fd3785fSPavel Zakharov /* 28701fd3785fSPavel Zakharov * Retrieve information needed to condense indirect vdev mappings. 28711fd3785fSPavel Zakharov */ 28721fd3785fSPavel Zakharov error = spa_condense_init(spa); 28731fd3785fSPavel Zakharov if (error != 0) { 28743ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_condense_init failed [error=%d]", 28753ee8c80cSPavel Zakharov error); 28761fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 28771fd3785fSPavel Zakharov } 28781fd3785fSPavel Zakharov 28791fd3785fSPavel Zakharov return (0); 28801fd3785fSPavel Zakharov } 28811fd3785fSPavel Zakharov 28821fd3785fSPavel Zakharov static int 28833ee8c80cSPavel Zakharov spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) 28841fd3785fSPavel Zakharov { 28851fd3785fSPavel Zakharov int error = 0; 28861fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 28871fd3785fSPavel Zakharov 2888ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2889ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 289057221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2891ad135b5dSChristopher Siden 2892ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 28933ee8c80cSPavel Zakharov &spa->spa_feat_for_read_obj, B_TRUE) != 0) { 2894ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2895ad135b5dSChristopher Siden } 2896ad135b5dSChristopher Siden 2897ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 28983ee8c80cSPavel Zakharov &spa->spa_feat_for_write_obj, B_TRUE) != 0) { 2899ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2900ad135b5dSChristopher Siden } 2901ad135b5dSChristopher Siden 2902ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 29033ee8c80cSPavel Zakharov &spa->spa_feat_desc_obj, B_TRUE) != 0) { 2904ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2905ad135b5dSChristopher Siden } 2906ad135b5dSChristopher Siden 290757221772SChristopher Siden enabled_feat = fnvlist_alloc(); 290857221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2909ad135b5dSChristopher Siden 29102acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 291157221772SChristopher Siden unsup_feat, enabled_feat)) 2912ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2913ad135b5dSChristopher Siden 29143ee8c80cSPavel Zakharov if (spa_writeable(spa) || 29153ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_TRYIMPORT) { 29162acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 291757221772SChristopher Siden unsup_feat, enabled_feat)) { 29181fd3785fSPavel Zakharov *missing_feat_writep = B_TRUE; 291957221772SChristopher Siden } 2920ad135b5dSChristopher Siden } 2921ad135b5dSChristopher Siden 292257221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 292357221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 292457221772SChristopher Siden 2925ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 292657221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 292757221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2928ad135b5dSChristopher Siden } 2929ad135b5dSChristopher Siden 293057221772SChristopher Siden fnvlist_free(enabled_feat); 293157221772SChristopher Siden fnvlist_free(unsup_feat); 2932ad135b5dSChristopher Siden 2933ad135b5dSChristopher Siden if (!missing_feat_read) { 2934ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2935ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2936ad135b5dSChristopher Siden } 2937ad135b5dSChristopher Siden 2938ad135b5dSChristopher Siden /* 2939ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2940ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2941ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2942ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2943ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2944ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2945ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2946ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2947ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2948ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2949ad135b5dSChristopher Siden * available for open in read-only mode. 2950ad135b5dSChristopher Siden * 2951ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2952ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2953ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2954ad135b5dSChristopher Siden * userland in order to know whether to display the 2955ad135b5dSChristopher Siden * abovementioned note. 2956ad135b5dSChristopher Siden */ 29571fd3785fSPavel Zakharov if (missing_feat_read || (*missing_feat_writep && 2958ad135b5dSChristopher Siden spa_writeable(spa))) { 29593ee8c80cSPavel Zakharov spa_load_failed(spa, "pool uses unsupported features"); 2960ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2961ad135b5dSChristopher Siden ENOTSUP)); 2962ad135b5dSChristopher Siden } 296343466aaeSMax Grossman 296443466aaeSMax Grossman /* 296543466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 296643466aaeSMax Grossman * cache during SPA initialization. 296743466aaeSMax Grossman */ 296843466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 296943466aaeSMax Grossman uint64_t refcount; 297043466aaeSMax Grossman 297143466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 297243466aaeSMax Grossman &spa_feature_table[i], &refcount); 297343466aaeSMax Grossman if (error == 0) { 297443466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 297543466aaeSMax Grossman } else if (error == ENOTSUP) { 297643466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 297743466aaeSMax Grossman SPA_FEATURE_DISABLED; 297843466aaeSMax Grossman } else { 29793ee8c80cSPavel Zakharov spa_load_failed(spa, "error getting refcount " 29803ee8c80cSPavel Zakharov "for feature %s [error=%d]", 29813ee8c80cSPavel Zakharov spa_feature_table[i].fi_guid, error); 298243466aaeSMax Grossman return (spa_vdev_err(rvd, 298343466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 298443466aaeSMax Grossman } 298543466aaeSMax Grossman } 298643466aaeSMax Grossman } 298743466aaeSMax Grossman 298843466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 298943466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 29903ee8c80cSPavel Zakharov &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) 299143466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2992ad135b5dSChristopher Siden } 2993ad135b5dSChristopher Siden 29941fd3785fSPavel Zakharov return (0); 29951fd3785fSPavel Zakharov } 29961fd3785fSPavel Zakharov 29971fd3785fSPavel Zakharov static int 29981fd3785fSPavel Zakharov spa_ld_load_special_directories(spa_t *spa) 29991fd3785fSPavel Zakharov { 30001fd3785fSPavel Zakharov int error = 0; 30011fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 30021fd3785fSPavel Zakharov 3003ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 3004ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 3005ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 30063ee8c80cSPavel Zakharov if (error != 0) { 30073ee8c80cSPavel Zakharov spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); 3008ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30093ee8c80cSPavel Zakharov } 3010ad135b5dSChristopher Siden 30111fd3785fSPavel Zakharov return (0); 30121fd3785fSPavel Zakharov } 3013871a9500SMark J Musante 30141fd3785fSPavel Zakharov static int 30151fd3785fSPavel Zakharov spa_ld_get_props(spa_t *spa) 30161fd3785fSPavel Zakharov { 30171fd3785fSPavel Zakharov int error = 0; 30181fd3785fSPavel Zakharov uint64_t obj; 30191fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 3020fa9e4066Sahrens 302145818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 302245818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 302345818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 302445818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 302545818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 302645818ee1SMatthew Ahrens if (error == ENOENT) { 302745818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 302845818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 302945818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 303045818ee1SMatthew Ahrens } else if (error != 0) { 30313ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to retrieve checksum salt from " 30323ee8c80cSPavel Zakharov "MOS [error=%d]", error); 303345818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 303445818ee1SMatthew Ahrens } 303545818ee1SMatthew Ahrens 30363ee8c80cSPavel Zakharov if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) 3037cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3038cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 30393ee8c80cSPavel Zakharov if (error != 0) { 30403ee8c80cSPavel Zakharov spa_load_failed(spa, "error opening deferred-frees bpobj " 30413ee8c80cSPavel Zakharov "[error=%d]", error); 30421195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30433ee8c80cSPavel Zakharov } 3044fa9e4066Sahrens 304599653d4eSeschrock /* 304699653d4eSeschrock * Load the bit that tells us to use the new accounting function 304799653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 304899653d4eSeschrock * be present. 304999653d4eSeschrock */ 30503ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); 30511195e687SMark J Musante if (error != 0 && error != ENOENT) 30521195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 305399653d4eSeschrock 30543f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 30553ee8c80cSPavel Zakharov &spa->spa_creation_version, B_FALSE); 30563f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 30573f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30583f9d6ad7SLin Ling 3059fa9e4066Sahrens /* 3060ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 3061ea8dc4b6Seschrock * not be present. 3062fa9e4066Sahrens */ 30633ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, 30643ee8c80cSPavel Zakharov B_FALSE); 30651195e687SMark J Musante if (error != 0 && error != ENOENT) 30661195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3067ea8dc4b6Seschrock 30681195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 30693ee8c80cSPavel Zakharov &spa->spa_errlog_scrub, B_FALSE); 30701195e687SMark J Musante if (error != 0 && error != ENOENT) 30711195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3072ea8dc4b6Seschrock 307306eeb2adSek /* 307406eeb2adSek * Load the history object. If we have an older pool, this 307506eeb2adSek * will not be present. 307606eeb2adSek */ 30773ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); 30781195e687SMark J Musante if (error != 0 && error != ENOENT) 30791195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30801195e687SMark J Musante 3081215198a6SJoe Stein /* 3082215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 3083215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 3084215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 3085215198a6SJoe Stein * spa_sync_config_object. 3086215198a6SJoe Stein */ 3087215198a6SJoe Stein 3088215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 3089215198a6SJoe Stein nvlist_t *mos_config; 30903ee8c80cSPavel Zakharov if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { 30913ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to retrieve MOS config"); 3092215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30933ee8c80cSPavel Zakharov } 3094215198a6SJoe Stein 3095215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 30963ee8c80cSPavel Zakharov &spa->spa_all_vdev_zaps, B_FALSE); 3097215198a6SJoe Stein 3098555da511SPaul Dagnelie if (error == ENOENT) { 3099555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 3100555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 3101555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 3102555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 3103555da511SPaul Dagnelie } else if (error != 0) { 3104215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3105555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 3106215198a6SJoe Stein /* 3107215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 3108215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 3109215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 3110215198a6SJoe Stein */ 3111215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 3112215198a6SJoe Stein /* 3113215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 3114215198a6SJoe Stein * before this. Better be sure of it. 3115215198a6SJoe Stein */ 3116215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 3117215198a6SJoe Stein } 3118215198a6SJoe Stein nvlist_free(mos_config); 3119215198a6SJoe Stein 31201fd3785fSPavel Zakharov spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 31211fd3785fSPavel Zakharov 31223ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, 31233ee8c80cSPavel Zakharov B_FALSE); 31241fd3785fSPavel Zakharov if (error && error != ENOENT) 31251fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 31261fd3785fSPavel Zakharov 31271fd3785fSPavel Zakharov if (error == 0) { 31281fd3785fSPavel Zakharov uint64_t autoreplace; 31291fd3785fSPavel Zakharov 31301fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 31311fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 31321fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 31331fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 31341fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 31351fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 31361fd3785fSPavel Zakharov &spa->spa_dedup_ditto); 31371fd3785fSPavel Zakharov 31381fd3785fSPavel Zakharov spa->spa_autoreplace = (autoreplace != 0); 31391fd3785fSPavel Zakharov } 31401fd3785fSPavel Zakharov 31416f793812SPavel Zakharov /* 31426f793812SPavel Zakharov * If we are importing a pool with missing top-level vdevs, 31436f793812SPavel Zakharov * we enforce that the pool doesn't panic or get suspended on 31446f793812SPavel Zakharov * error since the likelihood of missing data is extremely high. 31456f793812SPavel Zakharov */ 31466f793812SPavel Zakharov if (spa->spa_missing_tvds > 0 && 31476f793812SPavel Zakharov spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && 31486f793812SPavel Zakharov spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 31496f793812SPavel Zakharov spa_load_note(spa, "forcing failmode to 'continue' " 31506f793812SPavel Zakharov "as some top level vdevs are missing"); 31516f793812SPavel Zakharov spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; 31526f793812SPavel Zakharov } 31536f793812SPavel Zakharov 31541fd3785fSPavel Zakharov return (0); 31551fd3785fSPavel Zakharov } 31561fd3785fSPavel Zakharov 31571fd3785fSPavel Zakharov static int 31581fd3785fSPavel Zakharov spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) 31591fd3785fSPavel Zakharov { 31601fd3785fSPavel Zakharov int error = 0; 31611fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 31621fd3785fSPavel Zakharov 31631195e687SMark J Musante /* 31641195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 31651195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 31661195e687SMark J Musante * devices. 31671195e687SMark J Musante */ 316806eeb2adSek 316999653d4eSeschrock /* 317099653d4eSeschrock * Load any hot spares for this pool. 317199653d4eSeschrock */ 31723ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, 31733ee8c80cSPavel Zakharov B_FALSE); 31741195e687SMark J Musante if (error != 0 && error != ENOENT) 31751195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 31761195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3177e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 3178fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 31793ee8c80cSPavel Zakharov &spa->spa_spares.sav_config) != 0) { 31803ee8c80cSPavel Zakharov spa_load_failed(spa, "error loading spares nvlist"); 31811195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 31823ee8c80cSPavel Zakharov } 318399653d4eSeschrock 3184e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 318599653d4eSeschrock spa_load_spares(spa); 3186e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 31871195e687SMark J Musante } else if (error == 0) { 31881195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 318999653d4eSeschrock } 319099653d4eSeschrock 3191fa94a07fSbrendan /* 3192fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 3193fa94a07fSbrendan */ 31941195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 31953ee8c80cSPavel Zakharov &spa->spa_l2cache.sav_object, B_FALSE); 31961195e687SMark J Musante if (error != 0 && error != ENOENT) 31971195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 31981195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3199fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 3200fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 32013ee8c80cSPavel Zakharov &spa->spa_l2cache.sav_config) != 0) { 32023ee8c80cSPavel Zakharov spa_load_failed(spa, "error loading l2cache nvlist"); 32031195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 32043ee8c80cSPavel Zakharov } 3205fa94a07fSbrendan 3206e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3207fa94a07fSbrendan spa_load_l2cache(spa); 3208e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 32091195e687SMark J Musante } else if (error == 0) { 32101195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 3211fa94a07fSbrendan } 3212fa94a07fSbrendan 32131fd3785fSPavel Zakharov return (0); 32141fd3785fSPavel Zakharov } 32151195e687SMark J Musante 32161fd3785fSPavel Zakharov static int 32173ee8c80cSPavel Zakharov spa_ld_load_vdev_metadata(spa_t *spa) 32181fd3785fSPavel Zakharov { 32191fd3785fSPavel Zakharov int error = 0; 32201fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 3221b1b8ab34Slling 32223d7072f8Seschrock /* 32233d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 32243d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 32253d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 32263d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 32273d7072f8Seschrock * over. 32283d7072f8Seschrock */ 32293ee8c80cSPavel Zakharov if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 32303d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 3231b693757aSEric Schrock /* 3232b693757aSEric Schrock * For the import case, this is done in spa_import(), because 3233b693757aSEric Schrock * at this point we're using the spare definitions from 3234b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 3235b693757aSEric Schrock */ 32363ee8c80cSPavel Zakharov if (spa->spa_load_state != SPA_LOAD_IMPORT) { 3237b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3238b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3239b693757aSEric Schrock } 3240b693757aSEric Schrock } 32413d7072f8Seschrock 3242ea8dc4b6Seschrock /* 32431fd3785fSPavel Zakharov * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. 3244ea8dc4b6Seschrock */ 32455cabbc6bSPrashanth Sreenivasa error = vdev_load(rvd); 32465cabbc6bSPrashanth Sreenivasa if (error != 0) { 32473ee8c80cSPavel Zakharov spa_load_failed(spa, "vdev_load failed [error=%d]", error); 32485cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 32495cabbc6bSPrashanth Sreenivasa } 32505cabbc6bSPrashanth Sreenivasa 3251fa9e4066Sahrens /* 32521fd3785fSPavel Zakharov * Propagate the leaf DTLs we just loaded all the way up the vdev tree. 3253fa9e4066Sahrens */ 3254e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3255fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 3256e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3257fa9e4066Sahrens 32581fd3785fSPavel Zakharov return (0); 32591fd3785fSPavel Zakharov } 32601fd3785fSPavel Zakharov 32611fd3785fSPavel Zakharov static int 32621fd3785fSPavel Zakharov spa_ld_load_dedup_tables(spa_t *spa) 32631fd3785fSPavel Zakharov { 32641fd3785fSPavel Zakharov int error = 0; 32651fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 32661fd3785fSPavel Zakharov 3267b24ab676SJeff Bonwick error = ddt_load(spa); 32683ee8c80cSPavel Zakharov if (error != 0) { 32693ee8c80cSPavel Zakharov spa_load_failed(spa, "ddt_load failed [error=%d]", error); 32701195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 32713ee8c80cSPavel Zakharov } 3272b24ab676SJeff Bonwick 32731fd3785fSPavel Zakharov return (0); 32741fd3785fSPavel Zakharov } 32751fd3785fSPavel Zakharov 32761fd3785fSPavel Zakharov static int 32771fd3785fSPavel Zakharov spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) 32781fd3785fSPavel Zakharov { 32791fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 3280485bbbf5SGeorge Wilson 32813ee8c80cSPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { 32823ee8c80cSPavel Zakharov boolean_t missing = spa_check_logs(spa); 32833ee8c80cSPavel Zakharov if (missing) { 32846f793812SPavel Zakharov if (spa->spa_missing_tvds != 0) { 32856f793812SPavel Zakharov spa_load_note(spa, "spa_check_logs failed " 32866f793812SPavel Zakharov "so dropping the logs"); 32876f793812SPavel Zakharov } else { 32886f793812SPavel Zakharov *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 32896f793812SPavel Zakharov spa_load_failed(spa, "spa_check_logs failed"); 32906f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, 32916f793812SPavel Zakharov ENXIO)); 32926f793812SPavel Zakharov } 32933ee8c80cSPavel Zakharov } 3294b24ab676SJeff Bonwick } 3295b24ab676SJeff Bonwick 32961fd3785fSPavel Zakharov return (0); 32971fd3785fSPavel Zakharov } 32981fd3785fSPavel Zakharov 32991fd3785fSPavel Zakharov static int 33003ee8c80cSPavel Zakharov spa_ld_verify_pool_data(spa_t *spa) 33011fd3785fSPavel Zakharov { 33021fd3785fSPavel Zakharov int error = 0; 33031fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 33041fd3785fSPavel Zakharov 33051fd3785fSPavel Zakharov /* 33061fd3785fSPavel Zakharov * We've successfully opened the pool, verify that we're ready 33071fd3785fSPavel Zakharov * to start pushing transactions. 33081fd3785fSPavel Zakharov */ 33093ee8c80cSPavel Zakharov if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 33101fd3785fSPavel Zakharov error = spa_load_verify(spa); 33111fd3785fSPavel Zakharov if (error != 0) { 33123ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_load_verify failed " 33133ee8c80cSPavel Zakharov "[error=%d]", error); 33141fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 33151fd3785fSPavel Zakharov error)); 33161fd3785fSPavel Zakharov } 33171fd3785fSPavel Zakharov } 33181fd3785fSPavel Zakharov 33191fd3785fSPavel Zakharov return (0); 33201fd3785fSPavel Zakharov } 33211fd3785fSPavel Zakharov 33221fd3785fSPavel Zakharov static void 33231fd3785fSPavel Zakharov spa_ld_claim_log_blocks(spa_t *spa) 33241fd3785fSPavel Zakharov { 33251fd3785fSPavel Zakharov dmu_tx_t *tx; 33261fd3785fSPavel Zakharov dsl_pool_t *dp = spa_get_dsl(spa); 33271fd3785fSPavel Zakharov 33281fd3785fSPavel Zakharov /* 33291fd3785fSPavel Zakharov * Claim log blocks that haven't been committed yet. 33301fd3785fSPavel Zakharov * This must all happen in a single txg. 33311fd3785fSPavel Zakharov * Note: spa_claim_max_txg is updated by spa_claim_notify(), 33321fd3785fSPavel Zakharov * invoked from zil_claim_log_block()'s i/o done callback. 33331fd3785fSPavel Zakharov * Price of rollback is that we abandon the log. 33341fd3785fSPavel Zakharov */ 33351fd3785fSPavel Zakharov spa->spa_claiming = B_TRUE; 33361fd3785fSPavel Zakharov 33371fd3785fSPavel Zakharov tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 33381fd3785fSPavel Zakharov (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 33391fd3785fSPavel Zakharov zil_claim, tx, DS_FIND_CHILDREN); 33401fd3785fSPavel Zakharov dmu_tx_commit(tx); 33411fd3785fSPavel Zakharov 33421fd3785fSPavel Zakharov spa->spa_claiming = B_FALSE; 33431fd3785fSPavel Zakharov 33441fd3785fSPavel Zakharov spa_set_log_state(spa, SPA_LOG_GOOD); 33451fd3785fSPavel Zakharov } 33461fd3785fSPavel Zakharov 33471fd3785fSPavel Zakharov static void 33486f793812SPavel Zakharov spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, 3349*86714001SSerapheim Dimitropoulos boolean_t update_config_cache) 33501fd3785fSPavel Zakharov { 33511fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 33521fd3785fSPavel Zakharov int need_update = B_FALSE; 33531fd3785fSPavel Zakharov 33541fd3785fSPavel Zakharov /* 33551fd3785fSPavel Zakharov * If the config cache is stale, or we have uninitialized 33561fd3785fSPavel Zakharov * metaslabs (see spa_vdev_add()), then update the config. 33571fd3785fSPavel Zakharov * 33581fd3785fSPavel Zakharov * If this is a verbatim import, trust the current 33591fd3785fSPavel Zakharov * in-core spa_config and update the disk labels. 33601fd3785fSPavel Zakharov */ 3361*86714001SSerapheim Dimitropoulos if (update_config_cache || config_cache_txg != spa->spa_config_txg || 33623ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_IMPORT || 33633ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_RECOVER || 33641fd3785fSPavel Zakharov (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 33651fd3785fSPavel Zakharov need_update = B_TRUE; 33661fd3785fSPavel Zakharov 33671fd3785fSPavel Zakharov for (int c = 0; c < rvd->vdev_children; c++) 33681fd3785fSPavel Zakharov if (rvd->vdev_child[c]->vdev_ms_array == 0) 33691fd3785fSPavel Zakharov need_update = B_TRUE; 33701fd3785fSPavel Zakharov 33711fd3785fSPavel Zakharov /* 33721fd3785fSPavel Zakharov * Update the config cache asychronously in case we're the 33731fd3785fSPavel Zakharov * root pool, in which case the config cache isn't writable yet. 33741fd3785fSPavel Zakharov */ 33751fd3785fSPavel Zakharov if (need_update) 33761fd3785fSPavel Zakharov spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 33771fd3785fSPavel Zakharov } 33781fd3785fSPavel Zakharov 33796f793812SPavel Zakharov static void 33806f793812SPavel Zakharov spa_ld_prepare_for_reload(spa_t *spa) 33816f793812SPavel Zakharov { 33826f793812SPavel Zakharov int mode = spa->spa_mode; 33836f793812SPavel Zakharov int async_suspended = spa->spa_async_suspended; 33846f793812SPavel Zakharov 33856f793812SPavel Zakharov spa_unload(spa); 33866f793812SPavel Zakharov spa_deactivate(spa); 33876f793812SPavel Zakharov spa_activate(spa, mode); 33886f793812SPavel Zakharov 33896f793812SPavel Zakharov /* 33906f793812SPavel Zakharov * We save the value of spa_async_suspended as it gets reset to 0 by 33916f793812SPavel Zakharov * spa_unload(). We want to restore it back to the original value before 33926f793812SPavel Zakharov * returning as we might be calling spa_async_resume() later. 33936f793812SPavel Zakharov */ 33946f793812SPavel Zakharov spa->spa_async_suspended = async_suspended; 33956f793812SPavel Zakharov } 33966f793812SPavel Zakharov 33971fd3785fSPavel Zakharov static int 3398*86714001SSerapheim Dimitropoulos spa_ld_read_checkpoint_txg(spa_t *spa) 3399*86714001SSerapheim Dimitropoulos { 3400*86714001SSerapheim Dimitropoulos uberblock_t checkpoint; 3401*86714001SSerapheim Dimitropoulos int error = 0; 3402*86714001SSerapheim Dimitropoulos 3403*86714001SSerapheim Dimitropoulos ASSERT0(spa->spa_checkpoint_txg); 3404*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3405*86714001SSerapheim Dimitropoulos 3406*86714001SSerapheim Dimitropoulos error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 3407*86714001SSerapheim Dimitropoulos DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 3408*86714001SSerapheim Dimitropoulos sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 3409*86714001SSerapheim Dimitropoulos 3410*86714001SSerapheim Dimitropoulos if (error == ENOENT) 3411*86714001SSerapheim Dimitropoulos return (0); 3412*86714001SSerapheim Dimitropoulos 3413*86714001SSerapheim Dimitropoulos if (error != 0) 3414*86714001SSerapheim Dimitropoulos return (error); 3415*86714001SSerapheim Dimitropoulos 3416*86714001SSerapheim Dimitropoulos ASSERT3U(checkpoint.ub_txg, !=, 0); 3417*86714001SSerapheim Dimitropoulos ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); 3418*86714001SSerapheim Dimitropoulos ASSERT3U(checkpoint.ub_timestamp, !=, 0); 3419*86714001SSerapheim Dimitropoulos spa->spa_checkpoint_txg = checkpoint.ub_txg; 3420*86714001SSerapheim Dimitropoulos spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; 3421*86714001SSerapheim Dimitropoulos 3422*86714001SSerapheim Dimitropoulos return (0); 3423*86714001SSerapheim Dimitropoulos } 3424*86714001SSerapheim Dimitropoulos 3425*86714001SSerapheim Dimitropoulos static int 3426*86714001SSerapheim Dimitropoulos spa_ld_mos_init(spa_t *spa, spa_import_type_t type) 34271fd3785fSPavel Zakharov { 34281fd3785fSPavel Zakharov int error = 0; 34291fd3785fSPavel Zakharov 34303ee8c80cSPavel Zakharov ASSERT(MUTEX_HELD(&spa_namespace_lock)); 34316f793812SPavel Zakharov ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); 34323ee8c80cSPavel Zakharov 34331fd3785fSPavel Zakharov /* 34346f793812SPavel Zakharov * Never trust the config that is provided unless we are assembling 34356f793812SPavel Zakharov * a pool following a split. 34366f793812SPavel Zakharov * This means don't trust blkptrs and the vdev tree in general. This 34376f793812SPavel Zakharov * also effectively puts the spa in read-only mode since 34386f793812SPavel Zakharov * spa_writeable() checks for spa_trust_config to be true. 34396f793812SPavel Zakharov * We will later load a trusted config from the MOS. 34401fd3785fSPavel Zakharov */ 34416f793812SPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE) 34426f793812SPavel Zakharov spa->spa_trust_config = B_FALSE; 34436f793812SPavel Zakharov 34441fd3785fSPavel Zakharov /* 34451fd3785fSPavel Zakharov * Parse the config provided to create a vdev tree. 34461fd3785fSPavel Zakharov */ 34476f793812SPavel Zakharov error = spa_ld_parse_config(spa, type); 34481fd3785fSPavel Zakharov if (error != 0) 34491fd3785fSPavel Zakharov return (error); 34501fd3785fSPavel Zakharov 34511fd3785fSPavel Zakharov /* 34521fd3785fSPavel Zakharov * Now that we have the vdev tree, try to open each vdev. This involves 34531fd3785fSPavel Zakharov * opening the underlying physical device, retrieving its geometry and 34541fd3785fSPavel Zakharov * probing the vdev with a dummy I/O. The state of each vdev will be set 34551fd3785fSPavel Zakharov * based on the success of those operations. After this we'll be ready 34561fd3785fSPavel Zakharov * to read from the vdevs. 34571fd3785fSPavel Zakharov */ 34581fd3785fSPavel Zakharov error = spa_ld_open_vdevs(spa); 34591fd3785fSPavel Zakharov if (error != 0) 34601fd3785fSPavel Zakharov return (error); 34611fd3785fSPavel Zakharov 34621fd3785fSPavel Zakharov /* 34631fd3785fSPavel Zakharov * Read the label of each vdev and make sure that the GUIDs stored 34641fd3785fSPavel Zakharov * there match the GUIDs in the config provided. 34656f793812SPavel Zakharov * If we're assembling a new pool that's been split off from an 34666f793812SPavel Zakharov * existing pool, the labels haven't yet been updated so we skip 34676f793812SPavel Zakharov * validation for now. 34681fd3785fSPavel Zakharov */ 34696f793812SPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE) { 34706f793812SPavel Zakharov error = spa_ld_validate_vdevs(spa); 34716f793812SPavel Zakharov if (error != 0) 34726f793812SPavel Zakharov return (error); 34736f793812SPavel Zakharov } 34741fd3785fSPavel Zakharov 34751fd3785fSPavel Zakharov /* 3476*86714001SSerapheim Dimitropoulos * Read all vdev labels to find the best uberblock (i.e. latest, 3477*86714001SSerapheim Dimitropoulos * unless spa_load_max_txg is set) and store it in spa_uberblock. We 3478*86714001SSerapheim Dimitropoulos * get the list of features required to read blkptrs in the MOS from 3479*86714001SSerapheim Dimitropoulos * the vdev label with the best uberblock and verify that our version 3480*86714001SSerapheim Dimitropoulos * of zfs supports them all. 34811fd3785fSPavel Zakharov */ 34826f793812SPavel Zakharov error = spa_ld_select_uberblock(spa, type); 34831fd3785fSPavel Zakharov if (error != 0) 34841fd3785fSPavel Zakharov return (error); 34851fd3785fSPavel Zakharov 34861fd3785fSPavel Zakharov /* 34871fd3785fSPavel Zakharov * Pass that uberblock to the dsl_pool layer which will open the root 34881fd3785fSPavel Zakharov * blkptr. This blkptr points to the latest version of the MOS and will 34891fd3785fSPavel Zakharov * allow us to read its contents. 34901fd3785fSPavel Zakharov */ 34911fd3785fSPavel Zakharov error = spa_ld_open_rootbp(spa); 34921fd3785fSPavel Zakharov if (error != 0) 34931fd3785fSPavel Zakharov return (error); 34941fd3785fSPavel Zakharov 3495*86714001SSerapheim Dimitropoulos return (0); 3496*86714001SSerapheim Dimitropoulos } 3497*86714001SSerapheim Dimitropoulos 3498*86714001SSerapheim Dimitropoulos static int 3499*86714001SSerapheim Dimitropoulos spa_ld_checkpoint_rewind(spa_t *spa) 3500*86714001SSerapheim Dimitropoulos { 3501*86714001SSerapheim Dimitropoulos uberblock_t checkpoint; 3502*86714001SSerapheim Dimitropoulos int error = 0; 3503*86714001SSerapheim Dimitropoulos 3504*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3505*86714001SSerapheim Dimitropoulos ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 3506*86714001SSerapheim Dimitropoulos 3507*86714001SSerapheim Dimitropoulos error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 3508*86714001SSerapheim Dimitropoulos DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 3509*86714001SSerapheim Dimitropoulos sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 3510*86714001SSerapheim Dimitropoulos 3511*86714001SSerapheim Dimitropoulos if (error != 0) { 3512*86714001SSerapheim Dimitropoulos spa_load_failed(spa, "unable to retrieve checkpointed " 3513*86714001SSerapheim Dimitropoulos "uberblock from the MOS config [error=%d]", error); 3514*86714001SSerapheim Dimitropoulos 3515*86714001SSerapheim Dimitropoulos if (error == ENOENT) 3516*86714001SSerapheim Dimitropoulos error = ZFS_ERR_NO_CHECKPOINT; 3517*86714001SSerapheim Dimitropoulos 3518*86714001SSerapheim Dimitropoulos return (error); 3519*86714001SSerapheim Dimitropoulos } 3520*86714001SSerapheim Dimitropoulos 3521*86714001SSerapheim Dimitropoulos ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); 3522*86714001SSerapheim Dimitropoulos ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); 3523*86714001SSerapheim Dimitropoulos 3524*86714001SSerapheim Dimitropoulos /* 3525*86714001SSerapheim Dimitropoulos * We need to update the txg and timestamp of the checkpointed 3526*86714001SSerapheim Dimitropoulos * uberblock to be higher than the latest one. This ensures that 3527*86714001SSerapheim Dimitropoulos * the checkpointed uberblock is selected if we were to close and 3528*86714001SSerapheim Dimitropoulos * reopen the pool right after we've written it in the vdev labels. 3529*86714001SSerapheim Dimitropoulos * (also see block comment in vdev_uberblock_compare) 3530*86714001SSerapheim Dimitropoulos */ 3531*86714001SSerapheim Dimitropoulos checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; 3532*86714001SSerapheim Dimitropoulos checkpoint.ub_timestamp = gethrestime_sec(); 3533*86714001SSerapheim Dimitropoulos 3534*86714001SSerapheim Dimitropoulos /* 3535*86714001SSerapheim Dimitropoulos * Set current uberblock to be the checkpointed uberblock. 3536*86714001SSerapheim Dimitropoulos */ 3537*86714001SSerapheim Dimitropoulos spa->spa_uberblock = checkpoint; 3538*86714001SSerapheim Dimitropoulos 3539*86714001SSerapheim Dimitropoulos /* 3540*86714001SSerapheim Dimitropoulos * If we are doing a normal rewind, then the pool is open for 3541*86714001SSerapheim Dimitropoulos * writing and we sync the "updated" checkpointed uberblock to 3542*86714001SSerapheim Dimitropoulos * disk. Once this is done, we've basically rewound the whole 3543*86714001SSerapheim Dimitropoulos * pool and there is no way back. 3544*86714001SSerapheim Dimitropoulos * 3545*86714001SSerapheim Dimitropoulos * There are cases when we don't want to attempt and sync the 3546*86714001SSerapheim Dimitropoulos * checkpointed uberblock to disk because we are opening a 3547*86714001SSerapheim Dimitropoulos * pool as read-only. Specifically, verifying the checkpointed 3548*86714001SSerapheim Dimitropoulos * state with zdb, and importing the checkpointed state to get 3549*86714001SSerapheim Dimitropoulos * a "preview" of its content. 3550*86714001SSerapheim Dimitropoulos */ 3551*86714001SSerapheim Dimitropoulos if (spa_writeable(spa)) { 3552*86714001SSerapheim Dimitropoulos vdev_t *rvd = spa->spa_root_vdev; 3553*86714001SSerapheim Dimitropoulos 3554*86714001SSerapheim Dimitropoulos spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3555*86714001SSerapheim Dimitropoulos vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; 3556*86714001SSerapheim Dimitropoulos int svdcount = 0; 3557*86714001SSerapheim Dimitropoulos int children = rvd->vdev_children; 3558*86714001SSerapheim Dimitropoulos int c0 = spa_get_random(children); 3559*86714001SSerapheim Dimitropoulos 3560*86714001SSerapheim Dimitropoulos for (int c = 0; c < children; c++) { 3561*86714001SSerapheim Dimitropoulos vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; 3562*86714001SSerapheim Dimitropoulos 3563*86714001SSerapheim Dimitropoulos /* Stop when revisiting the first vdev */ 3564*86714001SSerapheim Dimitropoulos if (c > 0 && svd[0] == vd) 3565*86714001SSerapheim Dimitropoulos break; 3566*86714001SSerapheim Dimitropoulos 3567*86714001SSerapheim Dimitropoulos if (vd->vdev_ms_array == 0 || vd->vdev_islog || 3568*86714001SSerapheim Dimitropoulos !vdev_is_concrete(vd)) 3569*86714001SSerapheim Dimitropoulos continue; 3570*86714001SSerapheim Dimitropoulos 3571*86714001SSerapheim Dimitropoulos svd[svdcount++] = vd; 3572*86714001SSerapheim Dimitropoulos if (svdcount == SPA_SYNC_MIN_VDEVS) 3573*86714001SSerapheim Dimitropoulos break; 3574*86714001SSerapheim Dimitropoulos } 3575*86714001SSerapheim Dimitropoulos error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); 3576*86714001SSerapheim Dimitropoulos if (error == 0) 3577*86714001SSerapheim Dimitropoulos spa->spa_last_synced_guid = rvd->vdev_guid; 3578*86714001SSerapheim Dimitropoulos spa_config_exit(spa, SCL_ALL, FTAG); 3579*86714001SSerapheim Dimitropoulos 3580*86714001SSerapheim Dimitropoulos if (error != 0) { 3581*86714001SSerapheim Dimitropoulos spa_load_failed(spa, "failed to write checkpointed " 3582*86714001SSerapheim Dimitropoulos "uberblock to the vdev labels [error=%d]", error); 3583*86714001SSerapheim Dimitropoulos return (error); 3584*86714001SSerapheim Dimitropoulos } 3585*86714001SSerapheim Dimitropoulos } 3586*86714001SSerapheim Dimitropoulos 3587*86714001SSerapheim Dimitropoulos return (0); 3588*86714001SSerapheim Dimitropoulos } 3589*86714001SSerapheim Dimitropoulos 3590*86714001SSerapheim Dimitropoulos static int 3591*86714001SSerapheim Dimitropoulos spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, 3592*86714001SSerapheim Dimitropoulos boolean_t *update_config_cache) 3593*86714001SSerapheim Dimitropoulos { 3594*86714001SSerapheim Dimitropoulos int error; 3595*86714001SSerapheim Dimitropoulos 3596*86714001SSerapheim Dimitropoulos /* 3597*86714001SSerapheim Dimitropoulos * Parse the config for pool, open and validate vdevs, 3598*86714001SSerapheim Dimitropoulos * select an uberblock, and use that uberblock to open 3599*86714001SSerapheim Dimitropoulos * the MOS. 3600*86714001SSerapheim Dimitropoulos */ 3601*86714001SSerapheim Dimitropoulos error = spa_ld_mos_init(spa, type); 3602*86714001SSerapheim Dimitropoulos if (error != 0) 3603*86714001SSerapheim Dimitropoulos return (error); 3604*86714001SSerapheim Dimitropoulos 36051fd3785fSPavel Zakharov /* 36066f793812SPavel Zakharov * Retrieve the trusted config stored in the MOS and use it to create 36076f793812SPavel Zakharov * a new, exact version of the vdev tree, then reopen all vdevs. 36081fd3785fSPavel Zakharov */ 3609*86714001SSerapheim Dimitropoulos error = spa_ld_trusted_config(spa, type, B_FALSE); 36106f793812SPavel Zakharov if (error == EAGAIN) { 3611*86714001SSerapheim Dimitropoulos if (update_config_cache != NULL) 3612*86714001SSerapheim Dimitropoulos *update_config_cache = B_TRUE; 3613*86714001SSerapheim Dimitropoulos 36146f793812SPavel Zakharov /* 36156f793812SPavel Zakharov * Redo the loading process with the trusted config if it is 36166f793812SPavel Zakharov * too different from the untrusted config. 36176f793812SPavel Zakharov */ 36186f793812SPavel Zakharov spa_ld_prepare_for_reload(spa); 3619*86714001SSerapheim Dimitropoulos spa_load_note(spa, "RELOADING"); 3620*86714001SSerapheim Dimitropoulos error = spa_ld_mos_init(spa, type); 3621*86714001SSerapheim Dimitropoulos if (error != 0) 3622*86714001SSerapheim Dimitropoulos return (error); 3623*86714001SSerapheim Dimitropoulos 3624*86714001SSerapheim Dimitropoulos error = spa_ld_trusted_config(spa, type, B_TRUE); 3625*86714001SSerapheim Dimitropoulos if (error != 0) 3626*86714001SSerapheim Dimitropoulos return (error); 3627*86714001SSerapheim Dimitropoulos 36286f793812SPavel Zakharov } else if (error != 0) { 36291fd3785fSPavel Zakharov return (error); 36306f793812SPavel Zakharov } 36311fd3785fSPavel Zakharov 3632*86714001SSerapheim Dimitropoulos return (0); 3633*86714001SSerapheim Dimitropoulos } 3634*86714001SSerapheim Dimitropoulos 3635*86714001SSerapheim Dimitropoulos /* 3636*86714001SSerapheim Dimitropoulos * Load an existing storage pool, using the config provided. This config 3637*86714001SSerapheim Dimitropoulos * describes which vdevs are part of the pool and is later validated against 3638*86714001SSerapheim Dimitropoulos * partial configs present in each vdev's label and an entire copy of the 3639*86714001SSerapheim Dimitropoulos * config stored in the MOS. 3640*86714001SSerapheim Dimitropoulos */ 3641*86714001SSerapheim Dimitropoulos static int 3642*86714001SSerapheim Dimitropoulos spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) 3643*86714001SSerapheim Dimitropoulos { 3644*86714001SSerapheim Dimitropoulos int error = 0; 3645*86714001SSerapheim Dimitropoulos boolean_t missing_feat_write = B_FALSE; 3646*86714001SSerapheim Dimitropoulos boolean_t checkpoint_rewind = 3647*86714001SSerapheim Dimitropoulos (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 3648*86714001SSerapheim Dimitropoulos boolean_t update_config_cache = B_FALSE; 3649*86714001SSerapheim Dimitropoulos 3650*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3651*86714001SSerapheim Dimitropoulos ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); 3652*86714001SSerapheim Dimitropoulos 3653*86714001SSerapheim Dimitropoulos spa_load_note(spa, "LOADING"); 3654*86714001SSerapheim Dimitropoulos 3655*86714001SSerapheim Dimitropoulos error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); 3656*86714001SSerapheim Dimitropoulos if (error != 0) 3657*86714001SSerapheim Dimitropoulos return (error); 3658*86714001SSerapheim Dimitropoulos 3659*86714001SSerapheim Dimitropoulos /* 3660*86714001SSerapheim Dimitropoulos * If we are rewinding to the checkpoint then we need to repeat 3661*86714001SSerapheim Dimitropoulos * everything we've done so far in this function but this time 3662*86714001SSerapheim Dimitropoulos * selecting the checkpointed uberblock and using that to open 3663*86714001SSerapheim Dimitropoulos * the MOS. 3664*86714001SSerapheim Dimitropoulos */ 3665*86714001SSerapheim Dimitropoulos if (checkpoint_rewind) { 3666*86714001SSerapheim Dimitropoulos /* 3667*86714001SSerapheim Dimitropoulos * If we are rewinding to the checkpoint update config cache 3668*86714001SSerapheim Dimitropoulos * anyway. 3669*86714001SSerapheim Dimitropoulos */ 3670*86714001SSerapheim Dimitropoulos update_config_cache = B_TRUE; 3671*86714001SSerapheim Dimitropoulos 3672*86714001SSerapheim Dimitropoulos /* 3673*86714001SSerapheim Dimitropoulos * Extract the checkpointed uberblock from the current MOS 3674*86714001SSerapheim Dimitropoulos * and use this as the pool's uberblock from now on. If the 3675*86714001SSerapheim Dimitropoulos * pool is imported as writeable we also write the checkpoint 3676*86714001SSerapheim Dimitropoulos * uberblock to the labels, making the rewind permanent. 3677*86714001SSerapheim Dimitropoulos */ 3678*86714001SSerapheim Dimitropoulos error = spa_ld_checkpoint_rewind(spa); 3679*86714001SSerapheim Dimitropoulos if (error != 0) 3680*86714001SSerapheim Dimitropoulos return (error); 3681*86714001SSerapheim Dimitropoulos 3682*86714001SSerapheim Dimitropoulos /* 3683*86714001SSerapheim Dimitropoulos * Redo the loading process process again with the 3684*86714001SSerapheim Dimitropoulos * checkpointed uberblock. 3685*86714001SSerapheim Dimitropoulos */ 3686*86714001SSerapheim Dimitropoulos spa_ld_prepare_for_reload(spa); 3687*86714001SSerapheim Dimitropoulos spa_load_note(spa, "LOADING checkpointed uberblock"); 3688*86714001SSerapheim Dimitropoulos error = spa_ld_mos_with_trusted_config(spa, type, NULL); 3689*86714001SSerapheim Dimitropoulos if (error != 0) 3690*86714001SSerapheim Dimitropoulos return (error); 3691*86714001SSerapheim Dimitropoulos } 3692*86714001SSerapheim Dimitropoulos 3693*86714001SSerapheim Dimitropoulos /* 3694*86714001SSerapheim Dimitropoulos * Retrieve the checkpoint txg if the pool has a checkpoint. 3695*86714001SSerapheim Dimitropoulos */ 3696*86714001SSerapheim Dimitropoulos error = spa_ld_read_checkpoint_txg(spa); 3697*86714001SSerapheim Dimitropoulos if (error != 0) 3698*86714001SSerapheim Dimitropoulos return (error); 3699*86714001SSerapheim Dimitropoulos 37001fd3785fSPavel Zakharov /* 37011fd3785fSPavel Zakharov * Retrieve the mapping of indirect vdevs. Those vdevs were removed 37021fd3785fSPavel Zakharov * from the pool and their contents were re-mapped to other vdevs. Note 37031fd3785fSPavel Zakharov * that everything that we read before this step must have been 37041fd3785fSPavel Zakharov * rewritten on concrete vdevs after the last device removal was 37051fd3785fSPavel Zakharov * initiated. Otherwise we could be reading from indirect vdevs before 37061fd3785fSPavel Zakharov * we have loaded their mappings. 37071fd3785fSPavel Zakharov */ 37081fd3785fSPavel Zakharov error = spa_ld_open_indirect_vdev_metadata(spa); 37091fd3785fSPavel Zakharov if (error != 0) 37101fd3785fSPavel Zakharov return (error); 37111fd3785fSPavel Zakharov 37121fd3785fSPavel Zakharov /* 37131fd3785fSPavel Zakharov * Retrieve the full list of active features from the MOS and check if 37141fd3785fSPavel Zakharov * they are all supported. 37151fd3785fSPavel Zakharov */ 37163ee8c80cSPavel Zakharov error = spa_ld_check_features(spa, &missing_feat_write); 37171fd3785fSPavel Zakharov if (error != 0) 37181fd3785fSPavel Zakharov return (error); 37191fd3785fSPavel Zakharov 37201fd3785fSPavel Zakharov /* 37211fd3785fSPavel Zakharov * Load several special directories from the MOS needed by the dsl_pool 37221fd3785fSPavel Zakharov * layer. 37231fd3785fSPavel Zakharov */ 37241fd3785fSPavel Zakharov error = spa_ld_load_special_directories(spa); 37251fd3785fSPavel Zakharov if (error != 0) 37261fd3785fSPavel Zakharov return (error); 37271fd3785fSPavel Zakharov 37281fd3785fSPavel Zakharov /* 37291fd3785fSPavel Zakharov * Retrieve pool properties from the MOS. 37301fd3785fSPavel Zakharov */ 37311fd3785fSPavel Zakharov error = spa_ld_get_props(spa); 37321fd3785fSPavel Zakharov if (error != 0) 37331fd3785fSPavel Zakharov return (error); 37341fd3785fSPavel Zakharov 37351fd3785fSPavel Zakharov /* 37361fd3785fSPavel Zakharov * Retrieve the list of auxiliary devices - cache devices and spares - 37371fd3785fSPavel Zakharov * and open them. 37381fd3785fSPavel Zakharov */ 37391fd3785fSPavel Zakharov error = spa_ld_open_aux_vdevs(spa, type); 37401fd3785fSPavel Zakharov if (error != 0) 37411fd3785fSPavel Zakharov return (error); 37421fd3785fSPavel Zakharov 37431fd3785fSPavel Zakharov /* 37441fd3785fSPavel Zakharov * Load the metadata for all vdevs. Also check if unopenable devices 37451fd3785fSPavel Zakharov * should be autoreplaced. 37461fd3785fSPavel Zakharov */ 37473ee8c80cSPavel Zakharov error = spa_ld_load_vdev_metadata(spa); 37481fd3785fSPavel Zakharov if (error != 0) 37491fd3785fSPavel Zakharov return (error); 37501fd3785fSPavel Zakharov 37511fd3785fSPavel Zakharov error = spa_ld_load_dedup_tables(spa); 37521fd3785fSPavel Zakharov if (error != 0) 37531fd3785fSPavel Zakharov return (error); 37541fd3785fSPavel Zakharov 37551fd3785fSPavel Zakharov /* 37561fd3785fSPavel Zakharov * Verify the logs now to make sure we don't have any unexpected errors 37571fd3785fSPavel Zakharov * when we claim log blocks later. 37581fd3785fSPavel Zakharov */ 37591fd3785fSPavel Zakharov error = spa_ld_verify_logs(spa, type, ereport); 37601fd3785fSPavel Zakharov if (error != 0) 37611fd3785fSPavel Zakharov return (error); 37621fd3785fSPavel Zakharov 3763ad135b5dSChristopher Siden if (missing_feat_write) { 37646f793812SPavel Zakharov ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); 3765ad135b5dSChristopher Siden 3766ad135b5dSChristopher Siden /* 3767ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 3768ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 3769ad135b5dSChristopher Siden * information and can return to userland. 3770ad135b5dSChristopher Siden */ 37711fd3785fSPavel Zakharov return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, 37721fd3785fSPavel Zakharov ENOTSUP)); 3773ad135b5dSChristopher Siden } 3774ad135b5dSChristopher Siden 37754b964adaSGeorge Wilson /* 37761fd3785fSPavel Zakharov * Traverse the last txgs to make sure the pool was left off in a safe 37771fd3785fSPavel Zakharov * state. When performing an extreme rewind, we verify the whole pool, 37781fd3785fSPavel Zakharov * which can take a very long time. 37794b964adaSGeorge Wilson */ 37803ee8c80cSPavel Zakharov error = spa_ld_verify_pool_data(spa); 37811fd3785fSPavel Zakharov if (error != 0) 37821fd3785fSPavel Zakharov return (error); 37831fd3785fSPavel Zakharov 37841fd3785fSPavel Zakharov /* 37851fd3785fSPavel Zakharov * Calculate the deflated space for the pool. This must be done before 37861fd3785fSPavel Zakharov * we write anything to the pool because we'd need to update the space 37871fd3785fSPavel Zakharov * accounting using the deflated sizes. 37881fd3785fSPavel Zakharov */ 37891fd3785fSPavel Zakharov spa_update_dspace(spa); 37904b964adaSGeorge Wilson 37911fd3785fSPavel Zakharov /* 37921fd3785fSPavel Zakharov * We have now retrieved all the information we needed to open the 37931fd3785fSPavel Zakharov * pool. If we are importing the pool in read-write mode, a few 37941fd3785fSPavel Zakharov * additional steps must be performed to finish the import. 37951fd3785fSPavel Zakharov */ 37966f793812SPavel Zakharov if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || 3797468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 37986f793812SPavel Zakharov uint64_t config_cache_txg = spa->spa_config_txg; 37996f793812SPavel Zakharov 38006f793812SPavel Zakharov ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); 38018ad4d6ddSJeff Bonwick 3802*86714001SSerapheim Dimitropoulos /* 3803*86714001SSerapheim Dimitropoulos * In case of a checkpoint rewind, log the original txg 3804*86714001SSerapheim Dimitropoulos * of the checkpointed uberblock. 3805*86714001SSerapheim Dimitropoulos */ 3806*86714001SSerapheim Dimitropoulos if (checkpoint_rewind) { 3807*86714001SSerapheim Dimitropoulos spa_history_log_internal(spa, "checkpoint rewind", 3808*86714001SSerapheim Dimitropoulos NULL, "rewound state to txg=%llu", 3809*86714001SSerapheim Dimitropoulos (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); 3810*86714001SSerapheim Dimitropoulos } 3811*86714001SSerapheim Dimitropoulos 38120373e76bSbonwick /* 38131fd3785fSPavel Zakharov * Traverse the ZIL and claim all blocks. 38140373e76bSbonwick */ 38151fd3785fSPavel Zakharov spa_ld_claim_log_blocks(spa); 3816b24ab676SJeff Bonwick 38171fd3785fSPavel Zakharov /* 38181fd3785fSPavel Zakharov * Kick-off the syncing thread. 38191fd3785fSPavel Zakharov */ 3820fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3821fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3822fa9e4066Sahrens 3823fa9e4066Sahrens /* 3824b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 3825b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 3826b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 38271fd3785fSPavel Zakharov * will have been set for us by ZIL traversal operations 38281fd3785fSPavel Zakharov * performed above. 3829fa9e4066Sahrens */ 3830b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 38310e34b6a7Sbonwick 38320e34b6a7Sbonwick /* 38331fd3785fSPavel Zakharov * Check if we need to request an update of the config. On the 38341fd3785fSPavel Zakharov * next sync, we would update the config stored in vdev labels 38351fd3785fSPavel Zakharov * and the cachefile (by default /etc/zfs/zpool.cache). 38360e34b6a7Sbonwick */ 38376f793812SPavel Zakharov spa_ld_check_for_config_update(spa, config_cache_txg, 3838*86714001SSerapheim Dimitropoulos update_config_cache); 38398ad4d6ddSJeff Bonwick 38408ad4d6ddSJeff Bonwick /* 38418ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 38428ad4d6ddSJeff Bonwick */ 38433f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 38441fd3785fSPavel Zakharov vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) 38458ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 3846503ad85cSMatthew Ahrens 38474445fffbSMatthew Ahrens /* 38484445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 38494445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 38504445fffbSMatthew Ahrens */ 38514445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 38524445fffbSMatthew Ahrens 3853503ad85cSMatthew Ahrens /* 3854503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 3855503ad85cSMatthew Ahrens */ 3856503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 3857503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 3858ca45db41SChris Kirby 3859ca45db41SChris Kirby /* 3860ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 3861ca45db41SChris Kirby */ 3862ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 38635cabbc6bSPrashanth Sreenivasa 38645cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 38655cabbc6bSPrashanth Sreenivasa 3866667ec66fSSerapheim Dimitropoulos spa_spawn_aux_threads(spa); 3867fa9e4066Sahrens } 3868fa9e4066Sahrens 38693ee8c80cSPavel Zakharov spa_load_note(spa, "LOADED"); 38703ee8c80cSPavel Zakharov 38711195e687SMark J Musante return (0); 3872fa9e4066Sahrens } 3873fa9e4066Sahrens 3874468c413aSTim Haley static int 38756f793812SPavel Zakharov spa_load_retry(spa_t *spa, spa_load_state_t state) 3876468c413aSTim Haley { 3877f9af39baSGeorge Wilson int mode = spa->spa_mode; 3878f9af39baSGeorge Wilson 3879468c413aSTim Haley spa_unload(spa); 3880468c413aSTim Haley spa_deactivate(spa); 3881468c413aSTim Haley 3882e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 3883468c413aSTim Haley 3884f9af39baSGeorge Wilson spa_activate(spa, mode); 3885468c413aSTim Haley spa_async_suspend(spa); 3886468c413aSTim Haley 38873ee8c80cSPavel Zakharov spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", 38883ee8c80cSPavel Zakharov (u_longlong_t)spa->spa_load_max_txg); 38893ee8c80cSPavel Zakharov 38906f793812SPavel Zakharov return (spa_load(spa, state, SPA_IMPORT_EXISTING)); 3891468c413aSTim Haley } 3892468c413aSTim Haley 3893ad135b5dSChristopher Siden /* 3894ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 3895ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 3896ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 3897ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 3898ad135b5dSChristopher Siden * spa_load(). 3899ad135b5dSChristopher Siden */ 3900468c413aSTim Haley static int 39016f793812SPavel Zakharov spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, 39026f793812SPavel Zakharov int rewind_flags) 3903468c413aSTim Haley { 3904ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 3905468c413aSTim Haley nvlist_t *config = NULL; 3906468c413aSTim Haley int load_error, rewind_error; 3907c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 3908468c413aSTim Haley uint64_t min_txg; 3909468c413aSTim Haley 3910a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 3911468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 39121195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3913a33cae98STim Haley } else { 3914468c413aSTim Haley spa->spa_load_max_txg = max_request; 3915e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 3916e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 3917a33cae98STim Haley } 3918468c413aSTim Haley 39196f793812SPavel Zakharov load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); 3920468c413aSTim Haley if (load_error == 0) 3921468c413aSTim Haley return (0); 3922*86714001SSerapheim Dimitropoulos if (load_error == ZFS_ERR_NO_CHECKPOINT) { 3923*86714001SSerapheim Dimitropoulos /* 3924*86714001SSerapheim Dimitropoulos * When attempting checkpoint-rewind on a pool with no 3925*86714001SSerapheim Dimitropoulos * checkpoint, we should not attempt to load uberblocks 3926*86714001SSerapheim Dimitropoulos * from previous txgs when spa_load fails. 3927*86714001SSerapheim Dimitropoulos */ 3928*86714001SSerapheim Dimitropoulos ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 3929*86714001SSerapheim Dimitropoulos return (load_error); 3930*86714001SSerapheim Dimitropoulos } 3931468c413aSTim Haley 3932468c413aSTim Haley if (spa->spa_root_vdev != NULL) 3933468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3934468c413aSTim Haley 3935468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 3936468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 3937468c413aSTim Haley 3938c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 3939468c413aSTim Haley nvlist_free(config); 3940468c413aSTim Haley return (load_error); 3941468c413aSTim Haley } 3942468c413aSTim Haley 3943ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3944ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 39451195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3946ad135b5dSChristopher Siden } else { 3947ad135b5dSChristopher Siden /* 3948ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 3949ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 3950ad135b5dSChristopher Siden * to rewind. 3951ad135b5dSChristopher Siden */ 3952ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3953ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3954ad135b5dSChristopher Siden } 3955468c413aSTim Haley 3956c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3957c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3958c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3959c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3960468c413aSTim Haley 3961c8ee1847SVictor Latushkin /* 3962c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3963c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3964c8ee1847SVictor Latushkin */ 3965c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3966c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3967c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3968468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 39696f793812SPavel Zakharov rewind_error = spa_load_retry(spa, state); 3970468c413aSTim Haley } 3971468c413aSTim Haley 3972468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3973468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3974468c413aSTim Haley 3975468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3976468c413aSTim Haley spa_config_set(spa, config); 39777d3000f7SPavel Zakharov else 39787d3000f7SPavel Zakharov nvlist_free(config); 3979468c413aSTim Haley 3980ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3981ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3982ad135b5dSChristopher Siden return (rewind_error); 3983ad135b5dSChristopher Siden } else { 3984ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3985ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3986ad135b5dSChristopher Siden spa->spa_load_info); 3987ad135b5dSChristopher Siden 3988ad135b5dSChristopher Siden /* Restore the initial load info */ 3989ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3990ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3991ad135b5dSChristopher Siden 3992ad135b5dSChristopher Siden return (load_error); 3993ad135b5dSChristopher Siden } 3994468c413aSTim Haley } 3995468c413aSTim Haley 3996fa9e4066Sahrens /* 3997fa9e4066Sahrens * Pool Open/Import 3998fa9e4066Sahrens * 3999fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 4000fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 4001fa9e4066Sahrens * case of an open, the pool configuration will exist in the 40023d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 4003fa9e4066Sahrens * 4004fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 4005fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 4006fa9e4066Sahrens * ambiguous state. 4007fa9e4066Sahrens */ 4008fa9e4066Sahrens static int 4009468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 4010468c413aSTim Haley nvlist_t **config) 4011fa9e4066Sahrens { 4012fa9e4066Sahrens spa_t *spa; 40134b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 4014fa9e4066Sahrens int error; 4015fa9e4066Sahrens int locked = B_FALSE; 4016fa9e4066Sahrens 4017fa9e4066Sahrens *spapp = NULL; 4018fa9e4066Sahrens 4019fa9e4066Sahrens /* 4020fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 4021fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 4022fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 4023fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 4024fa9e4066Sahrens */ 4025fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 4026fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4027fa9e4066Sahrens locked = B_TRUE; 4028fa9e4066Sahrens } 4029fa9e4066Sahrens 4030fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4031fa9e4066Sahrens if (locked) 4032fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4033be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4034fa9e4066Sahrens } 4035468c413aSTim Haley 4036fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 40374b44c88cSTim Haley zpool_rewind_policy_t policy; 40384b44c88cSTim Haley 40394b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 40404b44c88cSTim Haley &policy); 40414b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 40424b44c88cSTim Haley state = SPA_LOAD_RECOVER; 4043fa9e4066Sahrens 40448ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 4045fa9e4066Sahrens 4046468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4047468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 40486f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 4049468c413aSTim Haley 40503ee8c80cSPavel Zakharov zfs_dbgmsg("spa_open_common: opening %s", pool); 40516f793812SPavel Zakharov error = spa_load_best(spa, state, policy.zrp_txg, 4052c8ee1847SVictor Latushkin policy.zrp_request); 4053fa9e4066Sahrens 4054fa9e4066Sahrens if (error == EBADF) { 4055fa9e4066Sahrens /* 4056560e6e96Seschrock * If vdev_validate() returns failure (indicated by 4057560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 4058560e6e96Seschrock * that the pool has been exported or destroyed. If 4059560e6e96Seschrock * this is the case, the config cache is out of sync and 4060560e6e96Seschrock * we should remove the pool from the namespace. 4061fa9e4066Sahrens */ 4062fa9e4066Sahrens spa_unload(spa); 4063fa9e4066Sahrens spa_deactivate(spa); 40645cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 4065fa9e4066Sahrens spa_remove(spa); 4066fa9e4066Sahrens if (locked) 4067fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4068be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4069ea8dc4b6Seschrock } 4070ea8dc4b6Seschrock 4071ea8dc4b6Seschrock if (error) { 4072fa9e4066Sahrens /* 4073fa9e4066Sahrens * We can't open the pool, but we still have useful 4074fa9e4066Sahrens * information: the state of each vdev after the 4075fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 4076fa9e4066Sahrens */ 40774b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 4078468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 4079468c413aSTim Haley KM_SLEEP) == 0); 40804b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 40814b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 40824b964adaSGeorge Wilson spa->spa_load_info) == 0); 40834b964adaSGeorge Wilson } 4084fa9e4066Sahrens spa_unload(spa); 4085fa9e4066Sahrens spa_deactivate(spa); 4086468c413aSTim Haley spa->spa_last_open_failed = error; 4087fa9e4066Sahrens if (locked) 4088fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4089fa9e4066Sahrens *spapp = NULL; 4090fa9e4066Sahrens return (error); 4091fa9e4066Sahrens } 4092fa9e4066Sahrens } 4093fa9e4066Sahrens 4094fa9e4066Sahrens spa_open_ref(spa, tag); 40953d7072f8Seschrock 4096468c413aSTim Haley if (config != NULL) 4097468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4098468c413aSTim Haley 40994b964adaSGeorge Wilson /* 41004b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 41014b964adaSGeorge Wilson * gathered while doing the load. 41024b964adaSGeorge Wilson */ 41034b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 41044b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 41054b964adaSGeorge Wilson spa->spa_load_info) == 0); 41064b964adaSGeorge Wilson } 41074b964adaSGeorge Wilson 4108a33cae98STim Haley if (locked) { 4109a33cae98STim Haley spa->spa_last_open_failed = 0; 4110a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 4111a33cae98STim Haley spa->spa_load_txg = 0; 4112fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4113a33cae98STim Haley } 4114fa9e4066Sahrens 4115fa9e4066Sahrens *spapp = spa; 4116fa9e4066Sahrens 4117fa9e4066Sahrens return (0); 4118fa9e4066Sahrens } 4119fa9e4066Sahrens 4120468c413aSTim Haley int 4121468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 4122468c413aSTim Haley nvlist_t **config) 4123468c413aSTim Haley { 4124468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 4125468c413aSTim Haley } 4126468c413aSTim Haley 4127fa9e4066Sahrens int 4128fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 4129fa9e4066Sahrens { 4130468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 4131fa9e4066Sahrens } 4132fa9e4066Sahrens 4133ea8dc4b6Seschrock /* 4134ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 4135ea8dc4b6Seschrock * preventing it from being exported or destroyed. 4136ea8dc4b6Seschrock */ 4137ea8dc4b6Seschrock spa_t * 4138ea8dc4b6Seschrock spa_inject_addref(char *name) 4139ea8dc4b6Seschrock { 4140ea8dc4b6Seschrock spa_t *spa; 4141ea8dc4b6Seschrock 4142ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4143ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 4144ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4145ea8dc4b6Seschrock return (NULL); 4146ea8dc4b6Seschrock } 4147ea8dc4b6Seschrock spa->spa_inject_ref++; 4148ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4149ea8dc4b6Seschrock 4150ea8dc4b6Seschrock return (spa); 4151ea8dc4b6Seschrock } 4152ea8dc4b6Seschrock 4153ea8dc4b6Seschrock void 4154ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 4155ea8dc4b6Seschrock { 4156ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4157ea8dc4b6Seschrock spa->spa_inject_ref--; 4158ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4159ea8dc4b6Seschrock } 4160ea8dc4b6Seschrock 4161fa94a07fSbrendan /* 4162fa94a07fSbrendan * Add spares device information to the nvlist. 4163fa94a07fSbrendan */ 416499653d4eSeschrock static void 416599653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 416699653d4eSeschrock { 416799653d4eSeschrock nvlist_t **spares; 416899653d4eSeschrock uint_t i, nspares; 416999653d4eSeschrock nvlist_t *nvroot; 417099653d4eSeschrock uint64_t guid; 417199653d4eSeschrock vdev_stat_t *vs; 417299653d4eSeschrock uint_t vsc; 417339c23413Seschrock uint64_t pool; 417499653d4eSeschrock 41756809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 41766809eb4eSEric Schrock 4177fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 417899653d4eSeschrock return; 417999653d4eSeschrock 418099653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 418199653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4182fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 418399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 418499653d4eSeschrock if (nspares != 0) { 418599653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 418699653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 418799653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 418899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 418999653d4eSeschrock 419099653d4eSeschrock /* 419199653d4eSeschrock * Go through and find any spares which have since been 419299653d4eSeschrock * repurposed as an active spare. If this is the case, update 419399653d4eSeschrock * their status appropriately. 419499653d4eSeschrock */ 419599653d4eSeschrock for (i = 0; i < nspares; i++) { 419699653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 419799653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 419889a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 419989a89ebfSlling pool != 0ULL) { 420099653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 42013f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 420299653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 420399653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 420499653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 420599653d4eSeschrock } 420699653d4eSeschrock } 420799653d4eSeschrock } 420899653d4eSeschrock } 420999653d4eSeschrock 4210fa94a07fSbrendan /* 4211fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 4212fa94a07fSbrendan */ 4213fa94a07fSbrendan static void 4214fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 4215fa94a07fSbrendan { 4216fa94a07fSbrendan nvlist_t **l2cache; 4217fa94a07fSbrendan uint_t i, j, nl2cache; 4218fa94a07fSbrendan nvlist_t *nvroot; 4219fa94a07fSbrendan uint64_t guid; 4220fa94a07fSbrendan vdev_t *vd; 4221fa94a07fSbrendan vdev_stat_t *vs; 4222fa94a07fSbrendan uint_t vsc; 4223fa94a07fSbrendan 42246809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 42256809eb4eSEric Schrock 4226fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 4227fa94a07fSbrendan return; 4228fa94a07fSbrendan 4229fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 4230fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4231fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 4232fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 4233fa94a07fSbrendan if (nl2cache != 0) { 4234fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 4235fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 4236fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 4237fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 4238fa94a07fSbrendan 4239fa94a07fSbrendan /* 4240fa94a07fSbrendan * Update level 2 cache device stats. 4241fa94a07fSbrendan */ 4242fa94a07fSbrendan 4243fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 4244fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 4245fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 4246fa94a07fSbrendan 4247fa94a07fSbrendan vd = NULL; 4248fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 4249fa94a07fSbrendan if (guid == 4250fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 4251fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 4252fa94a07fSbrendan break; 4253fa94a07fSbrendan } 4254fa94a07fSbrendan } 4255fa94a07fSbrendan ASSERT(vd != NULL); 4256fa94a07fSbrendan 4257fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 42583f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 42593f9d6ad7SLin Ling == 0); 4260fa94a07fSbrendan vdev_get_stats(vd, vs); 4261fa94a07fSbrendan } 4262fa94a07fSbrendan } 4263fa94a07fSbrendan } 4264fa94a07fSbrendan 4265ad135b5dSChristopher Siden static void 4266ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 4267ad135b5dSChristopher Siden { 4268ad135b5dSChristopher Siden nvlist_t *features; 4269ad135b5dSChristopher Siden zap_cursor_t zc; 4270ad135b5dSChristopher Siden zap_attribute_t za; 4271ad135b5dSChristopher Siden 4272ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 4273ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4274ad135b5dSChristopher Siden 4275ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 4276ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 4277ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 4278ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 4279ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 4280ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 4281ad135b5dSChristopher Siden za.za_num_integers == 1); 4282b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 4283ad135b5dSChristopher Siden za.za_first_integer)); 4284ad135b5dSChristopher Siden } 4285ad135b5dSChristopher Siden zap_cursor_fini(&zc); 4286ad135b5dSChristopher Siden } 4287ad135b5dSChristopher Siden 4288ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 4289ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 4290ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 4291ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 4292ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 4293ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 4294ad135b5dSChristopher Siden za.za_num_integers == 1); 4295b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 4296ad135b5dSChristopher Siden za.za_first_integer)); 4297ad135b5dSChristopher Siden } 4298ad135b5dSChristopher Siden zap_cursor_fini(&zc); 4299ad135b5dSChristopher Siden } 4300ad135b5dSChristopher Siden 4301ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 4302ad135b5dSChristopher Siden features) == 0); 4303ad135b5dSChristopher Siden nvlist_free(features); 4304ad135b5dSChristopher Siden } 4305ad135b5dSChristopher Siden 4306fa9e4066Sahrens int 4307ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 4308ad135b5dSChristopher Siden char *altroot, size_t buflen) 4309fa9e4066Sahrens { 4310fa9e4066Sahrens int error; 4311fa9e4066Sahrens spa_t *spa; 4312fa9e4066Sahrens 4313fa9e4066Sahrens *config = NULL; 4314468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 4315fa9e4066Sahrens 43166809eb4eSEric Schrock if (spa != NULL) { 43176809eb4eSEric Schrock /* 43186809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 43196809eb4eSEric Schrock * or l2cache devices could change and the config would be 43206809eb4eSEric Schrock * self-inconsistent. 43216809eb4eSEric Schrock */ 43226809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4323ea8dc4b6Seschrock 43246809eb4eSEric Schrock if (*config != NULL) { 432511027bc7STim Haley uint64_t loadtimes[2]; 432611027bc7STim Haley 432711027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 432811027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 432911027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 433011027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 433111027bc7STim Haley 4332e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 43336809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 43346809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 4335e14bb325SJeff Bonwick 43366809eb4eSEric Schrock if (spa_suspended(spa)) 43376809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 43386809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 43396809eb4eSEric Schrock spa->spa_failmode) == 0); 43406809eb4eSEric Schrock 43416809eb4eSEric Schrock spa_add_spares(spa, *config); 43426809eb4eSEric Schrock spa_add_l2cache(spa, *config); 4343ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 43446809eb4eSEric Schrock } 434599653d4eSeschrock } 434699653d4eSeschrock 4347ea8dc4b6Seschrock /* 4348ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 4349ea8dc4b6Seschrock * and call spa_lookup() directly. 4350ea8dc4b6Seschrock */ 4351ea8dc4b6Seschrock if (altroot) { 4352ea8dc4b6Seschrock if (spa == NULL) { 4353ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4354ea8dc4b6Seschrock spa = spa_lookup(name); 4355ea8dc4b6Seschrock if (spa) 4356ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 4357ea8dc4b6Seschrock else 4358ea8dc4b6Seschrock altroot[0] = '\0'; 4359ea8dc4b6Seschrock spa = NULL; 4360ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4361ea8dc4b6Seschrock } else { 4362ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 4363ea8dc4b6Seschrock } 4364ea8dc4b6Seschrock } 4365ea8dc4b6Seschrock 43666809eb4eSEric Schrock if (spa != NULL) { 43676809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4368fa9e4066Sahrens spa_close(spa, FTAG); 43696809eb4eSEric Schrock } 4370fa9e4066Sahrens 4371fa9e4066Sahrens return (error); 4372fa9e4066Sahrens } 4373fa9e4066Sahrens 437499653d4eSeschrock /* 4375fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 4376fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 4377fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 4378fa94a07fSbrendan * specified, as long as they are well-formed. 437999653d4eSeschrock */ 438099653d4eSeschrock static int 4381fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 4382fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 4383fa94a07fSbrendan vdev_labeltype_t label) 438499653d4eSeschrock { 4385fa94a07fSbrendan nvlist_t **dev; 4386fa94a07fSbrendan uint_t i, ndev; 438799653d4eSeschrock vdev_t *vd; 438899653d4eSeschrock int error; 438999653d4eSeschrock 4390e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4391e14bb325SJeff Bonwick 439299653d4eSeschrock /* 4393fa94a07fSbrendan * It's acceptable to have no devs specified. 439499653d4eSeschrock */ 4395fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 439699653d4eSeschrock return (0); 439799653d4eSeschrock 4398fa94a07fSbrendan if (ndev == 0) 4399be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 440099653d4eSeschrock 440199653d4eSeschrock /* 4402fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 4403fa94a07fSbrendan * device type. 440499653d4eSeschrock */ 4405fa94a07fSbrendan if (spa_version(spa) < version) 4406be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 440799653d4eSeschrock 440839c23413Seschrock /* 4409fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 441039c23413Seschrock * checking. 441139c23413Seschrock */ 4412fa94a07fSbrendan sav->sav_pending = dev; 4413fa94a07fSbrendan sav->sav_npending = ndev; 441439c23413Seschrock 4415fa94a07fSbrendan for (i = 0; i < ndev; i++) { 4416fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 441799653d4eSeschrock mode)) != 0) 441839c23413Seschrock goto out; 441999653d4eSeschrock 442099653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 442199653d4eSeschrock vdev_free(vd); 4422be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 442339c23413Seschrock goto out; 442499653d4eSeschrock } 442599653d4eSeschrock 4426fa94a07fSbrendan /* 4427e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 4428e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 4429fa94a07fSbrendan */ 4430e14bb325SJeff Bonwick #ifdef _KERNEL 4431fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 4432fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 4433be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 4434cd0837ccSGeorge Wilson vdev_free(vd); 4435fa94a07fSbrendan goto out; 4436fa94a07fSbrendan } 4437e14bb325SJeff Bonwick #endif 443899653d4eSeschrock vd->vdev_top = vd; 443999653d4eSeschrock 444039c23413Seschrock if ((error = vdev_open(vd)) == 0 && 4441fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 4442fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 444339c23413Seschrock vd->vdev_guid) == 0); 444439c23413Seschrock } 444599653d4eSeschrock 444699653d4eSeschrock vdev_free(vd); 444739c23413Seschrock 4448fa94a07fSbrendan if (error && 4449fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 445039c23413Seschrock goto out; 445139c23413Seschrock else 445239c23413Seschrock error = 0; 445399653d4eSeschrock } 445499653d4eSeschrock 445539c23413Seschrock out: 4456fa94a07fSbrendan sav->sav_pending = NULL; 4457fa94a07fSbrendan sav->sav_npending = 0; 445839c23413Seschrock return (error); 445999653d4eSeschrock } 446099653d4eSeschrock 4461fa94a07fSbrendan static int 4462fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 4463fa94a07fSbrendan { 4464fa94a07fSbrendan int error; 4465fa94a07fSbrendan 4466e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4467e14bb325SJeff Bonwick 4468fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4469fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 4470fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 4471fa94a07fSbrendan return (error); 4472fa94a07fSbrendan } 4473fa94a07fSbrendan 4474fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4475fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 4476fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 4477fa94a07fSbrendan } 4478fa94a07fSbrendan 4479fa94a07fSbrendan static void 4480fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 4481fa94a07fSbrendan const char *config) 4482fa94a07fSbrendan { 4483fa94a07fSbrendan int i; 4484fa94a07fSbrendan 4485fa94a07fSbrendan if (sav->sav_config != NULL) { 4486fa94a07fSbrendan nvlist_t **olddevs; 4487fa94a07fSbrendan uint_t oldndevs; 4488fa94a07fSbrendan nvlist_t **newdevs; 4489fa94a07fSbrendan 4490fa94a07fSbrendan /* 4491fa94a07fSbrendan * Generate new dev list by concatentating with the 4492fa94a07fSbrendan * current dev list. 4493fa94a07fSbrendan */ 4494fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 4495fa94a07fSbrendan &olddevs, &oldndevs) == 0); 4496fa94a07fSbrendan 4497fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 4498fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 4499fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 4500fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 4501fa94a07fSbrendan KM_SLEEP) == 0); 4502fa94a07fSbrendan for (i = 0; i < ndevs; i++) 4503fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 4504fa94a07fSbrendan KM_SLEEP) == 0); 4505fa94a07fSbrendan 4506fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 4507fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 4508fa94a07fSbrendan 4509fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 4510fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 4511fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 4512fa94a07fSbrendan nvlist_free(newdevs[i]); 4513fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 4514fa94a07fSbrendan } else { 4515fa94a07fSbrendan /* 4516fa94a07fSbrendan * Generate a new dev list. 4517fa94a07fSbrendan */ 4518fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 4519fa94a07fSbrendan KM_SLEEP) == 0); 4520fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 4521fa94a07fSbrendan devs, ndevs) == 0); 4522fa94a07fSbrendan } 4523fa94a07fSbrendan } 4524fa94a07fSbrendan 4525fa94a07fSbrendan /* 4526fa94a07fSbrendan * Stop and drop level 2 ARC devices 4527fa94a07fSbrendan */ 4528fa94a07fSbrendan void 4529fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 4530fa94a07fSbrendan { 4531fa94a07fSbrendan vdev_t *vd; 4532fa94a07fSbrendan int i; 4533fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 4534fa94a07fSbrendan 4535fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 4536fa94a07fSbrendan uint64_t pool; 4537fa94a07fSbrendan 4538fa94a07fSbrendan vd = sav->sav_vdevs[i]; 4539fa94a07fSbrendan ASSERT(vd != NULL); 4540fa94a07fSbrendan 45418ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 45428ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 4543fa94a07fSbrendan l2arc_remove_vdev(vd); 4544fa94a07fSbrendan } 4545fa94a07fSbrendan } 4546fa94a07fSbrendan 4547fa9e4066Sahrens /* 4548fa9e4066Sahrens * Pool Creation 4549fa9e4066Sahrens */ 4550fa9e4066Sahrens int 4551990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 45524445fffbSMatthew Ahrens nvlist_t *zplprops) 4553fa9e4066Sahrens { 4554fa9e4066Sahrens spa_t *spa; 4555990b4856Slling char *altroot = NULL; 45560373e76bSbonwick vdev_t *rvd; 4557fa9e4066Sahrens dsl_pool_t *dp; 4558fa9e4066Sahrens dmu_tx_t *tx; 4559573ca77eSGeorge Wilson int error = 0; 4560fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 4561fa94a07fSbrendan nvlist_t **spares, **l2cache; 4562fa94a07fSbrendan uint_t nspares, nl2cache; 4563cde58dbcSMatthew Ahrens uint64_t version, obj; 4564ad135b5dSChristopher Siden boolean_t has_features; 4565fa9e4066Sahrens 4566fa9e4066Sahrens /* 4567fa9e4066Sahrens * If this pool already exists, return failure. 4568fa9e4066Sahrens */ 4569fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4570fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 4571fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4572be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 4573fa9e4066Sahrens } 4574fa9e4066Sahrens 4575fa9e4066Sahrens /* 4576fa9e4066Sahrens * Allocate a new spa_t structure. 4577fa9e4066Sahrens */ 4578990b4856Slling (void) nvlist_lookup_string(props, 4579990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4580468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 45818ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 4582fa9e4066Sahrens 4583990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 4584990b4856Slling spa_deactivate(spa); 4585990b4856Slling spa_remove(spa); 4586c5904d13Seschrock mutex_exit(&spa_namespace_lock); 4587990b4856Slling return (error); 4588990b4856Slling } 4589990b4856Slling 4590ad135b5dSChristopher Siden has_features = B_FALSE; 4591ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 4592ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 4593ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 4594ad135b5dSChristopher Siden has_features = B_TRUE; 4595ad135b5dSChristopher Siden } 4596ad135b5dSChristopher Siden 4597ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 4598ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 4599990b4856Slling version = SPA_VERSION; 4600ad135b5dSChristopher Siden } 4601ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 4602b24ab676SJeff Bonwick 4603b24ab676SJeff Bonwick spa->spa_first_txg = txg; 4604b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 4605990b4856Slling spa->spa_uberblock.ub_version = version; 4606fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 46070f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 46085cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_state = DSS_NONE; 46095cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_removing_vdev = -1; 46105cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev = -1; 4611fa9e4066Sahrens 461254d692b7SGeorge Wilson /* 461354d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 461454d692b7SGeorge Wilson */ 46156f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 46166f834bc1SMatthew Ahrens KM_SLEEP); 46176f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 46186f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 46196f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 46206f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 46216f834bc1SMatthew Ahrens } 462254d692b7SGeorge Wilson 46230373e76bSbonwick /* 46240373e76bSbonwick * Create the root vdev. 46250373e76bSbonwick */ 4626e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 46270373e76bSbonwick 462899653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 46290373e76bSbonwick 463099653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 463199653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 46320373e76bSbonwick 4633b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 4634be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 463599653d4eSeschrock 463699653d4eSeschrock if (error == 0 && 463799653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 4638fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 463999653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 4640573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 4641573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 4642573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 4643573ca77eSGeorge Wilson } 46440373e76bSbonwick } 46450373e76bSbonwick 4646e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4647fa9e4066Sahrens 464899653d4eSeschrock if (error != 0) { 4649fa9e4066Sahrens spa_unload(spa); 4650fa9e4066Sahrens spa_deactivate(spa); 4651fa9e4066Sahrens spa_remove(spa); 4652fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4653fa9e4066Sahrens return (error); 4654fa9e4066Sahrens } 4655fa9e4066Sahrens 465699653d4eSeschrock /* 465799653d4eSeschrock * Get the list of spares, if specified. 465899653d4eSeschrock */ 465999653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 466099653d4eSeschrock &spares, &nspares) == 0) { 4661fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 466299653d4eSeschrock KM_SLEEP) == 0); 4663fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 466499653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 4665e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 466699653d4eSeschrock spa_load_spares(spa); 4667e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4668fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4669fa94a07fSbrendan } 4670fa94a07fSbrendan 4671fa94a07fSbrendan /* 4672fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 4673fa94a07fSbrendan */ 4674fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4675fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 4676fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 4677fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 4678fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 4679fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 4680e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4681fa94a07fSbrendan spa_load_l2cache(spa); 4682e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4683fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 468499653d4eSeschrock } 468599653d4eSeschrock 4686ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 46870a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 4688fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 4689ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 4690fa9e4066Sahrens 4691485bbbf5SGeorge Wilson /* 4692485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 4693485bbbf5SGeorge Wilson */ 4694485bbbf5SGeorge Wilson ddt_create(spa); 4695485bbbf5SGeorge Wilson 4696485bbbf5SGeorge Wilson spa_update_dspace(spa); 4697485bbbf5SGeorge Wilson 4698fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 4699fa9e4066Sahrens 4700fa9e4066Sahrens /* 4701fa9e4066Sahrens * Create the pool config object. 4702fa9e4066Sahrens */ 4703fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 4704f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 4705fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 4706fa9e4066Sahrens 4707ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 4708fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 4709ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 4710ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 4711ea8dc4b6Seschrock } 4712fa9e4066Sahrens 4713ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 4714ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 4715ad135b5dSChristopher Siden 47163f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 47173f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 47183f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 47193f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 47203f9d6ad7SLin Ling } 47213f9d6ad7SLin Ling 4722990b4856Slling /* Newly created pools with the right version are always deflated. */ 4723990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 4724990b4856Slling spa->spa_deflate = TRUE; 4725990b4856Slling if (zap_add(spa->spa_meta_objset, 4726990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 4727990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 4728990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 4729990b4856Slling } 473099653d4eSeschrock } 473199653d4eSeschrock 4732fa9e4066Sahrens /* 4733cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 4734fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 4735fa9e4066Sahrens * keeps changing. 4736fa9e4066Sahrens */ 4737cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 4738cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 4739cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 4740ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 4741cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 4742cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 4743cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 4744ea8dc4b6Seschrock } 4745b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 4746cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 4747fa9e4066Sahrens 474806eeb2adSek /* 474906eeb2adSek * Create the pool's history object. 475006eeb2adSek */ 4751990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 4752990b4856Slling spa_history_create_obj(spa, tx); 4753990b4856Slling 475445818ee1SMatthew Ahrens /* 475545818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 475645818ee1SMatthew Ahrens */ 475745818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 475845818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 475945818ee1SMatthew Ahrens 4760990b4856Slling /* 4761990b4856Slling * Set pool properties. 4762990b4856Slling */ 4763990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 4764990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 47650a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 4766573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 4767b24ab676SJeff Bonwick 4768379c004dSEric Schrock if (props != NULL) { 4769379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 47703b2aab18SMatthew Ahrens spa_sync_props(props, tx); 4771379c004dSEric Schrock } 477206eeb2adSek 4773fa9e4066Sahrens dmu_tx_commit(tx); 4774fa9e4066Sahrens 4775fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 4776fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 4777fa9e4066Sahrens 4778fa9e4066Sahrens /* 4779fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 4780fa9e4066Sahrens * bean counters are appropriately updated. 4781fa9e4066Sahrens */ 4782fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 4783fa9e4066Sahrens 4784667ec66fSSerapheim Dimitropoulos spa_spawn_aux_threads(spa); 4785667ec66fSSerapheim Dimitropoulos 47865cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 4787ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 4788fa9e4066Sahrens 47894445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 4790228975ccSek 4791bc9014e6SJustin Gibbs /* 4792bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 4793bc9014e6SJustin Gibbs * and are making their way through the eviction process. 4794bc9014e6SJustin Gibbs */ 4795bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4796088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 47970f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 4798088f3894Sahrens 4799daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 4800daaa36a7SGeorge Wilson 4801fa9e4066Sahrens return (0); 4802fa9e4066Sahrens } 4803fa9e4066Sahrens 4804e7cbe64fSgw #ifdef _KERNEL 4805e7cbe64fSgw /* 480621ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 480721ecdf64SLin Ling * during the system boot up time. 4808e7cbe64fSgw */ 480921ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 481021ecdf64SLin Ling 481121ecdf64SLin Ling static nvlist_t * 481221ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 4813e7cbe64fSgw { 481421ecdf64SLin Ling nvlist_t *config; 4815e7cbe64fSgw nvlist_t *nvtop, *nvroot; 4816e7cbe64fSgw uint64_t pgid; 4817e7cbe64fSgw 481821ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 481921ecdf64SLin Ling return (NULL); 482021ecdf64SLin Ling 4821e7cbe64fSgw /* 4822e7cbe64fSgw * Add this top-level vdev to the child array. 4823e7cbe64fSgw */ 482421ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 482521ecdf64SLin Ling &nvtop) == 0); 482621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 482721ecdf64SLin Ling &pgid) == 0); 482821ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 4829e7cbe64fSgw 4830e7cbe64fSgw /* 4831e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 4832e7cbe64fSgw */ 4833e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 483421ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 483521ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 4836e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 4837e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 4838e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4839e7cbe64fSgw &nvtop, 1) == 0); 4840e7cbe64fSgw 4841e7cbe64fSgw /* 4842e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 4843e7cbe64fSgw * this pool's configuration (remove the old, add the new). 4844e7cbe64fSgw */ 4845e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 4846e7cbe64fSgw nvlist_free(nvroot); 484721ecdf64SLin Ling return (config); 4848e7cbe64fSgw } 4849e7cbe64fSgw 4850e7cbe64fSgw /* 485121ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 485221ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 485321ecdf64SLin Ling * device has a more recent txg. 4854051aabe6Staylor */ 485521ecdf64SLin Ling static void 485621ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 4857051aabe6Staylor { 4858573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 485921ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 4860051aabe6Staylor 486121ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 486221ecdf64SLin Ling nvlist_t *label; 486321ecdf64SLin Ling uint64_t label_txg; 4864051aabe6Staylor 486521ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 486621ecdf64SLin Ling &label) != 0) 486721ecdf64SLin Ling return; 4868051aabe6Staylor 486921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 487021ecdf64SLin Ling &label_txg) == 0); 4871051aabe6Staylor 487221ecdf64SLin Ling /* 487321ecdf64SLin Ling * Do we have a better boot device? 487421ecdf64SLin Ling */ 487521ecdf64SLin Ling if (label_txg > *txg) { 487621ecdf64SLin Ling *txg = label_txg; 487721ecdf64SLin Ling *avd = vd; 4878051aabe6Staylor } 487921ecdf64SLin Ling nvlist_free(label); 4880051aabe6Staylor } 4881051aabe6Staylor } 4882051aabe6Staylor 4883e7cbe64fSgw /* 4884e7cbe64fSgw * Import a root pool. 4885e7cbe64fSgw * 4886051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 4887051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 4888051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 4889e7cbe64fSgw * 4890e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 4891e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 4892e7cbe64fSgw * e.g. 4893e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 4894e7cbe64fSgw */ 4895e7cbe64fSgw int 4896051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 4897e7cbe64fSgw { 489821ecdf64SLin Ling spa_t *spa; 489921ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 490021ecdf64SLin Ling nvlist_t *config, *nvtop; 490121ecdf64SLin Ling uint64_t guid, txg; 4902e7cbe64fSgw char *pname; 4903e7cbe64fSgw int error; 4904e7cbe64fSgw 4905e7cbe64fSgw /* 490621ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 4907e7cbe64fSgw */ 4908dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4909dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 4910dedec472SJack Meng if (config == NULL) { 4911dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 4912dedec472SJack Meng /* iscsi boot */ 4913dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 4914dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4915dedec472SJack Meng } 4916dedec472SJack Meng } 4917dedec472SJack Meng #endif 4918dedec472SJack Meng if (config == NULL) { 4919ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 492021ecdf64SLin Ling devpath); 4921be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 492221ecdf64SLin Ling } 4923e7cbe64fSgw 492421ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 492521ecdf64SLin Ling &pname) == 0); 492621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 4927e7cbe64fSgw 49286809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 49296809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 49306809eb4eSEric Schrock /* 49316809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 49326809eb4eSEric Schrock * can replace it with the correct config we just read in. 49336809eb4eSEric Schrock */ 49346809eb4eSEric Schrock spa_remove(spa); 49356809eb4eSEric Schrock } 49366809eb4eSEric Schrock 4937468c413aSTim Haley spa = spa_add(pname, config, NULL); 49386809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 49394b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 49400fb055e8SAndy Fiddaman if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 49410fb055e8SAndy Fiddaman &spa->spa_ubsync.ub_version) != 0) 49420fb055e8SAndy Fiddaman spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 4943e7cbe64fSgw 494421ecdf64SLin Ling /* 494521ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 494621ecdf64SLin Ling */ 494721ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 494821ecdf64SLin Ling &nvtop) == 0); 494921ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 495021ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 495121ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 495221ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 495321ecdf64SLin Ling if (error) { 495421ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 495521ecdf64SLin Ling nvlist_free(config); 495621ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 495721ecdf64SLin Ling pname); 495821ecdf64SLin Ling return (error); 495921ecdf64SLin Ling } 496021ecdf64SLin Ling 496121ecdf64SLin Ling /* 496221ecdf64SLin Ling * Get the boot vdev. 496321ecdf64SLin Ling */ 496421ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 496521ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 496621ecdf64SLin Ling (u_longlong_t)guid); 4967be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 496821ecdf64SLin Ling goto out; 496921ecdf64SLin Ling } 4970e7cbe64fSgw 497121ecdf64SLin Ling /* 497221ecdf64SLin Ling * Determine if there is a better boot device. 497321ecdf64SLin Ling */ 497421ecdf64SLin Ling avd = bvd; 497521ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 497621ecdf64SLin Ling if (avd != bvd) { 497721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 497821ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4979be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 498021ecdf64SLin Ling goto out; 498121ecdf64SLin Ling } 4982e7cbe64fSgw 498321ecdf64SLin Ling /* 498421ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 498521ecdf64SLin Ling * we're booting off the active spare. 498621ecdf64SLin Ling */ 498721ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 498821ecdf64SLin Ling !bvd->vdev_isspare) { 498921ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 499021ecdf64SLin Ling "try booting from '%s'", 4991cb04b873SMark J Musante bvd->vdev_parent-> 4992cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4993be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 499421ecdf64SLin Ling goto out; 499521ecdf64SLin Ling } 499621ecdf64SLin Ling 499721ecdf64SLin Ling error = 0; 499821ecdf64SLin Ling out: 499921ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 500021ecdf64SLin Ling vdev_free(rvd); 500121ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 500221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 500321ecdf64SLin Ling 500421ecdf64SLin Ling nvlist_free(config); 5005e7cbe64fSgw return (error); 5006e7cbe64fSgw } 500721ecdf64SLin Ling 5008e7cbe64fSgw #endif 5009e7cbe64fSgw 50106809eb4eSEric Schrock /* 50116809eb4eSEric Schrock * Import a non-root pool into the system. 50126809eb4eSEric Schrock */ 5013c5904d13Seschrock int 50144b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 5015c5904d13Seschrock { 50166809eb4eSEric Schrock spa_t *spa; 50176809eb4eSEric Schrock char *altroot = NULL; 5018468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 5019468c413aSTim Haley zpool_rewind_policy_t policy; 5020f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 5021f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 50226809eb4eSEric Schrock int error; 50236809eb4eSEric Schrock nvlist_t *nvroot; 50246809eb4eSEric Schrock nvlist_t **spares, **l2cache; 50256809eb4eSEric Schrock uint_t nspares, nl2cache; 50266809eb4eSEric Schrock 50276809eb4eSEric Schrock /* 50286809eb4eSEric Schrock * If a pool with this name exists, return failure. 50296809eb4eSEric Schrock */ 50306809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 50311195e687SMark J Musante if (spa_lookup(pool) != NULL) { 50326809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 5033be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 50346809eb4eSEric Schrock } 50356809eb4eSEric Schrock 50366809eb4eSEric Schrock /* 50376809eb4eSEric Schrock * Create and initialize the spa structure. 50386809eb4eSEric Schrock */ 50396809eb4eSEric Schrock (void) nvlist_lookup_string(props, 50406809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 5041f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 5042f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 5043f9af39baSGeorge Wilson if (readonly) 5044f9af39baSGeorge Wilson mode = FREAD; 5045468c413aSTim Haley spa = spa_add(pool, config, altroot); 50464b964adaSGeorge Wilson spa->spa_import_flags = flags; 50474b964adaSGeorge Wilson 50484b964adaSGeorge Wilson /* 50494b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 50504b964adaSGeorge Wilson * as if it had been loaded at boot. 50514b964adaSGeorge Wilson */ 50524b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 50534b964adaSGeorge Wilson if (props != NULL) 50544b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 50554b964adaSGeorge Wilson 50565cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 5057ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 50583ee8c80cSPavel Zakharov zfs_dbgmsg("spa_import: verbatim import of %s", pool); 50594b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 50604b964adaSGeorge Wilson return (0); 50614b964adaSGeorge Wilson } 50624b964adaSGeorge Wilson 5063f9af39baSGeorge Wilson spa_activate(spa, mode); 50646809eb4eSEric Schrock 506525f89ee2SJeff Bonwick /* 506625f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 506725f89ee2SJeff Bonwick */ 506825f89ee2SJeff Bonwick spa_async_suspend(spa); 506925f89ee2SJeff Bonwick 50704b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 50714b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 50724b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 50734b964adaSGeorge Wilson 50746f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; 50754b964adaSGeorge Wilson 50766f793812SPavel Zakharov if (state != SPA_LOAD_RECOVER) { 50776f793812SPavel Zakharov spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 50786f793812SPavel Zakharov zfs_dbgmsg("spa_import: importing %s", pool); 50796f793812SPavel Zakharov } else { 50806f793812SPavel Zakharov zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " 50816f793812SPavel Zakharov "(RECOVERY MODE)", pool, (longlong_t)policy.zrp_txg); 50826f793812SPavel Zakharov } 50836f793812SPavel Zakharov error = spa_load_best(spa, state, policy.zrp_txg, policy.zrp_request); 5084468c413aSTim Haley 5085468c413aSTim Haley /* 50864b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 50874b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 5088468c413aSTim Haley */ 50894b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 50904b964adaSGeorge Wilson spa->spa_load_info) == 0); 50916809eb4eSEric Schrock 50926809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 50936809eb4eSEric Schrock /* 50946809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 50956809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 50966809eb4eSEric Schrock */ 50976809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 50986809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 50996809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 51006809eb4eSEric Schrock spa_load_spares(spa); 51016809eb4eSEric Schrock } 51026809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 51036809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 51046809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 51056809eb4eSEric Schrock spa_load_l2cache(spa); 51066809eb4eSEric Schrock } 51076809eb4eSEric Schrock 51086809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 51096809eb4eSEric Schrock &nvroot) == 0); 51106809eb4eSEric Schrock if (error == 0) 51116809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 51126809eb4eSEric Schrock VDEV_ALLOC_SPARE); 51136809eb4eSEric Schrock if (error == 0) 51146809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 51156809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 51166809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 51176809eb4eSEric Schrock 51186809eb4eSEric Schrock if (props != NULL) 51196809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 51206809eb4eSEric Schrock 51216809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 51226809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 51236809eb4eSEric Schrock spa_unload(spa); 51246809eb4eSEric Schrock spa_deactivate(spa); 51256809eb4eSEric Schrock spa_remove(spa); 51266809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 51276809eb4eSEric Schrock return (error); 51286809eb4eSEric Schrock } 51296809eb4eSEric Schrock 5130955ef359SLin Ling spa_async_resume(spa); 5131955ef359SLin Ling 51326809eb4eSEric Schrock /* 51336809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 51346809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 51356809eb4eSEric Schrock */ 51366809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 51376809eb4eSEric Schrock &spares, &nspares) == 0) { 51386809eb4eSEric Schrock if (spa->spa_spares.sav_config) 51396809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 51406809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 51416809eb4eSEric Schrock else 51426809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 51436809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 51446809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 51456809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 51466809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 51476809eb4eSEric Schrock spa_load_spares(spa); 51486809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 51496809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 51506809eb4eSEric Schrock } 51516809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 51526809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 51536809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 51546809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 51556809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 51566809eb4eSEric Schrock else 51576809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 51586809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 51596809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 51606809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 51616809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 51626809eb4eSEric Schrock spa_load_l2cache(spa); 51636809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 51646809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 51656809eb4eSEric Schrock } 51666809eb4eSEric Schrock 5167b693757aSEric Schrock /* 5168b693757aSEric Schrock * Check for any removed devices. 5169b693757aSEric Schrock */ 5170b693757aSEric Schrock if (spa->spa_autoreplace) { 5171b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 5172b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 5173b693757aSEric Schrock } 5174b693757aSEric Schrock 51756809eb4eSEric Schrock if (spa_writeable(spa)) { 51766809eb4eSEric Schrock /* 51776809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 51786809eb4eSEric Schrock */ 5179bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 51806809eb4eSEric Schrock } 51816809eb4eSEric Schrock 5182573ca77eSGeorge Wilson /* 5183573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 5184573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 5185573ca77eSGeorge Wilson */ 5186573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 5187573ca77eSGeorge Wilson 51884445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 51896809eb4eSEric Schrock 5190ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 519114372834SHans Rosenfeld 519214372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 519314372834SHans Rosenfeld 51946809eb4eSEric Schrock return (0); 5195c5904d13Seschrock } 5196c5904d13Seschrock 5197fa9e4066Sahrens nvlist_t * 5198fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 5199fa9e4066Sahrens { 5200fa9e4066Sahrens nvlist_t *config = NULL; 52016f793812SPavel Zakharov char *poolname, *cachefile; 5202fa9e4066Sahrens spa_t *spa; 5203fa9e4066Sahrens uint64_t state; 52047b7154beSLin Ling int error; 52056f793812SPavel Zakharov zpool_rewind_policy_t policy; 5206fa9e4066Sahrens 5207fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 5208fa9e4066Sahrens return (NULL); 5209fa9e4066Sahrens 5210fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 5211fa9e4066Sahrens return (NULL); 5212fa9e4066Sahrens 5213fa9e4066Sahrens /* 52140373e76bSbonwick * Create and initialize the spa structure. 5215fa9e4066Sahrens */ 52160373e76bSbonwick mutex_enter(&spa_namespace_lock); 5217468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 52188ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 5219fa9e4066Sahrens 5220fa9e4066Sahrens /* 52216f793812SPavel Zakharov * Rewind pool if a max txg was provided. Note that even though we 52226f793812SPavel Zakharov * retrieve the complete rewind policy, only the rewind txg is relevant 52236f793812SPavel Zakharov * for tryimport. 5224fa9e4066Sahrens */ 52256f793812SPavel Zakharov zpool_get_rewind_policy(spa->spa_config, &policy); 52266f793812SPavel Zakharov if (policy.zrp_txg != UINT64_MAX) { 52276f793812SPavel Zakharov spa->spa_load_max_txg = policy.zrp_txg; 52286f793812SPavel Zakharov spa->spa_extreme_rewind = B_TRUE; 52296f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", 52306f793812SPavel Zakharov poolname, (longlong_t)policy.zrp_txg); 52316f793812SPavel Zakharov } else { 52326f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: importing %s", poolname); 52336f793812SPavel Zakharov } 52346f793812SPavel Zakharov 52356f793812SPavel Zakharov if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) 52366f793812SPavel Zakharov == 0) { 52376f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); 52386f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 52396f793812SPavel Zakharov } else { 52406f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_SCAN; 52416f793812SPavel Zakharov } 52426f793812SPavel Zakharov 52436f793812SPavel Zakharov error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); 5244fa9e4066Sahrens 5245fa9e4066Sahrens /* 5246fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 5247fa9e4066Sahrens */ 5248fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 5249fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 5250fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 5251fa9e4066Sahrens poolname) == 0); 5252fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 5253fa9e4066Sahrens state) == 0); 525495173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 525595173954Sek spa->spa_uberblock.ub_timestamp) == 0); 5256ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 5257ad135b5dSChristopher Siden spa->spa_load_info) == 0); 525899653d4eSeschrock 5259e7cbe64fSgw /* 5260e7cbe64fSgw * If the bootfs property exists on this pool then we 5261e7cbe64fSgw * copy it out so that external consumers can tell which 5262e7cbe64fSgw * pools are bootable. 5263e7cbe64fSgw */ 52647b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 5265e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5266e7cbe64fSgw 5267e7cbe64fSgw /* 5268e7cbe64fSgw * We have to play games with the name since the 5269e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 5270e7cbe64fSgw */ 5271e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 5272e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 5273e7cbe64fSgw char *cp; 5274e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5275e7cbe64fSgw 5276e7cbe64fSgw cp = strchr(tmpname, '/'); 5277e7cbe64fSgw if (cp == NULL) { 5278e7cbe64fSgw (void) strlcpy(dsname, tmpname, 5279e7cbe64fSgw MAXPATHLEN); 5280e7cbe64fSgw } else { 5281e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 5282e7cbe64fSgw "%s/%s", poolname, ++cp); 5283e7cbe64fSgw } 5284e7cbe64fSgw VERIFY(nvlist_add_string(config, 5285e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 5286e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 5287e7cbe64fSgw } 5288e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 5289e7cbe64fSgw } 5290e7cbe64fSgw 529199653d4eSeschrock /* 5292fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 529399653d4eSeschrock */ 52946809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 529599653d4eSeschrock spa_add_spares(spa, config); 5296fa94a07fSbrendan spa_add_l2cache(spa, config); 52976809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 5298fa9e4066Sahrens } 5299fa9e4066Sahrens 5300fa9e4066Sahrens spa_unload(spa); 5301fa9e4066Sahrens spa_deactivate(spa); 5302fa9e4066Sahrens spa_remove(spa); 5303fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5304fa9e4066Sahrens 5305fa9e4066Sahrens return (config); 5306fa9e4066Sahrens } 5307fa9e4066Sahrens 5308fa9e4066Sahrens /* 5309fa9e4066Sahrens * Pool export/destroy 5310fa9e4066Sahrens * 5311fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 5312fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 5313fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 5314394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 5315394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 5316fa9e4066Sahrens */ 5317fa9e4066Sahrens static int 531889a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 5319394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 5320fa9e4066Sahrens { 5321fa9e4066Sahrens spa_t *spa; 5322fa9e4066Sahrens 532344cd46caSbillm if (oldconfig) 532444cd46caSbillm *oldconfig = NULL; 532544cd46caSbillm 53268ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 5327be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 5328fa9e4066Sahrens 5329fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5330fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 5331fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5332be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 5333fa9e4066Sahrens } 5334fa9e4066Sahrens 5335ea8dc4b6Seschrock /* 5336ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 5337ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 5338ea8dc4b6Seschrock */ 5339ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 5340ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 5341ea8dc4b6Seschrock spa_async_suspend(spa); 5342ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 5343ea8dc4b6Seschrock spa_close(spa, FTAG); 5344ea8dc4b6Seschrock 5345fa9e4066Sahrens /* 5346fa9e4066Sahrens * The pool will be in core if it's openable, 5347fa9e4066Sahrens * in which case we can modify its state. 5348fa9e4066Sahrens */ 5349fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 5350fa9e4066Sahrens /* 5351fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 5352fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 5353fa9e4066Sahrens */ 5354fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 5355bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 5356fa9e4066Sahrens 5357ea8dc4b6Seschrock /* 5358ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 5359ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 5360ea8dc4b6Seschrock * fault injection handlers. 5361ea8dc4b6Seschrock */ 5362ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 5363ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 5364ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 5365ea8dc4b6Seschrock spa_async_resume(spa); 5366fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5367be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5368fa9e4066Sahrens } 5369fa9e4066Sahrens 537089a89ebfSlling /* 537189a89ebfSlling * A pool cannot be exported if it has an active shared spare. 537289a89ebfSlling * This is to prevent other pools stealing the active spare 537389a89ebfSlling * from an exported pool. At user's own will, such pool can 537489a89ebfSlling * be forcedly exported. 537589a89ebfSlling */ 537689a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 537789a89ebfSlling spa_has_active_shared_spare(spa)) { 537889a89ebfSlling spa_async_resume(spa); 537989a89ebfSlling mutex_exit(&spa_namespace_lock); 5380be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 538189a89ebfSlling } 538289a89ebfSlling 5383fa9e4066Sahrens /* 5384fa9e4066Sahrens * We want this to be reflected on every label, 5385fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 5386fa9e4066Sahrens * final sync that pushes these changes out. 5387fa9e4066Sahrens */ 5388394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 5389e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5390ea8dc4b6Seschrock spa->spa_state = new_state; 53913f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 53923f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 5393ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 5394e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5395ea8dc4b6Seschrock } 5396fa9e4066Sahrens } 5397fa9e4066Sahrens 5398ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 53993d7072f8Seschrock 5400fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5401fa9e4066Sahrens spa_unload(spa); 5402fa9e4066Sahrens spa_deactivate(spa); 5403fa9e4066Sahrens } 5404fa9e4066Sahrens 540544cd46caSbillm if (oldconfig && spa->spa_config) 540644cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 540744cd46caSbillm 5408ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 5409394ab0cbSGeorge Wilson if (!hardforce) 54105cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 5411ea8dc4b6Seschrock spa_remove(spa); 5412ea8dc4b6Seschrock } 5413fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5414fa9e4066Sahrens 5415fa9e4066Sahrens return (0); 5416fa9e4066Sahrens } 5417fa9e4066Sahrens 5418fa9e4066Sahrens /* 5419fa9e4066Sahrens * Destroy a storage pool. 5420fa9e4066Sahrens */ 5421fa9e4066Sahrens int 5422fa9e4066Sahrens spa_destroy(char *pool) 5423fa9e4066Sahrens { 5424394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 5425394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 5426fa9e4066Sahrens } 5427fa9e4066Sahrens 5428fa9e4066Sahrens /* 5429fa9e4066Sahrens * Export a storage pool. 5430fa9e4066Sahrens */ 5431fa9e4066Sahrens int 5432394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 5433394ab0cbSGeorge Wilson boolean_t hardforce) 5434fa9e4066Sahrens { 5435394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 5436394ab0cbSGeorge Wilson force, hardforce)); 5437fa9e4066Sahrens } 5438fa9e4066Sahrens 5439ea8dc4b6Seschrock /* 5440ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 5441ea8dc4b6Seschrock * from the namespace in any way. 5442ea8dc4b6Seschrock */ 5443ea8dc4b6Seschrock int 5444ea8dc4b6Seschrock spa_reset(char *pool) 5445ea8dc4b6Seschrock { 544689a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 5447394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 5448ea8dc4b6Seschrock } 5449ea8dc4b6Seschrock 5450fa9e4066Sahrens /* 5451fa9e4066Sahrens * ========================================================================== 5452fa9e4066Sahrens * Device manipulation 5453fa9e4066Sahrens * ========================================================================== 5454fa9e4066Sahrens */ 5455fa9e4066Sahrens 5456fa9e4066Sahrens /* 54578654d025Sperrin * Add a device to a storage pool. 5458fa9e4066Sahrens */ 5459fa9e4066Sahrens int 5460fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 5461fa9e4066Sahrens { 546288ecc943SGeorge Wilson uint64_t txg, id; 54638ad4d6ddSJeff Bonwick int error; 5464fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 54650e34b6a7Sbonwick vdev_t *vd, *tvd; 5466fa94a07fSbrendan nvlist_t **spares, **l2cache; 5467fa94a07fSbrendan uint_t nspares, nl2cache; 5468fa9e4066Sahrens 5469f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5470f9af39baSGeorge Wilson 5471fa9e4066Sahrens txg = spa_vdev_enter(spa); 5472fa9e4066Sahrens 547399653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 547499653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 547599653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 5476fa9e4066Sahrens 5477e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 547899653d4eSeschrock 5479fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 5480fa94a07fSbrendan &nspares) != 0) 548199653d4eSeschrock nspares = 0; 548299653d4eSeschrock 5483fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 5484fa94a07fSbrendan &nl2cache) != 0) 5485fa94a07fSbrendan nl2cache = 0; 5486fa94a07fSbrendan 5487e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 5488fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 5489fa9e4066Sahrens 5490e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 5491e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 5492e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 549399653d4eSeschrock 549439c23413Seschrock /* 5495fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 5496fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 549739c23413Seschrock */ 5498e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 549939c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 550039c23413Seschrock 550139c23413Seschrock /* 55025cabbc6bSPrashanth Sreenivasa * If we are in the middle of a device removal, we can only add 55035cabbc6bSPrashanth Sreenivasa * devices which match the existing devices in the pool. 55045cabbc6bSPrashanth Sreenivasa * If we are in the middle of a removal, or have some indirect 55055cabbc6bSPrashanth Sreenivasa * vdevs, we can not add raidz toplevels. 550639c23413Seschrock */ 55075cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 55085cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 55095cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 55105cabbc6bSPrashanth Sreenivasa tvd = vd->vdev_child[c]; 55115cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL && 55125cabbc6bSPrashanth Sreenivasa tvd->vdev_ashift != 55135cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal->svr_vdev->vdev_ashift) { 55145cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 55155cabbc6bSPrashanth Sreenivasa } 55165cabbc6bSPrashanth Sreenivasa /* Fail if top level vdev is raidz */ 55175cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_raidz_ops) { 55185cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 55195cabbc6bSPrashanth Sreenivasa } 55205cabbc6bSPrashanth Sreenivasa /* 55215cabbc6bSPrashanth Sreenivasa * Need the top level mirror to be 55225cabbc6bSPrashanth Sreenivasa * a mirror of leaf vdevs only 55235cabbc6bSPrashanth Sreenivasa */ 55245cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_mirror_ops) { 55255cabbc6bSPrashanth Sreenivasa for (uint64_t cid = 0; 55265cabbc6bSPrashanth Sreenivasa cid < tvd->vdev_children; cid++) { 55275cabbc6bSPrashanth Sreenivasa vdev_t *cvd = tvd->vdev_child[cid]; 55285cabbc6bSPrashanth Sreenivasa if (!cvd->vdev_ops->vdev_op_leaf) { 55295cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, 55305cabbc6bSPrashanth Sreenivasa txg, EINVAL)); 55315cabbc6bSPrashanth Sreenivasa } 55325cabbc6bSPrashanth Sreenivasa } 55335cabbc6bSPrashanth Sreenivasa } 55345cabbc6bSPrashanth Sreenivasa } 55355cabbc6bSPrashanth Sreenivasa } 55365cabbc6bSPrashanth Sreenivasa 55378ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 553888ecc943SGeorge Wilson 553988ecc943SGeorge Wilson /* 554088ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 554188ecc943SGeorge Wilson */ 554288ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 554388ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 554488ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 554588ecc943SGeorge Wilson break; 554688ecc943SGeorge Wilson } 554788ecc943SGeorge Wilson } 554839c23413Seschrock tvd = vd->vdev_child[c]; 554939c23413Seschrock vdev_remove_child(vd, tvd); 555088ecc943SGeorge Wilson tvd->vdev_id = id; 555139c23413Seschrock vdev_add_child(rvd, tvd); 555239c23413Seschrock vdev_config_dirty(tvd); 555339c23413Seschrock } 555439c23413Seschrock 555599653d4eSeschrock if (nspares != 0) { 5556fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 5557fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 555899653d4eSeschrock spa_load_spares(spa); 5559fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 5560fa94a07fSbrendan } 5561fa94a07fSbrendan 5562fa94a07fSbrendan if (nl2cache != 0) { 5563fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 5564fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 5565fa94a07fSbrendan spa_load_l2cache(spa); 5566fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 5567fa9e4066Sahrens } 5568fa9e4066Sahrens 5569fa9e4066Sahrens /* 55700e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 55710e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 55720e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 55730e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 55740e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 55750e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 55760373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 55770e34b6a7Sbonwick * 55780e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 55790e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 55800e34b6a7Sbonwick * steps will be completed the next time we load the pool. 55810e34b6a7Sbonwick */ 55820373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 55830e34b6a7Sbonwick 55840373e76bSbonwick mutex_enter(&spa_namespace_lock); 55850373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5586ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 55870373e76bSbonwick mutex_exit(&spa_namespace_lock); 5588fa9e4066Sahrens 55890373e76bSbonwick return (0); 5590fa9e4066Sahrens } 5591fa9e4066Sahrens 5592fa9e4066Sahrens /* 5593fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 5594fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 5595fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 5596fa9e4066Sahrens * 5597fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 5598fa9e4066Sahrens * existing device; in this case the two devices are made into their own 55993d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 5600fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 5601fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 5602fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 5603fa9e4066Sahrens * is automatically detached. 5604fa9e4066Sahrens */ 5605fa9e4066Sahrens int 5606ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 5607fa9e4066Sahrens { 56083f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 5609fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 5610fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 561199653d4eSeschrock vdev_ops_t *pvops; 56129b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 56139b3f6b42SEric Kustarz int newvd_isspare; 56149b3f6b42SEric Kustarz int error; 5615fa9e4066Sahrens 5616f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5617f9af39baSGeorge Wilson 5618fa9e4066Sahrens txg = spa_vdev_enter(spa); 5619fa9e4066Sahrens 5620c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 5621fa9e4066Sahrens 5622*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5623*86714001SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 5624*86714001SSerapheim Dimitropoulos error = (spa_has_checkpoint(spa)) ? 5625*86714001SSerapheim Dimitropoulos ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 5626*86714001SSerapheim Dimitropoulos return (spa_vdev_exit(spa, NULL, txg, error)); 5627*86714001SSerapheim Dimitropoulos } 5628*86714001SSerapheim Dimitropoulos 56295cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 56305cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 56315cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 56325cabbc6bSPrashanth Sreenivasa } 56335cabbc6bSPrashanth Sreenivasa 5634fa9e4066Sahrens if (oldvd == NULL) 5635fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 5636fa9e4066Sahrens 56370e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 56380e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 56390e34b6a7Sbonwick 5640fa9e4066Sahrens pvd = oldvd->vdev_parent; 5641fa9e4066Sahrens 564299653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 5643cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 56443d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 56453d7072f8Seschrock 56463d7072f8Seschrock if (newrootvd->vdev_children != 1) 5647fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 5648fa9e4066Sahrens 5649fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 5650fa9e4066Sahrens 5651fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 5652fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 5653fa9e4066Sahrens 565499653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 5655fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 5656fa9e4066Sahrens 56578654d025Sperrin /* 56588654d025Sperrin * Spares can't replace logs 56598654d025Sperrin */ 5660ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 56618654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 56628654d025Sperrin 566399653d4eSeschrock if (!replacing) { 566499653d4eSeschrock /* 566599653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 566699653d4eSeschrock * vdev. 566799653d4eSeschrock */ 566899653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 566999653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 567099653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 567199653d4eSeschrock 567299653d4eSeschrock pvops = &vdev_mirror_ops; 567399653d4eSeschrock } else { 567499653d4eSeschrock /* 567599653d4eSeschrock * Active hot spares can only be replaced by inactive hot 567699653d4eSeschrock * spares. 567799653d4eSeschrock */ 567899653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 5679cb04b873SMark J Musante oldvd->vdev_isspare && 568099653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 568199653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 568299653d4eSeschrock 568399653d4eSeschrock /* 568499653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 568599653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 568639c23413Seschrock * want to create a replacing vdev. The user is not allowed to 568739c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 568839c23413Seschrock * the same (spare replaces spare, non-spare replaces 568939c23413Seschrock * non-spare). 569099653d4eSeschrock */ 5691cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 5692cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 569399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 5694cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 5695cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 569639c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 5697cb04b873SMark J Musante } 5698cb04b873SMark J Musante 5699cb04b873SMark J Musante if (newvd->vdev_isspare) 570099653d4eSeschrock pvops = &vdev_spare_ops; 570199653d4eSeschrock else 570299653d4eSeschrock pvops = &vdev_replacing_ops; 570399653d4eSeschrock } 570499653d4eSeschrock 57052a79c5feSlling /* 5706573ca77eSGeorge Wilson * Make sure the new device is big enough. 57072a79c5feSlling */ 5708573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 5709fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 5710fa9e4066Sahrens 5711ecc2d604Sbonwick /* 5712ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 5713ecc2d604Sbonwick * than the top-level vdev. 5714ecc2d604Sbonwick */ 5715ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 5716fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 5717fa9e4066Sahrens 5718fa9e4066Sahrens /* 5719fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 5720fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 5721fa9e4066Sahrens */ 5722fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 5723fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 5724fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 5725fa9e4066Sahrens KM_SLEEP); 5726fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 5727fa9e4066Sahrens newvd->vdev_path, "old"); 5728fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 5729fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 5730fa9e4066Sahrens oldvd->vdev_devid = NULL; 5731fa9e4066Sahrens } 5732fa9e4066Sahrens } 5733fa9e4066Sahrens 5734cb04b873SMark J Musante /* mark the device being resilvered */ 5735b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 5736cb04b873SMark J Musante 5737fa9e4066Sahrens /* 573899653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 573999653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 5740fa9e4066Sahrens */ 5741fa9e4066Sahrens if (pvd->vdev_ops != pvops) 5742fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 5743fa9e4066Sahrens 5744fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 5745fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 5746fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 5747fa9e4066Sahrens 5748fa9e4066Sahrens /* 5749fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 5750fa9e4066Sahrens */ 5751fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 5752fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 575388ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 5754fa9e4066Sahrens vdev_add_child(pvd, newvd); 5755fa9e4066Sahrens 5756fa9e4066Sahrens tvd = newvd->vdev_top; 5757fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 5758fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 5759fa9e4066Sahrens 5760fa9e4066Sahrens vdev_config_dirty(tvd); 5761fa9e4066Sahrens 5762fa9e4066Sahrens /* 57633f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 57643f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 57653f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 5766fa9e4066Sahrens */ 57673f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 5768fa9e4066Sahrens 57693f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 57703f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 5771fa9e4066Sahrens 57726809eb4eSEric Schrock if (newvd->vdev_isspare) { 577339c23413Seschrock spa_spare_activate(newvd); 5774ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 57756809eb4eSEric Schrock } 57766809eb4eSEric Schrock 5777e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 5778e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 57799b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 5780ea8dc4b6Seschrock 5781fa9e4066Sahrens /* 5782fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 5783fa9e4066Sahrens */ 5784ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 5785fa9e4066Sahrens 57863f9d6ad7SLin Ling /* 57870713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 57880713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 57890713e232SGeorge Wilson * respective datasets. 57903f9d6ad7SLin Ling */ 57913f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 57923f9d6ad7SLin Ling 579314372834SHans Rosenfeld if (spa->spa_bootfs) 5794ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 579514372834SHans Rosenfeld 5796ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 579714372834SHans Rosenfeld 57983f9d6ad7SLin Ling /* 57993f9d6ad7SLin Ling * Commit the config 58003f9d6ad7SLin Ling */ 58013f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 5802fa9e4066Sahrens 58034445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 58043f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 5805c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 5806c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 5807c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 58089b3f6b42SEric Kustarz 58099b3f6b42SEric Kustarz spa_strfree(oldvdpath); 58109b3f6b42SEric Kustarz spa_strfree(newvdpath); 58119b3f6b42SEric Kustarz 5812fa9e4066Sahrens return (0); 5813fa9e4066Sahrens } 5814fa9e4066Sahrens 5815fa9e4066Sahrens /* 5816fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 5817f7170741SWill Andrews * 5818fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 5819fa9e4066Sahrens * is a replacing vdev. 5820fa9e4066Sahrens */ 5821fa9e4066Sahrens int 58228ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 5823fa9e4066Sahrens { 5824fa9e4066Sahrens uint64_t txg; 58258ad4d6ddSJeff Bonwick int error; 5826fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 5827fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 582899653d4eSeschrock boolean_t unspare = B_FALSE; 5829d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 58301195e687SMark J Musante char *vdpath; 5831fa9e4066Sahrens 5832f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5833f9af39baSGeorge Wilson 5834fa9e4066Sahrens txg = spa_vdev_enter(spa); 5835fa9e4066Sahrens 5836c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5837fa9e4066Sahrens 5838*86714001SSerapheim Dimitropoulos /* 5839*86714001SSerapheim Dimitropoulos * Besides being called directly from the userland through the 5840*86714001SSerapheim Dimitropoulos * ioctl interface, spa_vdev_detach() can be potentially called 5841*86714001SSerapheim Dimitropoulos * at the end of spa_vdev_resilver_done(). 5842*86714001SSerapheim Dimitropoulos * 5843*86714001SSerapheim Dimitropoulos * In the regular case, when we have a checkpoint this shouldn't 5844*86714001SSerapheim Dimitropoulos * happen as we never empty the DTLs of a vdev during the scrub 5845*86714001SSerapheim Dimitropoulos * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() 5846*86714001SSerapheim Dimitropoulos * should never get here when we have a checkpoint. 5847*86714001SSerapheim Dimitropoulos * 5848*86714001SSerapheim Dimitropoulos * That said, even in a case when we checkpoint the pool exactly 5849*86714001SSerapheim Dimitropoulos * as spa_vdev_resilver_done() calls this function everything 5850*86714001SSerapheim Dimitropoulos * should be fine as the resilver will return right away. 5851*86714001SSerapheim Dimitropoulos */ 5852*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 5853*86714001SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 5854*86714001SSerapheim Dimitropoulos error = (spa_has_checkpoint(spa)) ? 5855*86714001SSerapheim Dimitropoulos ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 5856*86714001SSerapheim Dimitropoulos return (spa_vdev_exit(spa, NULL, txg, error)); 5857*86714001SSerapheim Dimitropoulos } 5858*86714001SSerapheim Dimitropoulos 5859fa9e4066Sahrens if (vd == NULL) 5860fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 5861fa9e4066Sahrens 58620e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 58630e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 58640e34b6a7Sbonwick 5865fa9e4066Sahrens pvd = vd->vdev_parent; 5866fa9e4066Sahrens 58678ad4d6ddSJeff Bonwick /* 58688ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 58698ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 58708ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 58718ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 58728ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 58738ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 58748ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 58758ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 58768ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 58778ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 58788ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 58798ad4d6ddSJeff Bonwick */ 58808ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 58818ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 58828ad4d6ddSJeff Bonwick 5883fa9e4066Sahrens /* 5884cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 588599653d4eSeschrock */ 5886cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 5887cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 5888cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 588999653d4eSeschrock 589099653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 5891e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 5892fa9e4066Sahrens 5893fa9e4066Sahrens /* 589499653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 5895fa9e4066Sahrens */ 5896fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 589799653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 589899653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 5899fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 5900fa9e4066Sahrens 5901fa9e4066Sahrens /* 59028ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 59038ad4d6ddSJeff Bonwick * we cannot safely detach it. 5904fa9e4066Sahrens */ 59058ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 5906fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 5907fa9e4066Sahrens 59088ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 5909fa9e4066Sahrens 5910bf82a41bSeschrock /* 5911bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 5912bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 5913bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 5914bf82a41bSeschrock */ 5915cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 5916cb04b873SMark J Musante vd->vdev_path != NULL) { 5917cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 5918cb04b873SMark J Musante 5919cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 5920cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 5921cb04b873SMark J Musante 5922cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 5923cb04b873SMark J Musante continue; 5924cb04b873SMark J Musante 5925cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 5926cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 5927cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 5928cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 5929cb04b873SMark J Musante break; 5930cb04b873SMark J Musante } 5931bf82a41bSeschrock } 5932bf82a41bSeschrock } 5933bf82a41bSeschrock 593499653d4eSeschrock /* 593599653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 593699653d4eSeschrock * that the spare should become a real disk, and be removed from the 593799653d4eSeschrock * active spare list for the pool. 593899653d4eSeschrock */ 593999653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 5940cb04b873SMark J Musante vd->vdev_id == 0 && 5941cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 594299653d4eSeschrock unspare = B_TRUE; 594399653d4eSeschrock 5944fa9e4066Sahrens /* 5945fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 5946fa9e4066Sahrens * This must be done after all other error cases are handled, 5947fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 5948fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 5949fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 5950fa9e4066Sahrens * it's being detached! 5951fa9e4066Sahrens */ 595239c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5953fa9e4066Sahrens 5954fa9e4066Sahrens /* 5955fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 5956fa9e4066Sahrens */ 5957fa9e4066Sahrens vdev_remove_child(pvd, vd); 5958fa9e4066Sahrens vdev_compact_children(pvd); 5959fa9e4066Sahrens 5960fa9e4066Sahrens /* 5961fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 5962fa9e4066Sahrens */ 5963cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 5964fa9e4066Sahrens 596599653d4eSeschrock /* 596699653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 59678ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 59688ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 59698ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 59708ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 59718ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 59728ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 597399653d4eSeschrock */ 597499653d4eSeschrock if (unspare) { 597599653d4eSeschrock ASSERT(cvd->vdev_isspare); 597639c23413Seschrock spa_spare_remove(cvd); 597799653d4eSeschrock unspare_guid = cvd->vdev_guid; 59788ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 5979cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 598099653d4eSeschrock } 598199653d4eSeschrock 5982fa9e4066Sahrens /* 5983fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 5984fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 5985fa9e4066Sahrens */ 5986cb04b873SMark J Musante if (pvd->vdev_children == 1) { 5987cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 5988cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 5989fa9e4066Sahrens vdev_remove_parent(cvd); 5990cb04b873SMark J Musante } 5991cb04b873SMark J Musante 5992fa9e4066Sahrens 5993fa9e4066Sahrens /* 5994fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 5995fa9e4066Sahrens * may have been the previous top-level vdev. 5996fa9e4066Sahrens */ 5997fa9e4066Sahrens tvd = cvd->vdev_top; 5998fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 5999fa9e4066Sahrens 6000fa9e4066Sahrens /* 600139c23413Seschrock * Reevaluate the parent vdev state. 6002fa9e4066Sahrens */ 60033d7072f8Seschrock vdev_propagate_state(cvd); 6004fa9e4066Sahrens 6005fa9e4066Sahrens /* 6006573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 6007573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 6008573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 6009573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 6010573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 6011fa9e4066Sahrens */ 6012573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 6013573ca77eSGeorge Wilson vdev_reopen(tvd); 6014573ca77eSGeorge Wilson vdev_expand(tvd, txg); 6015573ca77eSGeorge Wilson } 6016fa9e4066Sahrens 6017fa9e4066Sahrens vdev_config_dirty(tvd); 6018fa9e4066Sahrens 6019fa9e4066Sahrens /* 602039c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 602139c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 602239c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 602339c23413Seschrock * prevent vd from being accessed after it's freed. 6024fa9e4066Sahrens */ 60251195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 60268ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 6027fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 6028ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 6029ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 6030fa9e4066Sahrens 6031ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 60323d7072f8Seschrock 6033cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 6034cb04b873SMark J Musante spa_open_ref(spa, FTAG); 6035cb04b873SMark J Musante 603699653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 603799653d4eSeschrock 60384445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 60391195e687SMark J Musante "vdev=%s", vdpath); 60401195e687SMark J Musante spa_strfree(vdpath); 60411195e687SMark J Musante 604299653d4eSeschrock /* 604339c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 604439c23413Seschrock * then we want to go through and remove the device from the hot spare 604539c23413Seschrock * list of every other pool. 604699653d4eSeschrock */ 604799653d4eSeschrock if (unspare) { 6048cb04b873SMark J Musante spa_t *altspa = NULL; 6049cb04b873SMark J Musante 605099653d4eSeschrock mutex_enter(&spa_namespace_lock); 6051cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 6052cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 6053cb04b873SMark J Musante altspa == spa) 605499653d4eSeschrock continue; 6055cb04b873SMark J Musante 6056cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 60579af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 6058cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 60599af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 6060cb04b873SMark J Musante spa_close(altspa, FTAG); 606199653d4eSeschrock } 606299653d4eSeschrock mutex_exit(&spa_namespace_lock); 6063cb04b873SMark J Musante 6064cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 6065cb04b873SMark J Musante spa_vdev_resilver_done(spa); 606699653d4eSeschrock } 606799653d4eSeschrock 6068cb04b873SMark J Musante /* all done with the spa; OK to release */ 6069cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 6070cb04b873SMark J Musante spa_close(spa, FTAG); 6071cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 6072cb04b873SMark J Musante 607399653d4eSeschrock return (error); 607499653d4eSeschrock } 607599653d4eSeschrock 60761195e687SMark J Musante /* 60771195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 60781195e687SMark J Musante */ 60791195e687SMark J Musante int 60801195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 60811195e687SMark J Musante nvlist_t *props, boolean_t exp) 60821195e687SMark J Musante { 60831195e687SMark J Musante int error = 0; 60841195e687SMark J Musante uint64_t txg, *glist; 60851195e687SMark J Musante spa_t *newspa; 60861195e687SMark J Musante uint_t c, children, lastlog; 60871195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 60881195e687SMark J Musante dmu_tx_t *tx; 60891195e687SMark J Musante char *altroot = NULL; 60901195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 60911195e687SMark J Musante boolean_t activate_slog; 60921195e687SMark J Musante 6093f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 60941195e687SMark J Musante 60951195e687SMark J Musante txg = spa_vdev_enter(spa); 60961195e687SMark J Musante 6097*86714001SSerapheim Dimitropoulos ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6098*86714001SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 6099*86714001SSerapheim Dimitropoulos error = (spa_has_checkpoint(spa)) ? 6100*86714001SSerapheim Dimitropoulos ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 6101*86714001SSerapheim Dimitropoulos return (spa_vdev_exit(spa, NULL, txg, error)); 6102*86714001SSerapheim Dimitropoulos } 6103*86714001SSerapheim Dimitropoulos 61041195e687SMark J Musante /* clear the log and flush everything up to now */ 61051195e687SMark J Musante activate_slog = spa_passivate_log(spa); 61061195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 61075cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 61081195e687SMark J Musante txg = spa_vdev_config_enter(spa); 61091195e687SMark J Musante 61101195e687SMark J Musante if (activate_slog) 61111195e687SMark J Musante spa_activate_log(spa); 61121195e687SMark J Musante 61131195e687SMark J Musante if (error != 0) 61141195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 61151195e687SMark J Musante 61161195e687SMark J Musante /* check new spa name before going any further */ 61171195e687SMark J Musante if (spa_lookup(newname) != NULL) 61181195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 61191195e687SMark J Musante 61201195e687SMark J Musante /* 61211195e687SMark J Musante * scan through all the children to ensure they're all mirrors 61221195e687SMark J Musante */ 61231195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 61241195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 61251195e687SMark J Musante &children) != 0) 61261195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 61271195e687SMark J Musante 61281195e687SMark J Musante /* first, check to ensure we've got the right child count */ 61291195e687SMark J Musante rvd = spa->spa_root_vdev; 61301195e687SMark J Musante lastlog = 0; 61311195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 61321195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 61331195e687SMark J Musante 61341195e687SMark J Musante /* don't count the holes & logs as children */ 61355cabbc6bSPrashanth Sreenivasa if (vd->vdev_islog || !vdev_is_concrete(vd)) { 61361195e687SMark J Musante if (lastlog == 0) 61371195e687SMark J Musante lastlog = c; 61381195e687SMark J Musante continue; 61391195e687SMark J Musante } 61401195e687SMark J Musante 61411195e687SMark J Musante lastlog = 0; 61421195e687SMark J Musante } 61431195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 61441195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 61451195e687SMark J Musante 61461195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 61471195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 61481195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 61491195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 61501195e687SMark J Musante 61511195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 61521195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 61531195e687SMark J Musante 61541195e687SMark J Musante /* then, loop over each vdev and validate it */ 61551195e687SMark J Musante for (c = 0; c < children; c++) { 61561195e687SMark J Musante uint64_t is_hole = 0; 61571195e687SMark J Musante 61581195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 61591195e687SMark J Musante &is_hole); 61601195e687SMark J Musante 61611195e687SMark J Musante if (is_hole != 0) { 61621195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 61631195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 61641195e687SMark J Musante continue; 61651195e687SMark J Musante } else { 6166be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 61671195e687SMark J Musante break; 61681195e687SMark J Musante } 61691195e687SMark J Musante } 61701195e687SMark J Musante 61711195e687SMark J Musante /* which disk is going to be split? */ 61721195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 61731195e687SMark J Musante &glist[c]) != 0) { 6174be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 61751195e687SMark J Musante break; 61761195e687SMark J Musante } 61771195e687SMark J Musante 61781195e687SMark J Musante /* look it up in the spa */ 61791195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 61801195e687SMark J Musante if (vml[c] == NULL) { 6181be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 61821195e687SMark J Musante break; 61831195e687SMark J Musante } 61841195e687SMark J Musante 61851195e687SMark J Musante /* make sure there's nothing stopping the split */ 61861195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 61871195e687SMark J Musante vml[c]->vdev_islog || 61885cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vml[c]) || 61891195e687SMark J Musante vml[c]->vdev_isspare || 61901195e687SMark J Musante vml[c]->vdev_isl2cache || 61911195e687SMark J Musante !vdev_writeable(vml[c]) || 6192d41c4376SMark J Musante vml[c]->vdev_children != 0 || 61931195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 61941195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 6195be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 61961195e687SMark J Musante break; 61971195e687SMark J Musante } 61981195e687SMark J Musante 61991195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 6200be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 62011195e687SMark J Musante break; 62021195e687SMark J Musante } 62031195e687SMark J Musante 62041195e687SMark J Musante /* we need certain info from the top level */ 62051195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 62061195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 62071195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 62081195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 62091195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 62101195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 62111195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 62121195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 6213215198a6SJoe Stein 6214215198a6SJoe Stein /* transfer per-vdev ZAPs */ 6215215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 6216215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 6217215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 6218215198a6SJoe Stein 6219215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 6220215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 6221215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 6222215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 62231195e687SMark J Musante } 62241195e687SMark J Musante 62251195e687SMark J Musante if (error != 0) { 62261195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 62271195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 62281195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 62291195e687SMark J Musante } 62301195e687SMark J Musante 62311195e687SMark J Musante /* stop writers from using the disks */ 62321195e687SMark J Musante for (c = 0; c < children; c++) { 62331195e687SMark J Musante if (vml[c] != NULL) 62341195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 62351195e687SMark J Musante } 62361195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 62371195e687SMark J Musante 62381195e687SMark J Musante /* 62391195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 62401195e687SMark J Musante * will disappear once the config is regenerated. 62411195e687SMark J Musante */ 62421195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 62431195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 62441195e687SMark J Musante glist, children) == 0); 62451195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 62461195e687SMark J Musante 624798295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 62481195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 62491195e687SMark J Musante nvl) == 0); 625098295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 62511195e687SMark J Musante spa->spa_config_splitting = nvl; 62521195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 62531195e687SMark J Musante 62541195e687SMark J Musante /* configure and create the new pool */ 62551195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 62561195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 62571195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 62581195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 62591195e687SMark J Musante spa_version(spa)) == 0); 62601195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 62611195e687SMark J Musante spa->spa_config_txg) == 0); 62621195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 62631195e687SMark J Musante spa_generate_guid(NULL)) == 0); 6264215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 62651195e687SMark J Musante (void) nvlist_lookup_string(props, 62661195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 62671195e687SMark J Musante 6268d41c4376SMark J Musante /* add the new pool to the namespace */ 62691195e687SMark J Musante newspa = spa_add(newname, config, altroot); 6270215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 62711195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 62721195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 62731195e687SMark J Musante 62741195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 62751195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 62761195e687SMark J Musante 62771195e687SMark J Musante if (zio_injection_enabled) 62781195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 62791195e687SMark J Musante 62801195e687SMark J Musante spa_activate(newspa, spa_mode_global); 62811195e687SMark J Musante spa_async_suspend(newspa); 62821195e687SMark J Musante 62836f793812SPavel Zakharov newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; 62846f793812SPavel Zakharov 62851195e687SMark J Musante /* create the new pool from the disks of the original pool */ 62866f793812SPavel Zakharov error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); 62871195e687SMark J Musante if (error) 62881195e687SMark J Musante goto out; 62891195e687SMark J Musante 62901195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 62911195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 62921195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 62931195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 62941195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 62951195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 62961195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 62971195e687SMark J Musante B_TRUE)); 62981195e687SMark J Musante } 62991195e687SMark J Musante 63001195e687SMark J Musante /* set the props */ 63011195e687SMark J Musante if (props != NULL) { 63021195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 63031195e687SMark J Musante error = spa_prop_set(newspa, props); 63041195e687SMark J Musante if (error) 63051195e687SMark J Musante goto out; 63061195e687SMark J Musante } 63071195e687SMark J Musante 63081195e687SMark J Musante /* flush everything */ 63091195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 63101195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 63111195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 63121195e687SMark J Musante 63131195e687SMark J Musante if (zio_injection_enabled) 63141195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 63151195e687SMark J Musante 63161195e687SMark J Musante spa_async_resume(newspa); 63171195e687SMark J Musante 63181195e687SMark J Musante /* finally, update the original pool's config */ 63191195e687SMark J Musante txg = spa_vdev_config_enter(spa); 63201195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 63211195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 63221195e687SMark J Musante if (error != 0) 63231195e687SMark J Musante dmu_tx_abort(tx); 63241195e687SMark J Musante for (c = 0; c < children; c++) { 63251195e687SMark J Musante if (vml[c] != NULL) { 63261195e687SMark J Musante vdev_split(vml[c]); 63271195e687SMark J Musante if (error == 0) 63284445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 63294445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 6330215198a6SJoe Stein 63311195e687SMark J Musante vdev_free(vml[c]); 63321195e687SMark J Musante } 63331195e687SMark J Musante } 6334215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 63351195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 63361195e687SMark J Musante spa->spa_config_splitting = NULL; 63371195e687SMark J Musante nvlist_free(nvl); 63381195e687SMark J Musante if (error == 0) 63391195e687SMark J Musante dmu_tx_commit(tx); 63401195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 63411195e687SMark J Musante 63421195e687SMark J Musante if (zio_injection_enabled) 63431195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 63441195e687SMark J Musante 63451195e687SMark J Musante /* split is complete; log a history record */ 63464445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 63474445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 63481195e687SMark J Musante 63491195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 63501195e687SMark J Musante 63511195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 63521195e687SMark J Musante if (exp) 63531195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 63541195e687SMark J Musante B_FALSE, B_FALSE); 63551195e687SMark J Musante 63561195e687SMark J Musante return (error); 63571195e687SMark J Musante 63581195e687SMark J Musante out: 63591195e687SMark J Musante spa_unload(newspa); 63601195e687SMark J Musante spa_deactivate(newspa); 63611195e687SMark J Musante spa_remove(newspa); 63621195e687SMark J Musante 63631195e687SMark J Musante txg = spa_vdev_config_enter(spa); 636498295d61SMark J Musante 636598295d61SMark J Musante /* re-online all offlined disks */ 636698295d61SMark J Musante for (c = 0; c < children; c++) { 636798295d61SMark J Musante if (vml[c] != NULL) 636898295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 636998295d61SMark J Musante } 637098295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 637198295d61SMark J Musante 63721195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 63731195e687SMark J Musante spa->spa_config_splitting = NULL; 6374d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 63751195e687SMark J Musante 63761195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 63771195e687SMark J Musante return (error); 63781195e687SMark J Musante } 63791195e687SMark J Musante 6380fa9e4066Sahrens /* 63813d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 6382f7170741SWill Andrews * currently spared, so we can detach it. 6383fa9e4066Sahrens */ 6384ea8dc4b6Seschrock static vdev_t * 63853d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 6386fa9e4066Sahrens { 6387ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 6388fa9e4066Sahrens 6389573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 63903d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 6391ea8dc4b6Seschrock if (oldvd != NULL) 6392ea8dc4b6Seschrock return (oldvd); 6393ea8dc4b6Seschrock } 6394fa9e4066Sahrens 63953d7072f8Seschrock /* 6396cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 6397cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 6398cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 6399cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 6400cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 6401cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 64023d7072f8Seschrock */ 6403cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 6404cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 6405cb04b873SMark J Musante 6406cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 6407ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 6408ea8dc4b6Seschrock 64098ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 6410e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 64118ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 6412ea8dc4b6Seschrock return (oldvd); 6413fa9e4066Sahrens } 6414ea8dc4b6Seschrock 64153d7072f8Seschrock /* 64163d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 64173d7072f8Seschrock */ 6418cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 6419cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 6420cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 6421cb04b873SMark J Musante 6422cb04b873SMark J Musante if (last->vdev_unspare) { 6423cb04b873SMark J Musante oldvd = first; 6424cb04b873SMark J Musante newvd = last; 6425cb04b873SMark J Musante } else if (first->vdev_unspare) { 6426cb04b873SMark J Musante oldvd = last; 6427cb04b873SMark J Musante newvd = first; 6428cb04b873SMark J Musante } else { 6429cb04b873SMark J Musante oldvd = NULL; 6430cb04b873SMark J Musante } 64313d7072f8Seschrock 6432cb04b873SMark J Musante if (oldvd != NULL && 64338ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 6434e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 6435cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 64363d7072f8Seschrock return (oldvd); 6437cb04b873SMark J Musante 6438cb04b873SMark J Musante /* 6439cb04b873SMark J Musante * If there are more than two spares attached to a disk, 6440cb04b873SMark J Musante * and those spares are not required, then we want to 6441cb04b873SMark J Musante * attempt to free them up now so that they can be used 6442cb04b873SMark J Musante * by other pools. Once we're back down to a single 6443cb04b873SMark J Musante * disk+spare, we stop removing them. 6444cb04b873SMark J Musante */ 6445cb04b873SMark J Musante if (vd->vdev_children > 2) { 6446cb04b873SMark J Musante newvd = vd->vdev_child[1]; 6447cb04b873SMark J Musante 6448cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 6449cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 6450cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 6451cb04b873SMark J Musante !vdev_dtl_required(newvd)) 6452cb04b873SMark J Musante return (newvd); 64533d7072f8Seschrock } 64543d7072f8Seschrock } 64553d7072f8Seschrock 6456ea8dc4b6Seschrock return (NULL); 6457fa9e4066Sahrens } 6458fa9e4066Sahrens 6459ea8dc4b6Seschrock static void 64603d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 6461fa9e4066Sahrens { 64628ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 64638ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 6464ea8dc4b6Seschrock 64658ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6466ea8dc4b6Seschrock 64673d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 64688ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 64698ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 6470ea8dc4b6Seschrock guid = vd->vdev_guid; 64718ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 64728ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 64738ad4d6ddSJeff Bonwick sguid = 0; 647499653d4eSeschrock /* 647599653d4eSeschrock * If we have just finished replacing a hot spared device, then 647699653d4eSeschrock * we need to detach the parent's first child (the original hot 647799653d4eSeschrock * spare) as well. 647899653d4eSeschrock */ 6479cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 6480cb04b873SMark J Musante ppvd->vdev_children == 2) { 648199653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 64828ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 648399653d4eSeschrock } 6484b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 6485b4952e17SGeorge Wilson 64868ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 64878ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 6488ea8dc4b6Seschrock return; 64898ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 649099653d4eSeschrock return; 64918ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6492fa9e4066Sahrens } 6493fa9e4066Sahrens 64948ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 6495fa9e4066Sahrens } 6496fa9e4066Sahrens 6497c67d9675Seschrock /* 6498b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 6499c67d9675Seschrock */ 6500c67d9675Seschrock int 65016809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 65026809eb4eSEric Schrock boolean_t ispath) 6503c67d9675Seschrock { 6504c5904d13Seschrock vdev_t *vd; 6505208044b8SGeorge Wilson boolean_t sync = B_FALSE; 6506c67d9675Seschrock 6507f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6508f9af39baSGeorge Wilson 6509b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 6510c67d9675Seschrock 65116809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 6512b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 6513c67d9675Seschrock 65140e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 6515b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 65160e34b6a7Sbonwick 65176809eb4eSEric Schrock if (ispath) { 6518208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 6519208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 6520208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 6521208044b8SGeorge Wilson sync = B_TRUE; 6522208044b8SGeorge Wilson } 65236809eb4eSEric Schrock } else { 6524208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 6525208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 6526208044b8SGeorge Wilson sync = B_TRUE; 6527208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 65286809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 6529208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 6530208044b8SGeorge Wilson sync = B_TRUE; 6531208044b8SGeorge Wilson } 65326809eb4eSEric Schrock } 6533c67d9675Seschrock 6534208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 6535c67d9675Seschrock } 6536c67d9675Seschrock 65376809eb4eSEric Schrock int 65386809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 65396809eb4eSEric Schrock { 65406809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 65416809eb4eSEric Schrock } 65426809eb4eSEric Schrock 65436809eb4eSEric Schrock int 65446809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 65456809eb4eSEric Schrock { 65466809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 65476809eb4eSEric Schrock } 65486809eb4eSEric Schrock 6549fa9e4066Sahrens /* 6550fa9e4066Sahrens * ========================================================================== 65513f9d6ad7SLin Ling * SPA Scanning 6552fa9e4066Sahrens * ========================================================================== 6553fa9e4066Sahrens */ 65541702cce7SAlek Pinchuk int 65551702cce7SAlek Pinchuk spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) 65561702cce7SAlek Pinchuk { 65571702cce7SAlek Pinchuk ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 65581702cce7SAlek Pinchuk 65591702cce7SAlek Pinchuk if (dsl_scan_resilvering(spa->spa_dsl_pool)) 65601702cce7SAlek Pinchuk return (SET_ERROR(EBUSY)); 65611702cce7SAlek Pinchuk 65621702cce7SAlek Pinchuk return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); 65631702cce7SAlek Pinchuk } 6564fa9e4066Sahrens 6565ea8dc4b6Seschrock int 65663f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 6567fa9e4066Sahrens { 6568e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 65693f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 6570be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 65713f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 65723f9d6ad7SLin Ling } 6573bb8b5132Sek 65743f9d6ad7SLin Ling int 65753f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 65763f9d6ad7SLin Ling { 65773f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 65783f9d6ad7SLin Ling 65793f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 6580be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 6581fa9e4066Sahrens 6582fa9e4066Sahrens /* 6583088f3894Sahrens * If a resilver was requested, but there is no DTL on a 6584088f3894Sahrens * writeable leaf device, we have nothing to do. 6585fa9e4066Sahrens */ 65863f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 6587088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 6588088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 6589ea8dc4b6Seschrock return (0); 6590ea8dc4b6Seschrock } 6591fa9e4066Sahrens 65923f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 6593fa9e4066Sahrens } 6594fa9e4066Sahrens 6595ea8dc4b6Seschrock /* 6596ea8dc4b6Seschrock * ========================================================================== 6597ea8dc4b6Seschrock * SPA async task processing 6598ea8dc4b6Seschrock * ========================================================================== 6599ea8dc4b6Seschrock */ 6600ea8dc4b6Seschrock 6601ea8dc4b6Seschrock static void 66023d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 6603fa9e4066Sahrens { 660449cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 660598d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 660698d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 660749cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 66081d713200SEric Schrock 66091d713200SEric Schrock /* 66101d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 66111d713200SEric Schrock * vdev_clear() as that will cause us to throw away 66121d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 66131d713200SEric Schrock * device, all of which is a waste. 66141d713200SEric Schrock */ 66151d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 66161d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 66171d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 66181d713200SEric Schrock 6619e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 6620ea8dc4b6Seschrock } 662149cf58c0SBrendan Gregg - Sun Microsystems 6622e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 662349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 6624ea8dc4b6Seschrock } 6625fa9e4066Sahrens 6626e14bb325SJeff Bonwick static void 6627e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 6628e14bb325SJeff Bonwick { 6629e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 663098d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 6631e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 6632e14bb325SJeff Bonwick } 6633e14bb325SJeff Bonwick 6634e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 6635e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 6636e14bb325SJeff Bonwick } 6637e14bb325SJeff Bonwick 6638573ca77eSGeorge Wilson static void 6639573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 6640573ca77eSGeorge Wilson { 6641573ca77eSGeorge Wilson sysevent_id_t eid; 6642573ca77eSGeorge Wilson nvlist_t *attr; 6643573ca77eSGeorge Wilson char *physpath; 6644573ca77eSGeorge Wilson 6645573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 6646573ca77eSGeorge Wilson return; 6647573ca77eSGeorge Wilson 6648573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 6649573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 6650573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 6651573ca77eSGeorge Wilson } 6652573ca77eSGeorge Wilson 6653573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 6654573ca77eSGeorge Wilson return; 6655573ca77eSGeorge Wilson 6656573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 6657573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 6658573ca77eSGeorge Wilson 6659573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6660573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 6661573ca77eSGeorge Wilson 6662573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 6663573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 6664573ca77eSGeorge Wilson 6665573ca77eSGeorge Wilson nvlist_free(attr); 6666573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 6667573ca77eSGeorge Wilson } 6668573ca77eSGeorge Wilson 6669ea8dc4b6Seschrock static void 66703f7978d0SAlan Somers spa_async_thread(void *arg) 6671ea8dc4b6Seschrock { 66723f7978d0SAlan Somers spa_t *spa = (spa_t *)arg; 6673e14bb325SJeff Bonwick int tasks; 6674ea8dc4b6Seschrock 6675ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 6676ea8dc4b6Seschrock 6677ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6678ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 6679ea8dc4b6Seschrock spa->spa_async_tasks = 0; 6680ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6681ea8dc4b6Seschrock 66820373e76bSbonwick /* 66830373e76bSbonwick * See if the config needs to be updated. 66840373e76bSbonwick */ 66850373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 6686b24ab676SJeff Bonwick uint64_t old_space, new_space; 6687573ca77eSGeorge Wilson 66880373e76bSbonwick mutex_enter(&spa_namespace_lock); 6689b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 66900373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 6691b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 66920373e76bSbonwick mutex_exit(&spa_namespace_lock); 6693573ca77eSGeorge Wilson 6694573ca77eSGeorge Wilson /* 6695573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 6696573ca77eSGeorge Wilson * then log an internal history event. 6697573ca77eSGeorge Wilson */ 6698b24ab676SJeff Bonwick if (new_space != old_space) { 66994445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 6700c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 6701b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 6702573ca77eSGeorge Wilson } 67030373e76bSbonwick } 67040373e76bSbonwick 6705ea8dc4b6Seschrock /* 67063d7072f8Seschrock * See if any devices need to be marked REMOVED. 6707ea8dc4b6Seschrock */ 6708e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 67098f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 67103d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 6711e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 671249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 6713e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 671449cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 6715e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 6716e14bb325SJeff Bonwick } 6717e14bb325SJeff Bonwick 6718573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 6719573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6720573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 6721573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 6722573ca77eSGeorge Wilson } 6723573ca77eSGeorge Wilson 6724e14bb325SJeff Bonwick /* 6725e14bb325SJeff Bonwick * See if any devices need to be probed. 6726e14bb325SJeff Bonwick */ 6727e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 67288f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 6729e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 6730e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 67313d7072f8Seschrock } 6732ea8dc4b6Seschrock 6733ea8dc4b6Seschrock /* 6734ea8dc4b6Seschrock * If any devices are done replacing, detach them. 6735ea8dc4b6Seschrock */ 67363d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 67373d7072f8Seschrock spa_vdev_resilver_done(spa); 6738fa9e4066Sahrens 6739ea8dc4b6Seschrock /* 6740ea8dc4b6Seschrock * Kick off a resilver. 6741ea8dc4b6Seschrock */ 6742088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 67433f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 6744ea8dc4b6Seschrock 6745ea8dc4b6Seschrock /* 6746ea8dc4b6Seschrock * Let the world know that we're done. 6747ea8dc4b6Seschrock */ 6748ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6749ea8dc4b6Seschrock spa->spa_async_thread = NULL; 6750ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 6751ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6752ea8dc4b6Seschrock thread_exit(); 6753ea8dc4b6Seschrock } 6754ea8dc4b6Seschrock 6755ea8dc4b6Seschrock void 6756ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 6757ea8dc4b6Seschrock { 6758ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6759ea8dc4b6Seschrock spa->spa_async_suspended++; 6760667ec66fSSerapheim Dimitropoulos while (spa->spa_async_thread != NULL) 6761ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 6762ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 67635cabbc6bSPrashanth Sreenivasa 67645cabbc6bSPrashanth Sreenivasa spa_vdev_remove_suspend(spa); 6765667ec66fSSerapheim Dimitropoulos 6766667ec66fSSerapheim Dimitropoulos zthr_t *condense_thread = spa->spa_condense_zthr; 6767667ec66fSSerapheim Dimitropoulos if (condense_thread != NULL && zthr_isrunning(condense_thread)) 6768667ec66fSSerapheim Dimitropoulos VERIFY0(zthr_cancel(condense_thread)); 6769*86714001SSerapheim Dimitropoulos 6770*86714001SSerapheim Dimitropoulos zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; 6771*86714001SSerapheim Dimitropoulos if (discard_thread != NULL && zthr_isrunning(discard_thread)) 6772*86714001SSerapheim Dimitropoulos VERIFY0(zthr_cancel(discard_thread)); 6773ea8dc4b6Seschrock } 6774ea8dc4b6Seschrock 6775ea8dc4b6Seschrock void 6776ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 6777ea8dc4b6Seschrock { 6778ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6779ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 6780ea8dc4b6Seschrock spa->spa_async_suspended--; 6781ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 67825cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 6783667ec66fSSerapheim Dimitropoulos 6784667ec66fSSerapheim Dimitropoulos zthr_t *condense_thread = spa->spa_condense_zthr; 6785667ec66fSSerapheim Dimitropoulos if (condense_thread != NULL && !zthr_isrunning(condense_thread)) 6786667ec66fSSerapheim Dimitropoulos zthr_resume(condense_thread); 6787*86714001SSerapheim Dimitropoulos 6788*86714001SSerapheim Dimitropoulos zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; 6789*86714001SSerapheim Dimitropoulos if (discard_thread != NULL && !zthr_isrunning(discard_thread)) 6790*86714001SSerapheim Dimitropoulos zthr_resume(discard_thread); 6791ea8dc4b6Seschrock } 6792ea8dc4b6Seschrock 67933cb69f73SWill Andrews static boolean_t 67943cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 67953cb69f73SWill Andrews { 67963cb69f73SWill Andrews uint_t non_config_tasks; 67973cb69f73SWill Andrews uint_t config_task; 67983cb69f73SWill Andrews boolean_t config_task_suspended; 67993cb69f73SWill Andrews 68003cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 68013cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 68023cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 68033cb69f73SWill Andrews config_task_suspended = B_FALSE; 68043cb69f73SWill Andrews } else { 68053cb69f73SWill Andrews config_task_suspended = 68063cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 68073cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 68083cb69f73SWill Andrews } 68093cb69f73SWill Andrews 68103cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 68113cb69f73SWill Andrews } 68123cb69f73SWill Andrews 6813ea8dc4b6Seschrock static void 6814ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 6815ea8dc4b6Seschrock { 6816ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 68173cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 68183cb69f73SWill Andrews !spa->spa_async_suspended && 68190373e76bSbonwick spa->spa_async_thread == NULL && 68203cb69f73SWill Andrews rootdir != NULL) 6821ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 6822ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 6823ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6824ea8dc4b6Seschrock } 6825ea8dc4b6Seschrock 6826ea8dc4b6Seschrock void 6827ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 6828ea8dc4b6Seschrock { 68293f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 6830ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6831ea8dc4b6Seschrock spa->spa_async_tasks |= task; 6832ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6833fa9e4066Sahrens } 6834fa9e4066Sahrens 6835fa9e4066Sahrens /* 6836fa9e4066Sahrens * ========================================================================== 6837fa9e4066Sahrens * SPA syncing routines 6838fa9e4066Sahrens * ========================================================================== 6839fa9e4066Sahrens */ 6840fa9e4066Sahrens 6841cde58dbcSMatthew Ahrens static int 6842cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6843cde58dbcSMatthew Ahrens { 6844cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 6845cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 6846cde58dbcSMatthew Ahrens return (0); 6847b24ab676SJeff Bonwick } 6848b24ab676SJeff Bonwick 6849cde58dbcSMatthew Ahrens static int 6850cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6851b24ab676SJeff Bonwick { 6852b24ab676SJeff Bonwick zio_t *zio = arg; 6853b24ab676SJeff Bonwick 6854b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 6855b24ab676SJeff Bonwick zio->io_flags)); 6856cde58dbcSMatthew Ahrens return (0); 6857fa9e4066Sahrens } 6858fa9e4066Sahrens 685969962b56SMatthew Ahrens /* 686069962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 686169962b56SMatthew Ahrens * amount of time spent syncing frees. 686269962b56SMatthew Ahrens */ 686369962b56SMatthew Ahrens static void 686469962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 686569962b56SMatthew Ahrens { 686669962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 686769962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 686869962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 686969962b56SMatthew Ahrens } 687069962b56SMatthew Ahrens 687169962b56SMatthew Ahrens /* 687269962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 687369962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 687469962b56SMatthew Ahrens */ 687569962b56SMatthew Ahrens static void 687669962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 687769962b56SMatthew Ahrens { 687869962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 687969962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 688069962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 688169962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 688269962b56SMatthew Ahrens } 688369962b56SMatthew Ahrens 688469962b56SMatthew Ahrens 6885fa9e4066Sahrens static void 688699653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6887fa9e4066Sahrens { 6888fa9e4066Sahrens char *packed = NULL; 6889f7991ba4STim Haley size_t bufsize; 6890fa9e4066Sahrens size_t nvsize = 0; 6891fa9e4066Sahrens dmu_buf_t *db; 6892fa9e4066Sahrens 689399653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6894fa9e4066Sahrens 6895f7991ba4STim Haley /* 6896f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 689743466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6898f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6899f7991ba4STim Haley */ 6900ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6901f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6902fa9e4066Sahrens 690399653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6904ea8dc4b6Seschrock KM_SLEEP) == 0); 6905f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6906fa9e4066Sahrens 6907f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6908fa9e4066Sahrens 6909f7991ba4STim Haley kmem_free(packed, bufsize); 6910fa9e4066Sahrens 691199653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6912fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6913fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6914ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6915fa9e4066Sahrens } 6916fa9e4066Sahrens 691799653d4eSeschrock static void 6918fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6919fa94a07fSbrendan const char *config, const char *entry) 692099653d4eSeschrock { 692199653d4eSeschrock nvlist_t *nvroot; 6922fa94a07fSbrendan nvlist_t **list; 692399653d4eSeschrock int i; 692499653d4eSeschrock 6925fa94a07fSbrendan if (!sav->sav_sync) 692699653d4eSeschrock return; 692799653d4eSeschrock 692899653d4eSeschrock /* 6929fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6930fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 69313d7072f8Seschrock * valid and the vdevs are labeled appropriately. 693299653d4eSeschrock */ 6933fa94a07fSbrendan if (sav->sav_object == 0) { 6934fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6935fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6936fa94a07fSbrendan sizeof (uint64_t), tx); 693799653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6938fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6939fa94a07fSbrendan &sav->sav_object, tx) == 0); 694099653d4eSeschrock } 694199653d4eSeschrock 694299653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6943fa94a07fSbrendan if (sav->sav_count == 0) { 6944fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 694599653d4eSeschrock } else { 6946fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6947fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6948fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 69493f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6950fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6951fa94a07fSbrendan sav->sav_count) == 0); 6952fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6953fa94a07fSbrendan nvlist_free(list[i]); 6954fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 695599653d4eSeschrock } 695699653d4eSeschrock 6957fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 695806eeb2adSek nvlist_free(nvroot); 695999653d4eSeschrock 6960fa94a07fSbrendan sav->sav_sync = B_FALSE; 696199653d4eSeschrock } 696299653d4eSeschrock 6963215198a6SJoe Stein /* 6964215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6965215198a6SJoe Stein * The all-vdev ZAP must be empty. 6966215198a6SJoe Stein */ 6967215198a6SJoe Stein static void 6968215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6969215198a6SJoe Stein { 6970215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6971215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6972215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6973215198a6SJoe Stein vd->vdev_top_zap, tx)); 6974215198a6SJoe Stein } 6975215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6976215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6977215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6978215198a6SJoe Stein } 6979215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6980215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6981215198a6SJoe Stein } 6982215198a6SJoe Stein } 6983215198a6SJoe Stein 698499653d4eSeschrock static void 698599653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 698699653d4eSeschrock { 698799653d4eSeschrock nvlist_t *config; 698899653d4eSeschrock 6989215198a6SJoe Stein /* 6990215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6991215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6992215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6993215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6994215198a6SJoe Stein */ 6995215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6996215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 699799653d4eSeschrock return; 699899653d4eSeschrock 6999e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 7000e14bb325SJeff Bonwick 7001215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 7002555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 7003215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 7004215198a6SJoe Stein 7005215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 7006215198a6SJoe Stein /* Make and build the new AVZ */ 7007215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 7008215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 7009215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 7010215198a6SJoe Stein 7011215198a6SJoe Stein /* Diff old AVZ with new one */ 7012215198a6SJoe Stein zap_cursor_t zc; 7013215198a6SJoe Stein zap_attribute_t za; 7014215198a6SJoe Stein 7015215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 7016215198a6SJoe Stein spa->spa_all_vdev_zaps); 7017215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 7018215198a6SJoe Stein zap_cursor_advance(&zc)) { 7019215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 7020215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 7021215198a6SJoe Stein vdzap) == ENOENT) { 7022215198a6SJoe Stein /* 7023215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 7024215198a6SJoe Stein * destroy it 7025215198a6SJoe Stein */ 7026215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 7027215198a6SJoe Stein tx)); 7028215198a6SJoe Stein } 7029215198a6SJoe Stein } 7030215198a6SJoe Stein 7031215198a6SJoe Stein zap_cursor_fini(&zc); 7032215198a6SJoe Stein 7033215198a6SJoe Stein /* Destroy the old AVZ */ 7034215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 7035215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 7036215198a6SJoe Stein 7037215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 7038215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 7039215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 7040215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 7041215198a6SJoe Stein 7042215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 7043215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 7044215198a6SJoe Stein zap_cursor_t zc; 7045215198a6SJoe Stein zap_attribute_t za; 7046215198a6SJoe Stein 7047215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 7048215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 7049215198a6SJoe Stein spa->spa_all_vdev_zaps); 7050215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 7051215198a6SJoe Stein zap_cursor_advance(&zc)) { 7052215198a6SJoe Stein uint64_t zap = za.za_first_integer; 7053215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 7054215198a6SJoe Stein } 7055215198a6SJoe Stein 7056215198a6SJoe Stein zap_cursor_fini(&zc); 7057215198a6SJoe Stein 7058215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 7059215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 7060215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 7061215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 7062215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 7063215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 7064215198a6SJoe Stein } 7065215198a6SJoe Stein 7066215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 7067215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 7068215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 7069215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 7070215198a6SJoe Stein } 7071215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 7072215198a6SJoe Stein 7073215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 7074215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 7075215198a6SJoe Stein 7076e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 7077e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 7078e14bb325SJeff Bonwick 707925345e46SGeorge Wilson /* 708025345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 708125345e46SGeorge Wilson * the config object gets updated with the correct version. 708225345e46SGeorge Wilson */ 708325345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 708425345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 708525345e46SGeorge Wilson spa->spa_uberblock.ub_version); 708625345e46SGeorge Wilson 7087e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 708899653d4eSeschrock 7089aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 709099653d4eSeschrock spa->spa_config_syncing = config; 709199653d4eSeschrock 709299653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 709399653d4eSeschrock } 709499653d4eSeschrock 7095ad135b5dSChristopher Siden static void 70963b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 7097ad135b5dSChristopher Siden { 70983b2aab18SMatthew Ahrens uint64_t *versionp = arg; 70993b2aab18SMatthew Ahrens uint64_t version = *versionp; 71003b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 7101ad135b5dSChristopher Siden 7102ad135b5dSChristopher Siden /* 7103ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 7104ad135b5dSChristopher Siden */ 7105ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 7106ad135b5dSChristopher Siden 710762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 7108ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 7109ad135b5dSChristopher Siden 7110ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 7111ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 71124445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 7113ad135b5dSChristopher Siden } 7114ad135b5dSChristopher Siden 7115990b4856Slling /* 7116990b4856Slling * Set zpool properties. 7117990b4856Slling */ 7118b1b8ab34Slling static void 71193b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 7120b1b8ab34Slling { 71213b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 71223b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 7123b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 7124ad135b5dSChristopher Siden nvpair_t *elem = NULL; 7125b1b8ab34Slling 7126e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 7127e14bb325SJeff Bonwick 7128990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 7129ad135b5dSChristopher Siden uint64_t intval; 7130ad135b5dSChristopher Siden char *strval, *fname; 7131ad135b5dSChristopher Siden zpool_prop_t prop; 7132ad135b5dSChristopher Siden const char *propname; 7133ad135b5dSChristopher Siden zprop_type_t proptype; 71342acef22dSMatthew Ahrens spa_feature_t fid; 7135ad135b5dSChristopher Siden 7136990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 71374ae5f5f0SAlan Somers case ZPOOL_PROP_INVAL: 7138ad135b5dSChristopher Siden /* 7139ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 7140ad135b5dSChristopher Siden */ 7141ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 7142ad135b5dSChristopher Siden 7143ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 71442acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 7145ad135b5dSChristopher Siden 71462acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 71474445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 71484445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 7149ad135b5dSChristopher Siden break; 7150ad135b5dSChristopher Siden 7151990b4856Slling case ZPOOL_PROP_VERSION: 71520713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 7153990b4856Slling /* 7154ad135b5dSChristopher Siden * The version is synced seperatly before other 7155ad135b5dSChristopher Siden * properties and should be correct by now. 7156990b4856Slling */ 7157ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 7158ecd6cf80Smarks break; 7159990b4856Slling 7160990b4856Slling case ZPOOL_PROP_ALTROOT: 7161990b4856Slling /* 7162990b4856Slling * 'altroot' is a non-persistent property. It should 7163990b4856Slling * have been set temporarily at creation or import time. 7164990b4856Slling */ 7165990b4856Slling ASSERT(spa->spa_root != NULL); 7166b1b8ab34Slling break; 71673d7072f8Seschrock 7168f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 71692f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 7170990b4856Slling /* 7171f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 7172f9af39baSGeorge Wilson * properties. 7173990b4856Slling */ 71743d7072f8Seschrock break; 71758704186eSDan McDonald case ZPOOL_PROP_COMMENT: 71760713e232SGeorge Wilson strval = fnvpair_value_string(elem); 71778704186eSDan McDonald if (spa->spa_comment != NULL) 71788704186eSDan McDonald spa_strfree(spa->spa_comment); 71798704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 71808704186eSDan McDonald /* 71818704186eSDan McDonald * We need to dirty the configuration on all the vdevs 71828704186eSDan McDonald * so that their labels get updated. It's unnecessary 71838704186eSDan McDonald * to do this for pool creation since the vdev's 71848704186eSDan McDonald * configuratoin has already been dirtied. 71858704186eSDan McDonald */ 71868704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 71878704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 71884445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 71894445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 71908704186eSDan McDonald break; 7191990b4856Slling default: 7192990b4856Slling /* 7193990b4856Slling * Set pool property values in the poolprops mos object. 7194990b4856Slling */ 7195990b4856Slling if (spa->spa_pool_props_object == 0) { 7196ad135b5dSChristopher Siden spa->spa_pool_props_object = 7197ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 7198990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 7199ad135b5dSChristopher Siden tx); 7200990b4856Slling } 7201990b4856Slling 7202990b4856Slling /* normalize the property name */ 7203990b4856Slling propname = zpool_prop_to_name(prop); 7204990b4856Slling proptype = zpool_prop_get_type(prop); 7205990b4856Slling 7206990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 7207990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 72080713e232SGeorge Wilson strval = fnvpair_value_string(elem); 72090713e232SGeorge Wilson VERIFY0(zap_update(mos, 7210990b4856Slling spa->spa_pool_props_object, propname, 72110713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 72124445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 72134445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 7214990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 72150713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 7216990b4856Slling 7217990b4856Slling if (proptype == PROP_TYPE_INDEX) { 7218990b4856Slling const char *unused; 72190713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 72200713e232SGeorge Wilson prop, intval, &unused)); 7221990b4856Slling } 72220713e232SGeorge Wilson VERIFY0(zap_update(mos, 7223990b4856Slling spa->spa_pool_props_object, propname, 72240713e232SGeorge Wilson 8, 1, &intval, tx)); 72254445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 72264445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 7227990b4856Slling } else { 7228990b4856Slling ASSERT(0); /* not allowed */ 7229990b4856Slling } 7230990b4856Slling 72310a4e9518Sgw switch (prop) { 72320a4e9518Sgw case ZPOOL_PROP_DELEGATION: 7233990b4856Slling spa->spa_delegation = intval; 72340a4e9518Sgw break; 72350a4e9518Sgw case ZPOOL_PROP_BOOTFS: 7236990b4856Slling spa->spa_bootfs = intval; 72370a4e9518Sgw break; 72380a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 72390a4e9518Sgw spa->spa_failmode = intval; 72400a4e9518Sgw break; 7241573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 7242573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 7243b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 7244b98131cfSEric Taylor spa_async_request(spa, 7245b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 7246573ca77eSGeorge Wilson break; 7247b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 7248b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 7249b24ab676SJeff Bonwick break; 72500a4e9518Sgw default: 72510a4e9518Sgw break; 72520a4e9518Sgw } 7253990b4856Slling } 7254990b4856Slling 7255b1b8ab34Slling } 7256e14bb325SJeff Bonwick 7257e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 7258b1b8ab34Slling } 7259b1b8ab34Slling 7260cde58dbcSMatthew Ahrens /* 7261cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 7262cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 7263cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 7264cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 7265cde58dbcSMatthew Ahrens * tasks. 7266cde58dbcSMatthew Ahrens */ 7267cde58dbcSMatthew Ahrens static void 7268cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 7269cde58dbcSMatthew Ahrens { 7270cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 7271cde58dbcSMatthew Ahrens 7272cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 7273cde58dbcSMatthew Ahrens 72743b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 72753b2aab18SMatthew Ahrens 7276cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 7277cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 7278cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 7279cde58dbcSMatthew Ahrens 7280cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 7281cde58dbcSMatthew Ahrens spa->spa_minref += 3; 7282cde58dbcSMatthew Ahrens } 7283cde58dbcSMatthew Ahrens 7284cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 7285cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 7286cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 7287cde58dbcSMatthew Ahrens } 7288cde58dbcSMatthew Ahrens 7289cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 7290cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 7291cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 7292cde58dbcSMatthew Ahrens 7293cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 7294cde58dbcSMatthew Ahrens spa->spa_minref += 3; 7295cde58dbcSMatthew Ahrens } 7296ad135b5dSChristopher Siden 7297ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 7298ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 7299ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 7300ad135b5dSChristopher Siden } 7301b8289d24SDaniil Lunev 7302b8289d24SDaniil Lunev /* 7303b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 7304b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 7305b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 7306b8289d24SDaniil Lunev * this feature active 7307b8289d24SDaniil Lunev */ 7308b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 7309b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 7310b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 7311b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 7312b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 7313b8289d24SDaniil Lunev 7314b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 7315b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 7316b8289d24SDaniil Lunev } 731745818ee1SMatthew Ahrens 731845818ee1SMatthew Ahrens /* 731945818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 732045818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 732145818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 732245818ee1SMatthew Ahrens */ 732345818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 732445818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 732545818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 732645818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 732745818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 732845818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 732945818ee1SMatthew Ahrens } 733045818ee1SMatthew Ahrens 73313b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 7332cde58dbcSMatthew Ahrens } 7333cde58dbcSMatthew Ahrens 73345cabbc6bSPrashanth Sreenivasa static void 73355cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vdev_t *vd) 73365cabbc6bSPrashanth Sreenivasa { 73375cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 73385cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 73395cabbc6bSPrashanth Sreenivasa 73405cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 73415cabbc6bSPrashanth Sreenivasa ASSERT(vim != NULL); 73425cabbc6bSPrashanth Sreenivasa ASSERT(vib != NULL); 73435cabbc6bSPrashanth Sreenivasa } 73445cabbc6bSPrashanth Sreenivasa 73455cabbc6bSPrashanth Sreenivasa if (vdev_obsolete_sm_object(vd) != 0) { 73465cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_sm != NULL); 73475cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 73485cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 73495cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); 73505cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); 73515cabbc6bSPrashanth Sreenivasa 73525cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_obsolete_sm_object(vd), ==, 73535cabbc6bSPrashanth Sreenivasa space_map_object(vd->vdev_obsolete_sm)); 73545cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, 73555cabbc6bSPrashanth Sreenivasa space_map_allocated(vd->vdev_obsolete_sm)); 73565cabbc6bSPrashanth Sreenivasa } 73575cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_segments != NULL); 73585cabbc6bSPrashanth Sreenivasa 73595cabbc6bSPrashanth Sreenivasa /* 73605cabbc6bSPrashanth Sreenivasa * Since frees / remaps to an indirect vdev can only 73615cabbc6bSPrashanth Sreenivasa * happen in syncing context, the obsolete segments 73625cabbc6bSPrashanth Sreenivasa * tree must be empty when we start syncing. 73635cabbc6bSPrashanth Sreenivasa */ 73645cabbc6bSPrashanth Sreenivasa ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); 73655cabbc6bSPrashanth Sreenivasa } 73665cabbc6bSPrashanth Sreenivasa 7367fa9e4066Sahrens /* 7368fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 7369fa9e4066Sahrens * part of the process, so we iterate until it converges. 7370fa9e4066Sahrens */ 7371fa9e4066Sahrens void 7372fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 7373fa9e4066Sahrens { 7374fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 7375fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 7376b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 73770373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 7378fa9e4066Sahrens vdev_t *vd; 7379fa9e4066Sahrens dmu_tx_t *tx; 7380e14bb325SJeff Bonwick int error; 73810f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 73820f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 7383fa9e4066Sahrens 7384f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 7385f9af39baSGeorge Wilson 73865cabbc6bSPrashanth Sreenivasa /* 73875cabbc6bSPrashanth Sreenivasa * Wait for i/os issued in open context that need to complete 73885cabbc6bSPrashanth Sreenivasa * before this txg syncs. 73895cabbc6bSPrashanth Sreenivasa */ 73905cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[txg & TXG_MASK])); 73915cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, 0); 73925cabbc6bSPrashanth Sreenivasa 7393fa9e4066Sahrens /* 7394fa9e4066Sahrens * Lock out configuration changes. 7395fa9e4066Sahrens */ 7396e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7397fa9e4066Sahrens 7398fa9e4066Sahrens spa->spa_syncing_txg = txg; 7399fa9e4066Sahrens spa->spa_sync_pass = 0; 7400fa9e4066Sahrens 74010f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 74020f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 74030f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 74040f7643c7SGeorge Wilson 7405e14bb325SJeff Bonwick /* 7406e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 7407e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 7408e14bb325SJeff Bonwick */ 7409e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 74108ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 74118ad4d6ddSJeff Bonwick /* 74128ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 74138ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 74148ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 74158ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 74168ad4d6ddSJeff Bonwick * into the root vdev tree. 74178ad4d6ddSJeff Bonwick */ 74188ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 74198ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 74208ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 74218ad4d6ddSJeff Bonwick vdev_state_clean(vd); 74228ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 74238ad4d6ddSJeff Bonwick } 74248ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 74258ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 7426e14bb325SJeff Bonwick } 7427e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 7428e14bb325SJeff Bonwick 742999653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 743099653d4eSeschrock 7431283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 7432283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 7433283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 7434283b8460SGeorge.Wilson 743599653d4eSeschrock /* 7436e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 743799653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 743899653d4eSeschrock */ 7439e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 7440e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 744199653d4eSeschrock int i; 744299653d4eSeschrock 744399653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 744499653d4eSeschrock vd = rvd->vdev_child[i]; 744599653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 744699653d4eSeschrock break; 744799653d4eSeschrock } 744899653d4eSeschrock if (i == rvd->vdev_children) { 744999653d4eSeschrock spa->spa_deflate = TRUE; 745099653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 745199653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 745299653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 745399653d4eSeschrock } 745499653d4eSeschrock } 745599653d4eSeschrock 74560f7643c7SGeorge Wilson /* 74570f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 74580f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 74590f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 74600f7643c7SGeorge Wilson * out this txg. 74610f7643c7SGeorge Wilson */ 74620f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 74630f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 74640f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 74650f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 74660f7643c7SGeorge Wilson 74670f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 74680f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 74690f7643c7SGeorge Wilson continue; 74700f7643c7SGeorge Wilson 74710f7643c7SGeorge Wilson /* 74720f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 74730f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 74740f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 74750f7643c7SGeorge Wilson */ 74760f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 74770f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 74780f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 74790f7643c7SGeorge Wilson } 74800f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 74810f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 74820f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 74830f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 74840f7643c7SGeorge Wilson 74850f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 74860f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 74870f7643c7SGeorge Wilson 74885cabbc6bSPrashanth Sreenivasa for (int c = 0; c < rvd->vdev_children; c++) { 74895cabbc6bSPrashanth Sreenivasa vdev_t *vd = rvd->vdev_child[c]; 74905cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vd); 74915cabbc6bSPrashanth Sreenivasa 74925cabbc6bSPrashanth Sreenivasa if (vdev_indirect_should_condense(vd)) { 74935cabbc6bSPrashanth Sreenivasa spa_condense_indirect_start_sync(vd, tx); 74945cabbc6bSPrashanth Sreenivasa break; 74955cabbc6bSPrashanth Sreenivasa } 74965cabbc6bSPrashanth Sreenivasa } 74975cabbc6bSPrashanth Sreenivasa 7498fa9e4066Sahrens /* 7499fa9e4066Sahrens * Iterate to convergence. 7500fa9e4066Sahrens */ 7501fa9e4066Sahrens do { 7502b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 7503fa9e4066Sahrens 7504fa9e4066Sahrens spa_sync_config_object(spa, tx); 7505fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 7506fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 7507fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 7508fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 7509ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 7510fa9e4066Sahrens dsl_pool_sync(dp, txg); 7511fa9e4066Sahrens 751201f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 751369962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 7514b24ab676SJeff Bonwick } else { 7515231aab85SMatthew Ahrens /* 7516231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 7517231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 7518231aab85SMatthew Ahrens */ 7519231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 7520cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 752169962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 7522fa9e4066Sahrens } 7523fa9e4066Sahrens 7524b24ab676SJeff Bonwick ddt_sync(spa, txg); 75253f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 7526afee20e4SGeorge Wilson 75275cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) 75285cabbc6bSPrashanth Sreenivasa svr_sync(spa, tx); 75295cabbc6bSPrashanth Sreenivasa 75305cabbc6bSPrashanth Sreenivasa while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 75315cabbc6bSPrashanth Sreenivasa != NULL) 7532b24ab676SJeff Bonwick vdev_sync(vd, txg); 7533b24ab676SJeff Bonwick 7534231aab85SMatthew Ahrens if (pass == 1) { 7535cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 7536231aab85SMatthew Ahrens ASSERT3U(txg, >=, 7537231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 7538231aab85SMatthew Ahrens /* 7539231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 7540231aab85SMatthew Ahrens * because we could have marked the MOS dirty 7541231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 7542231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 7543231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 7544231aab85SMatthew Ahrens * it is updated if we have synced out dirty 7545231aab85SMatthew Ahrens * data (though in this case the MOS will most 7546231aab85SMatthew Ahrens * likely also be dirty due to second order 7547231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 7548231aab85SMatthew Ahrens */ 7549231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 7550231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 7551231aab85SMatthew Ahrens /* 7552231aab85SMatthew Ahrens * Nothing changed on the first pass, 7553231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 7554231aab85SMatthew Ahrens * syncing deferred frees, so that we 7555231aab85SMatthew Ahrens * can keep this TXG as a no-op. 7556231aab85SMatthew Ahrens */ 7557231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 7558231aab85SMatthew Ahrens txg)); 7559231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 7560231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 7561*86714001SSerapheim Dimitropoulos ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, 7562*86714001SSerapheim Dimitropoulos txg)); 7563231aab85SMatthew Ahrens break; 7564231aab85SMatthew Ahrens } 7565231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 7566231aab85SMatthew Ahrens } 7567fa9e4066Sahrens 7568cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 7569fa9e4066Sahrens 7570215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 7571215198a6SJoe Stein /* 7572215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 7573215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 7574215198a6SJoe Stein * called if the config is dirty; otherwise there may be 7575215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 7576215198a6SJoe Stein * spa_sync_config_object. 7577215198a6SJoe Stein */ 7578215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 7579215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 7580215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 7581215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 7582215198a6SJoe Stein all_vdev_zap_entry_count); 7583215198a6SJoe Stein } 7584215198a6SJoe Stein 75855cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 75865cabbc6bSPrashanth Sreenivasa ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); 75875cabbc6bSPrashanth Sreenivasa } 75885cabbc6bSPrashanth Sreenivasa 7589fa9e4066Sahrens /* 7590fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 7591fa9e4066Sahrens * to commit the transaction group. 75920373e76bSbonwick * 759317f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 759417f17c2dSbonwick * random top-level vdevs that are known to be visible in the 7595e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 7596e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 75970373e76bSbonwick */ 7598e14bb325SJeff Bonwick for (;;) { 7599e14bb325SJeff Bonwick /* 7600e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 7601e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 7602e14bb325SJeff Bonwick */ 7603e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 7604e14bb325SJeff Bonwick 7605e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 7606*86714001SSerapheim Dimitropoulos vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; 7607e14bb325SJeff Bonwick int svdcount = 0; 7608e14bb325SJeff Bonwick int children = rvd->vdev_children; 7609e14bb325SJeff Bonwick int c0 = spa_get_random(children); 7610e14bb325SJeff Bonwick 7611573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 7612e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 7613*86714001SSerapheim Dimitropoulos 7614*86714001SSerapheim Dimitropoulos /* Stop when revisiting the first vdev */ 7615*86714001SSerapheim Dimitropoulos if (c > 0 && svd[0] == vd) 7616*86714001SSerapheim Dimitropoulos break; 7617*86714001SSerapheim Dimitropoulos 76185cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 || vd->vdev_islog || 76195cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vd)) 7620e14bb325SJeff Bonwick continue; 7621*86714001SSerapheim Dimitropoulos 7622e14bb325SJeff Bonwick svd[svdcount++] = vd; 76236f793812SPavel Zakharov if (svdcount == SPA_SYNC_MIN_VDEVS) 7624e14bb325SJeff Bonwick break; 7625e14bb325SJeff Bonwick } 7626eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 7627e14bb325SJeff Bonwick } else { 7628e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 7629eb5bb584SWill Andrews rvd->vdev_children, txg); 76300373e76bSbonwick } 7631e14bb325SJeff Bonwick 7632dfbb9432SGeorge Wilson if (error == 0) 7633dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 7634dfbb9432SGeorge Wilson 7635e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 7636e14bb325SJeff Bonwick 7637e14bb325SJeff Bonwick if (error == 0) 7638e14bb325SJeff Bonwick break; 7639e14bb325SJeff Bonwick zio_suspend(spa, NULL); 7640e14bb325SJeff Bonwick zio_resume_wait(spa); 76410373e76bSbonwick } 764299653d4eSeschrock dmu_tx_commit(tx); 764399653d4eSeschrock 7644283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 7645283b8460SGeorge.Wilson 76460373e76bSbonwick /* 76470373e76bSbonwick * Clear the dirty config list. 7648fa9e4066Sahrens */ 7649e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 76500373e76bSbonwick vdev_config_clean(vd); 76510373e76bSbonwick 76520373e76bSbonwick /* 76530373e76bSbonwick * Now that the new config has synced transactionally, 76540373e76bSbonwick * let it become visible to the config cache. 76550373e76bSbonwick */ 76560373e76bSbonwick if (spa->spa_config_syncing != NULL) { 76570373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 76580373e76bSbonwick spa->spa_config_txg = txg; 76590373e76bSbonwick spa->spa_config_syncing = NULL; 76600373e76bSbonwick } 7661fa9e4066Sahrens 7662b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 7663fa9e4066Sahrens 76640f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 76650f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 76660f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 76670f7643c7SGeorge Wilson 7668fa9e4066Sahrens /* 7669fa9e4066Sahrens * Update usable space statistics. 7670fa9e4066Sahrens */ 7671fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 7672fa9e4066Sahrens vdev_sync_done(vd, txg); 7673fa9e4066Sahrens 7674485bbbf5SGeorge Wilson spa_update_dspace(spa); 7675485bbbf5SGeorge Wilson 7676fa9e4066Sahrens /* 7677fa9e4066Sahrens * It had better be the case that we didn't dirty anything 767899653d4eSeschrock * since vdev_config_sync(). 7679fa9e4066Sahrens */ 7680fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 7681fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 7682fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 7683b24ab676SJeff Bonwick 7684*86714001SSerapheim Dimitropoulos while (zfs_pause_spa_sync) 7685*86714001SSerapheim Dimitropoulos delay(1); 7686*86714001SSerapheim Dimitropoulos 7687b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 7688fa9e4066Sahrens 768943297f97SGeorge Wilson /* 769043297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 769143297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 769243297f97SGeorge Wilson * will be guaranteed that all the processing associated with 769343297f97SGeorge Wilson * that txg has been completed. 769443297f97SGeorge Wilson */ 769543297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 7696e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 7697ea8dc4b6Seschrock 7698468c413aSTim Haley spa_handle_ignored_writes(spa); 7699468c413aSTim Haley 7700ea8dc4b6Seschrock /* 7701ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 7702ea8dc4b6Seschrock */ 7703ea8dc4b6Seschrock spa_async_dispatch(spa); 7704fa9e4066Sahrens } 7705fa9e4066Sahrens 7706fa9e4066Sahrens /* 7707fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 7708fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 7709fa9e4066Sahrens * sync. 7710fa9e4066Sahrens */ 7711fa9e4066Sahrens void 7712fa9e4066Sahrens spa_sync_allpools(void) 7713fa9e4066Sahrens { 7714fa9e4066Sahrens spa_t *spa = NULL; 7715fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7716fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 7717f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 7718f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 7719fa9e4066Sahrens continue; 7720fa9e4066Sahrens spa_open_ref(spa, FTAG); 7721fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7722fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 7723fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7724fa9e4066Sahrens spa_close(spa, FTAG); 7725fa9e4066Sahrens } 7726fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7727fa9e4066Sahrens } 7728fa9e4066Sahrens 7729fa9e4066Sahrens /* 7730fa9e4066Sahrens * ========================================================================== 7731fa9e4066Sahrens * Miscellaneous routines 7732fa9e4066Sahrens * ========================================================================== 7733fa9e4066Sahrens */ 7734fa9e4066Sahrens 7735fa9e4066Sahrens /* 7736fa9e4066Sahrens * Remove all pools in the system. 7737fa9e4066Sahrens */ 7738fa9e4066Sahrens void 7739fa9e4066Sahrens spa_evict_all(void) 7740fa9e4066Sahrens { 7741fa9e4066Sahrens spa_t *spa; 7742fa9e4066Sahrens 7743fa9e4066Sahrens /* 7744fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 7745fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 7746fa9e4066Sahrens */ 7747fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7748fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 7749fa9e4066Sahrens /* 7750ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 7751ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 7752ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 7753fa9e4066Sahrens */ 7754fa9e4066Sahrens spa_open_ref(spa, FTAG); 7755fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7756ea8dc4b6Seschrock spa_async_suspend(spa); 7757fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7758fa9e4066Sahrens spa_close(spa, FTAG); 7759fa9e4066Sahrens 7760fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 7761fa9e4066Sahrens spa_unload(spa); 7762fa9e4066Sahrens spa_deactivate(spa); 7763fa9e4066Sahrens } 7764fa9e4066Sahrens spa_remove(spa); 7765fa9e4066Sahrens } 7766fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7767fa9e4066Sahrens } 7768ea8dc4b6Seschrock 7769ea8dc4b6Seschrock vdev_t * 77706809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 7771ea8dc4b6Seschrock { 7772c5904d13Seschrock vdev_t *vd; 7773c5904d13Seschrock int i; 7774c5904d13Seschrock 7775c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 7776c5904d13Seschrock return (vd); 7777c5904d13Seschrock 77786809eb4eSEric Schrock if (aux) { 7779c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 7780c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 77816809eb4eSEric Schrock if (vd->vdev_guid == guid) 77826809eb4eSEric Schrock return (vd); 77836809eb4eSEric Schrock } 77846809eb4eSEric Schrock 77856809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 77866809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 7787c5904d13Seschrock if (vd->vdev_guid == guid) 7788c5904d13Seschrock return (vd); 7789c5904d13Seschrock } 7790c5904d13Seschrock } 7791c5904d13Seschrock 7792c5904d13Seschrock return (NULL); 7793ea8dc4b6Seschrock } 7794eaca9bbdSeschrock 7795eaca9bbdSeschrock void 7796990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 7797eaca9bbdSeschrock { 7798f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 7799f9af39baSGeorge Wilson 7800e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7801eaca9bbdSeschrock 7802eaca9bbdSeschrock /* 7803eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 7804eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 7805eaca9bbdSeschrock * possible. 7806eaca9bbdSeschrock */ 780762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 78085d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 7809eaca9bbdSeschrock 7810990b4856Slling spa->spa_uberblock.ub_version = version; 7811eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 7812eaca9bbdSeschrock 7813e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 781499653d4eSeschrock 781599653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 781699653d4eSeschrock } 781799653d4eSeschrock 781899653d4eSeschrock boolean_t 781999653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 782099653d4eSeschrock { 782199653d4eSeschrock int i; 782239c23413Seschrock uint64_t spareguid; 7823fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 782499653d4eSeschrock 7825fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 7826fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 782799653d4eSeschrock return (B_TRUE); 782899653d4eSeschrock 7829fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 7830fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 7831fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 783239c23413Seschrock return (B_TRUE); 783339c23413Seschrock } 783439c23413Seschrock 783599653d4eSeschrock return (B_FALSE); 7836eaca9bbdSeschrock } 7837b1b8ab34Slling 783889a89ebfSlling /* 783989a89ebfSlling * Check if a pool has an active shared spare device. 784089a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 784189a89ebfSlling */ 784289a89ebfSlling static boolean_t 784389a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 784489a89ebfSlling { 784589a89ebfSlling int i, refcnt; 784689a89ebfSlling uint64_t pool; 784789a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 784889a89ebfSlling 784989a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 785089a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 785189a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 785289a89ebfSlling refcnt > 2) 785389a89ebfSlling return (B_TRUE); 785489a89ebfSlling } 785589a89ebfSlling 785689a89ebfSlling return (B_FALSE); 785789a89ebfSlling } 785889a89ebfSlling 78595cabbc6bSPrashanth Sreenivasa sysevent_t * 7860ce1577b0SDave Eddy spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 78613d7072f8Seschrock { 7862b72b6bb1SAlan Somers sysevent_t *ev = NULL; 78633d7072f8Seschrock #ifdef _KERNEL 78643d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 78653d7072f8Seschrock sysevent_value_t value; 78663d7072f8Seschrock 78673d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 78683d7072f8Seschrock SE_SLEEP); 7869b72b6bb1SAlan Somers ASSERT(ev != NULL); 78703d7072f8Seschrock 78713d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 78723d7072f8Seschrock value.value.sv_string = spa_name(spa); 78733d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 78743d7072f8Seschrock goto done; 78753d7072f8Seschrock 78763d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 78773d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 78783d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 78793d7072f8Seschrock goto done; 78803d7072f8Seschrock 78813d7072f8Seschrock if (vd) { 78823d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 78833d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 78843d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 78853d7072f8Seschrock SE_SLEEP) != 0) 78863d7072f8Seschrock goto done; 78873d7072f8Seschrock 78883d7072f8Seschrock if (vd->vdev_path) { 78893d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 78903d7072f8Seschrock value.value.sv_string = vd->vdev_path; 78913d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 78923d7072f8Seschrock &value, SE_SLEEP) != 0) 78933d7072f8Seschrock goto done; 78943d7072f8Seschrock } 78953d7072f8Seschrock } 78963d7072f8Seschrock 7897ce1577b0SDave Eddy if (hist_nvl != NULL) { 7898ce1577b0SDave Eddy fnvlist_merge((nvlist_t *)attr, hist_nvl); 7899ce1577b0SDave Eddy } 7900ce1577b0SDave Eddy 7901b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 7902b01c3b58Seschrock goto done; 7903b01c3b58Seschrock attr = NULL; 7904b01c3b58Seschrock 79053d7072f8Seschrock done: 79063d7072f8Seschrock if (attr) 79073d7072f8Seschrock sysevent_free_attr(attr); 7908b72b6bb1SAlan Somers 7909b72b6bb1SAlan Somers #endif 7910b72b6bb1SAlan Somers return (ev); 7911b72b6bb1SAlan Somers } 7912b72b6bb1SAlan Somers 79135cabbc6bSPrashanth Sreenivasa void 7914b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 7915b72b6bb1SAlan Somers { 7916b72b6bb1SAlan Somers #ifdef _KERNEL 7917b72b6bb1SAlan Somers sysevent_id_t eid; 7918b72b6bb1SAlan Somers 7919b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 79203d7072f8Seschrock sysevent_free(ev); 79213d7072f8Seschrock #endif 79223d7072f8Seschrock } 7923b72b6bb1SAlan Somers 79245cabbc6bSPrashanth Sreenivasa void 79255cabbc6bSPrashanth Sreenivasa spa_event_discard(sysevent_t *ev) 79265cabbc6bSPrashanth Sreenivasa { 79275cabbc6bSPrashanth Sreenivasa #ifdef _KERNEL 79285cabbc6bSPrashanth Sreenivasa sysevent_free(ev); 79295cabbc6bSPrashanth Sreenivasa #endif 79305cabbc6bSPrashanth Sreenivasa } 79315cabbc6bSPrashanth Sreenivasa 7932b72b6bb1SAlan Somers /* 7933b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 7934b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 7935ce1577b0SDave Eddy * filled in from the spa and (optionally) the vdev and history nvl. This 7936ce1577b0SDave Eddy * doesn't do anything in the userland libzpool, as we don't want consumers to 7937ce1577b0SDave Eddy * misinterpret ztest or zdb as real changes. 7938b72b6bb1SAlan Somers */ 7939b72b6bb1SAlan Somers void 7940ce1577b0SDave Eddy spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 7941b72b6bb1SAlan Somers { 7942ce1577b0SDave Eddy spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 7943b72b6bb1SAlan Somers } 7944