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*6f793812SPavel Zakharov static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport, 155*6f793812SPavel Zakharov boolean_t reloading); 156cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 157990b4856Slling 15869962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15935a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 16035a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 16135a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 16235a5a358SJonathan Adams 16335a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 16401f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16535a5a358SJonathan Adams 166e144c4e6SPavel Zakharov /* 167e144c4e6SPavel Zakharov * Report any spa_load_verify errors found, but do not fail spa_load. 168e144c4e6SPavel Zakharov * This is used by zdb to analyze non-idle pools. 169e144c4e6SPavel Zakharov */ 170e144c4e6SPavel Zakharov boolean_t spa_load_verify_dryrun = B_FALSE; 171e144c4e6SPavel Zakharov 17235a5a358SJonathan Adams /* 17335a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 17435a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 17535a5a358SJonathan Adams */ 17635a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 17735a5a358SJonathan Adams 178*6f793812SPavel Zakharov /* 179*6f793812SPavel Zakharov * For debugging purposes: print out vdev tree during pool import. 180*6f793812SPavel Zakharov */ 181*6f793812SPavel Zakharov boolean_t spa_load_print_vdev_tree = B_FALSE; 182*6f793812SPavel Zakharov 183*6f793812SPavel Zakharov /* 184*6f793812SPavel Zakharov * A non-zero value for zfs_max_missing_tvds means that we allow importing 185*6f793812SPavel Zakharov * pools with missing top-level vdevs. This is strictly intended for advanced 186*6f793812SPavel Zakharov * pool recovery cases since missing data is almost inevitable. Pools with 187*6f793812SPavel Zakharov * missing devices can only be imported read-only for safety reasons, and their 188*6f793812SPavel Zakharov * fail-mode will be automatically set to "continue". 189*6f793812SPavel Zakharov * 190*6f793812SPavel Zakharov * With 1 missing vdev we should be able to import the pool and mount all 191*6f793812SPavel Zakharov * datasets. User data that was not modified after the missing device has been 192*6f793812SPavel Zakharov * added should be recoverable. This means that snapshots created prior to the 193*6f793812SPavel Zakharov * addition of that device should be completely intact. 194*6f793812SPavel Zakharov * 195*6f793812SPavel Zakharov * With 2 missing vdevs, some datasets may fail to mount since there are 196*6f793812SPavel Zakharov * dataset statistics that are stored as regular metadata. Some data might be 197*6f793812SPavel Zakharov * recoverable if those vdevs were added recently. 198*6f793812SPavel Zakharov * 199*6f793812SPavel Zakharov * With 3 or more missing vdevs, the pool is severely damaged and MOS entries 200*6f793812SPavel Zakharov * may be missing entirely. Chances of data recovery are very low. Note that 201*6f793812SPavel Zakharov * there are also risks of performing an inadvertent rewind as we might be 202*6f793812SPavel Zakharov * missing all the vdevs with the latest uberblocks. 203*6f793812SPavel Zakharov */ 204*6f793812SPavel Zakharov uint64_t zfs_max_missing_tvds = 0; 205*6f793812SPavel Zakharov 206*6f793812SPavel Zakharov /* 207*6f793812SPavel Zakharov * The parameters below are similar to zfs_max_missing_tvds but are only 208*6f793812SPavel Zakharov * intended for a preliminary open of the pool with an untrusted config which 209*6f793812SPavel Zakharov * might be incomplete or out-dated. 210*6f793812SPavel Zakharov * 211*6f793812SPavel Zakharov * We are more tolerant for pools opened from a cachefile since we could have 212*6f793812SPavel Zakharov * an out-dated cachefile where a device removal was not registered. 213*6f793812SPavel Zakharov * We could have set the limit arbitrarily high but in the case where devices 214*6f793812SPavel Zakharov * are really missing we would want to return the proper error codes; we chose 215*6f793812SPavel Zakharov * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available 216*6f793812SPavel Zakharov * and we get a chance to retrieve the trusted config. 217*6f793812SPavel Zakharov */ 218*6f793812SPavel Zakharov uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; 219*6f793812SPavel Zakharov /* 220*6f793812SPavel Zakharov * In the case where config was assembled by scanning device paths (/dev/dsks 221*6f793812SPavel Zakharov * by default) we are less tolerant since all the existing devices should have 222*6f793812SPavel Zakharov * been detected and we want spa_load to return the right error codes. 223*6f793812SPavel Zakharov */ 224*6f793812SPavel Zakharov uint64_t zfs_max_missing_tvds_scan = 0; 225*6f793812SPavel Zakharov 226990b4856Slling /* 227990b4856Slling * ========================================================================== 228990b4856Slling * SPA properties routines 229990b4856Slling * ========================================================================== 230990b4856Slling */ 231990b4856Slling 232990b4856Slling /* 233990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 234990b4856Slling */ 2359d82f4f6Slling static void 236990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 237990b4856Slling uint64_t intval, zprop_source_t src) 238990b4856Slling { 239990b4856Slling const char *propname = zpool_prop_to_name(prop); 240990b4856Slling nvlist_t *propval; 241990b4856Slling 2429d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2439d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 244990b4856Slling 2459d82f4f6Slling if (strval != NULL) 2469d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 2479d82f4f6Slling else 2489d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 249990b4856Slling 2509d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 251990b4856Slling nvlist_free(propval); 252990b4856Slling } 253990b4856Slling 254990b4856Slling /* 255990b4856Slling * Get property values from the spa configuration. 256990b4856Slling */ 2579d82f4f6Slling static void 258990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 259990b4856Slling { 2604263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 261ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2622e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 263990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 264c5904d13Seschrock spa_config_dirent_t *dp; 2652e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 266990b4856Slling 267e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 268e14bb325SJeff Bonwick 2694263d13fSGeorge Wilson if (rvd != NULL) { 270485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 271b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 272379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 273379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 274485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 275485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 276485bbbf5SGeorge Wilson size - alloc, src); 2774263d13fSGeorge Wilson 2782e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2792e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2802e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2812e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 282f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 283f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 284379c004dSEric Schrock 285485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 286379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 287379c004dSEric Schrock 288b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 289b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 290b24ab676SJeff Bonwick 291379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2924263d13fSGeorge Wilson rvd->vdev_state, src); 293379c004dSEric Schrock 294379c004dSEric Schrock version = spa_version(spa); 295379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 296379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 297379c004dSEric Schrock else 298379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 299379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 300379c004dSEric Schrock } 301990b4856Slling 302ad135b5dSChristopher Siden if (pool != NULL) { 303ad135b5dSChristopher Siden /* 304ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 305ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 306ad135b5dSChristopher Siden */ 3077fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 308ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 309c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 310c1379625SJustin T. Gibbs src); 311ad135b5dSChristopher Siden } else { 312ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 313ad135b5dSChristopher Siden NULL, 0, src); 314ad135b5dSChristopher Siden } 3157fd05ac4SMatthew Ahrens 3167fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 3177fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 318c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 319c1379625SJustin T. Gibbs src); 3207fd05ac4SMatthew Ahrens } else { 3217fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 3227fd05ac4SMatthew Ahrens NULL, 0, src); 3237fd05ac4SMatthew Ahrens } 324ad135b5dSChristopher Siden } 325ad135b5dSChristopher Siden 3269d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 327990b4856Slling 3288704186eSDan McDonald if (spa->spa_comment != NULL) { 3298704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 3308704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 3318704186eSDan McDonald } 3328704186eSDan McDonald 3339d82f4f6Slling if (spa->spa_root != NULL) 3349d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 3359d82f4f6Slling 0, ZPROP_SRC_LOCAL); 336990b4856Slling 337b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 338b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 339b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 340b5152584SMatthew Ahrens } else { 341b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 342b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 343b5152584SMatthew Ahrens } 344b5152584SMatthew Ahrens 345c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 346c5904d13Seschrock if (dp->scd_path == NULL) { 3479d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 348c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 349c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 3509d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 351c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 3522f8aaab3Seschrock } 3532f8aaab3Seschrock } 354990b4856Slling } 355990b4856Slling 356990b4856Slling /* 357990b4856Slling * Get zpool property values. 358990b4856Slling */ 359990b4856Slling int 360990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 361990b4856Slling { 362b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 363990b4856Slling zap_cursor_t zc; 364990b4856Slling zap_attribute_t za; 365990b4856Slling int err; 366990b4856Slling 3679d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 368990b4856Slling 369e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 370e14bb325SJeff Bonwick 371990b4856Slling /* 372990b4856Slling * Get properties from the spa config. 373990b4856Slling */ 3749d82f4f6Slling spa_prop_get_config(spa, nvp); 375990b4856Slling 376990b4856Slling /* If no pool property object, no more prop to get. */ 377afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 378990b4856Slling mutex_exit(&spa->spa_props_lock); 379990b4856Slling return (0); 380990b4856Slling } 381990b4856Slling 382990b4856Slling /* 383990b4856Slling * Get properties from the MOS pool property object. 384990b4856Slling */ 385990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 386990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 387990b4856Slling zap_cursor_advance(&zc)) { 388990b4856Slling uint64_t intval = 0; 389990b4856Slling char *strval = NULL; 390990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 391990b4856Slling zpool_prop_t prop; 392990b4856Slling 3934ae5f5f0SAlan Somers if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) 394990b4856Slling continue; 395990b4856Slling 396990b4856Slling switch (za.za_integer_length) { 397990b4856Slling case 8: 398990b4856Slling /* integer property */ 399990b4856Slling if (za.za_first_integer != 400990b4856Slling zpool_prop_default_numeric(prop)) 401990b4856Slling src = ZPROP_SRC_LOCAL; 402990b4856Slling 403990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 404990b4856Slling dsl_pool_t *dp; 405990b4856Slling dsl_dataset_t *ds = NULL; 406990b4856Slling 407990b4856Slling dp = spa_get_dsl(spa); 4083b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 409745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 410745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 4113b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 412990b4856Slling break; 413990b4856Slling } 414990b4856Slling 4159adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 416990b4856Slling KM_SLEEP); 417990b4856Slling dsl_dataset_name(ds, strval); 418745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 4193b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 420990b4856Slling } else { 421990b4856Slling strval = NULL; 422990b4856Slling intval = za.za_first_integer; 423990b4856Slling } 424990b4856Slling 4259d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 426990b4856Slling 427990b4856Slling if (strval != NULL) 4289adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 429990b4856Slling 430990b4856Slling break; 431990b4856Slling 432990b4856Slling case 1: 433990b4856Slling /* string property */ 434990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 435990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 436990b4856Slling za.za_name, 1, za.za_num_integers, strval); 437990b4856Slling if (err) { 438990b4856Slling kmem_free(strval, za.za_num_integers); 439990b4856Slling break; 440990b4856Slling } 4419d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 442990b4856Slling kmem_free(strval, za.za_num_integers); 443990b4856Slling break; 444990b4856Slling 445990b4856Slling default: 446990b4856Slling break; 447990b4856Slling } 448990b4856Slling } 449990b4856Slling zap_cursor_fini(&zc); 450990b4856Slling mutex_exit(&spa->spa_props_lock); 451990b4856Slling out: 452990b4856Slling if (err && err != ENOENT) { 453990b4856Slling nvlist_free(*nvp); 4549d82f4f6Slling *nvp = NULL; 455990b4856Slling return (err); 456990b4856Slling } 457990b4856Slling 458990b4856Slling return (0); 459990b4856Slling } 460990b4856Slling 461990b4856Slling /* 462990b4856Slling * Validate the given pool properties nvlist and modify the list 463990b4856Slling * for the property values to be set. 464990b4856Slling */ 465990b4856Slling static int 466990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 467990b4856Slling { 468990b4856Slling nvpair_t *elem; 469990b4856Slling int error = 0, reset_bootfs = 0; 470d5285caeSGeorge Wilson uint64_t objnum = 0; 471ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 472990b4856Slling 473990b4856Slling elem = NULL; 474990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 475990b4856Slling uint64_t intval; 476ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 477ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 478ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 479ad135b5dSChristopher Siden 480ad135b5dSChristopher Siden switch (prop) { 4814ae5f5f0SAlan Somers case ZPOOL_PROP_INVAL: 482ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 483be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 484ad135b5dSChristopher Siden break; 485ad135b5dSChristopher Siden } 486990b4856Slling 487ad135b5dSChristopher Siden /* 488ad135b5dSChristopher Siden * Sanitize the input. 489ad135b5dSChristopher Siden */ 490ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 491be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 492ad135b5dSChristopher Siden break; 493ad135b5dSChristopher Siden } 494990b4856Slling 495ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 496be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 497ad135b5dSChristopher Siden break; 498ad135b5dSChristopher Siden } 499ad135b5dSChristopher Siden 500ad135b5dSChristopher Siden if (intval != 0) { 501be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 502ad135b5dSChristopher Siden break; 503ad135b5dSChristopher Siden } 504ad135b5dSChristopher Siden 505ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 506ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 507be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 508ad135b5dSChristopher Siden break; 509ad135b5dSChristopher Siden } 510ad135b5dSChristopher Siden 511ad135b5dSChristopher Siden has_feature = B_TRUE; 512ad135b5dSChristopher Siden break; 513990b4856Slling 514990b4856Slling case ZPOOL_PROP_VERSION: 515990b4856Slling error = nvpair_value_uint64(elem, &intval); 516990b4856Slling if (!error && 517ad135b5dSChristopher Siden (intval < spa_version(spa) || 518ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 519ad135b5dSChristopher Siden has_feature)) 520be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 521990b4856Slling break; 522990b4856Slling 523990b4856Slling case ZPOOL_PROP_DELEGATION: 524990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 525d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 526573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 527990b4856Slling error = nvpair_value_uint64(elem, &intval); 528990b4856Slling if (!error && intval > 1) 529be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 530990b4856Slling break; 531990b4856Slling 532990b4856Slling case ZPOOL_PROP_BOOTFS: 53325f89ee2SJeff Bonwick /* 53425f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 53525f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 53625f89ee2SJeff Bonwick * the bootfs property cannot be set. 53725f89ee2SJeff Bonwick */ 538990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 539be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 540990b4856Slling break; 541990b4856Slling } 542990b4856Slling 543990b4856Slling /* 54415e6edf1Sgw * Make sure the vdev config is bootable 545990b4856Slling */ 54615e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 547be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 548990b4856Slling break; 549990b4856Slling } 550990b4856Slling 551990b4856Slling reset_bootfs = 1; 552990b4856Slling 553990b4856Slling error = nvpair_value_string(elem, &strval); 554990b4856Slling 555990b4856Slling if (!error) { 556ad135b5dSChristopher Siden objset_t *os; 557b5152584SMatthew Ahrens uint64_t propval; 55815e6edf1Sgw 559990b4856Slling if (strval == NULL || strval[0] == '\0') { 560990b4856Slling objnum = zpool_prop_default_numeric( 561990b4856Slling ZPOOL_PROP_BOOTFS); 562990b4856Slling break; 563990b4856Slling } 564990b4856Slling 565503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 566990b4856Slling break; 56715e6edf1Sgw 568b5152584SMatthew Ahrens /* 569b5152584SMatthew Ahrens * Must be ZPL, and its property settings 570b5152584SMatthew Ahrens * must be supported by GRUB (compression 571b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 572b5152584SMatthew Ahrens */ 573503ad85cSMatthew Ahrens 574503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 575be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5763b2aab18SMatthew Ahrens } else if ((error = 5773b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 57815e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 579b5152584SMatthew Ahrens &propval)) == 0 && 580b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 581b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 58215e6edf1Sgw } else { 58315e6edf1Sgw objnum = dmu_objset_id(os); 58415e6edf1Sgw } 585503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 586990b4856Slling } 587990b4856Slling break; 588e14bb325SJeff Bonwick 5890a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5900a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5910a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5920a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 593be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5940a4e9518Sgw 5950a4e9518Sgw /* 5960a4e9518Sgw * This is a special case which only occurs when 5970a4e9518Sgw * the pool has completely failed. This allows 5980a4e9518Sgw * the user to change the in-core failmode property 5990a4e9518Sgw * without syncing it out to disk (I/Os might 6000a4e9518Sgw * currently be blocked). We do this by returning 6010a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 6020a4e9518Sgw * into thinking we encountered a property validation 6030a4e9518Sgw * error. 6040a4e9518Sgw */ 605e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 6060a4e9518Sgw spa->spa_failmode = intval; 607be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 6080a4e9518Sgw } 6090a4e9518Sgw break; 6102f8aaab3Seschrock 6112f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6122f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 6132f8aaab3Seschrock break; 6142f8aaab3Seschrock 6152f8aaab3Seschrock if (strval[0] == '\0') 6162f8aaab3Seschrock break; 6172f8aaab3Seschrock 6182f8aaab3Seschrock if (strcmp(strval, "none") == 0) 6192f8aaab3Seschrock break; 6202f8aaab3Seschrock 6212f8aaab3Seschrock if (strval[0] != '/') { 622be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6232f8aaab3Seschrock break; 6242f8aaab3Seschrock } 6252f8aaab3Seschrock 6262f8aaab3Seschrock slash = strrchr(strval, '/'); 6272f8aaab3Seschrock ASSERT(slash != NULL); 6282f8aaab3Seschrock 6292f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6302f8aaab3Seschrock strcmp(slash, "/..") == 0) 631be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6322f8aaab3Seschrock break; 633b24ab676SJeff Bonwick 6348704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6358704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 6368704186eSDan McDonald break; 6378704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6388704186eSDan McDonald /* 6398704186eSDan McDonald * The kernel doesn't have an easy isprint() 6408704186eSDan McDonald * check. For this kernel check, we merely 6418704186eSDan McDonald * check ASCII apart from DEL. Fix this if 6428704186eSDan McDonald * there is an easy-to-use kernel isprint(). 6438704186eSDan McDonald */ 6448704186eSDan McDonald if (*check >= 0x7f) { 645be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 6468704186eSDan McDonald break; 6478704186eSDan McDonald } 6488704186eSDan McDonald } 6498704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 6508704186eSDan McDonald error = E2BIG; 6518704186eSDan McDonald break; 6528704186eSDan McDonald 653b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 654b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 655be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 656b24ab676SJeff Bonwick else 657b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 658b24ab676SJeff Bonwick if (error == 0 && 659b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 660be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 661b24ab676SJeff Bonwick break; 662990b4856Slling } 663990b4856Slling 664990b4856Slling if (error) 665990b4856Slling break; 666990b4856Slling } 667990b4856Slling 668990b4856Slling if (!error && reset_bootfs) { 669990b4856Slling error = nvlist_remove(props, 670990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 671990b4856Slling 672990b4856Slling if (!error) { 673990b4856Slling error = nvlist_add_uint64(props, 674990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 675990b4856Slling } 676990b4856Slling } 677990b4856Slling 678990b4856Slling return (error); 679990b4856Slling } 680990b4856Slling 681379c004dSEric Schrock void 682379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 683379c004dSEric Schrock { 684379c004dSEric Schrock char *cachefile; 685379c004dSEric Schrock spa_config_dirent_t *dp; 686379c004dSEric Schrock 687379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 688379c004dSEric Schrock &cachefile) != 0) 689379c004dSEric Schrock return; 690379c004dSEric Schrock 691379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 692379c004dSEric Schrock KM_SLEEP); 693379c004dSEric Schrock 694379c004dSEric Schrock if (cachefile[0] == '\0') 695379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 696379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 697379c004dSEric Schrock dp->scd_path = NULL; 698379c004dSEric Schrock else 699379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 700379c004dSEric Schrock 701379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 702379c004dSEric Schrock if (need_sync) 703379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 704379c004dSEric Schrock } 705379c004dSEric Schrock 706990b4856Slling int 707990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 708990b4856Slling { 709990b4856Slling int error; 710ad135b5dSChristopher Siden nvpair_t *elem = NULL; 711379c004dSEric Schrock boolean_t need_sync = B_FALSE; 712990b4856Slling 713990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 714990b4856Slling return (error); 715990b4856Slling 716379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 717ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 718379c004dSEric Schrock 719f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 720f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 721f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 722379c004dSEric Schrock continue; 723379c004dSEric Schrock 7244ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { 725ad135b5dSChristopher Siden uint64_t ver; 726ad135b5dSChristopher Siden 727ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 728ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 729ad135b5dSChristopher Siden } else { 730ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 731ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 732ad135b5dSChristopher Siden need_sync = B_TRUE; 733ad135b5dSChristopher Siden } 734ad135b5dSChristopher Siden 735ad135b5dSChristopher Siden /* Save time if the version is already set. */ 736ad135b5dSChristopher Siden if (ver == spa_version(spa)) 737ad135b5dSChristopher Siden continue; 738ad135b5dSChristopher Siden 739ad135b5dSChristopher Siden /* 740ad135b5dSChristopher Siden * In addition to the pool directory object, we might 741ad135b5dSChristopher Siden * create the pool properties object, the features for 742ad135b5dSChristopher Siden * read object, the features for write object, or the 743ad135b5dSChristopher Siden * feature descriptions object. 744ad135b5dSChristopher Siden */ 7453b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 7467d46dc6cSMatthew Ahrens spa_sync_version, &ver, 7477d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 748ad135b5dSChristopher Siden if (error) 749ad135b5dSChristopher Siden return (error); 750ad135b5dSChristopher Siden continue; 751ad135b5dSChristopher Siden } 752ad135b5dSChristopher Siden 753379c004dSEric Schrock need_sync = B_TRUE; 754379c004dSEric Schrock break; 755379c004dSEric Schrock } 756379c004dSEric Schrock 757ad135b5dSChristopher Siden if (need_sync) { 7583b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7597d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 760ad135b5dSChristopher Siden } 761ad135b5dSChristopher Siden 762ad135b5dSChristopher Siden return (0); 763990b4856Slling } 764990b4856Slling 765990b4856Slling /* 766990b4856Slling * If the bootfs property value is dsobj, clear it. 767990b4856Slling */ 768990b4856Slling void 769990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 770990b4856Slling { 771990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 772990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 773990b4856Slling spa->spa_pool_props_object, 774990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 775990b4856Slling spa->spa_bootfs = 0; 776990b4856Slling } 777990b4856Slling } 778990b4856Slling 779dfbb9432SGeorge Wilson /*ARGSUSED*/ 780dfbb9432SGeorge Wilson static int 7813b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 782dfbb9432SGeorge Wilson { 7833b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7843b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 785dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 786dfbb9432SGeorge Wilson uint64_t vdev_state; 787dfbb9432SGeorge Wilson 788dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 789dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 790dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 791dfbb9432SGeorge Wilson 792dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 793be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 794dfbb9432SGeorge Wilson 795dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 796dfbb9432SGeorge Wilson 797dfbb9432SGeorge Wilson return (0); 798dfbb9432SGeorge Wilson } 799dfbb9432SGeorge Wilson 800dfbb9432SGeorge Wilson static void 8013b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 802dfbb9432SGeorge Wilson { 8033b2aab18SMatthew Ahrens uint64_t *newguid = arg; 8043b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 805dfbb9432SGeorge Wilson uint64_t oldguid; 806dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 807dfbb9432SGeorge Wilson 808dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 809dfbb9432SGeorge Wilson 810dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 811dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 812dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 813dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 814dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 815dfbb9432SGeorge Wilson 81620128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 817dfbb9432SGeorge Wilson oldguid, *newguid); 818dfbb9432SGeorge Wilson } 819dfbb9432SGeorge Wilson 820e9103aaeSGarrett D'Amore /* 821e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 822e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 823e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 824e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 825e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 826e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 827e9103aaeSGarrett D'Amore * sysevent to update any watchers. 828e9103aaeSGarrett D'Amore */ 829e9103aaeSGarrett D'Amore int 830e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 831e9103aaeSGarrett D'Amore { 832dfbb9432SGeorge Wilson int error; 833dfbb9432SGeorge Wilson uint64_t guid; 834e9103aaeSGarrett D'Amore 8352c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 836dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 837dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 838e9103aaeSGarrett D'Amore 8393b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 8407d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 841e9103aaeSGarrett D'Amore 842dfbb9432SGeorge Wilson if (error == 0) { 8435cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 844ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 845dfbb9432SGeorge Wilson } 846e9103aaeSGarrett D'Amore 847dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 8482c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 849e9103aaeSGarrett D'Amore 850dfbb9432SGeorge Wilson return (error); 851e9103aaeSGarrett D'Amore } 852e9103aaeSGarrett D'Amore 853fa9e4066Sahrens /* 854fa9e4066Sahrens * ========================================================================== 855fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 856fa9e4066Sahrens * ========================================================================== 857fa9e4066Sahrens */ 858fa9e4066Sahrens 859ea8dc4b6Seschrock static int 860ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 861ea8dc4b6Seschrock { 862ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 863ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 864ea8dc4b6Seschrock int ret; 865ea8dc4b6Seschrock 866ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8677802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 868ea8dc4b6Seschrock 869ea8dc4b6Seschrock if (ret < 0) 870ea8dc4b6Seschrock return (-1); 871ea8dc4b6Seschrock else if (ret > 0) 872ea8dc4b6Seschrock return (1); 873ea8dc4b6Seschrock else 874ea8dc4b6Seschrock return (0); 875ea8dc4b6Seschrock } 876ea8dc4b6Seschrock 877ea8dc4b6Seschrock /* 878ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 879ea8dc4b6Seschrock * re-initializes them in the process. 880ea8dc4b6Seschrock */ 881ea8dc4b6Seschrock void 882ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 883ea8dc4b6Seschrock { 884ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 885ea8dc4b6Seschrock 886ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 887ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 888ea8dc4b6Seschrock 889ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 890ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 891ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 892ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 893ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 894ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 895ea8dc4b6Seschrock } 896ea8dc4b6Seschrock 897ec94d322SAdam Leventhal static void 898ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 899fa9e4066Sahrens { 900ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 901ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 902ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 903ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 904ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 905ec94d322SAdam Leventhal char name[32]; 9065aeb9474SGarrett D'Amore uint_t flags = 0; 90735a5a358SJonathan Adams boolean_t batch = B_FALSE; 908fa9e4066Sahrens 909ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 910ec94d322SAdam Leventhal tqs->stqs_count = 0; 911ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 912ec94d322SAdam Leventhal return; 913ec94d322SAdam Leventhal } 914fa9e4066Sahrens 915ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 916fa9e4066Sahrens 917ec94d322SAdam Leventhal tqs->stqs_count = count; 918ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 91935a5a358SJonathan Adams 92069962b56SMatthew Ahrens switch (mode) { 92169962b56SMatthew Ahrens case ZTI_MODE_FIXED: 92269962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 92369962b56SMatthew Ahrens value = MAX(value, 1); 92469962b56SMatthew Ahrens break; 925ec94d322SAdam Leventhal 92669962b56SMatthew Ahrens case ZTI_MODE_BATCH: 92769962b56SMatthew Ahrens batch = B_TRUE; 92869962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 92969962b56SMatthew Ahrens value = zio_taskq_batch_pct; 93069962b56SMatthew Ahrens break; 931ec94d322SAdam Leventhal 93269962b56SMatthew Ahrens default: 93369962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 93469962b56SMatthew Ahrens "spa_activate()", 93569962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 93669962b56SMatthew Ahrens break; 93769962b56SMatthew Ahrens } 938ec94d322SAdam Leventhal 93969962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 94069962b56SMatthew Ahrens taskq_t *tq; 941ec94d322SAdam Leventhal 942ec94d322SAdam Leventhal if (count > 1) { 943ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 944ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 945ec94d322SAdam Leventhal } else { 946ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 947ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 948ec94d322SAdam Leventhal } 949ec94d322SAdam Leventhal 950ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 951ec94d322SAdam Leventhal if (batch) 952ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 953ec94d322SAdam Leventhal 954ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 955ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 956ec94d322SAdam Leventhal } else { 95769962b56SMatthew Ahrens pri_t pri = maxclsyspri; 95869962b56SMatthew Ahrens /* 95969962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 96069962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 96169962b56SMatthew Ahrens * than the other taskqs. 96269962b56SMatthew Ahrens */ 96369962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 96469962b56SMatthew Ahrens pri--; 96569962b56SMatthew Ahrens 96669962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 967ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 968ec94d322SAdam Leventhal } 969ec94d322SAdam Leventhal 970ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 971ec94d322SAdam Leventhal } 972ec94d322SAdam Leventhal } 973ec94d322SAdam Leventhal 974ec94d322SAdam Leventhal static void 975ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 976ec94d322SAdam Leventhal { 977ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 978ec94d322SAdam Leventhal 979ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 980ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 981ec94d322SAdam Leventhal return; 982ec94d322SAdam Leventhal } 983ec94d322SAdam Leventhal 984ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 985ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 986ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 98735a5a358SJonathan Adams } 98835a5a358SJonathan Adams 989ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 990ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 991ec94d322SAdam Leventhal } 99235a5a358SJonathan Adams 993ec94d322SAdam Leventhal /* 994ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 995ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 996ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 997ec94d322SAdam Leventhal * the low bits of gethrtime(). 998ec94d322SAdam Leventhal */ 999ec94d322SAdam Leventhal void 1000ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 1001ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 1002ec94d322SAdam Leventhal { 1003ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1004ec94d322SAdam Leventhal taskq_t *tq; 1005ec94d322SAdam Leventhal 1006ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 1007ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 1008ec94d322SAdam Leventhal 1009ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 1010ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 1011ec94d322SAdam Leventhal } else { 1012ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 101335a5a358SJonathan Adams } 1014ec94d322SAdam Leventhal 1015ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 101635a5a358SJonathan Adams } 101735a5a358SJonathan Adams 101835a5a358SJonathan Adams static void 101935a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 102035a5a358SJonathan Adams { 1021e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1022e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1023ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 102435a5a358SJonathan Adams } 102535a5a358SJonathan Adams } 102635a5a358SJonathan Adams } 102735a5a358SJonathan Adams 102835a5a358SJonathan Adams #ifdef _KERNEL 102935a5a358SJonathan Adams static void 103035a5a358SJonathan Adams spa_thread(void *arg) 103135a5a358SJonathan Adams { 103235a5a358SJonathan Adams callb_cpr_t cprinfo; 10332e0c549eSJonathan Adams 103435a5a358SJonathan Adams spa_t *spa = arg; 103535a5a358SJonathan Adams user_t *pu = PTOU(curproc); 10362e0c549eSJonathan Adams 103735a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 103835a5a358SJonathan Adams spa->spa_name); 10392e0c549eSJonathan Adams 104035a5a358SJonathan Adams ASSERT(curproc != &p0); 104135a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 104235a5a358SJonathan Adams "zpool-%s", spa->spa_name); 104335a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 10442e0c549eSJonathan Adams 104535a5a358SJonathan Adams /* bind this thread to the requested psrset */ 104635a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 104735a5a358SJonathan Adams pool_lock(); 104835a5a358SJonathan Adams mutex_enter(&cpu_lock); 104935a5a358SJonathan Adams mutex_enter(&pidlock); 105035a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 105180eb36f2SGeorge Wilson 105235a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 105335a5a358SJonathan Adams 0, NULL, NULL) == 0) { 105435a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 105535a5a358SJonathan Adams } else { 105635a5a358SJonathan Adams cmn_err(CE_WARN, 105735a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 105835a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 105935a5a358SJonathan Adams } 106035a5a358SJonathan Adams 106135a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 106235a5a358SJonathan Adams mutex_exit(&pidlock); 106335a5a358SJonathan Adams mutex_exit(&cpu_lock); 106435a5a358SJonathan Adams pool_unlock(); 106535a5a358SJonathan Adams } 106635a5a358SJonathan Adams 106735a5a358SJonathan Adams if (zio_taskq_sysdc) { 106835a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 106935a5a358SJonathan Adams } 107035a5a358SJonathan Adams 107135a5a358SJonathan Adams spa->spa_proc = curproc; 107235a5a358SJonathan Adams spa->spa_did = curthread->t_did; 107335a5a358SJonathan Adams 107435a5a358SJonathan Adams spa_create_zio_taskqs(spa); 107535a5a358SJonathan Adams 107635a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 107735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 107835a5a358SJonathan Adams 107935a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 108035a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 108135a5a358SJonathan Adams 108235a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 108335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 108435a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 108535a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 108635a5a358SJonathan Adams 108735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 108835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 108935a5a358SJonathan Adams spa->spa_proc = &p0; 109035a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 109135a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 109235a5a358SJonathan Adams 109335a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 109435a5a358SJonathan Adams lwp_exit(); 109535a5a358SJonathan Adams } 109635a5a358SJonathan Adams #endif 109735a5a358SJonathan Adams 109835a5a358SJonathan Adams /* 109935a5a358SJonathan Adams * Activate an uninitialized pool. 110035a5a358SJonathan Adams */ 110135a5a358SJonathan Adams static void 110235a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 110335a5a358SJonathan Adams { 110435a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 110535a5a358SJonathan Adams 110635a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 110735a5a358SJonathan Adams spa->spa_mode = mode; 110835a5a358SJonathan Adams 110935a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 111035a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 111135a5a358SJonathan Adams 111235a5a358SJonathan Adams /* Try to create a covering process */ 111335a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 111435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 111535a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 111635a5a358SJonathan Adams spa->spa_did = 0; 111735a5a358SJonathan Adams 111835a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 111935a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 112035a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 112135a5a358SJonathan Adams NULL, 0) == 0) { 112235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 112335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 112435a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 112535a5a358SJonathan Adams &spa->spa_proc_lock); 11262e0c549eSJonathan Adams } 112735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 112835a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 112935a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 113035a5a358SJonathan Adams } else { 113135a5a358SJonathan Adams #ifdef _KERNEL 113235a5a358SJonathan Adams cmn_err(CE_WARN, 113335a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 113435a5a358SJonathan Adams spa->spa_name); 113535a5a358SJonathan Adams #endif 1136e14bb325SJeff Bonwick } 1137fa9e4066Sahrens } 113835a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 113935a5a358SJonathan Adams 114035a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 114135a5a358SJonathan Adams if (spa->spa_proc == &p0) { 114235a5a358SJonathan Adams spa_create_zio_taskqs(spa); 114335a5a358SJonathan Adams } 1144fa9e4066Sahrens 11455cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) 11465cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, 0); 11475cabbc6bSPrashanth Sreenivasa 1148e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1149e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1150bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1151bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1152e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1153e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1154fa9e4066Sahrens 1155b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1156fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1157ea8dc4b6Seschrock 1158ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1159ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1160ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1161ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1162ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1163ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1164fa9e4066Sahrens } 1165fa9e4066Sahrens 1166fa9e4066Sahrens /* 1167fa9e4066Sahrens * Opposite of spa_activate(). 1168fa9e4066Sahrens */ 1169fa9e4066Sahrens static void 1170fa9e4066Sahrens spa_deactivate(spa_t *spa) 1171fa9e4066Sahrens { 1172fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1173fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1174fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 117525f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1176fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1177fa9e4066Sahrens 1178bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1179bc9014e6SJustin Gibbs 1180fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1181fa9e4066Sahrens 1182e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1183bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1184e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1185fa9e4066Sahrens 1186e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1187e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1188ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1189e14bb325SJeff Bonwick } 1190fa9e4066Sahrens } 1191fa9e4066Sahrens 11925cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) { 11935cabbc6bSPrashanth Sreenivasa ASSERT3P(spa->spa_txg_zio[i], !=, NULL); 11945cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[i])); 11955cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = NULL; 11965cabbc6bSPrashanth Sreenivasa } 11975cabbc6bSPrashanth Sreenivasa 1198fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1199fa9e4066Sahrens spa->spa_normal_class = NULL; 1200fa9e4066Sahrens 12018654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 12028654d025Sperrin spa->spa_log_class = NULL; 12038654d025Sperrin 1204ea8dc4b6Seschrock /* 1205ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1206ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1207ea8dc4b6Seschrock */ 1208ea8dc4b6Seschrock spa_errlog_drain(spa); 1209ea8dc4b6Seschrock 1210ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1211ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1212ea8dc4b6Seschrock 1213fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 121435a5a358SJonathan Adams 121535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 121635a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 121735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 121835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 121935a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 122035a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 122135a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 122235a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 122335a5a358SJonathan Adams } 122435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 122535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 122635a5a358SJonathan Adams } 122735a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 122835a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 122935a5a358SJonathan Adams 123035a5a358SJonathan Adams /* 123135a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 123235a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 123335a5a358SJonathan Adams * it. 123435a5a358SJonathan Adams */ 123535a5a358SJonathan Adams if (spa->spa_did != 0) { 123635a5a358SJonathan Adams thread_join(spa->spa_did); 123735a5a358SJonathan Adams spa->spa_did = 0; 123835a5a358SJonathan Adams } 1239fa9e4066Sahrens } 1240fa9e4066Sahrens 1241fa9e4066Sahrens /* 1242fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1243fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1244fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1245fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1246fa9e4066Sahrens */ 124799653d4eSeschrock static int 124899653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 124999653d4eSeschrock uint_t id, int atype) 1250fa9e4066Sahrens { 1251fa9e4066Sahrens nvlist_t **child; 1252573ca77eSGeorge Wilson uint_t children; 125399653d4eSeschrock int error; 1254fa9e4066Sahrens 125599653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 125699653d4eSeschrock return (error); 1257fa9e4066Sahrens 125899653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 125999653d4eSeschrock return (0); 1260fa9e4066Sahrens 1261e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1262e14bb325SJeff Bonwick &child, &children); 1263e14bb325SJeff Bonwick 1264e14bb325SJeff Bonwick if (error == ENOENT) 1265e14bb325SJeff Bonwick return (0); 1266e14bb325SJeff Bonwick 1267e14bb325SJeff Bonwick if (error) { 126899653d4eSeschrock vdev_free(*vdp); 126999653d4eSeschrock *vdp = NULL; 1270be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1271fa9e4066Sahrens } 1272fa9e4066Sahrens 1273573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 127499653d4eSeschrock vdev_t *vd; 127599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 127699653d4eSeschrock atype)) != 0) { 127799653d4eSeschrock vdev_free(*vdp); 127899653d4eSeschrock *vdp = NULL; 127999653d4eSeschrock return (error); 1280fa9e4066Sahrens } 1281fa9e4066Sahrens } 1282fa9e4066Sahrens 128399653d4eSeschrock ASSERT(*vdp != NULL); 128499653d4eSeschrock 128599653d4eSeschrock return (0); 1286fa9e4066Sahrens } 1287fa9e4066Sahrens 1288fa9e4066Sahrens /* 1289fa9e4066Sahrens * Opposite of spa_load(). 1290fa9e4066Sahrens */ 1291fa9e4066Sahrens static void 1292fa9e4066Sahrens spa_unload(spa_t *spa) 1293fa9e4066Sahrens { 129499653d4eSeschrock int i; 129599653d4eSeschrock 1296e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1297e14bb325SJeff Bonwick 12983ee8c80cSPavel Zakharov spa_load_note(spa, "UNLOADING"); 12993ee8c80cSPavel Zakharov 1300ea8dc4b6Seschrock /* 1301ea8dc4b6Seschrock * Stop async tasks. 1302ea8dc4b6Seschrock */ 1303ea8dc4b6Seschrock spa_async_suspend(spa); 1304ea8dc4b6Seschrock 1305fa9e4066Sahrens /* 1306fa9e4066Sahrens * Stop syncing. 1307fa9e4066Sahrens */ 1308fa9e4066Sahrens if (spa->spa_sync_on) { 1309fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1310fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1311fa9e4066Sahrens } 1312fa9e4066Sahrens 13138363e80aSGeorge Wilson /* 13148363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 13158363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 13168363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 13178363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 13188363e80aSGeorge Wilson */ 13198363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 13208363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13218363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 13228363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 13238363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 13248363e80aSGeorge Wilson } 13258363e80aSGeorge Wilson 1326fa9e4066Sahrens /* 1327e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1328fa9e4066Sahrens */ 132954d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 13306f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 13316f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 13326f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 133354d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 133454d692b7SGeorge Wilson } 1335fa9e4066Sahrens 13365cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 13375cabbc6bSPrashanth Sreenivasa spa_vdev_removal_destroy(spa->spa_vdev_removal); 13385cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal = NULL; 13395cabbc6bSPrashanth Sreenivasa } 13405cabbc6bSPrashanth Sreenivasa 13415cabbc6bSPrashanth Sreenivasa spa_condense_fini(spa); 13425cabbc6bSPrashanth Sreenivasa 1343cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1344cde58dbcSMatthew Ahrens 13450713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 13460713e232SGeorge Wilson 13470713e232SGeorge Wilson /* 13480713e232SGeorge Wilson * Close all vdevs. 13490713e232SGeorge Wilson */ 13500713e232SGeorge Wilson if (spa->spa_root_vdev) 13510713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 13520713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 13530713e232SGeorge Wilson 1354fa9e4066Sahrens /* 1355fa9e4066Sahrens * Close the dsl pool. 1356fa9e4066Sahrens */ 1357fa9e4066Sahrens if (spa->spa_dsl_pool) { 1358fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1359fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1360afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1361fa9e4066Sahrens } 1362fa9e4066Sahrens 1363b24ab676SJeff Bonwick ddt_unload(spa); 1364b24ab676SJeff Bonwick 13658ad4d6ddSJeff Bonwick /* 13668ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 13678ad4d6ddSJeff Bonwick */ 13688ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 13698ad4d6ddSJeff Bonwick 1370fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1371fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1372fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1373fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1374fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1375fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 137699653d4eSeschrock } 1377fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1378fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1379fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1380fa94a07fSbrendan } 13812ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1382fa94a07fSbrendan 1383cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1384cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1385fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1386cd0837ccSGeorge Wilson } 1387fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1388fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1389fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1390fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1391fa94a07fSbrendan } 1392fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1393fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1394fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 139599653d4eSeschrock } 13962ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 139799653d4eSeschrock 1398ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13998ad4d6ddSJeff Bonwick 14005cabbc6bSPrashanth Sreenivasa spa->spa_indirect_vdevs_loaded = B_FALSE; 14015cabbc6bSPrashanth Sreenivasa 14028704186eSDan McDonald if (spa->spa_comment != NULL) { 14038704186eSDan McDonald spa_strfree(spa->spa_comment); 14048704186eSDan McDonald spa->spa_comment = NULL; 14058704186eSDan McDonald } 14068704186eSDan McDonald 14078ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1408fa9e4066Sahrens } 1409fa9e4066Sahrens 141099653d4eSeschrock /* 141199653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 141299653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1413fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1414fa94a07fSbrendan * then re-generate a more complete list including status information. 141599653d4eSeschrock */ 14165cabbc6bSPrashanth Sreenivasa void 141799653d4eSeschrock spa_load_spares(spa_t *spa) 141899653d4eSeschrock { 141999653d4eSeschrock nvlist_t **spares; 142099653d4eSeschrock uint_t nspares; 142199653d4eSeschrock int i; 142239c23413Seschrock vdev_t *vd, *tvd; 142399653d4eSeschrock 1424e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1425e14bb325SJeff Bonwick 142699653d4eSeschrock /* 142799653d4eSeschrock * First, close and free any existing spare vdevs. 142899653d4eSeschrock */ 1429fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1430fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 143139c23413Seschrock 143239c23413Seschrock /* Undo the call to spa_activate() below */ 1433c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1434c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 143539c23413Seschrock spa_spare_remove(tvd); 143639c23413Seschrock vdev_close(vd); 143739c23413Seschrock vdev_free(vd); 143899653d4eSeschrock } 143939c23413Seschrock 1440fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1441fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1442fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 144399653d4eSeschrock 1444fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 144599653d4eSeschrock nspares = 0; 144699653d4eSeschrock else 1447fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 144899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 144999653d4eSeschrock 1450fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1451fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 145299653d4eSeschrock 145399653d4eSeschrock if (nspares == 0) 145499653d4eSeschrock return; 145599653d4eSeschrock 145699653d4eSeschrock /* 145799653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 145839c23413Seschrock * process. For each spare, there is potentially two different vdev_t 145939c23413Seschrock * structures associated with it: one in the list of spares (used only 146039c23413Seschrock * for basic validation purposes) and one in the active vdev 146139c23413Seschrock * configuration (if it's spared in). During this phase we open and 146239c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 146339c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 146499653d4eSeschrock */ 1465fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1466fa94a07fSbrendan KM_SLEEP); 1467fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 146899653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 146999653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 147099653d4eSeschrock ASSERT(vd != NULL); 147199653d4eSeschrock 1472fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 147399653d4eSeschrock 1474c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1475c5904d13Seschrock B_FALSE)) != NULL) { 147639c23413Seschrock if (!tvd->vdev_isspare) 147739c23413Seschrock spa_spare_add(tvd); 147839c23413Seschrock 147939c23413Seschrock /* 148039c23413Seschrock * We only mark the spare active if we were successfully 148139c23413Seschrock * able to load the vdev. Otherwise, importing a pool 148239c23413Seschrock * with a bad active spare would result in strange 148339c23413Seschrock * behavior, because multiple pool would think the spare 148439c23413Seschrock * is actively in use. 148539c23413Seschrock * 148639c23413Seschrock * There is a vulnerability here to an equally bizarre 148739c23413Seschrock * circumstance, where a dead active spare is later 148839c23413Seschrock * brought back to life (onlined or otherwise). Given 148939c23413Seschrock * the rarity of this scenario, and the extra complexity 149039c23413Seschrock * it adds, we ignore the possibility. 149139c23413Seschrock */ 149239c23413Seschrock if (!vdev_is_dead(tvd)) 149339c23413Seschrock spa_spare_activate(tvd); 149439c23413Seschrock } 149539c23413Seschrock 1496e14bb325SJeff Bonwick vd->vdev_top = vd; 14976809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1498e14bb325SJeff Bonwick 149999653d4eSeschrock if (vdev_open(vd) != 0) 150099653d4eSeschrock continue; 150199653d4eSeschrock 1502fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1503fa94a07fSbrendan spa_spare_add(vd); 150499653d4eSeschrock } 150599653d4eSeschrock 150699653d4eSeschrock /* 150799653d4eSeschrock * Recompute the stashed list of spares, with status information 150899653d4eSeschrock * this time. 150999653d4eSeschrock */ 1510fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 151199653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 151299653d4eSeschrock 1513fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1514fa94a07fSbrendan KM_SLEEP); 1515fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1516fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 15173f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1518fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1519fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1520fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 152199653d4eSeschrock nvlist_free(spares[i]); 1522fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1523fa94a07fSbrendan } 1524fa94a07fSbrendan 1525fa94a07fSbrendan /* 1526fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1527fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1528fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1529fa94a07fSbrendan * then re-generate a more complete list including status information. 1530fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1531fa94a07fSbrendan * not re-opened. 1532fa94a07fSbrendan */ 15335cabbc6bSPrashanth Sreenivasa void 1534fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1535fa94a07fSbrendan { 1536fa94a07fSbrendan nvlist_t **l2cache; 1537fa94a07fSbrendan uint_t nl2cache; 1538fa94a07fSbrendan int i, j, oldnvdevs; 1539573ca77eSGeorge Wilson uint64_t guid; 1540fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1541fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1542fa94a07fSbrendan 1543e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1544e14bb325SJeff Bonwick 1545fa94a07fSbrendan if (sav->sav_config != NULL) { 1546fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1547fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1548fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1549fa94a07fSbrendan } else { 1550fa94a07fSbrendan nl2cache = 0; 1551d5285caeSGeorge Wilson newvdevs = NULL; 1552fa94a07fSbrendan } 1553fa94a07fSbrendan 1554fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1555fa94a07fSbrendan oldnvdevs = sav->sav_count; 1556fa94a07fSbrendan sav->sav_vdevs = NULL; 1557fa94a07fSbrendan sav->sav_count = 0; 1558fa94a07fSbrendan 1559fa94a07fSbrendan /* 1560fa94a07fSbrendan * Process new nvlist of vdevs. 1561fa94a07fSbrendan */ 1562fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1563fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1564fa94a07fSbrendan &guid) == 0); 1565fa94a07fSbrendan 1566fa94a07fSbrendan newvdevs[i] = NULL; 1567fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1568fa94a07fSbrendan vd = oldvdevs[j]; 1569fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1570fa94a07fSbrendan /* 1571fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1572fa94a07fSbrendan */ 1573fa94a07fSbrendan newvdevs[i] = vd; 1574fa94a07fSbrendan oldvdevs[j] = NULL; 1575fa94a07fSbrendan break; 1576fa94a07fSbrendan } 1577fa94a07fSbrendan } 1578fa94a07fSbrendan 1579fa94a07fSbrendan if (newvdevs[i] == NULL) { 1580fa94a07fSbrendan /* 1581fa94a07fSbrendan * Create new vdev 1582fa94a07fSbrendan */ 1583fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1584fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1585fa94a07fSbrendan ASSERT(vd != NULL); 1586fa94a07fSbrendan newvdevs[i] = vd; 1587fa94a07fSbrendan 1588fa94a07fSbrendan /* 1589fa94a07fSbrendan * Commit this vdev as an l2cache device, 1590fa94a07fSbrendan * even if it fails to open. 1591fa94a07fSbrendan */ 1592fa94a07fSbrendan spa_l2cache_add(vd); 1593fa94a07fSbrendan 1594c5904d13Seschrock vd->vdev_top = vd; 1595c5904d13Seschrock vd->vdev_aux = sav; 1596c5904d13Seschrock 1597c5904d13Seschrock spa_l2cache_activate(vd); 1598c5904d13Seschrock 1599fa94a07fSbrendan if (vdev_open(vd) != 0) 1600fa94a07fSbrendan continue; 1601fa94a07fSbrendan 1602fa94a07fSbrendan (void) vdev_validate_aux(vd); 1603fa94a07fSbrendan 1604573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1605573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1606fa94a07fSbrendan } 1607fa94a07fSbrendan } 1608fa94a07fSbrendan 1609fa94a07fSbrendan /* 1610fa94a07fSbrendan * Purge vdevs that were dropped 1611fa94a07fSbrendan */ 1612fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1613fa94a07fSbrendan uint64_t pool; 1614fa94a07fSbrendan 1615fa94a07fSbrendan vd = oldvdevs[i]; 1616fa94a07fSbrendan if (vd != NULL) { 1617cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1618cd0837ccSGeorge Wilson 16198ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 16208ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1621fa94a07fSbrendan l2arc_remove_vdev(vd); 1622cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1623cd0837ccSGeorge Wilson vdev_free(vd); 1624fa94a07fSbrendan } 1625fa94a07fSbrendan } 1626fa94a07fSbrendan 1627fa94a07fSbrendan if (oldvdevs) 1628fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1629fa94a07fSbrendan 1630fa94a07fSbrendan if (sav->sav_config == NULL) 1631fa94a07fSbrendan goto out; 1632fa94a07fSbrendan 1633fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1634fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1635fa94a07fSbrendan 1636fa94a07fSbrendan /* 1637fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1638fa94a07fSbrendan * information this time. 1639fa94a07fSbrendan */ 1640fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1641fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1642fa94a07fSbrendan 1643fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1644fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1645fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 16463f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1647fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1648fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1649fa94a07fSbrendan out: 1650fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1651fa94a07fSbrendan nvlist_free(l2cache[i]); 1652fa94a07fSbrendan if (sav->sav_count) 1653fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 165499653d4eSeschrock } 165599653d4eSeschrock 165699653d4eSeschrock static int 165799653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 165899653d4eSeschrock { 165999653d4eSeschrock dmu_buf_t *db; 166099653d4eSeschrock char *packed = NULL; 166199653d4eSeschrock size_t nvsize = 0; 166299653d4eSeschrock int error; 166399653d4eSeschrock *value = NULL; 166499653d4eSeschrock 1665a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1666a45f1c3cSBrian Behlendorf if (error != 0) 1667a45f1c3cSBrian Behlendorf return (error); 1668a45f1c3cSBrian Behlendorf 166999653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 167099653d4eSeschrock dmu_buf_rele(db, FTAG); 167199653d4eSeschrock 167299653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 16737bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 16747bfdf011SNeil Perrin DMU_READ_PREFETCH); 167599653d4eSeschrock if (error == 0) 167699653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 167799653d4eSeschrock kmem_free(packed, nvsize); 167899653d4eSeschrock 167999653d4eSeschrock return (error); 168099653d4eSeschrock } 168199653d4eSeschrock 1682*6f793812SPavel Zakharov /* 1683*6f793812SPavel Zakharov * Concrete top-level vdevs that are not missing and are not logs. At every 1684*6f793812SPavel Zakharov * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. 1685*6f793812SPavel Zakharov */ 1686*6f793812SPavel Zakharov static uint64_t 1687*6f793812SPavel Zakharov spa_healthy_core_tvds(spa_t *spa) 1688*6f793812SPavel Zakharov { 1689*6f793812SPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 1690*6f793812SPavel Zakharov uint64_t tvds = 0; 1691*6f793812SPavel Zakharov 1692*6f793812SPavel Zakharov for (uint64_t i = 0; i < rvd->vdev_children; i++) { 1693*6f793812SPavel Zakharov vdev_t *vd = rvd->vdev_child[i]; 1694*6f793812SPavel Zakharov if (vd->vdev_islog) 1695*6f793812SPavel Zakharov continue; 1696*6f793812SPavel Zakharov if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) 1697*6f793812SPavel Zakharov tvds++; 1698*6f793812SPavel Zakharov } 1699*6f793812SPavel Zakharov 1700*6f793812SPavel Zakharov return (tvds); 1701*6f793812SPavel Zakharov } 1702*6f793812SPavel Zakharov 17033d7072f8Seschrock /* 17043d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 17053d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 17063d7072f8Seschrock */ 17073d7072f8Seschrock static void 17083d7072f8Seschrock spa_check_removed(vdev_t *vd) 17093d7072f8Seschrock { 1710*6f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) 17113d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 17123d7072f8Seschrock 1713efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 17145cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 17153d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 1716ce1577b0SDave Eddy spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 17173d7072f8Seschrock } 17183d7072f8Seschrock } 17193d7072f8Seschrock 1720*6f793812SPavel Zakharov static int 1721*6f793812SPavel Zakharov spa_check_for_missing_logs(spa_t *spa) 1722e6ca193dSGeorge Wilson { 1723*6f793812SPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 1724e6ca193dSGeorge Wilson 172588ecc943SGeorge Wilson /* 17264b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 1727*6f793812SPavel Zakharov * diagnostic information about missing log devices. 17284b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 172988ecc943SGeorge Wilson */ 17304b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 17314b964adaSGeorge Wilson nvlist_t **child, *nv; 17324b964adaSGeorge Wilson uint64_t idx = 0; 17334b964adaSGeorge Wilson 17344b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 17354b964adaSGeorge Wilson KM_SLEEP); 17364b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1737e6ca193dSGeorge Wilson 1738*6f793812SPavel Zakharov for (uint64_t c = 0; c < rvd->vdev_children; c++) { 17394b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 17404b964adaSGeorge Wilson 1741*6f793812SPavel Zakharov /* 1742*6f793812SPavel Zakharov * We consider a device as missing only if it failed 1743*6f793812SPavel Zakharov * to open (i.e. offline or faulted is not considered 1744*6f793812SPavel Zakharov * as missing). 1745*6f793812SPavel Zakharov */ 1746*6f793812SPavel Zakharov if (tvd->vdev_islog && 1747*6f793812SPavel Zakharov tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 1748*6f793812SPavel Zakharov child[idx++] = vdev_config_generate(spa, tvd, 1749*6f793812SPavel Zakharov B_FALSE, VDEV_CONFIG_MISSING); 1750*6f793812SPavel Zakharov } 17514b964adaSGeorge Wilson } 17524b964adaSGeorge Wilson 1753*6f793812SPavel Zakharov if (idx > 0) { 1754*6f793812SPavel Zakharov fnvlist_add_nvlist_array(nv, 1755*6f793812SPavel Zakharov ZPOOL_CONFIG_CHILDREN, child, idx); 1756*6f793812SPavel Zakharov fnvlist_add_nvlist(spa->spa_load_info, 1757*6f793812SPavel Zakharov ZPOOL_CONFIG_MISSING_DEVICES, nv); 17584b964adaSGeorge Wilson 1759*6f793812SPavel Zakharov for (uint64_t i = 0; i < idx; i++) 17604b964adaSGeorge Wilson nvlist_free(child[i]); 17614b964adaSGeorge Wilson } 17624b964adaSGeorge Wilson nvlist_free(nv); 17634b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 17644b964adaSGeorge Wilson 1765*6f793812SPavel Zakharov if (idx > 0) { 1766*6f793812SPavel Zakharov spa_load_failed(spa, "some log devices are missing"); 1767*6f793812SPavel Zakharov return (SET_ERROR(ENXIO)); 1768*6f793812SPavel Zakharov } 1769*6f793812SPavel Zakharov } else { 1770*6f793812SPavel Zakharov for (uint64_t c = 0; c < rvd->vdev_children; c++) { 1771*6f793812SPavel Zakharov vdev_t *tvd = rvd->vdev_child[c]; 17725cabbc6bSPrashanth Sreenivasa 1773*6f793812SPavel Zakharov if (tvd->vdev_islog && 1774*6f793812SPavel Zakharov tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 17754b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 1776*6f793812SPavel Zakharov spa_load_note(spa, "some log devices are " 1777*6f793812SPavel Zakharov "missing, ZIL is dropped."); 1778*6f793812SPavel Zakharov break; 1779215198a6SJoe Stein } 17804b964adaSGeorge Wilson } 1781e6ca193dSGeorge Wilson } 1782215198a6SJoe Stein 1783*6f793812SPavel Zakharov return (0); 1784e6ca193dSGeorge Wilson } 1785e6ca193dSGeorge Wilson 1786b87f3af3Sperrin /* 1787b87f3af3Sperrin * Check for missing log devices 1788b87f3af3Sperrin */ 17893b2aab18SMatthew Ahrens static boolean_t 1790b87f3af3Sperrin spa_check_logs(spa_t *spa) 1791b87f3af3Sperrin { 17923b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 179312380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17943b2aab18SMatthew Ahrens 1795b87f3af3Sperrin switch (spa->spa_log_state) { 1796b87f3af3Sperrin case SPA_LOG_MISSING: 1797b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1798b87f3af3Sperrin case SPA_LOG_UNKNOWN: 179912380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 180012380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 18013b2aab18SMatthew Ahrens if (rv) 18021195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1803b87f3af3Sperrin break; 1804b87f3af3Sperrin } 18053b2aab18SMatthew Ahrens return (rv); 1806b87f3af3Sperrin } 1807b87f3af3Sperrin 18081195e687SMark J Musante static boolean_t 18091195e687SMark J Musante spa_passivate_log(spa_t *spa) 18101195e687SMark J Musante { 18111195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18121195e687SMark J Musante boolean_t slog_found = B_FALSE; 18131195e687SMark J Musante 18141195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18151195e687SMark J Musante 18161195e687SMark J Musante if (!spa_has_slogs(spa)) 18171195e687SMark J Musante return (B_FALSE); 18181195e687SMark J Musante 18191195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18201195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18211195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18221195e687SMark J Musante 18231195e687SMark J Musante if (tvd->vdev_islog) { 18241195e687SMark J Musante metaslab_group_passivate(mg); 18251195e687SMark J Musante slog_found = B_TRUE; 18261195e687SMark J Musante } 18271195e687SMark J Musante } 18281195e687SMark J Musante 18291195e687SMark J Musante return (slog_found); 18301195e687SMark J Musante } 18311195e687SMark J Musante 18321195e687SMark J Musante static void 18331195e687SMark J Musante spa_activate_log(spa_t *spa) 18341195e687SMark J Musante { 18351195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18361195e687SMark J Musante 18371195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18381195e687SMark J Musante 18391195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18401195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18411195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18421195e687SMark J Musante 18431195e687SMark J Musante if (tvd->vdev_islog) 18441195e687SMark J Musante metaslab_group_activate(mg); 18451195e687SMark J Musante } 18461195e687SMark J Musante } 18471195e687SMark J Musante 18481195e687SMark J Musante int 18495cabbc6bSPrashanth Sreenivasa spa_reset_logs(spa_t *spa) 18501195e687SMark J Musante { 18513b2aab18SMatthew Ahrens int error; 18521195e687SMark J Musante 18535cabbc6bSPrashanth Sreenivasa error = dmu_objset_find(spa_name(spa), zil_reset, 18543b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18553b2aab18SMatthew Ahrens if (error == 0) { 18561195e687SMark J Musante /* 18571195e687SMark J Musante * We successfully offlined the log device, sync out the 18581195e687SMark J Musante * current txg so that the "stubby" block can be removed 18591195e687SMark J Musante * by zil_sync(). 18601195e687SMark J Musante */ 18611195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18621195e687SMark J Musante } 18631195e687SMark J Musante return (error); 18641195e687SMark J Musante } 18651195e687SMark J Musante 1866b693757aSEric Schrock static void 1867b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1868b693757aSEric Schrock { 1869b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1870b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1871b693757aSEric Schrock } 1872b693757aSEric Schrock 1873b24ab676SJeff Bonwick void 1874b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1875b24ab676SJeff Bonwick { 1876b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1877b24ab676SJeff Bonwick 1878b24ab676SJeff Bonwick if (zio->io_error) 1879b24ab676SJeff Bonwick return; 1880b24ab676SJeff Bonwick 1881b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1882b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1883b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1884b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1885b24ab676SJeff Bonwick } 1886b24ab676SJeff Bonwick 1887468c413aSTim Haley typedef struct spa_load_error { 1888c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1889468c413aSTim Haley uint64_t sle_data_count; 1890468c413aSTim Haley } spa_load_error_t; 1891468c413aSTim Haley 1892468c413aSTim Haley static void 1893468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1894468c413aSTim Haley { 1895468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1896468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1897468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1898468c413aSTim Haley int error = zio->io_error; 1899e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1900468c413aSTim Haley 1901770499e1SDan Kimmel abd_free(zio->io_abd); 1902468c413aSTim Haley if (error) { 1903ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1904468c413aSTim Haley type != DMU_OT_INTENT_LOG) 19051a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1906468c413aSTim Haley else 19071a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1908468c413aSTim Haley } 1909e42d2059SMatthew Ahrens 1910e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1911e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1912e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1913e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1914468c413aSTim Haley } 1915468c413aSTim Haley 1916e42d2059SMatthew Ahrens /* 1917e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1918e42d2059SMatthew Ahrens * a pool while importing it. 1919e42d2059SMatthew Ahrens */ 1920e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1921e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1922e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1923e42d2059SMatthew Ahrens 1924468c413aSTim Haley /*ARGSUSED*/ 1925468c413aSTim Haley static int 1926b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19277802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1928468c413aSTim Haley { 1929a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1930e42d2059SMatthew Ahrens return (0); 1931e42d2059SMatthew Ahrens /* 1932e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1933e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1934e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1935e42d2059SMatthew Ahrens */ 1936e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1937e42d2059SMatthew Ahrens return (0); 1938770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1939e42d2059SMatthew Ahrens return (0); 1940468c413aSTim Haley 1941e42d2059SMatthew Ahrens zio_t *rio = arg; 1942e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1943e42d2059SMatthew Ahrens 1944e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1945e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1946e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1947e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1948e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1949e42d2059SMatthew Ahrens 1950770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 1951e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1952e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1953e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1954468c413aSTim Haley return (0); 1955468c413aSTim Haley } 1956468c413aSTim Haley 1957c971037bSPaul Dagnelie /* ARGSUSED */ 1958c971037bSPaul Dagnelie int 1959c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1960c971037bSPaul Dagnelie { 19619adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1962c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1963c971037bSPaul Dagnelie 1964c971037bSPaul Dagnelie return (0); 1965c971037bSPaul Dagnelie } 1966c971037bSPaul Dagnelie 1967468c413aSTim Haley static int 1968468c413aSTim Haley spa_load_verify(spa_t *spa) 1969468c413aSTim Haley { 1970468c413aSTim Haley zio_t *rio; 1971468c413aSTim Haley spa_load_error_t sle = { 0 }; 1972468c413aSTim Haley zpool_rewind_policy_t policy; 1973468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1974e42d2059SMatthew Ahrens int error = 0; 1975468c413aSTim Haley 1976c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1977c8ee1847SVictor Latushkin 1978c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1979c8ee1847SVictor Latushkin return (0); 1980c8ee1847SVictor Latushkin 1981c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1982c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1983c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1984c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1985c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1986c971037bSPaul Dagnelie if (error != 0) 1987c971037bSPaul Dagnelie return (error); 1988c971037bSPaul Dagnelie 1989468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1990468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1991468c413aSTim Haley 1992e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 19933ee8c80cSPavel Zakharov if (spa->spa_extreme_rewind) { 19943ee8c80cSPavel Zakharov spa_load_note(spa, "performing a complete scan of the " 19953ee8c80cSPavel Zakharov "pool since extreme rewind is on. This may take " 19963ee8c80cSPavel Zakharov "a very long time.\n (spa_load_verify_data=%u, " 19973ee8c80cSPavel Zakharov "spa_load_verify_metadata=%u)", 19983ee8c80cSPavel Zakharov spa_load_verify_data, spa_load_verify_metadata); 19993ee8c80cSPavel Zakharov } 2000e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 2001e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 2002e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 2003e42d2059SMatthew Ahrens } 2004468c413aSTim Haley 2005468c413aSTim Haley (void) zio_wait(rio); 2006468c413aSTim Haley 2007c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 2008468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 2009468c413aSTim Haley 2010e144c4e6SPavel Zakharov if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { 2011e144c4e6SPavel Zakharov spa_load_note(spa, "spa_load_verify found %llu metadata errors " 2012e144c4e6SPavel Zakharov "and %llu data errors", (u_longlong_t)sle.sle_meta_count, 2013e144c4e6SPavel Zakharov (u_longlong_t)sle.sle_data_count); 2014e144c4e6SPavel Zakharov } 2015e144c4e6SPavel Zakharov 2016e144c4e6SPavel Zakharov if (spa_load_verify_dryrun || 2017e144c4e6SPavel Zakharov (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 2018e144c4e6SPavel Zakharov sle.sle_data_count <= policy.zrp_maxdata)) { 20194b964adaSGeorge Wilson int64_t loss = 0; 20204b964adaSGeorge Wilson 2021468c413aSTim Haley verify_ok = B_TRUE; 2022468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 2023468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 20244b964adaSGeorge Wilson 20254b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 20264b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20274b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 20284b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 20294b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 20304b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20314b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 2032a33cae98STim Haley } else { 2033a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2034468c413aSTim Haley } 2035468c413aSTim Haley 2036e144c4e6SPavel Zakharov if (spa_load_verify_dryrun) 2037e144c4e6SPavel Zakharov return (0); 2038e144c4e6SPavel Zakharov 2039468c413aSTim Haley if (error) { 2040468c413aSTim Haley if (error != ENXIO && error != EIO) 2041be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2042468c413aSTim Haley return (error); 2043468c413aSTim Haley } 2044468c413aSTim Haley 2045468c413aSTim Haley return (verify_ok ? 0 : EIO); 2046468c413aSTim Haley } 2047468c413aSTim Haley 20481195e687SMark J Musante /* 20491195e687SMark J Musante * Find a value in the pool props object. 20501195e687SMark J Musante */ 20511195e687SMark J Musante static void 20521195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20531195e687SMark J Musante { 20541195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20551195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20561195e687SMark J Musante } 20571195e687SMark J Musante 20581195e687SMark J Musante /* 20591195e687SMark J Musante * Find a value in the pool directory object. 20601195e687SMark J Musante */ 20611195e687SMark J Musante static int 20623ee8c80cSPavel Zakharov spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) 20631195e687SMark J Musante { 20643ee8c80cSPavel Zakharov int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20653ee8c80cSPavel Zakharov name, sizeof (uint64_t), 1, val); 20663ee8c80cSPavel Zakharov 20673ee8c80cSPavel Zakharov if (error != 0 && (error != ENOENT || log_enoent)) { 20683ee8c80cSPavel Zakharov spa_load_failed(spa, "couldn't get '%s' value in MOS directory " 20693ee8c80cSPavel Zakharov "[error=%d]", name, error); 20703ee8c80cSPavel Zakharov } 20713ee8c80cSPavel Zakharov 20723ee8c80cSPavel Zakharov return (error); 20731195e687SMark J Musante } 20741195e687SMark J Musante 20751195e687SMark J Musante static int 20761195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20771195e687SMark J Musante { 20781195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 20795cabbc6bSPrashanth Sreenivasa return (SET_ERROR(err)); 20801195e687SMark J Musante } 20811195e687SMark J Musante 20821195e687SMark J Musante /* 20831195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20841195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20851195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20861195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20871195e687SMark J Musante * 20881195e687SMark J Musante * This function determines what to do with that list: either rejoin 20891195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20901195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20911195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20921195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20931195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20941195e687SMark J Musante * 2095d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2096d41c4376SMark J Musante * the original pool. 20971195e687SMark J Musante */ 20981195e687SMark J Musante static void 20991195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 21001195e687SMark J Musante { 21011195e687SMark J Musante uint_t extracted; 21021195e687SMark J Musante uint64_t *glist; 21031195e687SMark J Musante uint_t i, gcount; 21041195e687SMark J Musante nvlist_t *nvl; 21051195e687SMark J Musante vdev_t **vd; 21061195e687SMark J Musante boolean_t attempt_reopen; 21071195e687SMark J Musante 21081195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 21091195e687SMark J Musante return; 21101195e687SMark J Musante 21111195e687SMark J Musante /* check that the config is complete */ 21121195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 21131195e687SMark J Musante &glist, &gcount) != 0) 21141195e687SMark J Musante return; 21151195e687SMark J Musante 21161195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 21171195e687SMark J Musante 21181195e687SMark J Musante /* attempt to online all the vdevs & validate */ 21191195e687SMark J Musante attempt_reopen = B_TRUE; 21201195e687SMark J Musante for (i = 0; i < gcount; i++) { 21211195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 21221195e687SMark J Musante continue; 21231195e687SMark J Musante 21241195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 21251195e687SMark J Musante if (vd[i] == NULL) { 21261195e687SMark J Musante /* 21271195e687SMark J Musante * Don't bother attempting to reopen the disks; 21281195e687SMark J Musante * just do the split. 21291195e687SMark J Musante */ 21301195e687SMark J Musante attempt_reopen = B_FALSE; 21311195e687SMark J Musante } else { 21321195e687SMark J Musante /* attempt to re-online it */ 21331195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 21341195e687SMark J Musante } 21351195e687SMark J Musante } 21361195e687SMark J Musante 21371195e687SMark J Musante if (attempt_reopen) { 21381195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21391195e687SMark J Musante 21401195e687SMark J Musante /* check each device to see what state it's in */ 21411195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 21421195e687SMark J Musante if (vd[i] != NULL && 21431195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 21441195e687SMark J Musante break; 21451195e687SMark J Musante ++extracted; 21461195e687SMark J Musante } 21471195e687SMark J Musante } 21481195e687SMark J Musante 21491195e687SMark J Musante /* 21501195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 21511195e687SMark J Musante * even attempted to look at them, then we split them off for 21521195e687SMark J Musante * good. 21531195e687SMark J Musante */ 21541195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 21551195e687SMark J Musante for (i = 0; i < gcount; i++) 21561195e687SMark J Musante if (vd[i] != NULL) 21571195e687SMark J Musante vdev_split(vd[i]); 21581195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21591195e687SMark J Musante } 21601195e687SMark J Musante 21611195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21621195e687SMark J Musante } 21631195e687SMark J Musante 21641195e687SMark J Musante static int 2165*6f793812SPavel Zakharov spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) 21661195e687SMark J Musante { 21671195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21681195e687SMark J Musante int error; 21691195e687SMark J Musante 2170*6f793812SPavel Zakharov spa->spa_load_state = state; 2171ad135b5dSChristopher Siden 2172*6f793812SPavel Zakharov gethrestime(&spa->spa_loaded_ts); 2173*6f793812SPavel Zakharov error = spa_load_impl(spa, type, &ereport, B_FALSE); 21741195e687SMark J Musante 2175bc9014e6SJustin Gibbs /* 2176bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2177bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2178bc9014e6SJustin Gibbs */ 2179bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21801195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 218111027bc7STim Haley if (error) { 218211027bc7STim Haley if (error != EEXIST) { 218311027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 218411027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 218511027bc7STim Haley } 218611027bc7STim Haley if (error != EBADF) { 218711027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 218811027bc7STim Haley } 218911027bc7STim Haley } 21901195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21911195e687SMark J Musante spa->spa_ena = 0; 21921195e687SMark J Musante 21931195e687SMark J Musante return (error); 21941195e687SMark J Musante } 21951195e687SMark J Musante 2196215198a6SJoe Stein /* 2197215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2198215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2199215198a6SJoe Stein * spa's per-vdev ZAP list. 2200215198a6SJoe Stein */ 2201215198a6SJoe Stein static uint64_t 2202215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2203215198a6SJoe Stein { 2204215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2205215198a6SJoe Stein uint64_t total = 0; 2206215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2207215198a6SJoe Stein total++; 2208215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2209215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2210215198a6SJoe Stein } 2211215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2212215198a6SJoe Stein total++; 2213215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2214215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2215215198a6SJoe Stein } 2216215198a6SJoe Stein 2217215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2218215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2219215198a6SJoe Stein } 2220215198a6SJoe Stein 2221215198a6SJoe Stein return (total); 2222215198a6SJoe Stein } 2223215198a6SJoe Stein 2224fa9e4066Sahrens static int 2225*6f793812SPavel Zakharov spa_verify_host(spa_t *spa, nvlist_t *mos_config) 2226*6f793812SPavel Zakharov { 2227*6f793812SPavel Zakharov uint64_t hostid; 2228*6f793812SPavel Zakharov char *hostname; 2229*6f793812SPavel Zakharov uint64_t myhostid = 0; 2230*6f793812SPavel Zakharov 2231*6f793812SPavel Zakharov if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, 2232*6f793812SPavel Zakharov ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 2233*6f793812SPavel Zakharov hostname = fnvlist_lookup_string(mos_config, 2234*6f793812SPavel Zakharov ZPOOL_CONFIG_HOSTNAME); 2235*6f793812SPavel Zakharov 2236*6f793812SPavel Zakharov myhostid = zone_get_hostid(NULL); 2237*6f793812SPavel Zakharov 2238*6f793812SPavel Zakharov if (hostid != 0 && myhostid != 0 && hostid != myhostid) { 2239*6f793812SPavel Zakharov cmn_err(CE_WARN, "pool '%s' could not be " 2240*6f793812SPavel Zakharov "loaded as it was last accessed by " 2241*6f793812SPavel Zakharov "another system (host: %s hostid: 0x%llx). " 2242*6f793812SPavel Zakharov "See: http://illumos.org/msg/ZFS-8000-EY", 2243*6f793812SPavel Zakharov spa_name(spa), hostname, (u_longlong_t)hostid); 2244*6f793812SPavel Zakharov spa_load_failed(spa, "hostid verification failed: pool " 2245*6f793812SPavel Zakharov "last accessed by host: %s (hostid: 0x%llx)", 2246*6f793812SPavel Zakharov hostname, (u_longlong_t)hostid); 2247*6f793812SPavel Zakharov return (SET_ERROR(EBADF)); 2248*6f793812SPavel Zakharov } 2249*6f793812SPavel Zakharov } 2250*6f793812SPavel Zakharov 2251*6f793812SPavel Zakharov return (0); 2252*6f793812SPavel Zakharov } 2253*6f793812SPavel Zakharov 2254*6f793812SPavel Zakharov static int 2255*6f793812SPavel Zakharov spa_ld_parse_config(spa_t *spa, spa_import_type_t type) 2256fa9e4066Sahrens { 2257fa9e4066Sahrens int error = 0; 2258*6f793812SPavel Zakharov nvlist_t *nvtree, *nvl, *config = spa->spa_config; 22591195e687SMark J Musante int parse; 22601fd3785fSPavel Zakharov vdev_t *rvd; 2261*6f793812SPavel Zakharov uint64_t pool_guid; 2262*6f793812SPavel Zakharov char *comment; 2263*6f793812SPavel Zakharov 2264*6f793812SPavel Zakharov /* 2265*6f793812SPavel Zakharov * Versioning wasn't explicitly added to the label until later, so if 2266*6f793812SPavel Zakharov * it's not present treat it as the initial version. 2267*6f793812SPavel Zakharov */ 2268*6f793812SPavel Zakharov if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 2269*6f793812SPavel Zakharov &spa->spa_ubsync.ub_version) != 0) 2270*6f793812SPavel Zakharov spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 2271*6f793812SPavel Zakharov 2272*6f793812SPavel Zakharov if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 2273*6f793812SPavel Zakharov spa_load_failed(spa, "invalid config provided: '%s' missing", 2274*6f793812SPavel Zakharov ZPOOL_CONFIG_POOL_GUID); 2275*6f793812SPavel Zakharov return (SET_ERROR(EINVAL)); 2276*6f793812SPavel Zakharov } 2277*6f793812SPavel Zakharov 2278*6f793812SPavel Zakharov if ((spa->spa_load_state == SPA_LOAD_IMPORT || spa->spa_load_state == 2279*6f793812SPavel Zakharov SPA_LOAD_TRYIMPORT) && spa_guid_exists(pool_guid, 0)) { 2280*6f793812SPavel Zakharov spa_load_failed(spa, "a pool with guid %llu is already open", 2281*6f793812SPavel Zakharov (u_longlong_t)pool_guid); 2282*6f793812SPavel Zakharov return (SET_ERROR(EEXIST)); 2283*6f793812SPavel Zakharov } 2284*6f793812SPavel Zakharov 2285*6f793812SPavel Zakharov spa->spa_config_guid = pool_guid; 2286*6f793812SPavel Zakharov 2287*6f793812SPavel Zakharov nvlist_free(spa->spa_load_info); 2288*6f793812SPavel Zakharov spa->spa_load_info = fnvlist_alloc(); 2289*6f793812SPavel Zakharov 2290*6f793812SPavel Zakharov ASSERT(spa->spa_comment == NULL); 2291*6f793812SPavel Zakharov if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 2292*6f793812SPavel Zakharov spa->spa_comment = spa_strdup(comment); 2293*6f793812SPavel Zakharov 2294*6f793812SPavel Zakharov (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 2295*6f793812SPavel Zakharov &spa->spa_config_txg); 2296*6f793812SPavel Zakharov 2297*6f793812SPavel Zakharov if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) 2298*6f793812SPavel Zakharov spa->spa_config_splitting = fnvlist_dup(nvl); 22998ad4d6ddSJeff Bonwick 23003ee8c80cSPavel Zakharov if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { 23013ee8c80cSPavel Zakharov spa_load_failed(spa, "invalid config provided: '%s' missing", 23023ee8c80cSPavel Zakharov ZPOOL_CONFIG_VDEV_TREE); 2303be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 23043ee8c80cSPavel Zakharov } 2305fa9e4066Sahrens 230654d692b7SGeorge Wilson /* 230754d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 230854d692b7SGeorge Wilson */ 23096f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 23106f834bc1SMatthew Ahrens KM_SLEEP); 23116f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 23126f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 23136f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 23146f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 23156f834bc1SMatthew Ahrens } 231654d692b7SGeorge Wilson 2317fa9e4066Sahrens /* 231899653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 231999653d4eSeschrock * value that will be returned by spa_version() since parsing the 232099653d4eSeschrock * configuration requires knowing the version number. 2321fa9e4066Sahrens */ 2322e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2323*6f793812SPavel Zakharov parse = (type == SPA_IMPORT_EXISTING ? 2324*6f793812SPavel Zakharov VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 23251fd3785fSPavel Zakharov error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); 2326e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2327fa9e4066Sahrens 23283ee8c80cSPavel Zakharov if (error != 0) { 23293ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to parse config [error=%d]", 23303ee8c80cSPavel Zakharov error); 23311195e687SMark J Musante return (error); 23323ee8c80cSPavel Zakharov } 2333fa9e4066Sahrens 23340e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 233581cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 233681cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 23371195e687SMark J Musante 23381195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23391195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 23401195e687SMark J Musante } 2341fa9e4066Sahrens 23421fd3785fSPavel Zakharov return (0); 23431fd3785fSPavel Zakharov } 23441fd3785fSPavel Zakharov 2345*6f793812SPavel Zakharov /* 2346*6f793812SPavel Zakharov * Recursively open all vdevs in the vdev tree. This function is called twice: 2347*6f793812SPavel Zakharov * first with the untrusted config, then with the trusted config. 2348*6f793812SPavel Zakharov */ 23491fd3785fSPavel Zakharov static int 23501fd3785fSPavel Zakharov spa_ld_open_vdevs(spa_t *spa) 23511fd3785fSPavel Zakharov { 23521fd3785fSPavel Zakharov int error = 0; 23531fd3785fSPavel Zakharov 2354*6f793812SPavel Zakharov /* 2355*6f793812SPavel Zakharov * spa_missing_tvds_allowed defines how many top-level vdevs can be 2356*6f793812SPavel Zakharov * missing/unopenable for the root vdev to be still considered openable. 2357*6f793812SPavel Zakharov */ 2358*6f793812SPavel Zakharov if (spa->spa_trust_config) { 2359*6f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; 2360*6f793812SPavel Zakharov } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { 2361*6f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; 2362*6f793812SPavel Zakharov } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { 2363*6f793812SPavel Zakharov spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; 2364*6f793812SPavel Zakharov } else { 2365*6f793812SPavel Zakharov spa->spa_missing_tvds_allowed = 0; 2366*6f793812SPavel Zakharov } 2367*6f793812SPavel Zakharov 2368*6f793812SPavel Zakharov spa->spa_missing_tvds_allowed = 2369*6f793812SPavel Zakharov MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); 2370*6f793812SPavel Zakharov 2371e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 23721fd3785fSPavel Zakharov error = vdev_open(spa->spa_root_vdev); 2373e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2374*6f793812SPavel Zakharov 2375*6f793812SPavel Zakharov if (spa->spa_missing_tvds != 0) { 2376*6f793812SPavel Zakharov spa_load_note(spa, "vdev tree has %lld missing top-level " 2377*6f793812SPavel Zakharov "vdevs.", (u_longlong_t)spa->spa_missing_tvds); 2378*6f793812SPavel Zakharov if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { 2379*6f793812SPavel Zakharov /* 2380*6f793812SPavel Zakharov * Although theoretically we could allow users to open 2381*6f793812SPavel Zakharov * incomplete pools in RW mode, we'd need to add a lot 2382*6f793812SPavel Zakharov * of extra logic (e.g. adjust pool space to account 2383*6f793812SPavel Zakharov * for missing vdevs). 2384*6f793812SPavel Zakharov * This limitation also prevents users from accidentally 2385*6f793812SPavel Zakharov * opening the pool in RW mode during data recovery and 2386*6f793812SPavel Zakharov * damaging it further. 2387*6f793812SPavel Zakharov */ 2388*6f793812SPavel Zakharov spa_load_note(spa, "pools with missing top-level " 2389*6f793812SPavel Zakharov "vdevs can only be opened in read-only mode."); 2390*6f793812SPavel Zakharov error = SET_ERROR(ENXIO); 2391*6f793812SPavel Zakharov } else { 2392*6f793812SPavel Zakharov spa_load_note(spa, "current settings allow for maximum " 2393*6f793812SPavel Zakharov "%lld missing top-level vdevs at this stage.", 2394*6f793812SPavel Zakharov (u_longlong_t)spa->spa_missing_tvds_allowed); 2395*6f793812SPavel Zakharov } 2396*6f793812SPavel Zakharov } 23973ee8c80cSPavel Zakharov if (error != 0) { 23983ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to open vdev tree [error=%d]", 23993ee8c80cSPavel Zakharov error); 24003ee8c80cSPavel Zakharov } 2401*6f793812SPavel Zakharov if (spa->spa_missing_tvds != 0 || error != 0) 2402*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); 24031fd3785fSPavel Zakharov 24041fd3785fSPavel Zakharov return (error); 24051fd3785fSPavel Zakharov } 24061fd3785fSPavel Zakharov 2407*6f793812SPavel Zakharov /* 2408*6f793812SPavel Zakharov * We need to validate the vdev labels against the configuration that 2409*6f793812SPavel Zakharov * we have in hand. This function is called twice: first with an untrusted 2410*6f793812SPavel Zakharov * config, then with a trusted config. The validation is more strict when the 2411*6f793812SPavel Zakharov * config is trusted. 2412*6f793812SPavel Zakharov */ 24131fd3785fSPavel Zakharov static int 2414*6f793812SPavel Zakharov spa_ld_validate_vdevs(spa_t *spa) 24151fd3785fSPavel Zakharov { 24161fd3785fSPavel Zakharov int error = 0; 24171fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 2418fa9e4066Sahrens 2419*6f793812SPavel Zakharov spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2420*6f793812SPavel Zakharov error = vdev_validate(rvd); 2421*6f793812SPavel Zakharov spa_config_exit(spa, SCL_ALL, FTAG); 2422560e6e96Seschrock 2423*6f793812SPavel Zakharov if (error != 0) { 2424*6f793812SPavel Zakharov spa_load_failed(spa, "vdev_validate failed [error=%d]", error); 2425*6f793812SPavel Zakharov return (error); 2426*6f793812SPavel Zakharov } 24271195e687SMark J Musante 2428*6f793812SPavel Zakharov if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 2429*6f793812SPavel Zakharov spa_load_failed(spa, "cannot open vdev tree after invalidating " 2430*6f793812SPavel Zakharov "some vdevs"); 2431*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 2432*6f793812SPavel Zakharov return (SET_ERROR(ENXIO)); 2433560e6e96Seschrock } 2434560e6e96Seschrock 24351fd3785fSPavel Zakharov return (0); 24361fd3785fSPavel Zakharov } 24371fd3785fSPavel Zakharov 24381fd3785fSPavel Zakharov static int 2439*6f793812SPavel Zakharov spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) 24401fd3785fSPavel Zakharov { 24411fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 24421fd3785fSPavel Zakharov nvlist_t *label; 24431fd3785fSPavel Zakharov uberblock_t *ub = &spa->spa_uberblock; 24441fd3785fSPavel Zakharov 2445fa9e4066Sahrens /* 2446fa9e4066Sahrens * Find the best uberblock. 2447fa9e4066Sahrens */ 2448ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2449fa9e4066Sahrens 2450fa9e4066Sahrens /* 2451fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2452fa9e4066Sahrens */ 2453ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2454ad135b5dSChristopher Siden nvlist_free(label); 24553ee8c80cSPavel Zakharov spa_load_failed(spa, "no valid uberblock found"); 24561195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2457ad135b5dSChristopher Siden } 2458ea8dc4b6Seschrock 24593ee8c80cSPavel Zakharov spa_load_note(spa, "using uberblock with txg=%llu", 24603ee8c80cSPavel Zakharov (u_longlong_t)ub->ub_txg); 24613ee8c80cSPavel Zakharov 2462ea8dc4b6Seschrock /* 2463ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2464ea8dc4b6Seschrock */ 2465ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2466ad135b5dSChristopher Siden nvlist_free(label); 24673ee8c80cSPavel Zakharov spa_load_failed(spa, "version %llu is not supported", 24683ee8c80cSPavel Zakharov (u_longlong_t)ub->ub_version); 24691195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2470ad135b5dSChristopher Siden } 2471ad135b5dSChristopher Siden 2472ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2473ad135b5dSChristopher Siden nvlist_t *features; 2474ad135b5dSChristopher Siden 2475ad135b5dSChristopher Siden /* 2476ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2477ad135b5dSChristopher Siden * MOS in the label, return failure. 2478ad135b5dSChristopher Siden */ 24793ee8c80cSPavel Zakharov if (label == NULL) { 24803ee8c80cSPavel Zakharov spa_load_failed(spa, "label config unavailable"); 24813ee8c80cSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 24823ee8c80cSPavel Zakharov ENXIO)); 24833ee8c80cSPavel Zakharov } 24843ee8c80cSPavel Zakharov 24853ee8c80cSPavel Zakharov if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, 24863ee8c80cSPavel Zakharov &features) != 0) { 2487ad135b5dSChristopher Siden nvlist_free(label); 24883ee8c80cSPavel Zakharov spa_load_failed(spa, "invalid label: '%s' missing", 24893ee8c80cSPavel Zakharov ZPOOL_CONFIG_FEATURES_FOR_READ); 2490ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2491ad135b5dSChristopher Siden ENXIO)); 2492ad135b5dSChristopher Siden } 2493ad135b5dSChristopher Siden 2494ad135b5dSChristopher Siden /* 2495ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2496ad135b5dSChristopher Siden * from the label. 2497ad135b5dSChristopher Siden */ 2498ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2499ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2500ad135b5dSChristopher Siden } 2501ad135b5dSChristopher Siden 2502ad135b5dSChristopher Siden nvlist_free(label); 2503ad135b5dSChristopher Siden 2504ad135b5dSChristopher Siden /* 2505ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2506ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2507ad135b5dSChristopher Siden * cannot open a pool. 2508ad135b5dSChristopher Siden */ 2509ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2510ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2511ad135b5dSChristopher Siden 2512ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2513ad135b5dSChristopher Siden 0); 2514ad135b5dSChristopher Siden 2515ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2516ad135b5dSChristopher Siden NULL); nvp != NULL; 2517ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2518ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2519ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2520ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2521ad135b5dSChristopher Siden } 2522ad135b5dSChristopher Siden } 2523ad135b5dSChristopher Siden 2524ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2525ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2526ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2527ad135b5dSChristopher Siden nvlist_free(unsup_feat); 25283ee8c80cSPavel Zakharov spa_load_failed(spa, "some features are unsupported"); 2529ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2530ad135b5dSChristopher Siden ENOTSUP)); 2531ad135b5dSChristopher Siden } 2532ad135b5dSChristopher Siden 2533ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2534ad135b5dSChristopher Siden } 2535fa9e4066Sahrens 25361195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 25371195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2538*6f793812SPavel Zakharov spa_try_repair(spa, spa->spa_config); 25391195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 25401195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 25411195e687SMark J Musante spa->spa_config_splitting = NULL; 2542fa9e4066Sahrens } 2543fa9e4066Sahrens 2544fa9e4066Sahrens /* 2545fa9e4066Sahrens * Initialize internal SPA structures. 2546fa9e4066Sahrens */ 2547fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2548fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2549468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2550c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2551468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2552468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2553b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 25543f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2555b24ab676SJeff Bonwick 25561fd3785fSPavel Zakharov return (0); 25571fd3785fSPavel Zakharov } 25581fd3785fSPavel Zakharov 25591fd3785fSPavel Zakharov static int 25601fd3785fSPavel Zakharov spa_ld_open_rootbp(spa_t *spa) 25611fd3785fSPavel Zakharov { 25621fd3785fSPavel Zakharov int error = 0; 25631fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 25645cabbc6bSPrashanth Sreenivasa 2565ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 25663ee8c80cSPavel Zakharov if (error != 0) { 25673ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " 25683ee8c80cSPavel Zakharov "[error=%d]", error); 25691195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 25703ee8c80cSPavel Zakharov } 2571fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2572fa9e4066Sahrens 25731fd3785fSPavel Zakharov return (0); 25741fd3785fSPavel Zakharov } 25751fd3785fSPavel Zakharov 25761fd3785fSPavel Zakharov static int 2577*6f793812SPavel Zakharov spa_ld_load_trusted_config(spa_t *spa, spa_import_type_t type, 2578*6f793812SPavel Zakharov boolean_t reloading) 25791fd3785fSPavel Zakharov { 2580*6f793812SPavel Zakharov vdev_t *mrvd, *rvd = spa->spa_root_vdev; 2581*6f793812SPavel Zakharov nvlist_t *nv, *mos_config, *policy; 2582*6f793812SPavel Zakharov int error = 0, copy_error; 2583*6f793812SPavel Zakharov uint64_t healthy_tvds, healthy_tvds_mos; 2584*6f793812SPavel Zakharov uint64_t mos_config_txg; 25851fd3785fSPavel Zakharov 25863ee8c80cSPavel Zakharov if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) 25873ee8c80cSPavel Zakharov != 0) 25881195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2589fa9e4066Sahrens 25905cabbc6bSPrashanth Sreenivasa /* 2591*6f793812SPavel Zakharov * If we're assembling a pool from a split, the config provided is 2592*6f793812SPavel Zakharov * already trusted so there is nothing to do. 25935cabbc6bSPrashanth Sreenivasa */ 2594*6f793812SPavel Zakharov if (type == SPA_IMPORT_ASSEMBLE) 2595*6f793812SPavel Zakharov return (0); 25965cabbc6bSPrashanth Sreenivasa 2597*6f793812SPavel Zakharov healthy_tvds = spa_healthy_core_tvds(spa); 2598*6f793812SPavel Zakharov 2599*6f793812SPavel Zakharov if (load_nvlist(spa, spa->spa_config_object, &mos_config) 2600*6f793812SPavel Zakharov != 0) { 2601*6f793812SPavel Zakharov spa_load_failed(spa, "unable to retrieve MOS config"); 2602*6f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2603*6f793812SPavel Zakharov } 2604*6f793812SPavel Zakharov 2605*6f793812SPavel Zakharov /* 2606*6f793812SPavel Zakharov * If we are doing an open, pool owner wasn't verified yet, thus do 2607*6f793812SPavel Zakharov * the verification here. 2608*6f793812SPavel Zakharov */ 2609*6f793812SPavel Zakharov if (spa->spa_load_state == SPA_LOAD_OPEN) { 2610*6f793812SPavel Zakharov error = spa_verify_host(spa, mos_config); 2611*6f793812SPavel Zakharov if (error != 0) { 26125cabbc6bSPrashanth Sreenivasa nvlist_free(mos_config); 2613*6f793812SPavel Zakharov return (error); 26145cabbc6bSPrashanth Sreenivasa } 2615*6f793812SPavel Zakharov } 2616*6f793812SPavel Zakharov 2617*6f793812SPavel Zakharov nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); 2618*6f793812SPavel Zakharov 2619*6f793812SPavel Zakharov spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 26205cabbc6bSPrashanth Sreenivasa 2621*6f793812SPavel Zakharov /* 2622*6f793812SPavel Zakharov * Build a new vdev tree from the trusted config 2623*6f793812SPavel Zakharov */ 2624*6f793812SPavel Zakharov VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 2625*6f793812SPavel Zakharov 2626*6f793812SPavel Zakharov /* 2627*6f793812SPavel Zakharov * Vdev paths in the MOS may be obsolete. If the untrusted config was 2628*6f793812SPavel Zakharov * obtained by scanning /dev/dsk, then it will have the right vdev 2629*6f793812SPavel Zakharov * paths. We update the trusted MOS config with this information. 2630*6f793812SPavel Zakharov * We first try to copy the paths with vdev_copy_path_strict, which 2631*6f793812SPavel Zakharov * succeeds only when both configs have exactly the same vdev tree. 2632*6f793812SPavel Zakharov * If that fails, we fall back to a more flexible method that has a 2633*6f793812SPavel Zakharov * best effort policy. 2634*6f793812SPavel Zakharov */ 2635*6f793812SPavel Zakharov copy_error = vdev_copy_path_strict(rvd, mrvd); 2636*6f793812SPavel Zakharov if (copy_error != 0 || spa_load_print_vdev_tree) { 2637*6f793812SPavel Zakharov spa_load_note(spa, "provided vdev tree:"); 2638*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 2639*6f793812SPavel Zakharov spa_load_note(spa, "MOS vdev tree:"); 2640*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(mrvd, 2); 2641*6f793812SPavel Zakharov } 2642*6f793812SPavel Zakharov if (copy_error != 0) { 2643*6f793812SPavel Zakharov spa_load_note(spa, "vdev_copy_path_strict failed, falling " 2644*6f793812SPavel Zakharov "back to vdev_copy_path_relaxed"); 2645*6f793812SPavel Zakharov vdev_copy_path_relaxed(rvd, mrvd); 2646*6f793812SPavel Zakharov } 2647*6f793812SPavel Zakharov 2648*6f793812SPavel Zakharov vdev_close(rvd); 2649*6f793812SPavel Zakharov vdev_free(rvd); 2650*6f793812SPavel Zakharov spa->spa_root_vdev = mrvd; 2651*6f793812SPavel Zakharov rvd = mrvd; 2652*6f793812SPavel Zakharov spa_config_exit(spa, SCL_ALL, FTAG); 2653*6f793812SPavel Zakharov 2654*6f793812SPavel Zakharov /* 2655*6f793812SPavel Zakharov * We will use spa_config if we decide to reload the spa or if spa_load 2656*6f793812SPavel Zakharov * fails and we rewind. We must thus regenerate the config using the 2657*6f793812SPavel Zakharov * MOS information with the updated paths. Rewind policy is an import 2658*6f793812SPavel Zakharov * setting and is not in the MOS. We copy it over to our new, trusted 2659*6f793812SPavel Zakharov * config. 2660*6f793812SPavel Zakharov */ 2661*6f793812SPavel Zakharov mos_config_txg = fnvlist_lookup_uint64(mos_config, 2662*6f793812SPavel Zakharov ZPOOL_CONFIG_POOL_TXG); 2663*6f793812SPavel Zakharov nvlist_free(mos_config); 2664*6f793812SPavel Zakharov mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); 2665*6f793812SPavel Zakharov if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_REWIND_POLICY, 2666*6f793812SPavel Zakharov &policy) == 0) 2667*6f793812SPavel Zakharov fnvlist_add_nvlist(mos_config, ZPOOL_REWIND_POLICY, policy); 2668*6f793812SPavel Zakharov spa_config_set(spa, mos_config); 2669*6f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_MOS; 2670*6f793812SPavel Zakharov 2671*6f793812SPavel Zakharov /* 2672*6f793812SPavel Zakharov * Now that we got the config from the MOS, we should be more strict 2673*6f793812SPavel Zakharov * in checking blkptrs and can make assumptions about the consistency 2674*6f793812SPavel Zakharov * of the vdev tree. spa_trust_config must be set to true before opening 2675*6f793812SPavel Zakharov * vdevs in order for them to be writeable. 2676*6f793812SPavel Zakharov */ 2677*6f793812SPavel Zakharov spa->spa_trust_config = B_TRUE; 2678*6f793812SPavel Zakharov 2679*6f793812SPavel Zakharov /* 2680*6f793812SPavel Zakharov * Open and validate the new vdev tree 2681*6f793812SPavel Zakharov */ 2682*6f793812SPavel Zakharov error = spa_ld_open_vdevs(spa); 2683*6f793812SPavel Zakharov if (error != 0) 2684*6f793812SPavel Zakharov return (error); 2685*6f793812SPavel Zakharov 2686*6f793812SPavel Zakharov error = spa_ld_validate_vdevs(spa); 2687*6f793812SPavel Zakharov if (error != 0) 2688*6f793812SPavel Zakharov return (error); 2689*6f793812SPavel Zakharov 2690*6f793812SPavel Zakharov if (copy_error != 0 || spa_load_print_vdev_tree) { 2691*6f793812SPavel Zakharov spa_load_note(spa, "final vdev tree:"); 2692*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 2693*6f793812SPavel Zakharov } 2694*6f793812SPavel Zakharov 2695*6f793812SPavel Zakharov if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && 2696*6f793812SPavel Zakharov !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { 26975cabbc6bSPrashanth Sreenivasa /* 2698*6f793812SPavel Zakharov * Sanity check to make sure that we are indeed loading the 2699*6f793812SPavel Zakharov * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds 2700*6f793812SPavel Zakharov * in the config provided and they happened to be the only ones 2701*6f793812SPavel Zakharov * to have the latest uberblock, we could involuntarily perform 2702*6f793812SPavel Zakharov * an extreme rewind. 27035cabbc6bSPrashanth Sreenivasa */ 2704*6f793812SPavel Zakharov healthy_tvds_mos = spa_healthy_core_tvds(spa); 2705*6f793812SPavel Zakharov if (healthy_tvds_mos - healthy_tvds >= 2706*6f793812SPavel Zakharov SPA_SYNC_MIN_VDEVS) { 2707*6f793812SPavel Zakharov spa_load_note(spa, "config provided misses too many " 2708*6f793812SPavel Zakharov "top-level vdevs compared to MOS (%lld vs %lld). ", 2709*6f793812SPavel Zakharov (u_longlong_t)healthy_tvds, 2710*6f793812SPavel Zakharov (u_longlong_t)healthy_tvds_mos); 2711*6f793812SPavel Zakharov spa_load_note(spa, "vdev tree:"); 2712*6f793812SPavel Zakharov vdev_dbgmsg_print_tree(rvd, 2); 2713*6f793812SPavel Zakharov if (reloading) { 2714*6f793812SPavel Zakharov spa_load_failed(spa, "config was already " 2715*6f793812SPavel Zakharov "provided from MOS. Aborting."); 2716*6f793812SPavel Zakharov return (spa_vdev_err(rvd, 2717*6f793812SPavel Zakharov VDEV_AUX_CORRUPT_DATA, EIO)); 2718*6f793812SPavel Zakharov } 2719*6f793812SPavel Zakharov spa_load_note(spa, "spa must be reloaded using MOS " 2720*6f793812SPavel Zakharov "config"); 2721*6f793812SPavel Zakharov return (SET_ERROR(EAGAIN)); 27223ee8c80cSPavel Zakharov } 27235cabbc6bSPrashanth Sreenivasa } 27245cabbc6bSPrashanth Sreenivasa 2725*6f793812SPavel Zakharov error = spa_check_for_missing_logs(spa); 2726*6f793812SPavel Zakharov if (error != 0) 2727*6f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 2728*6f793812SPavel Zakharov 2729*6f793812SPavel Zakharov if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { 2730*6f793812SPavel Zakharov spa_load_failed(spa, "uberblock guid sum doesn't match MOS " 2731*6f793812SPavel Zakharov "guid sum (%llu != %llu)", 2732*6f793812SPavel Zakharov (u_longlong_t)spa->spa_uberblock.ub_guid_sum, 2733*6f793812SPavel Zakharov (u_longlong_t)rvd->vdev_guid_sum); 2734*6f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 2735*6f793812SPavel Zakharov ENXIO)); 2736*6f793812SPavel Zakharov } 2737*6f793812SPavel Zakharov 27381fd3785fSPavel Zakharov return (0); 27391fd3785fSPavel Zakharov } 27401fd3785fSPavel Zakharov 27411fd3785fSPavel Zakharov static int 27421fd3785fSPavel Zakharov spa_ld_open_indirect_vdev_metadata(spa_t *spa) 27431fd3785fSPavel Zakharov { 27441fd3785fSPavel Zakharov int error = 0; 27451fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 27461fd3785fSPavel Zakharov 27475cabbc6bSPrashanth Sreenivasa /* 27485cabbc6bSPrashanth Sreenivasa * Everything that we read before spa_remove_init() must be stored 27495cabbc6bSPrashanth Sreenivasa * on concreted vdevs. Therefore we do this as early as possible. 27505cabbc6bSPrashanth Sreenivasa */ 27513ee8c80cSPavel Zakharov error = spa_remove_init(spa); 27523ee8c80cSPavel Zakharov if (error != 0) { 27533ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_remove_init failed [error=%d]", 27543ee8c80cSPavel Zakharov error); 27555cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27563ee8c80cSPavel Zakharov } 27575cabbc6bSPrashanth Sreenivasa 27581fd3785fSPavel Zakharov /* 27591fd3785fSPavel Zakharov * Retrieve information needed to condense indirect vdev mappings. 27601fd3785fSPavel Zakharov */ 27611fd3785fSPavel Zakharov error = spa_condense_init(spa); 27621fd3785fSPavel Zakharov if (error != 0) { 27633ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_condense_init failed [error=%d]", 27643ee8c80cSPavel Zakharov error); 27651fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 27661fd3785fSPavel Zakharov } 27671fd3785fSPavel Zakharov 27681fd3785fSPavel Zakharov return (0); 27691fd3785fSPavel Zakharov } 27701fd3785fSPavel Zakharov 27711fd3785fSPavel Zakharov static int 27723ee8c80cSPavel Zakharov spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) 27731fd3785fSPavel Zakharov { 27741fd3785fSPavel Zakharov int error = 0; 27751fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 27761fd3785fSPavel Zakharov 2777ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2778ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 277957221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2780ad135b5dSChristopher Siden 2781ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 27823ee8c80cSPavel Zakharov &spa->spa_feat_for_read_obj, B_TRUE) != 0) { 2783ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2784ad135b5dSChristopher Siden } 2785ad135b5dSChristopher Siden 2786ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 27873ee8c80cSPavel Zakharov &spa->spa_feat_for_write_obj, B_TRUE) != 0) { 2788ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2789ad135b5dSChristopher Siden } 2790ad135b5dSChristopher Siden 2791ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 27923ee8c80cSPavel Zakharov &spa->spa_feat_desc_obj, B_TRUE) != 0) { 2793ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2794ad135b5dSChristopher Siden } 2795ad135b5dSChristopher Siden 279657221772SChristopher Siden enabled_feat = fnvlist_alloc(); 279757221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2798ad135b5dSChristopher Siden 27992acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 280057221772SChristopher Siden unsup_feat, enabled_feat)) 2801ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2802ad135b5dSChristopher Siden 28033ee8c80cSPavel Zakharov if (spa_writeable(spa) || 28043ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_TRYIMPORT) { 28052acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 280657221772SChristopher Siden unsup_feat, enabled_feat)) { 28071fd3785fSPavel Zakharov *missing_feat_writep = B_TRUE; 280857221772SChristopher Siden } 2809ad135b5dSChristopher Siden } 2810ad135b5dSChristopher Siden 281157221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 281257221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 281357221772SChristopher Siden 2814ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 281557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 281657221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2817ad135b5dSChristopher Siden } 2818ad135b5dSChristopher Siden 281957221772SChristopher Siden fnvlist_free(enabled_feat); 282057221772SChristopher Siden fnvlist_free(unsup_feat); 2821ad135b5dSChristopher Siden 2822ad135b5dSChristopher Siden if (!missing_feat_read) { 2823ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2824ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2825ad135b5dSChristopher Siden } 2826ad135b5dSChristopher Siden 2827ad135b5dSChristopher Siden /* 2828ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2829ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2830ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2831ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2832ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2833ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2834ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2835ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2836ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2837ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2838ad135b5dSChristopher Siden * available for open in read-only mode. 2839ad135b5dSChristopher Siden * 2840ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2841ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2842ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2843ad135b5dSChristopher Siden * userland in order to know whether to display the 2844ad135b5dSChristopher Siden * abovementioned note. 2845ad135b5dSChristopher Siden */ 28461fd3785fSPavel Zakharov if (missing_feat_read || (*missing_feat_writep && 2847ad135b5dSChristopher Siden spa_writeable(spa))) { 28483ee8c80cSPavel Zakharov spa_load_failed(spa, "pool uses unsupported features"); 2849ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2850ad135b5dSChristopher Siden ENOTSUP)); 2851ad135b5dSChristopher Siden } 285243466aaeSMax Grossman 285343466aaeSMax Grossman /* 285443466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 285543466aaeSMax Grossman * cache during SPA initialization. 285643466aaeSMax Grossman */ 285743466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 285843466aaeSMax Grossman uint64_t refcount; 285943466aaeSMax Grossman 286043466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 286143466aaeSMax Grossman &spa_feature_table[i], &refcount); 286243466aaeSMax Grossman if (error == 0) { 286343466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 286443466aaeSMax Grossman } else if (error == ENOTSUP) { 286543466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 286643466aaeSMax Grossman SPA_FEATURE_DISABLED; 286743466aaeSMax Grossman } else { 28683ee8c80cSPavel Zakharov spa_load_failed(spa, "error getting refcount " 28693ee8c80cSPavel Zakharov "for feature %s [error=%d]", 28703ee8c80cSPavel Zakharov spa_feature_table[i].fi_guid, error); 287143466aaeSMax Grossman return (spa_vdev_err(rvd, 287243466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 287343466aaeSMax Grossman } 287443466aaeSMax Grossman } 287543466aaeSMax Grossman } 287643466aaeSMax Grossman 287743466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 287843466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 28793ee8c80cSPavel Zakharov &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) 288043466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2881ad135b5dSChristopher Siden } 2882ad135b5dSChristopher Siden 28831fd3785fSPavel Zakharov return (0); 28841fd3785fSPavel Zakharov } 28851fd3785fSPavel Zakharov 28861fd3785fSPavel Zakharov static int 28871fd3785fSPavel Zakharov spa_ld_load_special_directories(spa_t *spa) 28881fd3785fSPavel Zakharov { 28891fd3785fSPavel Zakharov int error = 0; 28901fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 28911fd3785fSPavel Zakharov 2892ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2893ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2894ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 28953ee8c80cSPavel Zakharov if (error != 0) { 28963ee8c80cSPavel Zakharov spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); 2897ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 28983ee8c80cSPavel Zakharov } 2899ad135b5dSChristopher Siden 29001fd3785fSPavel Zakharov return (0); 29011fd3785fSPavel Zakharov } 2902871a9500SMark J Musante 29031fd3785fSPavel Zakharov static int 29041fd3785fSPavel Zakharov spa_ld_get_props(spa_t *spa) 29051fd3785fSPavel Zakharov { 29061fd3785fSPavel Zakharov int error = 0; 29071fd3785fSPavel Zakharov uint64_t obj; 29081fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 2909fa9e4066Sahrens 291045818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 291145818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 291245818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 291345818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 291445818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 291545818ee1SMatthew Ahrens if (error == ENOENT) { 291645818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 291745818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 291845818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 291945818ee1SMatthew Ahrens } else if (error != 0) { 29203ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to retrieve checksum salt from " 29213ee8c80cSPavel Zakharov "MOS [error=%d]", error); 292245818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 292345818ee1SMatthew Ahrens } 292445818ee1SMatthew Ahrens 29253ee8c80cSPavel Zakharov if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) 2926cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2927cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 29283ee8c80cSPavel Zakharov if (error != 0) { 29293ee8c80cSPavel Zakharov spa_load_failed(spa, "error opening deferred-frees bpobj " 29303ee8c80cSPavel Zakharov "[error=%d]", error); 29311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 29323ee8c80cSPavel Zakharov } 2933fa9e4066Sahrens 293499653d4eSeschrock /* 293599653d4eSeschrock * Load the bit that tells us to use the new accounting function 293699653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 293799653d4eSeschrock * be present. 293899653d4eSeschrock */ 29393ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); 29401195e687SMark J Musante if (error != 0 && error != ENOENT) 29411195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 294299653d4eSeschrock 29433f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 29443ee8c80cSPavel Zakharov &spa->spa_creation_version, B_FALSE); 29453f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 29463f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 29473f9d6ad7SLin Ling 2948fa9e4066Sahrens /* 2949ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2950ea8dc4b6Seschrock * not be present. 2951fa9e4066Sahrens */ 29523ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, 29533ee8c80cSPavel Zakharov B_FALSE); 29541195e687SMark J Musante if (error != 0 && error != ENOENT) 29551195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2956ea8dc4b6Seschrock 29571195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 29583ee8c80cSPavel Zakharov &spa->spa_errlog_scrub, B_FALSE); 29591195e687SMark J Musante if (error != 0 && error != ENOENT) 29601195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2961ea8dc4b6Seschrock 296206eeb2adSek /* 296306eeb2adSek * Load the history object. If we have an older pool, this 296406eeb2adSek * will not be present. 296506eeb2adSek */ 29663ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); 29671195e687SMark J Musante if (error != 0 && error != ENOENT) 29681195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 29691195e687SMark J Musante 2970215198a6SJoe Stein /* 2971215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2972215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2973215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2974215198a6SJoe Stein * spa_sync_config_object. 2975215198a6SJoe Stein */ 2976215198a6SJoe Stein 2977215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2978215198a6SJoe Stein nvlist_t *mos_config; 29793ee8c80cSPavel Zakharov if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { 29803ee8c80cSPavel Zakharov spa_load_failed(spa, "unable to retrieve MOS config"); 2981215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 29823ee8c80cSPavel Zakharov } 2983215198a6SJoe Stein 2984215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 29853ee8c80cSPavel Zakharov &spa->spa_all_vdev_zaps, B_FALSE); 2986215198a6SJoe Stein 2987555da511SPaul Dagnelie if (error == ENOENT) { 2988555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 2989555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 2990555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 2991555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2992555da511SPaul Dagnelie } else if (error != 0) { 2993215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2994555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2995215198a6SJoe Stein /* 2996215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2997215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2998215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2999215198a6SJoe Stein */ 3000215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 3001215198a6SJoe Stein /* 3002215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 3003215198a6SJoe Stein * before this. Better be sure of it. 3004215198a6SJoe Stein */ 3005215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 3006215198a6SJoe Stein } 3007215198a6SJoe Stein nvlist_free(mos_config); 3008215198a6SJoe Stein 30091fd3785fSPavel Zakharov spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 30101fd3785fSPavel Zakharov 30113ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, 30123ee8c80cSPavel Zakharov B_FALSE); 30131fd3785fSPavel Zakharov if (error && error != ENOENT) 30141fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30151fd3785fSPavel Zakharov 30161fd3785fSPavel Zakharov if (error == 0) { 30171fd3785fSPavel Zakharov uint64_t autoreplace; 30181fd3785fSPavel Zakharov 30191fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 30201fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 30211fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 30221fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 30231fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 30241fd3785fSPavel Zakharov spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 30251fd3785fSPavel Zakharov &spa->spa_dedup_ditto); 30261fd3785fSPavel Zakharov 30271fd3785fSPavel Zakharov spa->spa_autoreplace = (autoreplace != 0); 30281fd3785fSPavel Zakharov } 30291fd3785fSPavel Zakharov 3030*6f793812SPavel Zakharov /* 3031*6f793812SPavel Zakharov * If we are importing a pool with missing top-level vdevs, 3032*6f793812SPavel Zakharov * we enforce that the pool doesn't panic or get suspended on 3033*6f793812SPavel Zakharov * error since the likelihood of missing data is extremely high. 3034*6f793812SPavel Zakharov */ 3035*6f793812SPavel Zakharov if (spa->spa_missing_tvds > 0 && 3036*6f793812SPavel Zakharov spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && 3037*6f793812SPavel Zakharov spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 3038*6f793812SPavel Zakharov spa_load_note(spa, "forcing failmode to 'continue' " 3039*6f793812SPavel Zakharov "as some top level vdevs are missing"); 3040*6f793812SPavel Zakharov spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; 3041*6f793812SPavel Zakharov } 3042*6f793812SPavel Zakharov 30431fd3785fSPavel Zakharov return (0); 30441fd3785fSPavel Zakharov } 30451fd3785fSPavel Zakharov 30461fd3785fSPavel Zakharov static int 30471fd3785fSPavel Zakharov spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) 30481fd3785fSPavel Zakharov { 30491fd3785fSPavel Zakharov int error = 0; 30501fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 30511fd3785fSPavel Zakharov 30521195e687SMark J Musante /* 30531195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 30541195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 30551195e687SMark J Musante * devices. 30561195e687SMark J Musante */ 305706eeb2adSek 305899653d4eSeschrock /* 305999653d4eSeschrock * Load any hot spares for this pool. 306099653d4eSeschrock */ 30613ee8c80cSPavel Zakharov error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, 30623ee8c80cSPavel Zakharov B_FALSE); 30631195e687SMark J Musante if (error != 0 && error != ENOENT) 30641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30651195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3066e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 3067fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 30683ee8c80cSPavel Zakharov &spa->spa_spares.sav_config) != 0) { 30693ee8c80cSPavel Zakharov spa_load_failed(spa, "error loading spares nvlist"); 30701195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30713ee8c80cSPavel Zakharov } 307299653d4eSeschrock 3073e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 307499653d4eSeschrock spa_load_spares(spa); 3075e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 30761195e687SMark J Musante } else if (error == 0) { 30771195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 307899653d4eSeschrock } 307999653d4eSeschrock 3080fa94a07fSbrendan /* 3081fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 3082fa94a07fSbrendan */ 30831195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 30843ee8c80cSPavel Zakharov &spa->spa_l2cache.sav_object, B_FALSE); 30851195e687SMark J Musante if (error != 0 && error != ENOENT) 30861195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30871195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3088fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 3089fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 30903ee8c80cSPavel Zakharov &spa->spa_l2cache.sav_config) != 0) { 30913ee8c80cSPavel Zakharov spa_load_failed(spa, "error loading l2cache nvlist"); 30921195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 30933ee8c80cSPavel Zakharov } 3094fa94a07fSbrendan 3095e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3096fa94a07fSbrendan spa_load_l2cache(spa); 3097e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 30981195e687SMark J Musante } else if (error == 0) { 30991195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 3100fa94a07fSbrendan } 3101fa94a07fSbrendan 31021fd3785fSPavel Zakharov return (0); 31031fd3785fSPavel Zakharov } 31041195e687SMark J Musante 31051fd3785fSPavel Zakharov static int 31063ee8c80cSPavel Zakharov spa_ld_load_vdev_metadata(spa_t *spa) 31071fd3785fSPavel Zakharov { 31081fd3785fSPavel Zakharov int error = 0; 31091fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 3110b1b8ab34Slling 31113d7072f8Seschrock /* 31123d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 31133d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 31143d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 31153d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 31163d7072f8Seschrock * over. 31173d7072f8Seschrock */ 31183ee8c80cSPavel Zakharov if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 31193d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 3120b693757aSEric Schrock /* 3121b693757aSEric Schrock * For the import case, this is done in spa_import(), because 3122b693757aSEric Schrock * at this point we're using the spare definitions from 3123b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 3124b693757aSEric Schrock */ 31253ee8c80cSPavel Zakharov if (spa->spa_load_state != SPA_LOAD_IMPORT) { 3126b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3127b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3128b693757aSEric Schrock } 3129b693757aSEric Schrock } 31303d7072f8Seschrock 3131ea8dc4b6Seschrock /* 31321fd3785fSPavel Zakharov * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. 3133ea8dc4b6Seschrock */ 31345cabbc6bSPrashanth Sreenivasa error = vdev_load(rvd); 31355cabbc6bSPrashanth Sreenivasa if (error != 0) { 31363ee8c80cSPavel Zakharov spa_load_failed(spa, "vdev_load failed [error=%d]", error); 31375cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 31385cabbc6bSPrashanth Sreenivasa } 31395cabbc6bSPrashanth Sreenivasa 3140fa9e4066Sahrens /* 31411fd3785fSPavel Zakharov * Propagate the leaf DTLs we just loaded all the way up the vdev tree. 3142fa9e4066Sahrens */ 3143e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3144fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 3145e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3146fa9e4066Sahrens 31471fd3785fSPavel Zakharov return (0); 31481fd3785fSPavel Zakharov } 31491fd3785fSPavel Zakharov 31501fd3785fSPavel Zakharov static int 31511fd3785fSPavel Zakharov spa_ld_load_dedup_tables(spa_t *spa) 31521fd3785fSPavel Zakharov { 31531fd3785fSPavel Zakharov int error = 0; 31541fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 31551fd3785fSPavel Zakharov 3156b24ab676SJeff Bonwick error = ddt_load(spa); 31573ee8c80cSPavel Zakharov if (error != 0) { 31583ee8c80cSPavel Zakharov spa_load_failed(spa, "ddt_load failed [error=%d]", error); 31591195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 31603ee8c80cSPavel Zakharov } 3161b24ab676SJeff Bonwick 31621fd3785fSPavel Zakharov return (0); 31631fd3785fSPavel Zakharov } 31641fd3785fSPavel Zakharov 31651fd3785fSPavel Zakharov static int 31661fd3785fSPavel Zakharov spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) 31671fd3785fSPavel Zakharov { 31681fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 3169485bbbf5SGeorge Wilson 31703ee8c80cSPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { 31713ee8c80cSPavel Zakharov boolean_t missing = spa_check_logs(spa); 31723ee8c80cSPavel Zakharov if (missing) { 3173*6f793812SPavel Zakharov if (spa->spa_missing_tvds != 0) { 3174*6f793812SPavel Zakharov spa_load_note(spa, "spa_check_logs failed " 3175*6f793812SPavel Zakharov "so dropping the logs"); 3176*6f793812SPavel Zakharov } else { 3177*6f793812SPavel Zakharov *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 3178*6f793812SPavel Zakharov spa_load_failed(spa, "spa_check_logs failed"); 3179*6f793812SPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, 3180*6f793812SPavel Zakharov ENXIO)); 3181*6f793812SPavel Zakharov } 31823ee8c80cSPavel Zakharov } 3183b24ab676SJeff Bonwick } 3184b24ab676SJeff Bonwick 31851fd3785fSPavel Zakharov return (0); 31861fd3785fSPavel Zakharov } 31871fd3785fSPavel Zakharov 31881fd3785fSPavel Zakharov static int 31893ee8c80cSPavel Zakharov spa_ld_verify_pool_data(spa_t *spa) 31901fd3785fSPavel Zakharov { 31911fd3785fSPavel Zakharov int error = 0; 31921fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 31931fd3785fSPavel Zakharov 31941fd3785fSPavel Zakharov /* 31951fd3785fSPavel Zakharov * We've successfully opened the pool, verify that we're ready 31961fd3785fSPavel Zakharov * to start pushing transactions. 31971fd3785fSPavel Zakharov */ 31983ee8c80cSPavel Zakharov if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 31991fd3785fSPavel Zakharov error = spa_load_verify(spa); 32001fd3785fSPavel Zakharov if (error != 0) { 32013ee8c80cSPavel Zakharov spa_load_failed(spa, "spa_load_verify failed " 32023ee8c80cSPavel Zakharov "[error=%d]", error); 32031fd3785fSPavel Zakharov return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 32041fd3785fSPavel Zakharov error)); 32051fd3785fSPavel Zakharov } 32061fd3785fSPavel Zakharov } 32071fd3785fSPavel Zakharov 32081fd3785fSPavel Zakharov return (0); 32091fd3785fSPavel Zakharov } 32101fd3785fSPavel Zakharov 32111fd3785fSPavel Zakharov static void 32121fd3785fSPavel Zakharov spa_ld_claim_log_blocks(spa_t *spa) 32131fd3785fSPavel Zakharov { 32141fd3785fSPavel Zakharov dmu_tx_t *tx; 32151fd3785fSPavel Zakharov dsl_pool_t *dp = spa_get_dsl(spa); 32161fd3785fSPavel Zakharov 32171fd3785fSPavel Zakharov /* 32181fd3785fSPavel Zakharov * Claim log blocks that haven't been committed yet. 32191fd3785fSPavel Zakharov * This must all happen in a single txg. 32201fd3785fSPavel Zakharov * Note: spa_claim_max_txg is updated by spa_claim_notify(), 32211fd3785fSPavel Zakharov * invoked from zil_claim_log_block()'s i/o done callback. 32221fd3785fSPavel Zakharov * Price of rollback is that we abandon the log. 32231fd3785fSPavel Zakharov */ 32241fd3785fSPavel Zakharov spa->spa_claiming = B_TRUE; 32251fd3785fSPavel Zakharov 32261fd3785fSPavel Zakharov tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 32271fd3785fSPavel Zakharov (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 32281fd3785fSPavel Zakharov zil_claim, tx, DS_FIND_CHILDREN); 32291fd3785fSPavel Zakharov dmu_tx_commit(tx); 32301fd3785fSPavel Zakharov 32311fd3785fSPavel Zakharov spa->spa_claiming = B_FALSE; 32321fd3785fSPavel Zakharov 32331fd3785fSPavel Zakharov spa_set_log_state(spa, SPA_LOG_GOOD); 32341fd3785fSPavel Zakharov } 32351fd3785fSPavel Zakharov 32361fd3785fSPavel Zakharov static void 3237*6f793812SPavel Zakharov spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, 3238*6f793812SPavel Zakharov boolean_t reloading) 32391fd3785fSPavel Zakharov { 32401fd3785fSPavel Zakharov vdev_t *rvd = spa->spa_root_vdev; 32411fd3785fSPavel Zakharov int need_update = B_FALSE; 32421fd3785fSPavel Zakharov 32431fd3785fSPavel Zakharov /* 32441fd3785fSPavel Zakharov * If the config cache is stale, or we have uninitialized 32451fd3785fSPavel Zakharov * metaslabs (see spa_vdev_add()), then update the config. 32461fd3785fSPavel Zakharov * 32471fd3785fSPavel Zakharov * If this is a verbatim import, trust the current 32481fd3785fSPavel Zakharov * in-core spa_config and update the disk labels. 32491fd3785fSPavel Zakharov */ 3250*6f793812SPavel Zakharov if (reloading || config_cache_txg != spa->spa_config_txg || 32513ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_IMPORT || 32523ee8c80cSPavel Zakharov spa->spa_load_state == SPA_LOAD_RECOVER || 32531fd3785fSPavel Zakharov (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 32541fd3785fSPavel Zakharov need_update = B_TRUE; 32551fd3785fSPavel Zakharov 32561fd3785fSPavel Zakharov for (int c = 0; c < rvd->vdev_children; c++) 32571fd3785fSPavel Zakharov if (rvd->vdev_child[c]->vdev_ms_array == 0) 32581fd3785fSPavel Zakharov need_update = B_TRUE; 32591fd3785fSPavel Zakharov 32601fd3785fSPavel Zakharov /* 32611fd3785fSPavel Zakharov * Update the config cache asychronously in case we're the 32621fd3785fSPavel Zakharov * root pool, in which case the config cache isn't writable yet. 32631fd3785fSPavel Zakharov */ 32641fd3785fSPavel Zakharov if (need_update) 32651fd3785fSPavel Zakharov spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 32661fd3785fSPavel Zakharov } 32671fd3785fSPavel Zakharov 3268*6f793812SPavel Zakharov static void 3269*6f793812SPavel Zakharov spa_ld_prepare_for_reload(spa_t *spa) 3270*6f793812SPavel Zakharov { 3271*6f793812SPavel Zakharov int mode = spa->spa_mode; 3272*6f793812SPavel Zakharov int async_suspended = spa->spa_async_suspended; 3273*6f793812SPavel Zakharov 3274*6f793812SPavel Zakharov spa_unload(spa); 3275*6f793812SPavel Zakharov spa_deactivate(spa); 3276*6f793812SPavel Zakharov spa_activate(spa, mode); 3277*6f793812SPavel Zakharov 3278*6f793812SPavel Zakharov /* 3279*6f793812SPavel Zakharov * We save the value of spa_async_suspended as it gets reset to 0 by 3280*6f793812SPavel Zakharov * spa_unload(). We want to restore it back to the original value before 3281*6f793812SPavel Zakharov * returning as we might be calling spa_async_resume() later. 3282*6f793812SPavel Zakharov */ 3283*6f793812SPavel Zakharov spa->spa_async_suspended = async_suspended; 3284*6f793812SPavel Zakharov } 3285*6f793812SPavel Zakharov 32861fd3785fSPavel Zakharov /* 32871fd3785fSPavel Zakharov * Load an existing storage pool, using the config provided. This config 32881fd3785fSPavel Zakharov * describes which vdevs are part of the pool and is later validated against 32891fd3785fSPavel Zakharov * partial configs present in each vdev's label and an entire copy of the 32901fd3785fSPavel Zakharov * config stored in the MOS. 32911fd3785fSPavel Zakharov */ 32921fd3785fSPavel Zakharov static int 3293*6f793812SPavel Zakharov spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport, 3294*6f793812SPavel Zakharov boolean_t reloading) 32951fd3785fSPavel Zakharov { 32961fd3785fSPavel Zakharov int error = 0; 32971fd3785fSPavel Zakharov boolean_t missing_feat_write = B_FALSE; 32981fd3785fSPavel Zakharov 32993ee8c80cSPavel Zakharov ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3300*6f793812SPavel Zakharov ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); 33013ee8c80cSPavel Zakharov 33021fd3785fSPavel Zakharov /* 3303*6f793812SPavel Zakharov * Never trust the config that is provided unless we are assembling 3304*6f793812SPavel Zakharov * a pool following a split. 3305*6f793812SPavel Zakharov * This means don't trust blkptrs and the vdev tree in general. This 3306*6f793812SPavel Zakharov * also effectively puts the spa in read-only mode since 3307*6f793812SPavel Zakharov * spa_writeable() checks for spa_trust_config to be true. 3308*6f793812SPavel Zakharov * We will later load a trusted config from the MOS. 33091fd3785fSPavel Zakharov */ 3310*6f793812SPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE) 3311*6f793812SPavel Zakharov spa->spa_trust_config = B_FALSE; 3312*6f793812SPavel Zakharov 3313*6f793812SPavel Zakharov if (reloading) 3314*6f793812SPavel Zakharov spa_load_note(spa, "RELOADING"); 3315*6f793812SPavel Zakharov else 3316*6f793812SPavel Zakharov spa_load_note(spa, "LOADING"); 33171fd3785fSPavel Zakharov 33181fd3785fSPavel Zakharov /* 33191fd3785fSPavel Zakharov * Parse the config provided to create a vdev tree. 33201fd3785fSPavel Zakharov */ 3321*6f793812SPavel Zakharov error = spa_ld_parse_config(spa, type); 33221fd3785fSPavel Zakharov if (error != 0) 33231fd3785fSPavel Zakharov return (error); 33241fd3785fSPavel Zakharov 33251fd3785fSPavel Zakharov /* 33261fd3785fSPavel Zakharov * Now that we have the vdev tree, try to open each vdev. This involves 33271fd3785fSPavel Zakharov * opening the underlying physical device, retrieving its geometry and 33281fd3785fSPavel Zakharov * probing the vdev with a dummy I/O. The state of each vdev will be set 33291fd3785fSPavel Zakharov * based on the success of those operations. After this we'll be ready 33301fd3785fSPavel Zakharov * to read from the vdevs. 33311fd3785fSPavel Zakharov */ 33321fd3785fSPavel Zakharov error = spa_ld_open_vdevs(spa); 33331fd3785fSPavel Zakharov if (error != 0) 33341fd3785fSPavel Zakharov return (error); 33351fd3785fSPavel Zakharov 33361fd3785fSPavel Zakharov /* 33371fd3785fSPavel Zakharov * Read the label of each vdev and make sure that the GUIDs stored 33381fd3785fSPavel Zakharov * there match the GUIDs in the config provided. 3339*6f793812SPavel Zakharov * If we're assembling a new pool that's been split off from an 3340*6f793812SPavel Zakharov * existing pool, the labels haven't yet been updated so we skip 3341*6f793812SPavel Zakharov * validation for now. 33421fd3785fSPavel Zakharov */ 3343*6f793812SPavel Zakharov if (type != SPA_IMPORT_ASSEMBLE) { 3344*6f793812SPavel Zakharov error = spa_ld_validate_vdevs(spa); 3345*6f793812SPavel Zakharov if (error != 0) 3346*6f793812SPavel Zakharov return (error); 3347*6f793812SPavel Zakharov } 33481fd3785fSPavel Zakharov 33491fd3785fSPavel Zakharov /* 33501fd3785fSPavel Zakharov * Read vdev labels to find the best uberblock (i.e. latest, unless 33511fd3785fSPavel Zakharov * spa_load_max_txg is set) and store it in spa_uberblock. We get the 33521fd3785fSPavel Zakharov * list of features required to read blkptrs in the MOS from the vdev 33531fd3785fSPavel Zakharov * label with the best uberblock and verify that our version of zfs 33541fd3785fSPavel Zakharov * supports them all. 33551fd3785fSPavel Zakharov */ 3356*6f793812SPavel Zakharov error = spa_ld_select_uberblock(spa, type); 33571fd3785fSPavel Zakharov if (error != 0) 33581fd3785fSPavel Zakharov return (error); 33591fd3785fSPavel Zakharov 33601fd3785fSPavel Zakharov /* 33611fd3785fSPavel Zakharov * Pass that uberblock to the dsl_pool layer which will open the root 33621fd3785fSPavel Zakharov * blkptr. This blkptr points to the latest version of the MOS and will 33631fd3785fSPavel Zakharov * allow us to read its contents. 33641fd3785fSPavel Zakharov */ 33651fd3785fSPavel Zakharov error = spa_ld_open_rootbp(spa); 33661fd3785fSPavel Zakharov if (error != 0) 33671fd3785fSPavel Zakharov return (error); 33681fd3785fSPavel Zakharov 33691fd3785fSPavel Zakharov /* 3370*6f793812SPavel Zakharov * Retrieve the trusted config stored in the MOS and use it to create 3371*6f793812SPavel Zakharov * a new, exact version of the vdev tree, then reopen all vdevs. 33721fd3785fSPavel Zakharov */ 3373*6f793812SPavel Zakharov error = spa_ld_load_trusted_config(spa, type, reloading); 3374*6f793812SPavel Zakharov if (error == EAGAIN) { 3375*6f793812SPavel Zakharov VERIFY(!reloading); 3376*6f793812SPavel Zakharov /* 3377*6f793812SPavel Zakharov * Redo the loading process with the trusted config if it is 3378*6f793812SPavel Zakharov * too different from the untrusted config. 3379*6f793812SPavel Zakharov */ 3380*6f793812SPavel Zakharov spa_ld_prepare_for_reload(spa); 3381*6f793812SPavel Zakharov return (spa_load_impl(spa, type, ereport, B_TRUE)); 3382*6f793812SPavel Zakharov } else if (error != 0) { 33831fd3785fSPavel Zakharov return (error); 3384*6f793812SPavel Zakharov } 33851fd3785fSPavel Zakharov 33861fd3785fSPavel Zakharov /* 33871fd3785fSPavel Zakharov * Retrieve the mapping of indirect vdevs. Those vdevs were removed 33881fd3785fSPavel Zakharov * from the pool and their contents were re-mapped to other vdevs. Note 33891fd3785fSPavel Zakharov * that everything that we read before this step must have been 33901fd3785fSPavel Zakharov * rewritten on concrete vdevs after the last device removal was 33911fd3785fSPavel Zakharov * initiated. Otherwise we could be reading from indirect vdevs before 33921fd3785fSPavel Zakharov * we have loaded their mappings. 33931fd3785fSPavel Zakharov */ 33941fd3785fSPavel Zakharov error = spa_ld_open_indirect_vdev_metadata(spa); 33951fd3785fSPavel Zakharov if (error != 0) 33961fd3785fSPavel Zakharov return (error); 33971fd3785fSPavel Zakharov 33981fd3785fSPavel Zakharov /* 33991fd3785fSPavel Zakharov * Retrieve the full list of active features from the MOS and check if 34001fd3785fSPavel Zakharov * they are all supported. 34011fd3785fSPavel Zakharov */ 34023ee8c80cSPavel Zakharov error = spa_ld_check_features(spa, &missing_feat_write); 34031fd3785fSPavel Zakharov if (error != 0) 34041fd3785fSPavel Zakharov return (error); 34051fd3785fSPavel Zakharov 34061fd3785fSPavel Zakharov /* 34071fd3785fSPavel Zakharov * Load several special directories from the MOS needed by the dsl_pool 34081fd3785fSPavel Zakharov * layer. 34091fd3785fSPavel Zakharov */ 34101fd3785fSPavel Zakharov error = spa_ld_load_special_directories(spa); 34111fd3785fSPavel Zakharov if (error != 0) 34121fd3785fSPavel Zakharov return (error); 34131fd3785fSPavel Zakharov 34141fd3785fSPavel Zakharov /* 34151fd3785fSPavel Zakharov * Retrieve pool properties from the MOS. 34161fd3785fSPavel Zakharov */ 34171fd3785fSPavel Zakharov error = spa_ld_get_props(spa); 34181fd3785fSPavel Zakharov if (error != 0) 34191fd3785fSPavel Zakharov return (error); 34201fd3785fSPavel Zakharov 34211fd3785fSPavel Zakharov /* 34221fd3785fSPavel Zakharov * Retrieve the list of auxiliary devices - cache devices and spares - 34231fd3785fSPavel Zakharov * and open them. 34241fd3785fSPavel Zakharov */ 34251fd3785fSPavel Zakharov error = spa_ld_open_aux_vdevs(spa, type); 34261fd3785fSPavel Zakharov if (error != 0) 34271fd3785fSPavel Zakharov return (error); 34281fd3785fSPavel Zakharov 34291fd3785fSPavel Zakharov /* 34301fd3785fSPavel Zakharov * Load the metadata for all vdevs. Also check if unopenable devices 34311fd3785fSPavel Zakharov * should be autoreplaced. 34321fd3785fSPavel Zakharov */ 34333ee8c80cSPavel Zakharov error = spa_ld_load_vdev_metadata(spa); 34341fd3785fSPavel Zakharov if (error != 0) 34351fd3785fSPavel Zakharov return (error); 34361fd3785fSPavel Zakharov 34371fd3785fSPavel Zakharov error = spa_ld_load_dedup_tables(spa); 34381fd3785fSPavel Zakharov if (error != 0) 34391fd3785fSPavel Zakharov return (error); 34401fd3785fSPavel Zakharov 34411fd3785fSPavel Zakharov /* 34421fd3785fSPavel Zakharov * Verify the logs now to make sure we don't have any unexpected errors 34431fd3785fSPavel Zakharov * when we claim log blocks later. 34441fd3785fSPavel Zakharov */ 34451fd3785fSPavel Zakharov error = spa_ld_verify_logs(spa, type, ereport); 34461fd3785fSPavel Zakharov if (error != 0) 34471fd3785fSPavel Zakharov return (error); 34481fd3785fSPavel Zakharov 3449ad135b5dSChristopher Siden if (missing_feat_write) { 3450*6f793812SPavel Zakharov ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); 3451ad135b5dSChristopher Siden 3452ad135b5dSChristopher Siden /* 3453ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 3454ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 3455ad135b5dSChristopher Siden * information and can return to userland. 3456ad135b5dSChristopher Siden */ 34571fd3785fSPavel Zakharov return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, 34581fd3785fSPavel Zakharov ENOTSUP)); 3459ad135b5dSChristopher Siden } 3460ad135b5dSChristopher Siden 34614b964adaSGeorge Wilson /* 34621fd3785fSPavel Zakharov * Traverse the last txgs to make sure the pool was left off in a safe 34631fd3785fSPavel Zakharov * state. When performing an extreme rewind, we verify the whole pool, 34641fd3785fSPavel Zakharov * which can take a very long time. 34654b964adaSGeorge Wilson */ 34663ee8c80cSPavel Zakharov error = spa_ld_verify_pool_data(spa); 34671fd3785fSPavel Zakharov if (error != 0) 34681fd3785fSPavel Zakharov return (error); 34691fd3785fSPavel Zakharov 34701fd3785fSPavel Zakharov /* 34711fd3785fSPavel Zakharov * Calculate the deflated space for the pool. This must be done before 34721fd3785fSPavel Zakharov * we write anything to the pool because we'd need to update the space 34731fd3785fSPavel Zakharov * accounting using the deflated sizes. 34741fd3785fSPavel Zakharov */ 34751fd3785fSPavel Zakharov spa_update_dspace(spa); 34764b964adaSGeorge Wilson 34771fd3785fSPavel Zakharov /* 34781fd3785fSPavel Zakharov * We have now retrieved all the information we needed to open the 34791fd3785fSPavel Zakharov * pool. If we are importing the pool in read-write mode, a few 34801fd3785fSPavel Zakharov * additional steps must be performed to finish the import. 34811fd3785fSPavel Zakharov */ 3482*6f793812SPavel Zakharov if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || 3483468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 3484*6f793812SPavel Zakharov uint64_t config_cache_txg = spa->spa_config_txg; 3485*6f793812SPavel Zakharov 3486*6f793812SPavel Zakharov ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); 34878ad4d6ddSJeff Bonwick 34885cabbc6bSPrashanth Sreenivasa /* 34895cabbc6bSPrashanth Sreenivasa * We must check this before we start the sync thread, because 34905cabbc6bSPrashanth Sreenivasa * we only want to start a condense thread for condense 34915cabbc6bSPrashanth Sreenivasa * operations that were in progress when the pool was 34925cabbc6bSPrashanth Sreenivasa * imported. Once we start syncing, spa_sync() could 34935cabbc6bSPrashanth Sreenivasa * initiate a condense (and start a thread for it). In 34945cabbc6bSPrashanth Sreenivasa * that case it would be wrong to start a second 34955cabbc6bSPrashanth Sreenivasa * condense thread. 34965cabbc6bSPrashanth Sreenivasa */ 34975cabbc6bSPrashanth Sreenivasa boolean_t condense_in_progress = 34985cabbc6bSPrashanth Sreenivasa (spa->spa_condensing_indirect != NULL); 34995cabbc6bSPrashanth Sreenivasa 35000373e76bSbonwick /* 35011fd3785fSPavel Zakharov * Traverse the ZIL and claim all blocks. 35020373e76bSbonwick */ 35031fd3785fSPavel Zakharov spa_ld_claim_log_blocks(spa); 3504b24ab676SJeff Bonwick 35051fd3785fSPavel Zakharov /* 35061fd3785fSPavel Zakharov * Kick-off the syncing thread. 35071fd3785fSPavel Zakharov */ 3508fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3509fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3510fa9e4066Sahrens 3511fa9e4066Sahrens /* 3512b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 3513b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 3514b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 35151fd3785fSPavel Zakharov * will have been set for us by ZIL traversal operations 35161fd3785fSPavel Zakharov * performed above. 3517fa9e4066Sahrens */ 3518b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 35190e34b6a7Sbonwick 35200e34b6a7Sbonwick /* 35211fd3785fSPavel Zakharov * Check if we need to request an update of the config. On the 35221fd3785fSPavel Zakharov * next sync, we would update the config stored in vdev labels 35231fd3785fSPavel Zakharov * and the cachefile (by default /etc/zfs/zpool.cache). 35240e34b6a7Sbonwick */ 3525*6f793812SPavel Zakharov spa_ld_check_for_config_update(spa, config_cache_txg, 3526*6f793812SPavel Zakharov reloading); 35278ad4d6ddSJeff Bonwick 35288ad4d6ddSJeff Bonwick /* 35298ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 35308ad4d6ddSJeff Bonwick */ 35313f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 35321fd3785fSPavel Zakharov vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) 35338ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 3534503ad85cSMatthew Ahrens 35354445fffbSMatthew Ahrens /* 35364445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 35374445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 35384445fffbSMatthew Ahrens */ 35394445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 35404445fffbSMatthew Ahrens 3541503ad85cSMatthew Ahrens /* 3542503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 3543503ad85cSMatthew Ahrens */ 3544503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 3545503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 3546ca45db41SChris Kirby 3547ca45db41SChris Kirby /* 3548ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 3549ca45db41SChris Kirby */ 3550ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 35515cabbc6bSPrashanth Sreenivasa 35525cabbc6bSPrashanth Sreenivasa /* 35535cabbc6bSPrashanth Sreenivasa * Note: unlike condensing, we don't need an analogous 35545cabbc6bSPrashanth Sreenivasa * "removal_in_progress" dance because no other thread 35555cabbc6bSPrashanth Sreenivasa * can start a removal while we hold the spa_namespace_lock. 35565cabbc6bSPrashanth Sreenivasa */ 35575cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 35585cabbc6bSPrashanth Sreenivasa 35595cabbc6bSPrashanth Sreenivasa if (condense_in_progress) 35605cabbc6bSPrashanth Sreenivasa spa_condense_indirect_restart(spa); 3561fa9e4066Sahrens } 3562fa9e4066Sahrens 35633ee8c80cSPavel Zakharov spa_load_note(spa, "LOADED"); 35643ee8c80cSPavel Zakharov 35651195e687SMark J Musante return (0); 3566fa9e4066Sahrens } 3567fa9e4066Sahrens 3568468c413aSTim Haley static int 3569*6f793812SPavel Zakharov spa_load_retry(spa_t *spa, spa_load_state_t state) 3570468c413aSTim Haley { 3571f9af39baSGeorge Wilson int mode = spa->spa_mode; 3572f9af39baSGeorge Wilson 3573468c413aSTim Haley spa_unload(spa); 3574468c413aSTim Haley spa_deactivate(spa); 3575468c413aSTim Haley 3576e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 3577468c413aSTim Haley 3578f9af39baSGeorge Wilson spa_activate(spa, mode); 3579468c413aSTim Haley spa_async_suspend(spa); 3580468c413aSTim Haley 35813ee8c80cSPavel Zakharov spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", 35823ee8c80cSPavel Zakharov (u_longlong_t)spa->spa_load_max_txg); 35833ee8c80cSPavel Zakharov 3584*6f793812SPavel Zakharov return (spa_load(spa, state, SPA_IMPORT_EXISTING)); 3585468c413aSTim Haley } 3586468c413aSTim Haley 3587ad135b5dSChristopher Siden /* 3588ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 3589ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 3590ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 3591ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 3592ad135b5dSChristopher Siden * spa_load(). 3593ad135b5dSChristopher Siden */ 3594468c413aSTim Haley static int 3595*6f793812SPavel Zakharov spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, 3596*6f793812SPavel Zakharov int rewind_flags) 3597468c413aSTim Haley { 3598ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 3599468c413aSTim Haley nvlist_t *config = NULL; 3600468c413aSTim Haley int load_error, rewind_error; 3601c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 3602468c413aSTim Haley uint64_t min_txg; 3603468c413aSTim Haley 3604a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 3605468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 36061195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3607a33cae98STim Haley } else { 3608468c413aSTim Haley spa->spa_load_max_txg = max_request; 3609e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 3610e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 3611a33cae98STim Haley } 3612468c413aSTim Haley 3613*6f793812SPavel Zakharov load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); 3614468c413aSTim Haley if (load_error == 0) 3615468c413aSTim Haley return (0); 3616468c413aSTim Haley 3617468c413aSTim Haley if (spa->spa_root_vdev != NULL) 3618468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3619468c413aSTim Haley 3620468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 3621468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 3622468c413aSTim Haley 3623c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 3624468c413aSTim Haley nvlist_free(config); 3625468c413aSTim Haley return (load_error); 3626468c413aSTim Haley } 3627468c413aSTim Haley 3628ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3629ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 36301195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3631ad135b5dSChristopher Siden } else { 3632ad135b5dSChristopher Siden /* 3633ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 3634ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 3635ad135b5dSChristopher Siden * to rewind. 3636ad135b5dSChristopher Siden */ 3637ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3638ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3639ad135b5dSChristopher Siden } 3640468c413aSTim Haley 3641c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3642c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3643c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3644c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3645468c413aSTim Haley 3646c8ee1847SVictor Latushkin /* 3647c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3648c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3649c8ee1847SVictor Latushkin */ 3650c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3651c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3652c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3653468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 3654*6f793812SPavel Zakharov rewind_error = spa_load_retry(spa, state); 3655468c413aSTim Haley } 3656468c413aSTim Haley 3657468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3658468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3659468c413aSTim Haley 3660468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3661468c413aSTim Haley spa_config_set(spa, config); 36627d3000f7SPavel Zakharov else 36637d3000f7SPavel Zakharov nvlist_free(config); 3664468c413aSTim Haley 3665ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3666ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3667ad135b5dSChristopher Siden return (rewind_error); 3668ad135b5dSChristopher Siden } else { 3669ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3670ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3671ad135b5dSChristopher Siden spa->spa_load_info); 3672ad135b5dSChristopher Siden 3673ad135b5dSChristopher Siden /* Restore the initial load info */ 3674ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3675ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3676ad135b5dSChristopher Siden 3677ad135b5dSChristopher Siden return (load_error); 3678ad135b5dSChristopher Siden } 3679468c413aSTim Haley } 3680468c413aSTim Haley 3681fa9e4066Sahrens /* 3682fa9e4066Sahrens * Pool Open/Import 3683fa9e4066Sahrens * 3684fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3685fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3686fa9e4066Sahrens * case of an open, the pool configuration will exist in the 36873d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3688fa9e4066Sahrens * 3689fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3690fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3691fa9e4066Sahrens * ambiguous state. 3692fa9e4066Sahrens */ 3693fa9e4066Sahrens static int 3694468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3695468c413aSTim Haley nvlist_t **config) 3696fa9e4066Sahrens { 3697fa9e4066Sahrens spa_t *spa; 36984b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3699fa9e4066Sahrens int error; 3700fa9e4066Sahrens int locked = B_FALSE; 3701fa9e4066Sahrens 3702fa9e4066Sahrens *spapp = NULL; 3703fa9e4066Sahrens 3704fa9e4066Sahrens /* 3705fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3706fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3707fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3708fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3709fa9e4066Sahrens */ 3710fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3711fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3712fa9e4066Sahrens locked = B_TRUE; 3713fa9e4066Sahrens } 3714fa9e4066Sahrens 3715fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3716fa9e4066Sahrens if (locked) 3717fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3718be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3719fa9e4066Sahrens } 3720468c413aSTim Haley 3721fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 37224b44c88cSTim Haley zpool_rewind_policy_t policy; 37234b44c88cSTim Haley 37244b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 37254b44c88cSTim Haley &policy); 37264b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 37274b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3728fa9e4066Sahrens 37298ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3730fa9e4066Sahrens 3731468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3732468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3733*6f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 3734468c413aSTim Haley 37353ee8c80cSPavel Zakharov zfs_dbgmsg("spa_open_common: opening %s", pool); 3736*6f793812SPavel Zakharov error = spa_load_best(spa, state, policy.zrp_txg, 3737c8ee1847SVictor Latushkin policy.zrp_request); 3738fa9e4066Sahrens 3739fa9e4066Sahrens if (error == EBADF) { 3740fa9e4066Sahrens /* 3741560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3742560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3743560e6e96Seschrock * that the pool has been exported or destroyed. If 3744560e6e96Seschrock * this is the case, the config cache is out of sync and 3745560e6e96Seschrock * we should remove the pool from the namespace. 3746fa9e4066Sahrens */ 3747fa9e4066Sahrens spa_unload(spa); 3748fa9e4066Sahrens spa_deactivate(spa); 37495cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 3750fa9e4066Sahrens spa_remove(spa); 3751fa9e4066Sahrens if (locked) 3752fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3753be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3754ea8dc4b6Seschrock } 3755ea8dc4b6Seschrock 3756ea8dc4b6Seschrock if (error) { 3757fa9e4066Sahrens /* 3758fa9e4066Sahrens * We can't open the pool, but we still have useful 3759fa9e4066Sahrens * information: the state of each vdev after the 3760fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3761fa9e4066Sahrens */ 37624b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3763468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3764468c413aSTim Haley KM_SLEEP) == 0); 37654b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 37664b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 37674b964adaSGeorge Wilson spa->spa_load_info) == 0); 37684b964adaSGeorge Wilson } 3769fa9e4066Sahrens spa_unload(spa); 3770fa9e4066Sahrens spa_deactivate(spa); 3771468c413aSTim Haley spa->spa_last_open_failed = error; 3772fa9e4066Sahrens if (locked) 3773fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3774fa9e4066Sahrens *spapp = NULL; 3775fa9e4066Sahrens return (error); 3776fa9e4066Sahrens } 3777fa9e4066Sahrens } 3778fa9e4066Sahrens 3779fa9e4066Sahrens spa_open_ref(spa, tag); 37803d7072f8Seschrock 3781468c413aSTim Haley if (config != NULL) 3782468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3783468c413aSTim Haley 37844b964adaSGeorge Wilson /* 37854b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 37864b964adaSGeorge Wilson * gathered while doing the load. 37874b964adaSGeorge Wilson */ 37884b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 37894b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 37904b964adaSGeorge Wilson spa->spa_load_info) == 0); 37914b964adaSGeorge Wilson } 37924b964adaSGeorge Wilson 3793a33cae98STim Haley if (locked) { 3794a33cae98STim Haley spa->spa_last_open_failed = 0; 3795a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3796a33cae98STim Haley spa->spa_load_txg = 0; 3797fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3798a33cae98STim Haley } 3799fa9e4066Sahrens 3800fa9e4066Sahrens *spapp = spa; 3801fa9e4066Sahrens 3802fa9e4066Sahrens return (0); 3803fa9e4066Sahrens } 3804fa9e4066Sahrens 3805468c413aSTim Haley int 3806468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3807468c413aSTim Haley nvlist_t **config) 3808468c413aSTim Haley { 3809468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3810468c413aSTim Haley } 3811468c413aSTim Haley 3812fa9e4066Sahrens int 3813fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3814fa9e4066Sahrens { 3815468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3816fa9e4066Sahrens } 3817fa9e4066Sahrens 3818ea8dc4b6Seschrock /* 3819ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3820ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3821ea8dc4b6Seschrock */ 3822ea8dc4b6Seschrock spa_t * 3823ea8dc4b6Seschrock spa_inject_addref(char *name) 3824ea8dc4b6Seschrock { 3825ea8dc4b6Seschrock spa_t *spa; 3826ea8dc4b6Seschrock 3827ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3828ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3829ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3830ea8dc4b6Seschrock return (NULL); 3831ea8dc4b6Seschrock } 3832ea8dc4b6Seschrock spa->spa_inject_ref++; 3833ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3834ea8dc4b6Seschrock 3835ea8dc4b6Seschrock return (spa); 3836ea8dc4b6Seschrock } 3837ea8dc4b6Seschrock 3838ea8dc4b6Seschrock void 3839ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3840ea8dc4b6Seschrock { 3841ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3842ea8dc4b6Seschrock spa->spa_inject_ref--; 3843ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3844ea8dc4b6Seschrock } 3845ea8dc4b6Seschrock 3846fa94a07fSbrendan /* 3847fa94a07fSbrendan * Add spares device information to the nvlist. 3848fa94a07fSbrendan */ 384999653d4eSeschrock static void 385099653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 385199653d4eSeschrock { 385299653d4eSeschrock nvlist_t **spares; 385399653d4eSeschrock uint_t i, nspares; 385499653d4eSeschrock nvlist_t *nvroot; 385599653d4eSeschrock uint64_t guid; 385699653d4eSeschrock vdev_stat_t *vs; 385799653d4eSeschrock uint_t vsc; 385839c23413Seschrock uint64_t pool; 385999653d4eSeschrock 38606809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 38616809eb4eSEric Schrock 3862fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 386399653d4eSeschrock return; 386499653d4eSeschrock 386599653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 386699653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3867fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 386899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 386999653d4eSeschrock if (nspares != 0) { 387099653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 387199653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 387299653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 387399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 387499653d4eSeschrock 387599653d4eSeschrock /* 387699653d4eSeschrock * Go through and find any spares which have since been 387799653d4eSeschrock * repurposed as an active spare. If this is the case, update 387899653d4eSeschrock * their status appropriately. 387999653d4eSeschrock */ 388099653d4eSeschrock for (i = 0; i < nspares; i++) { 388199653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 388299653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 388389a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 388489a89ebfSlling pool != 0ULL) { 388599653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 38863f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 388799653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 388899653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 388999653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 389099653d4eSeschrock } 389199653d4eSeschrock } 389299653d4eSeschrock } 389399653d4eSeschrock } 389499653d4eSeschrock 3895fa94a07fSbrendan /* 3896fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3897fa94a07fSbrendan */ 3898fa94a07fSbrendan static void 3899fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3900fa94a07fSbrendan { 3901fa94a07fSbrendan nvlist_t **l2cache; 3902fa94a07fSbrendan uint_t i, j, nl2cache; 3903fa94a07fSbrendan nvlist_t *nvroot; 3904fa94a07fSbrendan uint64_t guid; 3905fa94a07fSbrendan vdev_t *vd; 3906fa94a07fSbrendan vdev_stat_t *vs; 3907fa94a07fSbrendan uint_t vsc; 3908fa94a07fSbrendan 39096809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 39106809eb4eSEric Schrock 3911fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3912fa94a07fSbrendan return; 3913fa94a07fSbrendan 3914fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3915fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3916fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3917fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3918fa94a07fSbrendan if (nl2cache != 0) { 3919fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3920fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3921fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3922fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3923fa94a07fSbrendan 3924fa94a07fSbrendan /* 3925fa94a07fSbrendan * Update level 2 cache device stats. 3926fa94a07fSbrendan */ 3927fa94a07fSbrendan 3928fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3929fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3930fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3931fa94a07fSbrendan 3932fa94a07fSbrendan vd = NULL; 3933fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3934fa94a07fSbrendan if (guid == 3935fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3936fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3937fa94a07fSbrendan break; 3938fa94a07fSbrendan } 3939fa94a07fSbrendan } 3940fa94a07fSbrendan ASSERT(vd != NULL); 3941fa94a07fSbrendan 3942fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 39433f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 39443f9d6ad7SLin Ling == 0); 3945fa94a07fSbrendan vdev_get_stats(vd, vs); 3946fa94a07fSbrendan } 3947fa94a07fSbrendan } 3948fa94a07fSbrendan } 3949fa94a07fSbrendan 3950ad135b5dSChristopher Siden static void 3951ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3952ad135b5dSChristopher Siden { 3953ad135b5dSChristopher Siden nvlist_t *features; 3954ad135b5dSChristopher Siden zap_cursor_t zc; 3955ad135b5dSChristopher Siden zap_attribute_t za; 3956ad135b5dSChristopher Siden 3957ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3958ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3959ad135b5dSChristopher Siden 3960ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3961ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3962ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3963ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3964ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3965ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3966ad135b5dSChristopher Siden za.za_num_integers == 1); 3967b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3968ad135b5dSChristopher Siden za.za_first_integer)); 3969ad135b5dSChristopher Siden } 3970ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3971ad135b5dSChristopher Siden } 3972ad135b5dSChristopher Siden 3973ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3974ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3975ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3976ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3977ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3978ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3979ad135b5dSChristopher Siden za.za_num_integers == 1); 3980b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3981ad135b5dSChristopher Siden za.za_first_integer)); 3982ad135b5dSChristopher Siden } 3983ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3984ad135b5dSChristopher Siden } 3985ad135b5dSChristopher Siden 3986ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3987ad135b5dSChristopher Siden features) == 0); 3988ad135b5dSChristopher Siden nvlist_free(features); 3989ad135b5dSChristopher Siden } 3990ad135b5dSChristopher Siden 3991fa9e4066Sahrens int 3992ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3993ad135b5dSChristopher Siden char *altroot, size_t buflen) 3994fa9e4066Sahrens { 3995fa9e4066Sahrens int error; 3996fa9e4066Sahrens spa_t *spa; 3997fa9e4066Sahrens 3998fa9e4066Sahrens *config = NULL; 3999468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 4000fa9e4066Sahrens 40016809eb4eSEric Schrock if (spa != NULL) { 40026809eb4eSEric Schrock /* 40036809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 40046809eb4eSEric Schrock * or l2cache devices could change and the config would be 40056809eb4eSEric Schrock * self-inconsistent. 40066809eb4eSEric Schrock */ 40076809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4008ea8dc4b6Seschrock 40096809eb4eSEric Schrock if (*config != NULL) { 401011027bc7STim Haley uint64_t loadtimes[2]; 401111027bc7STim Haley 401211027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 401311027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 401411027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 401511027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 401611027bc7STim Haley 4017e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 40186809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 40196809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 4020e14bb325SJeff Bonwick 40216809eb4eSEric Schrock if (spa_suspended(spa)) 40226809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 40236809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 40246809eb4eSEric Schrock spa->spa_failmode) == 0); 40256809eb4eSEric Schrock 40266809eb4eSEric Schrock spa_add_spares(spa, *config); 40276809eb4eSEric Schrock spa_add_l2cache(spa, *config); 4028ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 40296809eb4eSEric Schrock } 403099653d4eSeschrock } 403199653d4eSeschrock 4032ea8dc4b6Seschrock /* 4033ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 4034ea8dc4b6Seschrock * and call spa_lookup() directly. 4035ea8dc4b6Seschrock */ 4036ea8dc4b6Seschrock if (altroot) { 4037ea8dc4b6Seschrock if (spa == NULL) { 4038ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4039ea8dc4b6Seschrock spa = spa_lookup(name); 4040ea8dc4b6Seschrock if (spa) 4041ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 4042ea8dc4b6Seschrock else 4043ea8dc4b6Seschrock altroot[0] = '\0'; 4044ea8dc4b6Seschrock spa = NULL; 4045ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4046ea8dc4b6Seschrock } else { 4047ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 4048ea8dc4b6Seschrock } 4049ea8dc4b6Seschrock } 4050ea8dc4b6Seschrock 40516809eb4eSEric Schrock if (spa != NULL) { 40526809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4053fa9e4066Sahrens spa_close(spa, FTAG); 40546809eb4eSEric Schrock } 4055fa9e4066Sahrens 4056fa9e4066Sahrens return (error); 4057fa9e4066Sahrens } 4058fa9e4066Sahrens 405999653d4eSeschrock /* 4060fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 4061fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 4062fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 4063fa94a07fSbrendan * specified, as long as they are well-formed. 406499653d4eSeschrock */ 406599653d4eSeschrock static int 4066fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 4067fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 4068fa94a07fSbrendan vdev_labeltype_t label) 406999653d4eSeschrock { 4070fa94a07fSbrendan nvlist_t **dev; 4071fa94a07fSbrendan uint_t i, ndev; 407299653d4eSeschrock vdev_t *vd; 407399653d4eSeschrock int error; 407499653d4eSeschrock 4075e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4076e14bb325SJeff Bonwick 407799653d4eSeschrock /* 4078fa94a07fSbrendan * It's acceptable to have no devs specified. 407999653d4eSeschrock */ 4080fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 408199653d4eSeschrock return (0); 408299653d4eSeschrock 4083fa94a07fSbrendan if (ndev == 0) 4084be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 408599653d4eSeschrock 408699653d4eSeschrock /* 4087fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 4088fa94a07fSbrendan * device type. 408999653d4eSeschrock */ 4090fa94a07fSbrendan if (spa_version(spa) < version) 4091be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 409299653d4eSeschrock 409339c23413Seschrock /* 4094fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 409539c23413Seschrock * checking. 409639c23413Seschrock */ 4097fa94a07fSbrendan sav->sav_pending = dev; 4098fa94a07fSbrendan sav->sav_npending = ndev; 409939c23413Seschrock 4100fa94a07fSbrendan for (i = 0; i < ndev; i++) { 4101fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 410299653d4eSeschrock mode)) != 0) 410339c23413Seschrock goto out; 410499653d4eSeschrock 410599653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 410699653d4eSeschrock vdev_free(vd); 4107be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 410839c23413Seschrock goto out; 410999653d4eSeschrock } 411099653d4eSeschrock 4111fa94a07fSbrendan /* 4112e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 4113e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 4114fa94a07fSbrendan */ 4115e14bb325SJeff Bonwick #ifdef _KERNEL 4116fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 4117fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 4118be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 4119cd0837ccSGeorge Wilson vdev_free(vd); 4120fa94a07fSbrendan goto out; 4121fa94a07fSbrendan } 4122e14bb325SJeff Bonwick #endif 412399653d4eSeschrock vd->vdev_top = vd; 412499653d4eSeschrock 412539c23413Seschrock if ((error = vdev_open(vd)) == 0 && 4126fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 4127fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 412839c23413Seschrock vd->vdev_guid) == 0); 412939c23413Seschrock } 413099653d4eSeschrock 413199653d4eSeschrock vdev_free(vd); 413239c23413Seschrock 4133fa94a07fSbrendan if (error && 4134fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 413539c23413Seschrock goto out; 413639c23413Seschrock else 413739c23413Seschrock error = 0; 413899653d4eSeschrock } 413999653d4eSeschrock 414039c23413Seschrock out: 4141fa94a07fSbrendan sav->sav_pending = NULL; 4142fa94a07fSbrendan sav->sav_npending = 0; 414339c23413Seschrock return (error); 414499653d4eSeschrock } 414599653d4eSeschrock 4146fa94a07fSbrendan static int 4147fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 4148fa94a07fSbrendan { 4149fa94a07fSbrendan int error; 4150fa94a07fSbrendan 4151e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4152e14bb325SJeff Bonwick 4153fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4154fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 4155fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 4156fa94a07fSbrendan return (error); 4157fa94a07fSbrendan } 4158fa94a07fSbrendan 4159fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4160fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 4161fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 4162fa94a07fSbrendan } 4163fa94a07fSbrendan 4164fa94a07fSbrendan static void 4165fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 4166fa94a07fSbrendan const char *config) 4167fa94a07fSbrendan { 4168fa94a07fSbrendan int i; 4169fa94a07fSbrendan 4170fa94a07fSbrendan if (sav->sav_config != NULL) { 4171fa94a07fSbrendan nvlist_t **olddevs; 4172fa94a07fSbrendan uint_t oldndevs; 4173fa94a07fSbrendan nvlist_t **newdevs; 4174fa94a07fSbrendan 4175fa94a07fSbrendan /* 4176fa94a07fSbrendan * Generate new dev list by concatentating with the 4177fa94a07fSbrendan * current dev list. 4178fa94a07fSbrendan */ 4179fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 4180fa94a07fSbrendan &olddevs, &oldndevs) == 0); 4181fa94a07fSbrendan 4182fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 4183fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 4184fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 4185fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 4186fa94a07fSbrendan KM_SLEEP) == 0); 4187fa94a07fSbrendan for (i = 0; i < ndevs; i++) 4188fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 4189fa94a07fSbrendan KM_SLEEP) == 0); 4190fa94a07fSbrendan 4191fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 4192fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 4193fa94a07fSbrendan 4194fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 4195fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 4196fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 4197fa94a07fSbrendan nvlist_free(newdevs[i]); 4198fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 4199fa94a07fSbrendan } else { 4200fa94a07fSbrendan /* 4201fa94a07fSbrendan * Generate a new dev list. 4202fa94a07fSbrendan */ 4203fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 4204fa94a07fSbrendan KM_SLEEP) == 0); 4205fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 4206fa94a07fSbrendan devs, ndevs) == 0); 4207fa94a07fSbrendan } 4208fa94a07fSbrendan } 4209fa94a07fSbrendan 4210fa94a07fSbrendan /* 4211fa94a07fSbrendan * Stop and drop level 2 ARC devices 4212fa94a07fSbrendan */ 4213fa94a07fSbrendan void 4214fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 4215fa94a07fSbrendan { 4216fa94a07fSbrendan vdev_t *vd; 4217fa94a07fSbrendan int i; 4218fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 4219fa94a07fSbrendan 4220fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 4221fa94a07fSbrendan uint64_t pool; 4222fa94a07fSbrendan 4223fa94a07fSbrendan vd = sav->sav_vdevs[i]; 4224fa94a07fSbrendan ASSERT(vd != NULL); 4225fa94a07fSbrendan 42268ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 42278ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 4228fa94a07fSbrendan l2arc_remove_vdev(vd); 4229fa94a07fSbrendan } 4230fa94a07fSbrendan } 4231fa94a07fSbrendan 4232fa9e4066Sahrens /* 4233fa9e4066Sahrens * Pool Creation 4234fa9e4066Sahrens */ 4235fa9e4066Sahrens int 4236990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 42374445fffbSMatthew Ahrens nvlist_t *zplprops) 4238fa9e4066Sahrens { 4239fa9e4066Sahrens spa_t *spa; 4240990b4856Slling char *altroot = NULL; 42410373e76bSbonwick vdev_t *rvd; 4242fa9e4066Sahrens dsl_pool_t *dp; 4243fa9e4066Sahrens dmu_tx_t *tx; 4244573ca77eSGeorge Wilson int error = 0; 4245fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 4246fa94a07fSbrendan nvlist_t **spares, **l2cache; 4247fa94a07fSbrendan uint_t nspares, nl2cache; 4248cde58dbcSMatthew Ahrens uint64_t version, obj; 4249ad135b5dSChristopher Siden boolean_t has_features; 4250fa9e4066Sahrens 4251fa9e4066Sahrens /* 4252fa9e4066Sahrens * If this pool already exists, return failure. 4253fa9e4066Sahrens */ 4254fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4255fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 4256fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4257be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 4258fa9e4066Sahrens } 4259fa9e4066Sahrens 4260fa9e4066Sahrens /* 4261fa9e4066Sahrens * Allocate a new spa_t structure. 4262fa9e4066Sahrens */ 4263990b4856Slling (void) nvlist_lookup_string(props, 4264990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4265468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 42668ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 4267fa9e4066Sahrens 4268990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 4269990b4856Slling spa_deactivate(spa); 4270990b4856Slling spa_remove(spa); 4271c5904d13Seschrock mutex_exit(&spa_namespace_lock); 4272990b4856Slling return (error); 4273990b4856Slling } 4274990b4856Slling 4275ad135b5dSChristopher Siden has_features = B_FALSE; 4276ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 4277ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 4278ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 4279ad135b5dSChristopher Siden has_features = B_TRUE; 4280ad135b5dSChristopher Siden } 4281ad135b5dSChristopher Siden 4282ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 4283ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 4284990b4856Slling version = SPA_VERSION; 4285ad135b5dSChristopher Siden } 4286ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 4287b24ab676SJeff Bonwick 4288b24ab676SJeff Bonwick spa->spa_first_txg = txg; 4289b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 4290990b4856Slling spa->spa_uberblock.ub_version = version; 4291fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 42920f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 42935cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_state = DSS_NONE; 42945cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_removing_vdev = -1; 42955cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev = -1; 4296fa9e4066Sahrens 429754d692b7SGeorge Wilson /* 429854d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 429954d692b7SGeorge Wilson */ 43006f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 43016f834bc1SMatthew Ahrens KM_SLEEP); 43026f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 43036f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 43046f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 43056f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 43066f834bc1SMatthew Ahrens } 430754d692b7SGeorge Wilson 43080373e76bSbonwick /* 43090373e76bSbonwick * Create the root vdev. 43100373e76bSbonwick */ 4311e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 43120373e76bSbonwick 431399653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 43140373e76bSbonwick 431599653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 431699653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 43170373e76bSbonwick 4318b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 4319be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 432099653d4eSeschrock 432199653d4eSeschrock if (error == 0 && 432299653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 4323fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 432499653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 4325573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 4326573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 4327573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 4328573ca77eSGeorge Wilson } 43290373e76bSbonwick } 43300373e76bSbonwick 4331e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4332fa9e4066Sahrens 433399653d4eSeschrock if (error != 0) { 4334fa9e4066Sahrens spa_unload(spa); 4335fa9e4066Sahrens spa_deactivate(spa); 4336fa9e4066Sahrens spa_remove(spa); 4337fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4338fa9e4066Sahrens return (error); 4339fa9e4066Sahrens } 4340fa9e4066Sahrens 434199653d4eSeschrock /* 434299653d4eSeschrock * Get the list of spares, if specified. 434399653d4eSeschrock */ 434499653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 434599653d4eSeschrock &spares, &nspares) == 0) { 4346fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 434799653d4eSeschrock KM_SLEEP) == 0); 4348fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 434999653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 4350e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 435199653d4eSeschrock spa_load_spares(spa); 4352e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4353fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4354fa94a07fSbrendan } 4355fa94a07fSbrendan 4356fa94a07fSbrendan /* 4357fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 4358fa94a07fSbrendan */ 4359fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 4360fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 4361fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 4362fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 4363fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 4364fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 4365e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4366fa94a07fSbrendan spa_load_l2cache(spa); 4367e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4368fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 436999653d4eSeschrock } 437099653d4eSeschrock 4371ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 43720a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 4373fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 4374ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 4375fa9e4066Sahrens 4376485bbbf5SGeorge Wilson /* 4377485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 4378485bbbf5SGeorge Wilson */ 4379485bbbf5SGeorge Wilson ddt_create(spa); 4380485bbbf5SGeorge Wilson 4381485bbbf5SGeorge Wilson spa_update_dspace(spa); 4382485bbbf5SGeorge Wilson 4383fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 4384fa9e4066Sahrens 4385fa9e4066Sahrens /* 4386fa9e4066Sahrens * Create the pool config object. 4387fa9e4066Sahrens */ 4388fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 4389f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 4390fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 4391fa9e4066Sahrens 4392ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 4393fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 4394ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 4395ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 4396ea8dc4b6Seschrock } 4397fa9e4066Sahrens 4398ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 4399ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 4400ad135b5dSChristopher Siden 44013f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 44023f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 44033f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 44043f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 44053f9d6ad7SLin Ling } 44063f9d6ad7SLin Ling 4407990b4856Slling /* Newly created pools with the right version are always deflated. */ 4408990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 4409990b4856Slling spa->spa_deflate = TRUE; 4410990b4856Slling if (zap_add(spa->spa_meta_objset, 4411990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 4412990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 4413990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 4414990b4856Slling } 441599653d4eSeschrock } 441699653d4eSeschrock 4417fa9e4066Sahrens /* 4418cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 4419fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 4420fa9e4066Sahrens * keeps changing. 4421fa9e4066Sahrens */ 4422cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 4423cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 4424cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 4425ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 4426cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 4427cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 4428cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 4429ea8dc4b6Seschrock } 4430b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 4431cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 4432fa9e4066Sahrens 443306eeb2adSek /* 443406eeb2adSek * Create the pool's history object. 443506eeb2adSek */ 4436990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 4437990b4856Slling spa_history_create_obj(spa, tx); 4438990b4856Slling 443945818ee1SMatthew Ahrens /* 444045818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 444145818ee1SMatthew Ahrens */ 444245818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 444345818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 444445818ee1SMatthew Ahrens 4445990b4856Slling /* 4446990b4856Slling * Set pool properties. 4447990b4856Slling */ 4448990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 4449990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 44500a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 4451573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 4452b24ab676SJeff Bonwick 4453379c004dSEric Schrock if (props != NULL) { 4454379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 44553b2aab18SMatthew Ahrens spa_sync_props(props, tx); 4456379c004dSEric Schrock } 445706eeb2adSek 4458fa9e4066Sahrens dmu_tx_commit(tx); 4459fa9e4066Sahrens 4460fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 4461fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 4462fa9e4066Sahrens 4463fa9e4066Sahrens /* 4464fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 4465fa9e4066Sahrens * bean counters are appropriately updated. 4466fa9e4066Sahrens */ 4467fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 4468fa9e4066Sahrens 44695cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 4470ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 4471fa9e4066Sahrens 44724445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 4473228975ccSek 4474bc9014e6SJustin Gibbs /* 4475bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 4476bc9014e6SJustin Gibbs * and are making their way through the eviction process. 4477bc9014e6SJustin Gibbs */ 4478bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4479088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 44800f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 4481088f3894Sahrens 4482daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 4483daaa36a7SGeorge Wilson 4484fa9e4066Sahrens return (0); 4485fa9e4066Sahrens } 4486fa9e4066Sahrens 4487e7cbe64fSgw #ifdef _KERNEL 4488e7cbe64fSgw /* 448921ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 449021ecdf64SLin Ling * during the system boot up time. 4491e7cbe64fSgw */ 449221ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 449321ecdf64SLin Ling 449421ecdf64SLin Ling static nvlist_t * 449521ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 4496e7cbe64fSgw { 449721ecdf64SLin Ling nvlist_t *config; 4498e7cbe64fSgw nvlist_t *nvtop, *nvroot; 4499e7cbe64fSgw uint64_t pgid; 4500e7cbe64fSgw 450121ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 450221ecdf64SLin Ling return (NULL); 450321ecdf64SLin Ling 4504e7cbe64fSgw /* 4505e7cbe64fSgw * Add this top-level vdev to the child array. 4506e7cbe64fSgw */ 450721ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 450821ecdf64SLin Ling &nvtop) == 0); 450921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 451021ecdf64SLin Ling &pgid) == 0); 451121ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 4512e7cbe64fSgw 4513e7cbe64fSgw /* 4514e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 4515e7cbe64fSgw */ 4516e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 451721ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 451821ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 4519e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 4520e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 4521e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4522e7cbe64fSgw &nvtop, 1) == 0); 4523e7cbe64fSgw 4524e7cbe64fSgw /* 4525e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 4526e7cbe64fSgw * this pool's configuration (remove the old, add the new). 4527e7cbe64fSgw */ 4528e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 4529e7cbe64fSgw nvlist_free(nvroot); 453021ecdf64SLin Ling return (config); 4531e7cbe64fSgw } 4532e7cbe64fSgw 4533e7cbe64fSgw /* 453421ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 453521ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 453621ecdf64SLin Ling * device has a more recent txg. 4537051aabe6Staylor */ 453821ecdf64SLin Ling static void 453921ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 4540051aabe6Staylor { 4541573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 454221ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 4543051aabe6Staylor 454421ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 454521ecdf64SLin Ling nvlist_t *label; 454621ecdf64SLin Ling uint64_t label_txg; 4547051aabe6Staylor 454821ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 454921ecdf64SLin Ling &label) != 0) 455021ecdf64SLin Ling return; 4551051aabe6Staylor 455221ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 455321ecdf64SLin Ling &label_txg) == 0); 4554051aabe6Staylor 455521ecdf64SLin Ling /* 455621ecdf64SLin Ling * Do we have a better boot device? 455721ecdf64SLin Ling */ 455821ecdf64SLin Ling if (label_txg > *txg) { 455921ecdf64SLin Ling *txg = label_txg; 456021ecdf64SLin Ling *avd = vd; 4561051aabe6Staylor } 456221ecdf64SLin Ling nvlist_free(label); 4563051aabe6Staylor } 4564051aabe6Staylor } 4565051aabe6Staylor 4566e7cbe64fSgw /* 4567e7cbe64fSgw * Import a root pool. 4568e7cbe64fSgw * 4569051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 4570051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 4571051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 4572e7cbe64fSgw * 4573e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 4574e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 4575e7cbe64fSgw * e.g. 4576e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 4577e7cbe64fSgw */ 4578e7cbe64fSgw int 4579051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 4580e7cbe64fSgw { 458121ecdf64SLin Ling spa_t *spa; 458221ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 458321ecdf64SLin Ling nvlist_t *config, *nvtop; 458421ecdf64SLin Ling uint64_t guid, txg; 4585e7cbe64fSgw char *pname; 4586e7cbe64fSgw int error; 4587e7cbe64fSgw 4588e7cbe64fSgw /* 458921ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 4590e7cbe64fSgw */ 4591dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4592dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 4593dedec472SJack Meng if (config == NULL) { 4594dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 4595dedec472SJack Meng /* iscsi boot */ 4596dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 4597dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4598dedec472SJack Meng } 4599dedec472SJack Meng } 4600dedec472SJack Meng #endif 4601dedec472SJack Meng if (config == NULL) { 4602ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 460321ecdf64SLin Ling devpath); 4604be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 460521ecdf64SLin Ling } 4606e7cbe64fSgw 460721ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 460821ecdf64SLin Ling &pname) == 0); 460921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 4610e7cbe64fSgw 46116809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 46126809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 46136809eb4eSEric Schrock /* 46146809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 46156809eb4eSEric Schrock * can replace it with the correct config we just read in. 46166809eb4eSEric Schrock */ 46176809eb4eSEric Schrock spa_remove(spa); 46186809eb4eSEric Schrock } 46196809eb4eSEric Schrock 4620468c413aSTim Haley spa = spa_add(pname, config, NULL); 46216809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 46224b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 46230fb055e8SAndy Fiddaman if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 46240fb055e8SAndy Fiddaman &spa->spa_ubsync.ub_version) != 0) 46250fb055e8SAndy Fiddaman spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 4626e7cbe64fSgw 462721ecdf64SLin Ling /* 462821ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 462921ecdf64SLin Ling */ 463021ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 463121ecdf64SLin Ling &nvtop) == 0); 463221ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 463321ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 463421ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 463521ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 463621ecdf64SLin Ling if (error) { 463721ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 463821ecdf64SLin Ling nvlist_free(config); 463921ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 464021ecdf64SLin Ling pname); 464121ecdf64SLin Ling return (error); 464221ecdf64SLin Ling } 464321ecdf64SLin Ling 464421ecdf64SLin Ling /* 464521ecdf64SLin Ling * Get the boot vdev. 464621ecdf64SLin Ling */ 464721ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 464821ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 464921ecdf64SLin Ling (u_longlong_t)guid); 4650be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 465121ecdf64SLin Ling goto out; 465221ecdf64SLin Ling } 4653e7cbe64fSgw 465421ecdf64SLin Ling /* 465521ecdf64SLin Ling * Determine if there is a better boot device. 465621ecdf64SLin Ling */ 465721ecdf64SLin Ling avd = bvd; 465821ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 465921ecdf64SLin Ling if (avd != bvd) { 466021ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 466121ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4662be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 466321ecdf64SLin Ling goto out; 466421ecdf64SLin Ling } 4665e7cbe64fSgw 466621ecdf64SLin Ling /* 466721ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 466821ecdf64SLin Ling * we're booting off the active spare. 466921ecdf64SLin Ling */ 467021ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 467121ecdf64SLin Ling !bvd->vdev_isspare) { 467221ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 467321ecdf64SLin Ling "try booting from '%s'", 4674cb04b873SMark J Musante bvd->vdev_parent-> 4675cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4676be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 467721ecdf64SLin Ling goto out; 467821ecdf64SLin Ling } 467921ecdf64SLin Ling 468021ecdf64SLin Ling error = 0; 468121ecdf64SLin Ling out: 468221ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 468321ecdf64SLin Ling vdev_free(rvd); 468421ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 468521ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 468621ecdf64SLin Ling 468721ecdf64SLin Ling nvlist_free(config); 4688e7cbe64fSgw return (error); 4689e7cbe64fSgw } 469021ecdf64SLin Ling 4691e7cbe64fSgw #endif 4692e7cbe64fSgw 46936809eb4eSEric Schrock /* 46946809eb4eSEric Schrock * Import a non-root pool into the system. 46956809eb4eSEric Schrock */ 4696c5904d13Seschrock int 46974b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4698c5904d13Seschrock { 46996809eb4eSEric Schrock spa_t *spa; 47006809eb4eSEric Schrock char *altroot = NULL; 4701468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4702468c413aSTim Haley zpool_rewind_policy_t policy; 4703f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4704f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 47056809eb4eSEric Schrock int error; 47066809eb4eSEric Schrock nvlist_t *nvroot; 47076809eb4eSEric Schrock nvlist_t **spares, **l2cache; 47086809eb4eSEric Schrock uint_t nspares, nl2cache; 47096809eb4eSEric Schrock 47106809eb4eSEric Schrock /* 47116809eb4eSEric Schrock * If a pool with this name exists, return failure. 47126809eb4eSEric Schrock */ 47136809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 47141195e687SMark J Musante if (spa_lookup(pool) != NULL) { 47156809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4716be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 47176809eb4eSEric Schrock } 47186809eb4eSEric Schrock 47196809eb4eSEric Schrock /* 47206809eb4eSEric Schrock * Create and initialize the spa structure. 47216809eb4eSEric Schrock */ 47226809eb4eSEric Schrock (void) nvlist_lookup_string(props, 47236809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4724f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4725f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4726f9af39baSGeorge Wilson if (readonly) 4727f9af39baSGeorge Wilson mode = FREAD; 4728468c413aSTim Haley spa = spa_add(pool, config, altroot); 47294b964adaSGeorge Wilson spa->spa_import_flags = flags; 47304b964adaSGeorge Wilson 47314b964adaSGeorge Wilson /* 47324b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 47334b964adaSGeorge Wilson * as if it had been loaded at boot. 47344b964adaSGeorge Wilson */ 47354b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 47364b964adaSGeorge Wilson if (props != NULL) 47374b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 47384b964adaSGeorge Wilson 47395cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 4740ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 47413ee8c80cSPavel Zakharov zfs_dbgmsg("spa_import: verbatim import of %s", pool); 47424b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 47434b964adaSGeorge Wilson return (0); 47444b964adaSGeorge Wilson } 47454b964adaSGeorge Wilson 4746f9af39baSGeorge Wilson spa_activate(spa, mode); 47476809eb4eSEric Schrock 474825f89ee2SJeff Bonwick /* 474925f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 475025f89ee2SJeff Bonwick */ 475125f89ee2SJeff Bonwick spa_async_suspend(spa); 475225f89ee2SJeff Bonwick 47534b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 47544b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 47554b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 47564b964adaSGeorge Wilson 4757*6f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; 47584b964adaSGeorge Wilson 4759*6f793812SPavel Zakharov if (state != SPA_LOAD_RECOVER) { 4760*6f793812SPavel Zakharov spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 4761*6f793812SPavel Zakharov zfs_dbgmsg("spa_import: importing %s", pool); 4762*6f793812SPavel Zakharov } else { 4763*6f793812SPavel Zakharov zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " 4764*6f793812SPavel Zakharov "(RECOVERY MODE)", pool, (longlong_t)policy.zrp_txg); 4765*6f793812SPavel Zakharov } 4766*6f793812SPavel Zakharov error = spa_load_best(spa, state, policy.zrp_txg, policy.zrp_request); 4767468c413aSTim Haley 4768468c413aSTim Haley /* 47694b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 47704b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4771468c413aSTim Haley */ 47724b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 47734b964adaSGeorge Wilson spa->spa_load_info) == 0); 47746809eb4eSEric Schrock 47756809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 47766809eb4eSEric Schrock /* 47776809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 47786809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 47796809eb4eSEric Schrock */ 47806809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 47816809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 47826809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 47836809eb4eSEric Schrock spa_load_spares(spa); 47846809eb4eSEric Schrock } 47856809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 47866809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 47876809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 47886809eb4eSEric Schrock spa_load_l2cache(spa); 47896809eb4eSEric Schrock } 47906809eb4eSEric Schrock 47916809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 47926809eb4eSEric Schrock &nvroot) == 0); 47936809eb4eSEric Schrock if (error == 0) 47946809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 47956809eb4eSEric Schrock VDEV_ALLOC_SPARE); 47966809eb4eSEric Schrock if (error == 0) 47976809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 47986809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 47996809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 48006809eb4eSEric Schrock 48016809eb4eSEric Schrock if (props != NULL) 48026809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 48036809eb4eSEric Schrock 48046809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 48056809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 48066809eb4eSEric Schrock spa_unload(spa); 48076809eb4eSEric Schrock spa_deactivate(spa); 48086809eb4eSEric Schrock spa_remove(spa); 48096809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 48106809eb4eSEric Schrock return (error); 48116809eb4eSEric Schrock } 48126809eb4eSEric Schrock 4813955ef359SLin Ling spa_async_resume(spa); 4814955ef359SLin Ling 48156809eb4eSEric Schrock /* 48166809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 48176809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 48186809eb4eSEric Schrock */ 48196809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 48206809eb4eSEric Schrock &spares, &nspares) == 0) { 48216809eb4eSEric Schrock if (spa->spa_spares.sav_config) 48226809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 48236809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 48246809eb4eSEric Schrock else 48256809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 48266809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 48276809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 48286809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 48296809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 48306809eb4eSEric Schrock spa_load_spares(spa); 48316809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 48326809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 48336809eb4eSEric Schrock } 48346809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 48356809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 48366809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 48376809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 48386809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 48396809eb4eSEric Schrock else 48406809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 48416809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 48426809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 48436809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 48446809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 48456809eb4eSEric Schrock spa_load_l2cache(spa); 48466809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 48476809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 48486809eb4eSEric Schrock } 48496809eb4eSEric Schrock 4850b693757aSEric Schrock /* 4851b693757aSEric Schrock * Check for any removed devices. 4852b693757aSEric Schrock */ 4853b693757aSEric Schrock if (spa->spa_autoreplace) { 4854b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4855b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4856b693757aSEric Schrock } 4857b693757aSEric Schrock 48586809eb4eSEric Schrock if (spa_writeable(spa)) { 48596809eb4eSEric Schrock /* 48606809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 48616809eb4eSEric Schrock */ 4862bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 48636809eb4eSEric Schrock } 48646809eb4eSEric Schrock 4865573ca77eSGeorge Wilson /* 4866573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4867573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4868573ca77eSGeorge Wilson */ 4869573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4870573ca77eSGeorge Wilson 48714445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 48726809eb4eSEric Schrock 4873ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 487414372834SHans Rosenfeld 487514372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 487614372834SHans Rosenfeld 48776809eb4eSEric Schrock return (0); 4878c5904d13Seschrock } 4879c5904d13Seschrock 4880fa9e4066Sahrens nvlist_t * 4881fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4882fa9e4066Sahrens { 4883fa9e4066Sahrens nvlist_t *config = NULL; 4884*6f793812SPavel Zakharov char *poolname, *cachefile; 4885fa9e4066Sahrens spa_t *spa; 4886fa9e4066Sahrens uint64_t state; 48877b7154beSLin Ling int error; 4888*6f793812SPavel Zakharov zpool_rewind_policy_t policy; 4889fa9e4066Sahrens 4890fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4891fa9e4066Sahrens return (NULL); 4892fa9e4066Sahrens 4893fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4894fa9e4066Sahrens return (NULL); 4895fa9e4066Sahrens 4896fa9e4066Sahrens /* 48970373e76bSbonwick * Create and initialize the spa structure. 4898fa9e4066Sahrens */ 48990373e76bSbonwick mutex_enter(&spa_namespace_lock); 4900468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 49018ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4902fa9e4066Sahrens 4903fa9e4066Sahrens /* 4904*6f793812SPavel Zakharov * Rewind pool if a max txg was provided. Note that even though we 4905*6f793812SPavel Zakharov * retrieve the complete rewind policy, only the rewind txg is relevant 4906*6f793812SPavel Zakharov * for tryimport. 4907fa9e4066Sahrens */ 4908*6f793812SPavel Zakharov zpool_get_rewind_policy(spa->spa_config, &policy); 4909*6f793812SPavel Zakharov if (policy.zrp_txg != UINT64_MAX) { 4910*6f793812SPavel Zakharov spa->spa_load_max_txg = policy.zrp_txg; 4911*6f793812SPavel Zakharov spa->spa_extreme_rewind = B_TRUE; 4912*6f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", 4913*6f793812SPavel Zakharov poolname, (longlong_t)policy.zrp_txg); 4914*6f793812SPavel Zakharov } else { 4915*6f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: importing %s", poolname); 4916*6f793812SPavel Zakharov } 4917*6f793812SPavel Zakharov 4918*6f793812SPavel Zakharov if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) 4919*6f793812SPavel Zakharov == 0) { 4920*6f793812SPavel Zakharov zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); 4921*6f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 4922*6f793812SPavel Zakharov } else { 4923*6f793812SPavel Zakharov spa->spa_config_source = SPA_CONFIG_SRC_SCAN; 4924*6f793812SPavel Zakharov } 4925*6f793812SPavel Zakharov 4926*6f793812SPavel Zakharov error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); 4927fa9e4066Sahrens 4928fa9e4066Sahrens /* 4929fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4930fa9e4066Sahrens */ 4931fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4932fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4933fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4934fa9e4066Sahrens poolname) == 0); 4935fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4936fa9e4066Sahrens state) == 0); 493795173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 493895173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4939ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4940ad135b5dSChristopher Siden spa->spa_load_info) == 0); 494199653d4eSeschrock 4942e7cbe64fSgw /* 4943e7cbe64fSgw * If the bootfs property exists on this pool then we 4944e7cbe64fSgw * copy it out so that external consumers can tell which 4945e7cbe64fSgw * pools are bootable. 4946e7cbe64fSgw */ 49477b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4948e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4949e7cbe64fSgw 4950e7cbe64fSgw /* 4951e7cbe64fSgw * We have to play games with the name since the 4952e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4953e7cbe64fSgw */ 4954e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4955e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4956e7cbe64fSgw char *cp; 4957e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4958e7cbe64fSgw 4959e7cbe64fSgw cp = strchr(tmpname, '/'); 4960e7cbe64fSgw if (cp == NULL) { 4961e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4962e7cbe64fSgw MAXPATHLEN); 4963e7cbe64fSgw } else { 4964e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4965e7cbe64fSgw "%s/%s", poolname, ++cp); 4966e7cbe64fSgw } 4967e7cbe64fSgw VERIFY(nvlist_add_string(config, 4968e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4969e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4970e7cbe64fSgw } 4971e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4972e7cbe64fSgw } 4973e7cbe64fSgw 497499653d4eSeschrock /* 4975fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 497699653d4eSeschrock */ 49776809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 497899653d4eSeschrock spa_add_spares(spa, config); 4979fa94a07fSbrendan spa_add_l2cache(spa, config); 49806809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4981fa9e4066Sahrens } 4982fa9e4066Sahrens 4983fa9e4066Sahrens spa_unload(spa); 4984fa9e4066Sahrens spa_deactivate(spa); 4985fa9e4066Sahrens spa_remove(spa); 4986fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4987fa9e4066Sahrens 4988fa9e4066Sahrens return (config); 4989fa9e4066Sahrens } 4990fa9e4066Sahrens 4991fa9e4066Sahrens /* 4992fa9e4066Sahrens * Pool export/destroy 4993fa9e4066Sahrens * 4994fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4995fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4996fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4997394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4998394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4999fa9e4066Sahrens */ 5000fa9e4066Sahrens static int 500189a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 5002394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 5003fa9e4066Sahrens { 5004fa9e4066Sahrens spa_t *spa; 5005fa9e4066Sahrens 500644cd46caSbillm if (oldconfig) 500744cd46caSbillm *oldconfig = NULL; 500844cd46caSbillm 50098ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 5010be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 5011fa9e4066Sahrens 5012fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 5013fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 5014fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5015be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 5016fa9e4066Sahrens } 5017fa9e4066Sahrens 5018ea8dc4b6Seschrock /* 5019ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 5020ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 5021ea8dc4b6Seschrock */ 5022ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 5023ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 5024ea8dc4b6Seschrock spa_async_suspend(spa); 5025ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 5026ea8dc4b6Seschrock spa_close(spa, FTAG); 5027ea8dc4b6Seschrock 5028fa9e4066Sahrens /* 5029fa9e4066Sahrens * The pool will be in core if it's openable, 5030fa9e4066Sahrens * in which case we can modify its state. 5031fa9e4066Sahrens */ 5032fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 5033fa9e4066Sahrens /* 5034fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 5035fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 5036fa9e4066Sahrens */ 5037fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 5038bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 5039fa9e4066Sahrens 5040ea8dc4b6Seschrock /* 5041ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 5042ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 5043ea8dc4b6Seschrock * fault injection handlers. 5044ea8dc4b6Seschrock */ 5045ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 5046ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 5047ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 5048ea8dc4b6Seschrock spa_async_resume(spa); 5049fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5050be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5051fa9e4066Sahrens } 5052fa9e4066Sahrens 505389a89ebfSlling /* 505489a89ebfSlling * A pool cannot be exported if it has an active shared spare. 505589a89ebfSlling * This is to prevent other pools stealing the active spare 505689a89ebfSlling * from an exported pool. At user's own will, such pool can 505789a89ebfSlling * be forcedly exported. 505889a89ebfSlling */ 505989a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 506089a89ebfSlling spa_has_active_shared_spare(spa)) { 506189a89ebfSlling spa_async_resume(spa); 506289a89ebfSlling mutex_exit(&spa_namespace_lock); 5063be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 506489a89ebfSlling } 506589a89ebfSlling 5066fa9e4066Sahrens /* 5067fa9e4066Sahrens * We want this to be reflected on every label, 5068fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 5069fa9e4066Sahrens * final sync that pushes these changes out. 5070fa9e4066Sahrens */ 5071394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 5072e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5073ea8dc4b6Seschrock spa->spa_state = new_state; 50743f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 50753f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 5076ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 5077e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5078ea8dc4b6Seschrock } 5079fa9e4066Sahrens } 5080fa9e4066Sahrens 5081ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 50823d7072f8Seschrock 5083fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 5084fa9e4066Sahrens spa_unload(spa); 5085fa9e4066Sahrens spa_deactivate(spa); 5086fa9e4066Sahrens } 5087fa9e4066Sahrens 508844cd46caSbillm if (oldconfig && spa->spa_config) 508944cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 509044cd46caSbillm 5091ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 5092394ab0cbSGeorge Wilson if (!hardforce) 50935cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 5094ea8dc4b6Seschrock spa_remove(spa); 5095ea8dc4b6Seschrock } 5096fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 5097fa9e4066Sahrens 5098fa9e4066Sahrens return (0); 5099fa9e4066Sahrens } 5100fa9e4066Sahrens 5101fa9e4066Sahrens /* 5102fa9e4066Sahrens * Destroy a storage pool. 5103fa9e4066Sahrens */ 5104fa9e4066Sahrens int 5105fa9e4066Sahrens spa_destroy(char *pool) 5106fa9e4066Sahrens { 5107394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 5108394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 5109fa9e4066Sahrens } 5110fa9e4066Sahrens 5111fa9e4066Sahrens /* 5112fa9e4066Sahrens * Export a storage pool. 5113fa9e4066Sahrens */ 5114fa9e4066Sahrens int 5115394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 5116394ab0cbSGeorge Wilson boolean_t hardforce) 5117fa9e4066Sahrens { 5118394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 5119394ab0cbSGeorge Wilson force, hardforce)); 5120fa9e4066Sahrens } 5121fa9e4066Sahrens 5122ea8dc4b6Seschrock /* 5123ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 5124ea8dc4b6Seschrock * from the namespace in any way. 5125ea8dc4b6Seschrock */ 5126ea8dc4b6Seschrock int 5127ea8dc4b6Seschrock spa_reset(char *pool) 5128ea8dc4b6Seschrock { 512989a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 5130394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 5131ea8dc4b6Seschrock } 5132ea8dc4b6Seschrock 5133fa9e4066Sahrens /* 5134fa9e4066Sahrens * ========================================================================== 5135fa9e4066Sahrens * Device manipulation 5136fa9e4066Sahrens * ========================================================================== 5137fa9e4066Sahrens */ 5138fa9e4066Sahrens 5139fa9e4066Sahrens /* 51408654d025Sperrin * Add a device to a storage pool. 5141fa9e4066Sahrens */ 5142fa9e4066Sahrens int 5143fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 5144fa9e4066Sahrens { 514588ecc943SGeorge Wilson uint64_t txg, id; 51468ad4d6ddSJeff Bonwick int error; 5147fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 51480e34b6a7Sbonwick vdev_t *vd, *tvd; 5149fa94a07fSbrendan nvlist_t **spares, **l2cache; 5150fa94a07fSbrendan uint_t nspares, nl2cache; 5151fa9e4066Sahrens 5152f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5153f9af39baSGeorge Wilson 5154fa9e4066Sahrens txg = spa_vdev_enter(spa); 5155fa9e4066Sahrens 515699653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 515799653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 515899653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 5159fa9e4066Sahrens 5160e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 516199653d4eSeschrock 5162fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 5163fa94a07fSbrendan &nspares) != 0) 516499653d4eSeschrock nspares = 0; 516599653d4eSeschrock 5166fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 5167fa94a07fSbrendan &nl2cache) != 0) 5168fa94a07fSbrendan nl2cache = 0; 5169fa94a07fSbrendan 5170e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 5171fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 5172fa9e4066Sahrens 5173e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 5174e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 5175e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 517699653d4eSeschrock 517739c23413Seschrock /* 5178fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 5179fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 518039c23413Seschrock */ 5181e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 518239c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 518339c23413Seschrock 518439c23413Seschrock /* 51855cabbc6bSPrashanth Sreenivasa * If we are in the middle of a device removal, we can only add 51865cabbc6bSPrashanth Sreenivasa * devices which match the existing devices in the pool. 51875cabbc6bSPrashanth Sreenivasa * If we are in the middle of a removal, or have some indirect 51885cabbc6bSPrashanth Sreenivasa * vdevs, we can not add raidz toplevels. 518939c23413Seschrock */ 51905cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 51915cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 51925cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 51935cabbc6bSPrashanth Sreenivasa tvd = vd->vdev_child[c]; 51945cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL && 51955cabbc6bSPrashanth Sreenivasa tvd->vdev_ashift != 51965cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal->svr_vdev->vdev_ashift) { 51975cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 51985cabbc6bSPrashanth Sreenivasa } 51995cabbc6bSPrashanth Sreenivasa /* Fail if top level vdev is raidz */ 52005cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_raidz_ops) { 52015cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 52025cabbc6bSPrashanth Sreenivasa } 52035cabbc6bSPrashanth Sreenivasa /* 52045cabbc6bSPrashanth Sreenivasa * Need the top level mirror to be 52055cabbc6bSPrashanth Sreenivasa * a mirror of leaf vdevs only 52065cabbc6bSPrashanth Sreenivasa */ 52075cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_mirror_ops) { 52085cabbc6bSPrashanth Sreenivasa for (uint64_t cid = 0; 52095cabbc6bSPrashanth Sreenivasa cid < tvd->vdev_children; cid++) { 52105cabbc6bSPrashanth Sreenivasa vdev_t *cvd = tvd->vdev_child[cid]; 52115cabbc6bSPrashanth Sreenivasa if (!cvd->vdev_ops->vdev_op_leaf) { 52125cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, 52135cabbc6bSPrashanth Sreenivasa txg, EINVAL)); 52145cabbc6bSPrashanth Sreenivasa } 52155cabbc6bSPrashanth Sreenivasa } 52165cabbc6bSPrashanth Sreenivasa } 52175cabbc6bSPrashanth Sreenivasa } 52185cabbc6bSPrashanth Sreenivasa } 52195cabbc6bSPrashanth Sreenivasa 52208ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 522188ecc943SGeorge Wilson 522288ecc943SGeorge Wilson /* 522388ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 522488ecc943SGeorge Wilson */ 522588ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 522688ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 522788ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 522888ecc943SGeorge Wilson break; 522988ecc943SGeorge Wilson } 523088ecc943SGeorge Wilson } 523139c23413Seschrock tvd = vd->vdev_child[c]; 523239c23413Seschrock vdev_remove_child(vd, tvd); 523388ecc943SGeorge Wilson tvd->vdev_id = id; 523439c23413Seschrock vdev_add_child(rvd, tvd); 523539c23413Seschrock vdev_config_dirty(tvd); 523639c23413Seschrock } 523739c23413Seschrock 523899653d4eSeschrock if (nspares != 0) { 5239fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 5240fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 524199653d4eSeschrock spa_load_spares(spa); 5242fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 5243fa94a07fSbrendan } 5244fa94a07fSbrendan 5245fa94a07fSbrendan if (nl2cache != 0) { 5246fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 5247fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 5248fa94a07fSbrendan spa_load_l2cache(spa); 5249fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 5250fa9e4066Sahrens } 5251fa9e4066Sahrens 5252fa9e4066Sahrens /* 52530e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 52540e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 52550e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 52560e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 52570e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 52580e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 52590373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 52600e34b6a7Sbonwick * 52610e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 52620e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 52630e34b6a7Sbonwick * steps will be completed the next time we load the pool. 52640e34b6a7Sbonwick */ 52650373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 52660e34b6a7Sbonwick 52670373e76bSbonwick mutex_enter(&spa_namespace_lock); 52680373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5269ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 52700373e76bSbonwick mutex_exit(&spa_namespace_lock); 5271fa9e4066Sahrens 52720373e76bSbonwick return (0); 5273fa9e4066Sahrens } 5274fa9e4066Sahrens 5275fa9e4066Sahrens /* 5276fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 5277fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 5278fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 5279fa9e4066Sahrens * 5280fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 5281fa9e4066Sahrens * existing device; in this case the two devices are made into their own 52823d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 5283fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 5284fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 5285fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 5286fa9e4066Sahrens * is automatically detached. 5287fa9e4066Sahrens */ 5288fa9e4066Sahrens int 5289ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 5290fa9e4066Sahrens { 52913f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 5292fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 5293fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 529499653d4eSeschrock vdev_ops_t *pvops; 52959b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 52969b3f6b42SEric Kustarz int newvd_isspare; 52979b3f6b42SEric Kustarz int error; 5298fa9e4066Sahrens 5299f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5300f9af39baSGeorge Wilson 5301fa9e4066Sahrens txg = spa_vdev_enter(spa); 5302fa9e4066Sahrens 5303c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 5304fa9e4066Sahrens 53055cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 53065cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 53075cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 53085cabbc6bSPrashanth Sreenivasa } 53095cabbc6bSPrashanth Sreenivasa 5310fa9e4066Sahrens if (oldvd == NULL) 5311fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 5312fa9e4066Sahrens 53130e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 53140e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 53150e34b6a7Sbonwick 5316fa9e4066Sahrens pvd = oldvd->vdev_parent; 5317fa9e4066Sahrens 531899653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 5319cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 53203d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 53213d7072f8Seschrock 53223d7072f8Seschrock if (newrootvd->vdev_children != 1) 5323fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 5324fa9e4066Sahrens 5325fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 5326fa9e4066Sahrens 5327fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 5328fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 5329fa9e4066Sahrens 533099653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 5331fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 5332fa9e4066Sahrens 53338654d025Sperrin /* 53348654d025Sperrin * Spares can't replace logs 53358654d025Sperrin */ 5336ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 53378654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 53388654d025Sperrin 533999653d4eSeschrock if (!replacing) { 534099653d4eSeschrock /* 534199653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 534299653d4eSeschrock * vdev. 534399653d4eSeschrock */ 534499653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 534599653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 534699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 534799653d4eSeschrock 534899653d4eSeschrock pvops = &vdev_mirror_ops; 534999653d4eSeschrock } else { 535099653d4eSeschrock /* 535199653d4eSeschrock * Active hot spares can only be replaced by inactive hot 535299653d4eSeschrock * spares. 535399653d4eSeschrock */ 535499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 5355cb04b873SMark J Musante oldvd->vdev_isspare && 535699653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 535799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 535899653d4eSeschrock 535999653d4eSeschrock /* 536099653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 536199653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 536239c23413Seschrock * want to create a replacing vdev. The user is not allowed to 536339c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 536439c23413Seschrock * the same (spare replaces spare, non-spare replaces 536539c23413Seschrock * non-spare). 536699653d4eSeschrock */ 5367cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 5368cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 536999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 5370cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 5371cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 537239c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 5373cb04b873SMark J Musante } 5374cb04b873SMark J Musante 5375cb04b873SMark J Musante if (newvd->vdev_isspare) 537699653d4eSeschrock pvops = &vdev_spare_ops; 537799653d4eSeschrock else 537899653d4eSeschrock pvops = &vdev_replacing_ops; 537999653d4eSeschrock } 538099653d4eSeschrock 53812a79c5feSlling /* 5382573ca77eSGeorge Wilson * Make sure the new device is big enough. 53832a79c5feSlling */ 5384573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 5385fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 5386fa9e4066Sahrens 5387ecc2d604Sbonwick /* 5388ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 5389ecc2d604Sbonwick * than the top-level vdev. 5390ecc2d604Sbonwick */ 5391ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 5392fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 5393fa9e4066Sahrens 5394fa9e4066Sahrens /* 5395fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 5396fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 5397fa9e4066Sahrens */ 5398fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 5399fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 5400fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 5401fa9e4066Sahrens KM_SLEEP); 5402fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 5403fa9e4066Sahrens newvd->vdev_path, "old"); 5404fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 5405fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 5406fa9e4066Sahrens oldvd->vdev_devid = NULL; 5407fa9e4066Sahrens } 5408fa9e4066Sahrens } 5409fa9e4066Sahrens 5410cb04b873SMark J Musante /* mark the device being resilvered */ 5411b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 5412cb04b873SMark J Musante 5413fa9e4066Sahrens /* 541499653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 541599653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 5416fa9e4066Sahrens */ 5417fa9e4066Sahrens if (pvd->vdev_ops != pvops) 5418fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 5419fa9e4066Sahrens 5420fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 5421fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 5422fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 5423fa9e4066Sahrens 5424fa9e4066Sahrens /* 5425fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 5426fa9e4066Sahrens */ 5427fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 5428fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 542988ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 5430fa9e4066Sahrens vdev_add_child(pvd, newvd); 5431fa9e4066Sahrens 5432fa9e4066Sahrens tvd = newvd->vdev_top; 5433fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 5434fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 5435fa9e4066Sahrens 5436fa9e4066Sahrens vdev_config_dirty(tvd); 5437fa9e4066Sahrens 5438fa9e4066Sahrens /* 54393f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 54403f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 54413f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 5442fa9e4066Sahrens */ 54433f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 5444fa9e4066Sahrens 54453f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 54463f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 5447fa9e4066Sahrens 54486809eb4eSEric Schrock if (newvd->vdev_isspare) { 544939c23413Seschrock spa_spare_activate(newvd); 5450ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 54516809eb4eSEric Schrock } 54526809eb4eSEric Schrock 5453e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 5454e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 54559b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 5456ea8dc4b6Seschrock 5457fa9e4066Sahrens /* 5458fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 5459fa9e4066Sahrens */ 5460ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 5461fa9e4066Sahrens 54623f9d6ad7SLin Ling /* 54630713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 54640713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 54650713e232SGeorge Wilson * respective datasets. 54663f9d6ad7SLin Ling */ 54673f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 54683f9d6ad7SLin Ling 546914372834SHans Rosenfeld if (spa->spa_bootfs) 5470ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 547114372834SHans Rosenfeld 5472ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 547314372834SHans Rosenfeld 54743f9d6ad7SLin Ling /* 54753f9d6ad7SLin Ling * Commit the config 54763f9d6ad7SLin Ling */ 54773f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 5478fa9e4066Sahrens 54794445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 54803f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 5481c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 5482c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 5483c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 54849b3f6b42SEric Kustarz 54859b3f6b42SEric Kustarz spa_strfree(oldvdpath); 54869b3f6b42SEric Kustarz spa_strfree(newvdpath); 54879b3f6b42SEric Kustarz 5488fa9e4066Sahrens return (0); 5489fa9e4066Sahrens } 5490fa9e4066Sahrens 5491fa9e4066Sahrens /* 5492fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 5493f7170741SWill Andrews * 5494fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 5495fa9e4066Sahrens * is a replacing vdev. 5496fa9e4066Sahrens */ 5497fa9e4066Sahrens int 54988ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 5499fa9e4066Sahrens { 5500fa9e4066Sahrens uint64_t txg; 55018ad4d6ddSJeff Bonwick int error; 5502fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 5503fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 550499653d4eSeschrock boolean_t unspare = B_FALSE; 5505d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 55061195e687SMark J Musante char *vdpath; 5507fa9e4066Sahrens 5508f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5509f9af39baSGeorge Wilson 5510fa9e4066Sahrens txg = spa_vdev_enter(spa); 5511fa9e4066Sahrens 5512c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5513fa9e4066Sahrens 5514fa9e4066Sahrens if (vd == NULL) 5515fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 5516fa9e4066Sahrens 55170e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 55180e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 55190e34b6a7Sbonwick 5520fa9e4066Sahrens pvd = vd->vdev_parent; 5521fa9e4066Sahrens 55228ad4d6ddSJeff Bonwick /* 55238ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 55248ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 55258ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 55268ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 55278ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 55288ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 55298ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 55308ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 55318ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 55328ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 55338ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 55348ad4d6ddSJeff Bonwick */ 55358ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 55368ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 55378ad4d6ddSJeff Bonwick 5538fa9e4066Sahrens /* 5539cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 554099653d4eSeschrock */ 5541cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 5542cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 5543cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 554499653d4eSeschrock 554599653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 5546e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 5547fa9e4066Sahrens 5548fa9e4066Sahrens /* 554999653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 5550fa9e4066Sahrens */ 5551fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 555299653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 555399653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 5554fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 5555fa9e4066Sahrens 5556fa9e4066Sahrens /* 55578ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 55588ad4d6ddSJeff Bonwick * we cannot safely detach it. 5559fa9e4066Sahrens */ 55608ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 5561fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 5562fa9e4066Sahrens 55638ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 5564fa9e4066Sahrens 5565bf82a41bSeschrock /* 5566bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 5567bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 5568bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 5569bf82a41bSeschrock */ 5570cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 5571cb04b873SMark J Musante vd->vdev_path != NULL) { 5572cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 5573cb04b873SMark J Musante 5574cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 5575cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 5576cb04b873SMark J Musante 5577cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 5578cb04b873SMark J Musante continue; 5579cb04b873SMark J Musante 5580cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 5581cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 5582cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 5583cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 5584cb04b873SMark J Musante break; 5585cb04b873SMark J Musante } 5586bf82a41bSeschrock } 5587bf82a41bSeschrock } 5588bf82a41bSeschrock 558999653d4eSeschrock /* 559099653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 559199653d4eSeschrock * that the spare should become a real disk, and be removed from the 559299653d4eSeschrock * active spare list for the pool. 559399653d4eSeschrock */ 559499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 5595cb04b873SMark J Musante vd->vdev_id == 0 && 5596cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 559799653d4eSeschrock unspare = B_TRUE; 559899653d4eSeschrock 5599fa9e4066Sahrens /* 5600fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 5601fa9e4066Sahrens * This must be done after all other error cases are handled, 5602fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 5603fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 5604fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 5605fa9e4066Sahrens * it's being detached! 5606fa9e4066Sahrens */ 560739c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5608fa9e4066Sahrens 5609fa9e4066Sahrens /* 5610fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 5611fa9e4066Sahrens */ 5612fa9e4066Sahrens vdev_remove_child(pvd, vd); 5613fa9e4066Sahrens vdev_compact_children(pvd); 5614fa9e4066Sahrens 5615fa9e4066Sahrens /* 5616fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 5617fa9e4066Sahrens */ 5618cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 5619fa9e4066Sahrens 562099653d4eSeschrock /* 562199653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 56228ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 56238ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 56248ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 56258ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 56268ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 56278ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 562899653d4eSeschrock */ 562999653d4eSeschrock if (unspare) { 563099653d4eSeschrock ASSERT(cvd->vdev_isspare); 563139c23413Seschrock spa_spare_remove(cvd); 563299653d4eSeschrock unspare_guid = cvd->vdev_guid; 56338ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 5634cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 563599653d4eSeschrock } 563699653d4eSeschrock 5637fa9e4066Sahrens /* 5638fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 5639fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 5640fa9e4066Sahrens */ 5641cb04b873SMark J Musante if (pvd->vdev_children == 1) { 5642cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 5643cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 5644fa9e4066Sahrens vdev_remove_parent(cvd); 5645cb04b873SMark J Musante } 5646cb04b873SMark J Musante 5647fa9e4066Sahrens 5648fa9e4066Sahrens /* 5649fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 5650fa9e4066Sahrens * may have been the previous top-level vdev. 5651fa9e4066Sahrens */ 5652fa9e4066Sahrens tvd = cvd->vdev_top; 5653fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 5654fa9e4066Sahrens 5655fa9e4066Sahrens /* 565639c23413Seschrock * Reevaluate the parent vdev state. 5657fa9e4066Sahrens */ 56583d7072f8Seschrock vdev_propagate_state(cvd); 5659fa9e4066Sahrens 5660fa9e4066Sahrens /* 5661573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 5662573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 5663573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 5664573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 5665573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 5666fa9e4066Sahrens */ 5667573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 5668573ca77eSGeorge Wilson vdev_reopen(tvd); 5669573ca77eSGeorge Wilson vdev_expand(tvd, txg); 5670573ca77eSGeorge Wilson } 5671fa9e4066Sahrens 5672fa9e4066Sahrens vdev_config_dirty(tvd); 5673fa9e4066Sahrens 5674fa9e4066Sahrens /* 567539c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 567639c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 567739c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 567839c23413Seschrock * prevent vd from being accessed after it's freed. 5679fa9e4066Sahrens */ 56801195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 56818ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 5682fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 5683ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 5684ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 5685fa9e4066Sahrens 5686ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 56873d7072f8Seschrock 5688cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 5689cb04b873SMark J Musante spa_open_ref(spa, FTAG); 5690cb04b873SMark J Musante 569199653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 569299653d4eSeschrock 56934445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 56941195e687SMark J Musante "vdev=%s", vdpath); 56951195e687SMark J Musante spa_strfree(vdpath); 56961195e687SMark J Musante 569799653d4eSeschrock /* 569839c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 569939c23413Seschrock * then we want to go through and remove the device from the hot spare 570039c23413Seschrock * list of every other pool. 570199653d4eSeschrock */ 570299653d4eSeschrock if (unspare) { 5703cb04b873SMark J Musante spa_t *altspa = NULL; 5704cb04b873SMark J Musante 570599653d4eSeschrock mutex_enter(&spa_namespace_lock); 5706cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 5707cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 5708cb04b873SMark J Musante altspa == spa) 570999653d4eSeschrock continue; 5710cb04b873SMark J Musante 5711cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 57129af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 5713cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 57149af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 5715cb04b873SMark J Musante spa_close(altspa, FTAG); 571699653d4eSeschrock } 571799653d4eSeschrock mutex_exit(&spa_namespace_lock); 5718cb04b873SMark J Musante 5719cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 5720cb04b873SMark J Musante spa_vdev_resilver_done(spa); 572199653d4eSeschrock } 572299653d4eSeschrock 5723cb04b873SMark J Musante /* all done with the spa; OK to release */ 5724cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5725cb04b873SMark J Musante spa_close(spa, FTAG); 5726cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5727cb04b873SMark J Musante 572899653d4eSeschrock return (error); 572999653d4eSeschrock } 573099653d4eSeschrock 57311195e687SMark J Musante /* 57321195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 57331195e687SMark J Musante */ 57341195e687SMark J Musante int 57351195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 57361195e687SMark J Musante nvlist_t *props, boolean_t exp) 57371195e687SMark J Musante { 57381195e687SMark J Musante int error = 0; 57391195e687SMark J Musante uint64_t txg, *glist; 57401195e687SMark J Musante spa_t *newspa; 57411195e687SMark J Musante uint_t c, children, lastlog; 57421195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 57431195e687SMark J Musante dmu_tx_t *tx; 57441195e687SMark J Musante char *altroot = NULL; 57451195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 57461195e687SMark J Musante boolean_t activate_slog; 57471195e687SMark J Musante 5748f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 57491195e687SMark J Musante 57501195e687SMark J Musante txg = spa_vdev_enter(spa); 57511195e687SMark J Musante 57521195e687SMark J Musante /* clear the log and flush everything up to now */ 57531195e687SMark J Musante activate_slog = spa_passivate_log(spa); 57541195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 57555cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 57561195e687SMark J Musante txg = spa_vdev_config_enter(spa); 57571195e687SMark J Musante 57581195e687SMark J Musante if (activate_slog) 57591195e687SMark J Musante spa_activate_log(spa); 57601195e687SMark J Musante 57611195e687SMark J Musante if (error != 0) 57621195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 57631195e687SMark J Musante 57641195e687SMark J Musante /* check new spa name before going any further */ 57651195e687SMark J Musante if (spa_lookup(newname) != NULL) 57661195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 57671195e687SMark J Musante 57681195e687SMark J Musante /* 57691195e687SMark J Musante * scan through all the children to ensure they're all mirrors 57701195e687SMark J Musante */ 57711195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 57721195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 57731195e687SMark J Musante &children) != 0) 57741195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 57751195e687SMark J Musante 57761195e687SMark J Musante /* first, check to ensure we've got the right child count */ 57771195e687SMark J Musante rvd = spa->spa_root_vdev; 57781195e687SMark J Musante lastlog = 0; 57791195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 57801195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 57811195e687SMark J Musante 57821195e687SMark J Musante /* don't count the holes & logs as children */ 57835cabbc6bSPrashanth Sreenivasa if (vd->vdev_islog || !vdev_is_concrete(vd)) { 57841195e687SMark J Musante if (lastlog == 0) 57851195e687SMark J Musante lastlog = c; 57861195e687SMark J Musante continue; 57871195e687SMark J Musante } 57881195e687SMark J Musante 57891195e687SMark J Musante lastlog = 0; 57901195e687SMark J Musante } 57911195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 57921195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 57931195e687SMark J Musante 57941195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 57951195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 57961195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 57971195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 57981195e687SMark J Musante 57991195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 58001195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 58011195e687SMark J Musante 58021195e687SMark J Musante /* then, loop over each vdev and validate it */ 58031195e687SMark J Musante for (c = 0; c < children; c++) { 58041195e687SMark J Musante uint64_t is_hole = 0; 58051195e687SMark J Musante 58061195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 58071195e687SMark J Musante &is_hole); 58081195e687SMark J Musante 58091195e687SMark J Musante if (is_hole != 0) { 58101195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 58111195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 58121195e687SMark J Musante continue; 58131195e687SMark J Musante } else { 5814be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 58151195e687SMark J Musante break; 58161195e687SMark J Musante } 58171195e687SMark J Musante } 58181195e687SMark J Musante 58191195e687SMark J Musante /* which disk is going to be split? */ 58201195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 58211195e687SMark J Musante &glist[c]) != 0) { 5822be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 58231195e687SMark J Musante break; 58241195e687SMark J Musante } 58251195e687SMark J Musante 58261195e687SMark J Musante /* look it up in the spa */ 58271195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 58281195e687SMark J Musante if (vml[c] == NULL) { 5829be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 58301195e687SMark J Musante break; 58311195e687SMark J Musante } 58321195e687SMark J Musante 58331195e687SMark J Musante /* make sure there's nothing stopping the split */ 58341195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 58351195e687SMark J Musante vml[c]->vdev_islog || 58365cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vml[c]) || 58371195e687SMark J Musante vml[c]->vdev_isspare || 58381195e687SMark J Musante vml[c]->vdev_isl2cache || 58391195e687SMark J Musante !vdev_writeable(vml[c]) || 5840d41c4376SMark J Musante vml[c]->vdev_children != 0 || 58411195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 58421195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5843be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 58441195e687SMark J Musante break; 58451195e687SMark J Musante } 58461195e687SMark J Musante 58471195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5848be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 58491195e687SMark J Musante break; 58501195e687SMark J Musante } 58511195e687SMark J Musante 58521195e687SMark J Musante /* we need certain info from the top level */ 58531195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 58541195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 58551195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 58561195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 58571195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 58581195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 58591195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 58601195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5861215198a6SJoe Stein 5862215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5863215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5864215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5865215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5866215198a6SJoe Stein 5867215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5868215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5869215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5870215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 58711195e687SMark J Musante } 58721195e687SMark J Musante 58731195e687SMark J Musante if (error != 0) { 58741195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 58751195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 58761195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 58771195e687SMark J Musante } 58781195e687SMark J Musante 58791195e687SMark J Musante /* stop writers from using the disks */ 58801195e687SMark J Musante for (c = 0; c < children; c++) { 58811195e687SMark J Musante if (vml[c] != NULL) 58821195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 58831195e687SMark J Musante } 58841195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 58851195e687SMark J Musante 58861195e687SMark J Musante /* 58871195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 58881195e687SMark J Musante * will disappear once the config is regenerated. 58891195e687SMark J Musante */ 58901195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 58911195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 58921195e687SMark J Musante glist, children) == 0); 58931195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 58941195e687SMark J Musante 589598295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 58961195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 58971195e687SMark J Musante nvl) == 0); 589898295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 58991195e687SMark J Musante spa->spa_config_splitting = nvl; 59001195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 59011195e687SMark J Musante 59021195e687SMark J Musante /* configure and create the new pool */ 59031195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 59041195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 59051195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 59061195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 59071195e687SMark J Musante spa_version(spa)) == 0); 59081195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 59091195e687SMark J Musante spa->spa_config_txg) == 0); 59101195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 59111195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5912215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 59131195e687SMark J Musante (void) nvlist_lookup_string(props, 59141195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 59151195e687SMark J Musante 5916d41c4376SMark J Musante /* add the new pool to the namespace */ 59171195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5918215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 59191195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 59201195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 59211195e687SMark J Musante 59221195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 59231195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 59241195e687SMark J Musante 59251195e687SMark J Musante if (zio_injection_enabled) 59261195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 59271195e687SMark J Musante 59281195e687SMark J Musante spa_activate(newspa, spa_mode_global); 59291195e687SMark J Musante spa_async_suspend(newspa); 59301195e687SMark J Musante 5931*6f793812SPavel Zakharov newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; 5932*6f793812SPavel Zakharov 59331195e687SMark J Musante /* create the new pool from the disks of the original pool */ 5934*6f793812SPavel Zakharov error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); 59351195e687SMark J Musante if (error) 59361195e687SMark J Musante goto out; 59371195e687SMark J Musante 59381195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 59391195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 59401195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 59411195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 59421195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 59431195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 59441195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 59451195e687SMark J Musante B_TRUE)); 59461195e687SMark J Musante } 59471195e687SMark J Musante 59481195e687SMark J Musante /* set the props */ 59491195e687SMark J Musante if (props != NULL) { 59501195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 59511195e687SMark J Musante error = spa_prop_set(newspa, props); 59521195e687SMark J Musante if (error) 59531195e687SMark J Musante goto out; 59541195e687SMark J Musante } 59551195e687SMark J Musante 59561195e687SMark J Musante /* flush everything */ 59571195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 59581195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 59591195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 59601195e687SMark J Musante 59611195e687SMark J Musante if (zio_injection_enabled) 59621195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 59631195e687SMark J Musante 59641195e687SMark J Musante spa_async_resume(newspa); 59651195e687SMark J Musante 59661195e687SMark J Musante /* finally, update the original pool's config */ 59671195e687SMark J Musante txg = spa_vdev_config_enter(spa); 59681195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 59691195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 59701195e687SMark J Musante if (error != 0) 59711195e687SMark J Musante dmu_tx_abort(tx); 59721195e687SMark J Musante for (c = 0; c < children; c++) { 59731195e687SMark J Musante if (vml[c] != NULL) { 59741195e687SMark J Musante vdev_split(vml[c]); 59751195e687SMark J Musante if (error == 0) 59764445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 59774445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5978215198a6SJoe Stein 59791195e687SMark J Musante vdev_free(vml[c]); 59801195e687SMark J Musante } 59811195e687SMark J Musante } 5982215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 59831195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 59841195e687SMark J Musante spa->spa_config_splitting = NULL; 59851195e687SMark J Musante nvlist_free(nvl); 59861195e687SMark J Musante if (error == 0) 59871195e687SMark J Musante dmu_tx_commit(tx); 59881195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 59891195e687SMark J Musante 59901195e687SMark J Musante if (zio_injection_enabled) 59911195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 59921195e687SMark J Musante 59931195e687SMark J Musante /* split is complete; log a history record */ 59944445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 59954445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 59961195e687SMark J Musante 59971195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 59981195e687SMark J Musante 59991195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 60001195e687SMark J Musante if (exp) 60011195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 60021195e687SMark J Musante B_FALSE, B_FALSE); 60031195e687SMark J Musante 60041195e687SMark J Musante return (error); 60051195e687SMark J Musante 60061195e687SMark J Musante out: 60071195e687SMark J Musante spa_unload(newspa); 60081195e687SMark J Musante spa_deactivate(newspa); 60091195e687SMark J Musante spa_remove(newspa); 60101195e687SMark J Musante 60111195e687SMark J Musante txg = spa_vdev_config_enter(spa); 601298295d61SMark J Musante 601398295d61SMark J Musante /* re-online all offlined disks */ 601498295d61SMark J Musante for (c = 0; c < children; c++) { 601598295d61SMark J Musante if (vml[c] != NULL) 601698295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 601798295d61SMark J Musante } 601898295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 601998295d61SMark J Musante 60201195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 60211195e687SMark J Musante spa->spa_config_splitting = NULL; 6022d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 60231195e687SMark J Musante 60241195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 60251195e687SMark J Musante return (error); 60261195e687SMark J Musante } 60271195e687SMark J Musante 6028fa9e4066Sahrens /* 60293d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 6030f7170741SWill Andrews * currently spared, so we can detach it. 6031fa9e4066Sahrens */ 6032ea8dc4b6Seschrock static vdev_t * 60333d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 6034fa9e4066Sahrens { 6035ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 6036fa9e4066Sahrens 6037573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 60383d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 6039ea8dc4b6Seschrock if (oldvd != NULL) 6040ea8dc4b6Seschrock return (oldvd); 6041ea8dc4b6Seschrock } 6042fa9e4066Sahrens 60433d7072f8Seschrock /* 6044cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 6045cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 6046cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 6047cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 6048cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 6049cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 60503d7072f8Seschrock */ 6051cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 6052cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 6053cb04b873SMark J Musante 6054cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 6055ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 6056ea8dc4b6Seschrock 60578ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 6058e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 60598ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 6060ea8dc4b6Seschrock return (oldvd); 6061fa9e4066Sahrens } 6062ea8dc4b6Seschrock 60633d7072f8Seschrock /* 60643d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 60653d7072f8Seschrock */ 6066cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 6067cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 6068cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 6069cb04b873SMark J Musante 6070cb04b873SMark J Musante if (last->vdev_unspare) { 6071cb04b873SMark J Musante oldvd = first; 6072cb04b873SMark J Musante newvd = last; 6073cb04b873SMark J Musante } else if (first->vdev_unspare) { 6074cb04b873SMark J Musante oldvd = last; 6075cb04b873SMark J Musante newvd = first; 6076cb04b873SMark J Musante } else { 6077cb04b873SMark J Musante oldvd = NULL; 6078cb04b873SMark J Musante } 60793d7072f8Seschrock 6080cb04b873SMark J Musante if (oldvd != NULL && 60818ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 6082e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 6083cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 60843d7072f8Seschrock return (oldvd); 6085cb04b873SMark J Musante 6086cb04b873SMark J Musante /* 6087cb04b873SMark J Musante * If there are more than two spares attached to a disk, 6088cb04b873SMark J Musante * and those spares are not required, then we want to 6089cb04b873SMark J Musante * attempt to free them up now so that they can be used 6090cb04b873SMark J Musante * by other pools. Once we're back down to a single 6091cb04b873SMark J Musante * disk+spare, we stop removing them. 6092cb04b873SMark J Musante */ 6093cb04b873SMark J Musante if (vd->vdev_children > 2) { 6094cb04b873SMark J Musante newvd = vd->vdev_child[1]; 6095cb04b873SMark J Musante 6096cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 6097cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 6098cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 6099cb04b873SMark J Musante !vdev_dtl_required(newvd)) 6100cb04b873SMark J Musante return (newvd); 61013d7072f8Seschrock } 61023d7072f8Seschrock } 61033d7072f8Seschrock 6104ea8dc4b6Seschrock return (NULL); 6105fa9e4066Sahrens } 6106fa9e4066Sahrens 6107ea8dc4b6Seschrock static void 61083d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 6109fa9e4066Sahrens { 61108ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 61118ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 6112ea8dc4b6Seschrock 61138ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6114ea8dc4b6Seschrock 61153d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 61168ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 61178ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 6118ea8dc4b6Seschrock guid = vd->vdev_guid; 61198ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 61208ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 61218ad4d6ddSJeff Bonwick sguid = 0; 612299653d4eSeschrock /* 612399653d4eSeschrock * If we have just finished replacing a hot spared device, then 612499653d4eSeschrock * we need to detach the parent's first child (the original hot 612599653d4eSeschrock * spare) as well. 612699653d4eSeschrock */ 6127cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 6128cb04b873SMark J Musante ppvd->vdev_children == 2) { 612999653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 61308ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 613199653d4eSeschrock } 6132b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 6133b4952e17SGeorge Wilson 61348ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 61358ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 6136ea8dc4b6Seschrock return; 61378ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 613899653d4eSeschrock return; 61398ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6140fa9e4066Sahrens } 6141fa9e4066Sahrens 61428ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 6143fa9e4066Sahrens } 6144fa9e4066Sahrens 6145c67d9675Seschrock /* 6146b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 6147c67d9675Seschrock */ 6148c67d9675Seschrock int 61496809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 61506809eb4eSEric Schrock boolean_t ispath) 6151c67d9675Seschrock { 6152c5904d13Seschrock vdev_t *vd; 6153208044b8SGeorge Wilson boolean_t sync = B_FALSE; 6154c67d9675Seschrock 6155f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6156f9af39baSGeorge Wilson 6157b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 6158c67d9675Seschrock 61596809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 6160b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 6161c67d9675Seschrock 61620e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 6163b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 61640e34b6a7Sbonwick 61656809eb4eSEric Schrock if (ispath) { 6166208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 6167208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 6168208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 6169208044b8SGeorge Wilson sync = B_TRUE; 6170208044b8SGeorge Wilson } 61716809eb4eSEric Schrock } else { 6172208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 6173208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 6174208044b8SGeorge Wilson sync = B_TRUE; 6175208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 61766809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 6177208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 6178208044b8SGeorge Wilson sync = B_TRUE; 6179208044b8SGeorge Wilson } 61806809eb4eSEric Schrock } 6181c67d9675Seschrock 6182208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 6183c67d9675Seschrock } 6184c67d9675Seschrock 61856809eb4eSEric Schrock int 61866809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 61876809eb4eSEric Schrock { 61886809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 61896809eb4eSEric Schrock } 61906809eb4eSEric Schrock 61916809eb4eSEric Schrock int 61926809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 61936809eb4eSEric Schrock { 61946809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 61956809eb4eSEric Schrock } 61966809eb4eSEric Schrock 6197fa9e4066Sahrens /* 6198fa9e4066Sahrens * ========================================================================== 61993f9d6ad7SLin Ling * SPA Scanning 6200fa9e4066Sahrens * ========================================================================== 6201fa9e4066Sahrens */ 62021702cce7SAlek Pinchuk int 62031702cce7SAlek Pinchuk spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) 62041702cce7SAlek Pinchuk { 62051702cce7SAlek Pinchuk ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 62061702cce7SAlek Pinchuk 62071702cce7SAlek Pinchuk if (dsl_scan_resilvering(spa->spa_dsl_pool)) 62081702cce7SAlek Pinchuk return (SET_ERROR(EBUSY)); 62091702cce7SAlek Pinchuk 62101702cce7SAlek Pinchuk return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); 62111702cce7SAlek Pinchuk } 6212fa9e4066Sahrens 6213ea8dc4b6Seschrock int 62143f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 6215fa9e4066Sahrens { 6216e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 62173f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 6218be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 62193f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 62203f9d6ad7SLin Ling } 6221bb8b5132Sek 62223f9d6ad7SLin Ling int 62233f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 62243f9d6ad7SLin Ling { 62253f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 62263f9d6ad7SLin Ling 62273f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 6228be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 6229fa9e4066Sahrens 6230fa9e4066Sahrens /* 6231088f3894Sahrens * If a resilver was requested, but there is no DTL on a 6232088f3894Sahrens * writeable leaf device, we have nothing to do. 6233fa9e4066Sahrens */ 62343f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 6235088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 6236088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 6237ea8dc4b6Seschrock return (0); 6238ea8dc4b6Seschrock } 6239fa9e4066Sahrens 62403f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 6241fa9e4066Sahrens } 6242fa9e4066Sahrens 6243ea8dc4b6Seschrock /* 6244ea8dc4b6Seschrock * ========================================================================== 6245ea8dc4b6Seschrock * SPA async task processing 6246ea8dc4b6Seschrock * ========================================================================== 6247ea8dc4b6Seschrock */ 6248ea8dc4b6Seschrock 6249ea8dc4b6Seschrock static void 62503d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 6251fa9e4066Sahrens { 625249cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 625398d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 625498d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 625549cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 62561d713200SEric Schrock 62571d713200SEric Schrock /* 62581d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 62591d713200SEric Schrock * vdev_clear() as that will cause us to throw away 62601d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 62611d713200SEric Schrock * device, all of which is a waste. 62621d713200SEric Schrock */ 62631d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 62641d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 62651d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 62661d713200SEric Schrock 6267e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 6268ea8dc4b6Seschrock } 626949cf58c0SBrendan Gregg - Sun Microsystems 6270e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 627149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 6272ea8dc4b6Seschrock } 6273fa9e4066Sahrens 6274e14bb325SJeff Bonwick static void 6275e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 6276e14bb325SJeff Bonwick { 6277e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 627898d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 6279e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 6280e14bb325SJeff Bonwick } 6281e14bb325SJeff Bonwick 6282e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 6283e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 6284e14bb325SJeff Bonwick } 6285e14bb325SJeff Bonwick 6286573ca77eSGeorge Wilson static void 6287573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 6288573ca77eSGeorge Wilson { 6289573ca77eSGeorge Wilson sysevent_id_t eid; 6290573ca77eSGeorge Wilson nvlist_t *attr; 6291573ca77eSGeorge Wilson char *physpath; 6292573ca77eSGeorge Wilson 6293573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 6294573ca77eSGeorge Wilson return; 6295573ca77eSGeorge Wilson 6296573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 6297573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 6298573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 6299573ca77eSGeorge Wilson } 6300573ca77eSGeorge Wilson 6301573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 6302573ca77eSGeorge Wilson return; 6303573ca77eSGeorge Wilson 6304573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 6305573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 6306573ca77eSGeorge Wilson 6307573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6308573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 6309573ca77eSGeorge Wilson 6310573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 6311573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 6312573ca77eSGeorge Wilson 6313573ca77eSGeorge Wilson nvlist_free(attr); 6314573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 6315573ca77eSGeorge Wilson } 6316573ca77eSGeorge Wilson 6317ea8dc4b6Seschrock static void 63183f7978d0SAlan Somers spa_async_thread(void *arg) 6319ea8dc4b6Seschrock { 63203f7978d0SAlan Somers spa_t *spa = (spa_t *)arg; 6321e14bb325SJeff Bonwick int tasks; 6322ea8dc4b6Seschrock 6323ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 6324ea8dc4b6Seschrock 6325ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6326ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 6327ea8dc4b6Seschrock spa->spa_async_tasks = 0; 6328ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6329ea8dc4b6Seschrock 63300373e76bSbonwick /* 63310373e76bSbonwick * See if the config needs to be updated. 63320373e76bSbonwick */ 63330373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 6334b24ab676SJeff Bonwick uint64_t old_space, new_space; 6335573ca77eSGeorge Wilson 63360373e76bSbonwick mutex_enter(&spa_namespace_lock); 6337b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 63380373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 6339b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 63400373e76bSbonwick mutex_exit(&spa_namespace_lock); 6341573ca77eSGeorge Wilson 6342573ca77eSGeorge Wilson /* 6343573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 6344573ca77eSGeorge Wilson * then log an internal history event. 6345573ca77eSGeorge Wilson */ 6346b24ab676SJeff Bonwick if (new_space != old_space) { 63474445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 6348c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 6349b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 6350573ca77eSGeorge Wilson } 63510373e76bSbonwick } 63520373e76bSbonwick 6353ea8dc4b6Seschrock /* 63543d7072f8Seschrock * See if any devices need to be marked REMOVED. 6355ea8dc4b6Seschrock */ 6356e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 63578f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 63583d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 6359e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 636049cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 6361e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 636249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 6363e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 6364e14bb325SJeff Bonwick } 6365e14bb325SJeff Bonwick 6366573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 6367573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6368573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 6369573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 6370573ca77eSGeorge Wilson } 6371573ca77eSGeorge Wilson 6372e14bb325SJeff Bonwick /* 6373e14bb325SJeff Bonwick * See if any devices need to be probed. 6374e14bb325SJeff Bonwick */ 6375e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 63768f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 6377e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 6378e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 63793d7072f8Seschrock } 6380ea8dc4b6Seschrock 6381ea8dc4b6Seschrock /* 6382ea8dc4b6Seschrock * If any devices are done replacing, detach them. 6383ea8dc4b6Seschrock */ 63843d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 63853d7072f8Seschrock spa_vdev_resilver_done(spa); 6386fa9e4066Sahrens 6387ea8dc4b6Seschrock /* 6388ea8dc4b6Seschrock * Kick off a resilver. 6389ea8dc4b6Seschrock */ 6390088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 63913f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 6392ea8dc4b6Seschrock 6393ea8dc4b6Seschrock /* 6394ea8dc4b6Seschrock * Let the world know that we're done. 6395ea8dc4b6Seschrock */ 6396ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6397ea8dc4b6Seschrock spa->spa_async_thread = NULL; 6398ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 6399ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6400ea8dc4b6Seschrock thread_exit(); 6401ea8dc4b6Seschrock } 6402ea8dc4b6Seschrock 6403ea8dc4b6Seschrock void 6404ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 6405ea8dc4b6Seschrock { 6406ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6407ea8dc4b6Seschrock spa->spa_async_suspended++; 64085cabbc6bSPrashanth Sreenivasa while (spa->spa_async_thread != NULL || 64095cabbc6bSPrashanth Sreenivasa spa->spa_condense_thread != NULL) 6410ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 6411ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 64125cabbc6bSPrashanth Sreenivasa 64135cabbc6bSPrashanth Sreenivasa spa_vdev_remove_suspend(spa); 6414ea8dc4b6Seschrock } 6415ea8dc4b6Seschrock 6416ea8dc4b6Seschrock void 6417ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 6418ea8dc4b6Seschrock { 6419ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6420ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 6421ea8dc4b6Seschrock spa->spa_async_suspended--; 6422ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 64235cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 6424ea8dc4b6Seschrock } 6425ea8dc4b6Seschrock 64263cb69f73SWill Andrews static boolean_t 64273cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 64283cb69f73SWill Andrews { 64293cb69f73SWill Andrews uint_t non_config_tasks; 64303cb69f73SWill Andrews uint_t config_task; 64313cb69f73SWill Andrews boolean_t config_task_suspended; 64323cb69f73SWill Andrews 64333cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 64343cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 64353cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 64363cb69f73SWill Andrews config_task_suspended = B_FALSE; 64373cb69f73SWill Andrews } else { 64383cb69f73SWill Andrews config_task_suspended = 64393cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 64403cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 64413cb69f73SWill Andrews } 64423cb69f73SWill Andrews 64433cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 64443cb69f73SWill Andrews } 64453cb69f73SWill Andrews 6446ea8dc4b6Seschrock static void 6447ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 6448ea8dc4b6Seschrock { 6449ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 64503cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 64513cb69f73SWill Andrews !spa->spa_async_suspended && 64520373e76bSbonwick spa->spa_async_thread == NULL && 64533cb69f73SWill Andrews rootdir != NULL) 6454ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 6455ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 6456ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6457ea8dc4b6Seschrock } 6458ea8dc4b6Seschrock 6459ea8dc4b6Seschrock void 6460ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 6461ea8dc4b6Seschrock { 64623f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 6463ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6464ea8dc4b6Seschrock spa->spa_async_tasks |= task; 6465ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6466fa9e4066Sahrens } 6467fa9e4066Sahrens 6468fa9e4066Sahrens /* 6469fa9e4066Sahrens * ========================================================================== 6470fa9e4066Sahrens * SPA syncing routines 6471fa9e4066Sahrens * ========================================================================== 6472fa9e4066Sahrens */ 6473fa9e4066Sahrens 6474cde58dbcSMatthew Ahrens static int 6475cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6476cde58dbcSMatthew Ahrens { 6477cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 6478cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 6479cde58dbcSMatthew Ahrens return (0); 6480b24ab676SJeff Bonwick } 6481b24ab676SJeff Bonwick 6482cde58dbcSMatthew Ahrens static int 6483cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6484b24ab676SJeff Bonwick { 6485b24ab676SJeff Bonwick zio_t *zio = arg; 6486b24ab676SJeff Bonwick 6487b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 6488b24ab676SJeff Bonwick zio->io_flags)); 6489cde58dbcSMatthew Ahrens return (0); 6490fa9e4066Sahrens } 6491fa9e4066Sahrens 649269962b56SMatthew Ahrens /* 649369962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 649469962b56SMatthew Ahrens * amount of time spent syncing frees. 649569962b56SMatthew Ahrens */ 649669962b56SMatthew Ahrens static void 649769962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 649869962b56SMatthew Ahrens { 649969962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 650069962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 650169962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 650269962b56SMatthew Ahrens } 650369962b56SMatthew Ahrens 650469962b56SMatthew Ahrens /* 650569962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 650669962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 650769962b56SMatthew Ahrens */ 650869962b56SMatthew Ahrens static void 650969962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 651069962b56SMatthew Ahrens { 651169962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 651269962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 651369962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 651469962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 651569962b56SMatthew Ahrens } 651669962b56SMatthew Ahrens 651769962b56SMatthew Ahrens 6518fa9e4066Sahrens static void 651999653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6520fa9e4066Sahrens { 6521fa9e4066Sahrens char *packed = NULL; 6522f7991ba4STim Haley size_t bufsize; 6523fa9e4066Sahrens size_t nvsize = 0; 6524fa9e4066Sahrens dmu_buf_t *db; 6525fa9e4066Sahrens 652699653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6527fa9e4066Sahrens 6528f7991ba4STim Haley /* 6529f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 653043466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6531f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6532f7991ba4STim Haley */ 6533ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6534f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6535fa9e4066Sahrens 653699653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6537ea8dc4b6Seschrock KM_SLEEP) == 0); 6538f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6539fa9e4066Sahrens 6540f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6541fa9e4066Sahrens 6542f7991ba4STim Haley kmem_free(packed, bufsize); 6543fa9e4066Sahrens 654499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6545fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6546fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6547ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6548fa9e4066Sahrens } 6549fa9e4066Sahrens 655099653d4eSeschrock static void 6551fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6552fa94a07fSbrendan const char *config, const char *entry) 655399653d4eSeschrock { 655499653d4eSeschrock nvlist_t *nvroot; 6555fa94a07fSbrendan nvlist_t **list; 655699653d4eSeschrock int i; 655799653d4eSeschrock 6558fa94a07fSbrendan if (!sav->sav_sync) 655999653d4eSeschrock return; 656099653d4eSeschrock 656199653d4eSeschrock /* 6562fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6563fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 65643d7072f8Seschrock * valid and the vdevs are labeled appropriately. 656599653d4eSeschrock */ 6566fa94a07fSbrendan if (sav->sav_object == 0) { 6567fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6568fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6569fa94a07fSbrendan sizeof (uint64_t), tx); 657099653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6571fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6572fa94a07fSbrendan &sav->sav_object, tx) == 0); 657399653d4eSeschrock } 657499653d4eSeschrock 657599653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6576fa94a07fSbrendan if (sav->sav_count == 0) { 6577fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 657899653d4eSeschrock } else { 6579fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6580fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6581fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 65823f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6583fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6584fa94a07fSbrendan sav->sav_count) == 0); 6585fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6586fa94a07fSbrendan nvlist_free(list[i]); 6587fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 658899653d4eSeschrock } 658999653d4eSeschrock 6590fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 659106eeb2adSek nvlist_free(nvroot); 659299653d4eSeschrock 6593fa94a07fSbrendan sav->sav_sync = B_FALSE; 659499653d4eSeschrock } 659599653d4eSeschrock 6596215198a6SJoe Stein /* 6597215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6598215198a6SJoe Stein * The all-vdev ZAP must be empty. 6599215198a6SJoe Stein */ 6600215198a6SJoe Stein static void 6601215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6602215198a6SJoe Stein { 6603215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6604215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6605215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6606215198a6SJoe Stein vd->vdev_top_zap, tx)); 6607215198a6SJoe Stein } 6608215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6609215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6610215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6611215198a6SJoe Stein } 6612215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6613215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6614215198a6SJoe Stein } 6615215198a6SJoe Stein } 6616215198a6SJoe Stein 661799653d4eSeschrock static void 661899653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 661999653d4eSeschrock { 662099653d4eSeschrock nvlist_t *config; 662199653d4eSeschrock 6622215198a6SJoe Stein /* 6623215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6624215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6625215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6626215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6627215198a6SJoe Stein */ 6628215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6629215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 663099653d4eSeschrock return; 663199653d4eSeschrock 6632e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6633e14bb325SJeff Bonwick 6634215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6635555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 6636215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6637215198a6SJoe Stein 6638215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6639215198a6SJoe Stein /* Make and build the new AVZ */ 6640215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6641215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6642215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6643215198a6SJoe Stein 6644215198a6SJoe Stein /* Diff old AVZ with new one */ 6645215198a6SJoe Stein zap_cursor_t zc; 6646215198a6SJoe Stein zap_attribute_t za; 6647215198a6SJoe Stein 6648215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6649215198a6SJoe Stein spa->spa_all_vdev_zaps); 6650215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6651215198a6SJoe Stein zap_cursor_advance(&zc)) { 6652215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6653215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6654215198a6SJoe Stein vdzap) == ENOENT) { 6655215198a6SJoe Stein /* 6656215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6657215198a6SJoe Stein * destroy it 6658215198a6SJoe Stein */ 6659215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6660215198a6SJoe Stein tx)); 6661215198a6SJoe Stein } 6662215198a6SJoe Stein } 6663215198a6SJoe Stein 6664215198a6SJoe Stein zap_cursor_fini(&zc); 6665215198a6SJoe Stein 6666215198a6SJoe Stein /* Destroy the old AVZ */ 6667215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6668215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6669215198a6SJoe Stein 6670215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6671215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6672215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6673215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6674215198a6SJoe Stein 6675215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6676215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6677215198a6SJoe Stein zap_cursor_t zc; 6678215198a6SJoe Stein zap_attribute_t za; 6679215198a6SJoe Stein 6680215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6681215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6682215198a6SJoe Stein spa->spa_all_vdev_zaps); 6683215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6684215198a6SJoe Stein zap_cursor_advance(&zc)) { 6685215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6686215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6687215198a6SJoe Stein } 6688215198a6SJoe Stein 6689215198a6SJoe Stein zap_cursor_fini(&zc); 6690215198a6SJoe Stein 6691215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6692215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6693215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6694215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6695215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6696215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6697215198a6SJoe Stein } 6698215198a6SJoe Stein 6699215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6700215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6701215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6702215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6703215198a6SJoe Stein } 6704215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6705215198a6SJoe Stein 6706215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6707215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6708215198a6SJoe Stein 6709e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6710e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6711e14bb325SJeff Bonwick 671225345e46SGeorge Wilson /* 671325345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 671425345e46SGeorge Wilson * the config object gets updated with the correct version. 671525345e46SGeorge Wilson */ 671625345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 671725345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 671825345e46SGeorge Wilson spa->spa_uberblock.ub_version); 671925345e46SGeorge Wilson 6720e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 672199653d4eSeschrock 6722aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 672399653d4eSeschrock spa->spa_config_syncing = config; 672499653d4eSeschrock 672599653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 672699653d4eSeschrock } 672799653d4eSeschrock 6728ad135b5dSChristopher Siden static void 67293b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6730ad135b5dSChristopher Siden { 67313b2aab18SMatthew Ahrens uint64_t *versionp = arg; 67323b2aab18SMatthew Ahrens uint64_t version = *versionp; 67333b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6734ad135b5dSChristopher Siden 6735ad135b5dSChristopher Siden /* 6736ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6737ad135b5dSChristopher Siden */ 6738ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6739ad135b5dSChristopher Siden 674062eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6741ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6742ad135b5dSChristopher Siden 6743ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6744ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 67454445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6746ad135b5dSChristopher Siden } 6747ad135b5dSChristopher Siden 6748990b4856Slling /* 6749990b4856Slling * Set zpool properties. 6750990b4856Slling */ 6751b1b8ab34Slling static void 67523b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6753b1b8ab34Slling { 67543b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 67553b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6756b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6757ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6758b1b8ab34Slling 6759e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6760e14bb325SJeff Bonwick 6761990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6762ad135b5dSChristopher Siden uint64_t intval; 6763ad135b5dSChristopher Siden char *strval, *fname; 6764ad135b5dSChristopher Siden zpool_prop_t prop; 6765ad135b5dSChristopher Siden const char *propname; 6766ad135b5dSChristopher Siden zprop_type_t proptype; 67672acef22dSMatthew Ahrens spa_feature_t fid; 6768ad135b5dSChristopher Siden 6769990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 67704ae5f5f0SAlan Somers case ZPOOL_PROP_INVAL: 6771ad135b5dSChristopher Siden /* 6772ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6773ad135b5dSChristopher Siden */ 6774ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6775ad135b5dSChristopher Siden 6776ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 67772acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6778ad135b5dSChristopher Siden 67792acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 67804445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 67814445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6782ad135b5dSChristopher Siden break; 6783ad135b5dSChristopher Siden 6784990b4856Slling case ZPOOL_PROP_VERSION: 67850713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6786990b4856Slling /* 6787ad135b5dSChristopher Siden * The version is synced seperatly before other 6788ad135b5dSChristopher Siden * properties and should be correct by now. 6789990b4856Slling */ 6790ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6791ecd6cf80Smarks break; 6792990b4856Slling 6793990b4856Slling case ZPOOL_PROP_ALTROOT: 6794990b4856Slling /* 6795990b4856Slling * 'altroot' is a non-persistent property. It should 6796990b4856Slling * have been set temporarily at creation or import time. 6797990b4856Slling */ 6798990b4856Slling ASSERT(spa->spa_root != NULL); 6799b1b8ab34Slling break; 68003d7072f8Seschrock 6801f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 68022f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6803990b4856Slling /* 6804f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6805f9af39baSGeorge Wilson * properties. 6806990b4856Slling */ 68073d7072f8Seschrock break; 68088704186eSDan McDonald case ZPOOL_PROP_COMMENT: 68090713e232SGeorge Wilson strval = fnvpair_value_string(elem); 68108704186eSDan McDonald if (spa->spa_comment != NULL) 68118704186eSDan McDonald spa_strfree(spa->spa_comment); 68128704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 68138704186eSDan McDonald /* 68148704186eSDan McDonald * We need to dirty the configuration on all the vdevs 68158704186eSDan McDonald * so that their labels get updated. It's unnecessary 68168704186eSDan McDonald * to do this for pool creation since the vdev's 68178704186eSDan McDonald * configuratoin has already been dirtied. 68188704186eSDan McDonald */ 68198704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 68208704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 68214445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 68224445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 68238704186eSDan McDonald break; 6824990b4856Slling default: 6825990b4856Slling /* 6826990b4856Slling * Set pool property values in the poolprops mos object. 6827990b4856Slling */ 6828990b4856Slling if (spa->spa_pool_props_object == 0) { 6829ad135b5dSChristopher Siden spa->spa_pool_props_object = 6830ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6831990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6832ad135b5dSChristopher Siden tx); 6833990b4856Slling } 6834990b4856Slling 6835990b4856Slling /* normalize the property name */ 6836990b4856Slling propname = zpool_prop_to_name(prop); 6837990b4856Slling proptype = zpool_prop_get_type(prop); 6838990b4856Slling 6839990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6840990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 68410713e232SGeorge Wilson strval = fnvpair_value_string(elem); 68420713e232SGeorge Wilson VERIFY0(zap_update(mos, 6843990b4856Slling spa->spa_pool_props_object, propname, 68440713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 68454445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 68464445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6847990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 68480713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6849990b4856Slling 6850990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6851990b4856Slling const char *unused; 68520713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 68530713e232SGeorge Wilson prop, intval, &unused)); 6854990b4856Slling } 68550713e232SGeorge Wilson VERIFY0(zap_update(mos, 6856990b4856Slling spa->spa_pool_props_object, propname, 68570713e232SGeorge Wilson 8, 1, &intval, tx)); 68584445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 68594445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6860990b4856Slling } else { 6861990b4856Slling ASSERT(0); /* not allowed */ 6862990b4856Slling } 6863990b4856Slling 68640a4e9518Sgw switch (prop) { 68650a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6866990b4856Slling spa->spa_delegation = intval; 68670a4e9518Sgw break; 68680a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6869990b4856Slling spa->spa_bootfs = intval; 68700a4e9518Sgw break; 68710a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 68720a4e9518Sgw spa->spa_failmode = intval; 68730a4e9518Sgw break; 6874573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6875573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6876b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6877b98131cfSEric Taylor spa_async_request(spa, 6878b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6879573ca77eSGeorge Wilson break; 6880b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6881b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6882b24ab676SJeff Bonwick break; 68830a4e9518Sgw default: 68840a4e9518Sgw break; 68850a4e9518Sgw } 6886990b4856Slling } 6887990b4856Slling 6888b1b8ab34Slling } 6889e14bb325SJeff Bonwick 6890e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6891b1b8ab34Slling } 6892b1b8ab34Slling 6893cde58dbcSMatthew Ahrens /* 6894cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6895cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6896cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6897cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6898cde58dbcSMatthew Ahrens * tasks. 6899cde58dbcSMatthew Ahrens */ 6900cde58dbcSMatthew Ahrens static void 6901cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6902cde58dbcSMatthew Ahrens { 6903cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6904cde58dbcSMatthew Ahrens 6905cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6906cde58dbcSMatthew Ahrens 69073b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 69083b2aab18SMatthew Ahrens 6909cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6910cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6911cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6912cde58dbcSMatthew Ahrens 6913cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6914cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6915cde58dbcSMatthew Ahrens } 6916cde58dbcSMatthew Ahrens 6917cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6918cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6919cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6920cde58dbcSMatthew Ahrens } 6921cde58dbcSMatthew Ahrens 6922cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6923cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6924cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6925cde58dbcSMatthew Ahrens 6926cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6927cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6928cde58dbcSMatthew Ahrens } 6929ad135b5dSChristopher Siden 6930ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6931ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6932ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6933ad135b5dSChristopher Siden } 6934b8289d24SDaniil Lunev 6935b8289d24SDaniil Lunev /* 6936b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6937b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6938b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6939b8289d24SDaniil Lunev * this feature active 6940b8289d24SDaniil Lunev */ 6941b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6942b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6943b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6944b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6945b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6946b8289d24SDaniil Lunev 6947b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6948b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6949b8289d24SDaniil Lunev } 695045818ee1SMatthew Ahrens 695145818ee1SMatthew Ahrens /* 695245818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 695345818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 695445818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 695545818ee1SMatthew Ahrens */ 695645818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 695745818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 695845818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 695945818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 696045818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 696145818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 696245818ee1SMatthew Ahrens } 696345818ee1SMatthew Ahrens 69643b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6965cde58dbcSMatthew Ahrens } 6966cde58dbcSMatthew Ahrens 69675cabbc6bSPrashanth Sreenivasa static void 69685cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vdev_t *vd) 69695cabbc6bSPrashanth Sreenivasa { 69705cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 69715cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 69725cabbc6bSPrashanth Sreenivasa 69735cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 69745cabbc6bSPrashanth Sreenivasa ASSERT(vim != NULL); 69755cabbc6bSPrashanth Sreenivasa ASSERT(vib != NULL); 69765cabbc6bSPrashanth Sreenivasa } 69775cabbc6bSPrashanth Sreenivasa 69785cabbc6bSPrashanth Sreenivasa if (vdev_obsolete_sm_object(vd) != 0) { 69795cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_sm != NULL); 69805cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 69815cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 69825cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); 69835cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); 69845cabbc6bSPrashanth Sreenivasa 69855cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_obsolete_sm_object(vd), ==, 69865cabbc6bSPrashanth Sreenivasa space_map_object(vd->vdev_obsolete_sm)); 69875cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, 69885cabbc6bSPrashanth Sreenivasa space_map_allocated(vd->vdev_obsolete_sm)); 69895cabbc6bSPrashanth Sreenivasa } 69905cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_segments != NULL); 69915cabbc6bSPrashanth Sreenivasa 69925cabbc6bSPrashanth Sreenivasa /* 69935cabbc6bSPrashanth Sreenivasa * Since frees / remaps to an indirect vdev can only 69945cabbc6bSPrashanth Sreenivasa * happen in syncing context, the obsolete segments 69955cabbc6bSPrashanth Sreenivasa * tree must be empty when we start syncing. 69965cabbc6bSPrashanth Sreenivasa */ 69975cabbc6bSPrashanth Sreenivasa ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); 69985cabbc6bSPrashanth Sreenivasa } 69995cabbc6bSPrashanth Sreenivasa 7000fa9e4066Sahrens /* 7001fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 7002fa9e4066Sahrens * part of the process, so we iterate until it converges. 7003fa9e4066Sahrens */ 7004fa9e4066Sahrens void 7005fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 7006fa9e4066Sahrens { 7007fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 7008fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 7009b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 70100373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 7011fa9e4066Sahrens vdev_t *vd; 7012fa9e4066Sahrens dmu_tx_t *tx; 7013e14bb325SJeff Bonwick int error; 70140f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 70150f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 7016fa9e4066Sahrens 7017f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 7018f9af39baSGeorge Wilson 70195cabbc6bSPrashanth Sreenivasa /* 70205cabbc6bSPrashanth Sreenivasa * Wait for i/os issued in open context that need to complete 70215cabbc6bSPrashanth Sreenivasa * before this txg syncs. 70225cabbc6bSPrashanth Sreenivasa */ 70235cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[txg & TXG_MASK])); 70245cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, 0); 70255cabbc6bSPrashanth Sreenivasa 7026fa9e4066Sahrens /* 7027fa9e4066Sahrens * Lock out configuration changes. 7028fa9e4066Sahrens */ 7029e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7030fa9e4066Sahrens 7031fa9e4066Sahrens spa->spa_syncing_txg = txg; 7032fa9e4066Sahrens spa->spa_sync_pass = 0; 7033fa9e4066Sahrens 70340f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 70350f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 70360f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 70370f7643c7SGeorge Wilson 7038e14bb325SJeff Bonwick /* 7039e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 7040e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 7041e14bb325SJeff Bonwick */ 7042e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 70438ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 70448ad4d6ddSJeff Bonwick /* 70458ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 70468ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 70478ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 70488ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 70498ad4d6ddSJeff Bonwick * into the root vdev tree. 70508ad4d6ddSJeff Bonwick */ 70518ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 70528ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 70538ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 70548ad4d6ddSJeff Bonwick vdev_state_clean(vd); 70558ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 70568ad4d6ddSJeff Bonwick } 70578ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 70588ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 7059e14bb325SJeff Bonwick } 7060e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 7061e14bb325SJeff Bonwick 706299653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 706399653d4eSeschrock 7064283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 7065283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 7066283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 7067283b8460SGeorge.Wilson 706899653d4eSeschrock /* 7069e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 707099653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 707199653d4eSeschrock */ 7072e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 7073e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 707499653d4eSeschrock int i; 707599653d4eSeschrock 707699653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 707799653d4eSeschrock vd = rvd->vdev_child[i]; 707899653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 707999653d4eSeschrock break; 708099653d4eSeschrock } 708199653d4eSeschrock if (i == rvd->vdev_children) { 708299653d4eSeschrock spa->spa_deflate = TRUE; 708399653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 708499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 708599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 708699653d4eSeschrock } 708799653d4eSeschrock } 708899653d4eSeschrock 70890f7643c7SGeorge Wilson /* 70900f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 70910f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 70920f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 70930f7643c7SGeorge Wilson * out this txg. 70940f7643c7SGeorge Wilson */ 70950f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 70960f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 70970f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 70980f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 70990f7643c7SGeorge Wilson 71000f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 71010f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 71020f7643c7SGeorge Wilson continue; 71030f7643c7SGeorge Wilson 71040f7643c7SGeorge Wilson /* 71050f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 71060f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 71070f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 71080f7643c7SGeorge Wilson */ 71090f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 71100f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 71110f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 71120f7643c7SGeorge Wilson } 71130f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 71140f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 71150f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 71160f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 71170f7643c7SGeorge Wilson 71180f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 71190f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 71200f7643c7SGeorge Wilson 71215cabbc6bSPrashanth Sreenivasa for (int c = 0; c < rvd->vdev_children; c++) { 71225cabbc6bSPrashanth Sreenivasa vdev_t *vd = rvd->vdev_child[c]; 71235cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vd); 71245cabbc6bSPrashanth Sreenivasa 71255cabbc6bSPrashanth Sreenivasa if (vdev_indirect_should_condense(vd)) { 71265cabbc6bSPrashanth Sreenivasa spa_condense_indirect_start_sync(vd, tx); 71275cabbc6bSPrashanth Sreenivasa break; 71285cabbc6bSPrashanth Sreenivasa } 71295cabbc6bSPrashanth Sreenivasa } 71305cabbc6bSPrashanth Sreenivasa 7131fa9e4066Sahrens /* 7132fa9e4066Sahrens * Iterate to convergence. 7133fa9e4066Sahrens */ 7134fa9e4066Sahrens do { 7135b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 7136fa9e4066Sahrens 7137fa9e4066Sahrens spa_sync_config_object(spa, tx); 7138fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 7139fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 7140fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 7141fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 7142ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 7143fa9e4066Sahrens dsl_pool_sync(dp, txg); 7144fa9e4066Sahrens 714501f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 714669962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 7147b24ab676SJeff Bonwick } else { 7148231aab85SMatthew Ahrens /* 7149231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 7150231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 7151231aab85SMatthew Ahrens */ 7152231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 7153cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 715469962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 7155fa9e4066Sahrens } 7156fa9e4066Sahrens 7157b24ab676SJeff Bonwick ddt_sync(spa, txg); 71583f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 7159afee20e4SGeorge Wilson 71605cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) 71615cabbc6bSPrashanth Sreenivasa svr_sync(spa, tx); 71625cabbc6bSPrashanth Sreenivasa 71635cabbc6bSPrashanth Sreenivasa while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 71645cabbc6bSPrashanth Sreenivasa != NULL) 7165b24ab676SJeff Bonwick vdev_sync(vd, txg); 7166b24ab676SJeff Bonwick 7167231aab85SMatthew Ahrens if (pass == 1) { 7168cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 7169231aab85SMatthew Ahrens ASSERT3U(txg, >=, 7170231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 7171231aab85SMatthew Ahrens /* 7172231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 7173231aab85SMatthew Ahrens * because we could have marked the MOS dirty 7174231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 7175231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 7176231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 7177231aab85SMatthew Ahrens * it is updated if we have synced out dirty 7178231aab85SMatthew Ahrens * data (though in this case the MOS will most 7179231aab85SMatthew Ahrens * likely also be dirty due to second order 7180231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 7181231aab85SMatthew Ahrens */ 7182231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 7183231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 7184231aab85SMatthew Ahrens /* 7185231aab85SMatthew Ahrens * Nothing changed on the first pass, 7186231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 7187231aab85SMatthew Ahrens * syncing deferred frees, so that we 7188231aab85SMatthew Ahrens * can keep this TXG as a no-op. 7189231aab85SMatthew Ahrens */ 7190231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 7191231aab85SMatthew Ahrens txg)); 7192231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 7193231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 7194231aab85SMatthew Ahrens break; 7195231aab85SMatthew Ahrens } 7196231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 7197231aab85SMatthew Ahrens } 7198fa9e4066Sahrens 7199cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 7200fa9e4066Sahrens 7201215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 7202215198a6SJoe Stein /* 7203215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 7204215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 7205215198a6SJoe Stein * called if the config is dirty; otherwise there may be 7206215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 7207215198a6SJoe Stein * spa_sync_config_object. 7208215198a6SJoe Stein */ 7209215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 7210215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 7211215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 7212215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 7213215198a6SJoe Stein all_vdev_zap_entry_count); 7214215198a6SJoe Stein } 7215215198a6SJoe Stein 72165cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 72175cabbc6bSPrashanth Sreenivasa ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); 72185cabbc6bSPrashanth Sreenivasa } 72195cabbc6bSPrashanth Sreenivasa 7220fa9e4066Sahrens /* 7221fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 7222fa9e4066Sahrens * to commit the transaction group. 72230373e76bSbonwick * 722417f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 722517f17c2dSbonwick * random top-level vdevs that are known to be visible in the 7226e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 7227e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 72280373e76bSbonwick */ 7229e14bb325SJeff Bonwick for (;;) { 7230e14bb325SJeff Bonwick /* 7231e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 7232e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 7233e14bb325SJeff Bonwick */ 7234e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 7235e14bb325SJeff Bonwick 7236e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 7237*6f793812SPavel Zakharov vdev_t *svd[SPA_SYNC_MIN_VDEVS]; 7238e14bb325SJeff Bonwick int svdcount = 0; 7239e14bb325SJeff Bonwick int children = rvd->vdev_children; 7240e14bb325SJeff Bonwick int c0 = spa_get_random(children); 7241e14bb325SJeff Bonwick 7242573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 7243e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 72445cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 || vd->vdev_islog || 72455cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vd)) 7246e14bb325SJeff Bonwick continue; 7247e14bb325SJeff Bonwick svd[svdcount++] = vd; 7248*6f793812SPavel Zakharov if (svdcount == SPA_SYNC_MIN_VDEVS) 7249e14bb325SJeff Bonwick break; 7250e14bb325SJeff Bonwick } 7251eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 7252e14bb325SJeff Bonwick } else { 7253e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 7254eb5bb584SWill Andrews rvd->vdev_children, txg); 72550373e76bSbonwick } 7256e14bb325SJeff Bonwick 7257dfbb9432SGeorge Wilson if (error == 0) 7258dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 7259dfbb9432SGeorge Wilson 7260e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 7261e14bb325SJeff Bonwick 7262e14bb325SJeff Bonwick if (error == 0) 7263e14bb325SJeff Bonwick break; 7264e14bb325SJeff Bonwick zio_suspend(spa, NULL); 7265e14bb325SJeff Bonwick zio_resume_wait(spa); 72660373e76bSbonwick } 726799653d4eSeschrock dmu_tx_commit(tx); 726899653d4eSeschrock 7269283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 7270283b8460SGeorge.Wilson 72710373e76bSbonwick /* 72720373e76bSbonwick * Clear the dirty config list. 7273fa9e4066Sahrens */ 7274e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 72750373e76bSbonwick vdev_config_clean(vd); 72760373e76bSbonwick 72770373e76bSbonwick /* 72780373e76bSbonwick * Now that the new config has synced transactionally, 72790373e76bSbonwick * let it become visible to the config cache. 72800373e76bSbonwick */ 72810373e76bSbonwick if (spa->spa_config_syncing != NULL) { 72820373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 72830373e76bSbonwick spa->spa_config_txg = txg; 72840373e76bSbonwick spa->spa_config_syncing = NULL; 72850373e76bSbonwick } 7286fa9e4066Sahrens 7287b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 7288fa9e4066Sahrens 72890f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 72900f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 72910f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 72920f7643c7SGeorge Wilson 7293fa9e4066Sahrens /* 7294fa9e4066Sahrens * Update usable space statistics. 7295fa9e4066Sahrens */ 7296fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 7297fa9e4066Sahrens vdev_sync_done(vd, txg); 7298fa9e4066Sahrens 7299485bbbf5SGeorge Wilson spa_update_dspace(spa); 7300485bbbf5SGeorge Wilson 7301fa9e4066Sahrens /* 7302fa9e4066Sahrens * It had better be the case that we didn't dirty anything 730399653d4eSeschrock * since vdev_config_sync(). 7304fa9e4066Sahrens */ 7305fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 7306fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 7307fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 7308b24ab676SJeff Bonwick 7309b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 7310fa9e4066Sahrens 731143297f97SGeorge Wilson /* 731243297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 731343297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 731443297f97SGeorge Wilson * will be guaranteed that all the processing associated with 731543297f97SGeorge Wilson * that txg has been completed. 731643297f97SGeorge Wilson */ 731743297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 7318e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 7319ea8dc4b6Seschrock 7320468c413aSTim Haley spa_handle_ignored_writes(spa); 7321468c413aSTim Haley 7322ea8dc4b6Seschrock /* 7323ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 7324ea8dc4b6Seschrock */ 7325ea8dc4b6Seschrock spa_async_dispatch(spa); 7326fa9e4066Sahrens } 7327fa9e4066Sahrens 7328fa9e4066Sahrens /* 7329fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 7330fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 7331fa9e4066Sahrens * sync. 7332fa9e4066Sahrens */ 7333fa9e4066Sahrens void 7334fa9e4066Sahrens spa_sync_allpools(void) 7335fa9e4066Sahrens { 7336fa9e4066Sahrens spa_t *spa = NULL; 7337fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7338fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 7339f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 7340f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 7341fa9e4066Sahrens continue; 7342fa9e4066Sahrens spa_open_ref(spa, FTAG); 7343fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7344fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 7345fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7346fa9e4066Sahrens spa_close(spa, FTAG); 7347fa9e4066Sahrens } 7348fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7349fa9e4066Sahrens } 7350fa9e4066Sahrens 7351fa9e4066Sahrens /* 7352fa9e4066Sahrens * ========================================================================== 7353fa9e4066Sahrens * Miscellaneous routines 7354fa9e4066Sahrens * ========================================================================== 7355fa9e4066Sahrens */ 7356fa9e4066Sahrens 7357fa9e4066Sahrens /* 7358fa9e4066Sahrens * Remove all pools in the system. 7359fa9e4066Sahrens */ 7360fa9e4066Sahrens void 7361fa9e4066Sahrens spa_evict_all(void) 7362fa9e4066Sahrens { 7363fa9e4066Sahrens spa_t *spa; 7364fa9e4066Sahrens 7365fa9e4066Sahrens /* 7366fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 7367fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 7368fa9e4066Sahrens */ 7369fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7370fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 7371fa9e4066Sahrens /* 7372ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 7373ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 7374ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 7375fa9e4066Sahrens */ 7376fa9e4066Sahrens spa_open_ref(spa, FTAG); 7377fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7378ea8dc4b6Seschrock spa_async_suspend(spa); 7379fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 7380fa9e4066Sahrens spa_close(spa, FTAG); 7381fa9e4066Sahrens 7382fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 7383fa9e4066Sahrens spa_unload(spa); 7384fa9e4066Sahrens spa_deactivate(spa); 7385fa9e4066Sahrens } 7386fa9e4066Sahrens spa_remove(spa); 7387fa9e4066Sahrens } 7388fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 7389fa9e4066Sahrens } 7390ea8dc4b6Seschrock 7391ea8dc4b6Seschrock vdev_t * 73926809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 7393ea8dc4b6Seschrock { 7394c5904d13Seschrock vdev_t *vd; 7395c5904d13Seschrock int i; 7396c5904d13Seschrock 7397c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 7398c5904d13Seschrock return (vd); 7399c5904d13Seschrock 74006809eb4eSEric Schrock if (aux) { 7401c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 7402c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 74036809eb4eSEric Schrock if (vd->vdev_guid == guid) 74046809eb4eSEric Schrock return (vd); 74056809eb4eSEric Schrock } 74066809eb4eSEric Schrock 74076809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 74086809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 7409c5904d13Seschrock if (vd->vdev_guid == guid) 7410c5904d13Seschrock return (vd); 7411c5904d13Seschrock } 7412c5904d13Seschrock } 7413c5904d13Seschrock 7414c5904d13Seschrock return (NULL); 7415ea8dc4b6Seschrock } 7416eaca9bbdSeschrock 7417eaca9bbdSeschrock void 7418990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 7419eaca9bbdSeschrock { 7420f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 7421f9af39baSGeorge Wilson 7422e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7423eaca9bbdSeschrock 7424eaca9bbdSeschrock /* 7425eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 7426eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 7427eaca9bbdSeschrock * possible. 7428eaca9bbdSeschrock */ 742962eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 74305d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 7431eaca9bbdSeschrock 7432990b4856Slling spa->spa_uberblock.ub_version = version; 7433eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 7434eaca9bbdSeschrock 7435e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 743699653d4eSeschrock 743799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 743899653d4eSeschrock } 743999653d4eSeschrock 744099653d4eSeschrock boolean_t 744199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 744299653d4eSeschrock { 744399653d4eSeschrock int i; 744439c23413Seschrock uint64_t spareguid; 7445fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 744699653d4eSeschrock 7447fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 7448fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 744999653d4eSeschrock return (B_TRUE); 745099653d4eSeschrock 7451fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 7452fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 7453fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 745439c23413Seschrock return (B_TRUE); 745539c23413Seschrock } 745639c23413Seschrock 745799653d4eSeschrock return (B_FALSE); 7458eaca9bbdSeschrock } 7459b1b8ab34Slling 746089a89ebfSlling /* 746189a89ebfSlling * Check if a pool has an active shared spare device. 746289a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 746389a89ebfSlling */ 746489a89ebfSlling static boolean_t 746589a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 746689a89ebfSlling { 746789a89ebfSlling int i, refcnt; 746889a89ebfSlling uint64_t pool; 746989a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 747089a89ebfSlling 747189a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 747289a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 747389a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 747489a89ebfSlling refcnt > 2) 747589a89ebfSlling return (B_TRUE); 747689a89ebfSlling } 747789a89ebfSlling 747889a89ebfSlling return (B_FALSE); 747989a89ebfSlling } 748089a89ebfSlling 74815cabbc6bSPrashanth Sreenivasa sysevent_t * 7482ce1577b0SDave Eddy spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 74833d7072f8Seschrock { 7484b72b6bb1SAlan Somers sysevent_t *ev = NULL; 74853d7072f8Seschrock #ifdef _KERNEL 74863d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 74873d7072f8Seschrock sysevent_value_t value; 74883d7072f8Seschrock 74893d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 74903d7072f8Seschrock SE_SLEEP); 7491b72b6bb1SAlan Somers ASSERT(ev != NULL); 74923d7072f8Seschrock 74933d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 74943d7072f8Seschrock value.value.sv_string = spa_name(spa); 74953d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 74963d7072f8Seschrock goto done; 74973d7072f8Seschrock 74983d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 74993d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 75003d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 75013d7072f8Seschrock goto done; 75023d7072f8Seschrock 75033d7072f8Seschrock if (vd) { 75043d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 75053d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 75063d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 75073d7072f8Seschrock SE_SLEEP) != 0) 75083d7072f8Seschrock goto done; 75093d7072f8Seschrock 75103d7072f8Seschrock if (vd->vdev_path) { 75113d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 75123d7072f8Seschrock value.value.sv_string = vd->vdev_path; 75133d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 75143d7072f8Seschrock &value, SE_SLEEP) != 0) 75153d7072f8Seschrock goto done; 75163d7072f8Seschrock } 75173d7072f8Seschrock } 75183d7072f8Seschrock 7519ce1577b0SDave Eddy if (hist_nvl != NULL) { 7520ce1577b0SDave Eddy fnvlist_merge((nvlist_t *)attr, hist_nvl); 7521ce1577b0SDave Eddy } 7522ce1577b0SDave Eddy 7523b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 7524b01c3b58Seschrock goto done; 7525b01c3b58Seschrock attr = NULL; 7526b01c3b58Seschrock 75273d7072f8Seschrock done: 75283d7072f8Seschrock if (attr) 75293d7072f8Seschrock sysevent_free_attr(attr); 7530b72b6bb1SAlan Somers 7531b72b6bb1SAlan Somers #endif 7532b72b6bb1SAlan Somers return (ev); 7533b72b6bb1SAlan Somers } 7534b72b6bb1SAlan Somers 75355cabbc6bSPrashanth Sreenivasa void 7536b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 7537b72b6bb1SAlan Somers { 7538b72b6bb1SAlan Somers #ifdef _KERNEL 7539b72b6bb1SAlan Somers sysevent_id_t eid; 7540b72b6bb1SAlan Somers 7541b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 75423d7072f8Seschrock sysevent_free(ev); 75433d7072f8Seschrock #endif 75443d7072f8Seschrock } 7545b72b6bb1SAlan Somers 75465cabbc6bSPrashanth Sreenivasa void 75475cabbc6bSPrashanth Sreenivasa spa_event_discard(sysevent_t *ev) 75485cabbc6bSPrashanth Sreenivasa { 75495cabbc6bSPrashanth Sreenivasa #ifdef _KERNEL 75505cabbc6bSPrashanth Sreenivasa sysevent_free(ev); 75515cabbc6bSPrashanth Sreenivasa #endif 75525cabbc6bSPrashanth Sreenivasa } 75535cabbc6bSPrashanth Sreenivasa 7554b72b6bb1SAlan Somers /* 7555b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 7556b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 7557ce1577b0SDave Eddy * filled in from the spa and (optionally) the vdev and history nvl. This 7558ce1577b0SDave Eddy * doesn't do anything in the userland libzpool, as we don't want consumers to 7559ce1577b0SDave Eddy * misinterpret ztest or zdb as real changes. 7560b72b6bb1SAlan Somers */ 7561b72b6bb1SAlan Somers void 7562ce1577b0SDave Eddy spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 7563b72b6bb1SAlan Somers { 7564ce1577b0SDave Eddy spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 7565b72b6bb1SAlan Somers } 7566