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. 24*5cabbc6bSPrashanth 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. 325aeb9474SGarrett D'Amore */ 33fa9e4066Sahrens 34fa9e4066Sahrens /* 353e30c24aSWill Andrews * SPA: Storage Pool Allocator 363e30c24aSWill Andrews * 37fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 38fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 39fa9e4066Sahrens * pool. 40fa9e4066Sahrens */ 41fa9e4066Sahrens 42fa9e4066Sahrens #include <sys/zfs_context.h> 43ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 44fa9e4066Sahrens #include <sys/spa_impl.h> 45fa9e4066Sahrens #include <sys/zio.h> 46fa9e4066Sahrens #include <sys/zio_checksum.h> 47fa9e4066Sahrens #include <sys/dmu.h> 48fa9e4066Sahrens #include <sys/dmu_tx.h> 49fa9e4066Sahrens #include <sys/zap.h> 50fa9e4066Sahrens #include <sys/zil.h> 51b24ab676SJeff Bonwick #include <sys/ddt.h> 52fa9e4066Sahrens #include <sys/vdev_impl.h> 53*5cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h> 54*5cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_mapping.h> 55*5cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_births.h> 56fa9e4066Sahrens #include <sys/metaslab.h> 5788ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 58fa9e4066Sahrens #include <sys/uberblock_impl.h> 59fa9e4066Sahrens #include <sys/txg.h> 60fa9e4066Sahrens #include <sys/avl.h> 61*5cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h> 62fa9e4066Sahrens #include <sys/dmu_traverse.h> 63b1b8ab34Slling #include <sys/dmu_objset.h> 64fa9e4066Sahrens #include <sys/unique.h> 65fa9e4066Sahrens #include <sys/dsl_pool.h> 66b1b8ab34Slling #include <sys/dsl_dataset.h> 67fa9e4066Sahrens #include <sys/dsl_dir.h> 68fa9e4066Sahrens #include <sys/dsl_prop.h> 69b1b8ab34Slling #include <sys/dsl_synctask.h> 70fa9e4066Sahrens #include <sys/fs/zfs.h> 71fa94a07fSbrendan #include <sys/arc.h> 72fa9e4066Sahrens #include <sys/callb.h> 7395173954Sek #include <sys/systeminfo.h> 74e7cbe64fSgw #include <sys/spa_boot.h> 75573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 763f9d6ad7SLin Ling #include <sys/dsl_scan.h> 77ad135b5dSChristopher Siden #include <sys/zfeature.h> 783b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 79770499e1SDan Kimmel #include <sys/abd.h> 80fa9e4066Sahrens 815679c89fSjv #ifdef _KERNEL 82dedec472SJack Meng #include <sys/bootprops.h> 8335a5a358SJonathan Adams #include <sys/callb.h> 8435a5a358SJonathan Adams #include <sys/cpupart.h> 8535a5a358SJonathan Adams #include <sys/pool.h> 8635a5a358SJonathan Adams #include <sys/sysdc.h> 8735a5a358SJonathan Adams #include <sys/zone.h> 885679c89fSjv #endif /* _KERNEL */ 895679c89fSjv 90990b4856Slling #include "zfs_prop.h" 91b7b97454Sperrin #include "zfs_comutil.h" 92990b4856Slling 933cb69f73SWill Andrews /* 943cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 953cb69f73SWill Andrews * should be retried. 963cb69f73SWill Andrews */ 97*5cabbc6bSPrashanth Sreenivasa int zfs_ccw_retry_interval = 300; 983cb69f73SWill Andrews 9935a5a358SJonathan Adams typedef enum zti_modes { 100ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 101ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 102ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 103ec94d322SAdam Leventhal ZTI_NMODES 10435a5a358SJonathan Adams } zti_modes_t; 105416e0cd8Sek 106ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 107ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 108ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1092e0c549eSJonathan Adams 110ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 111ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1122e0c549eSJonathan Adams 1132e0c549eSJonathan Adams typedef struct zio_taskq_info { 114ec94d322SAdam Leventhal zti_modes_t zti_mode; 11580eb36f2SGeorge Wilson uint_t zti_value; 116ec94d322SAdam Leventhal uint_t zti_count; 1172e0c549eSJonathan Adams } zio_taskq_info_t; 1182e0c549eSJonathan Adams 1192e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 12035a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1212e0c549eSJonathan Adams }; 1222e0c549eSJonathan Adams 12380eb36f2SGeorge Wilson /* 124ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 125ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 126ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 127ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 128ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 129ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 130ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 131ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 132ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 133ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 134ec94d322SAdam Leventhal * particular taskq is chosen at random. 135ec94d322SAdam Leventhal * 136ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 137ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 138ec94d322SAdam Leventhal * need to be handled with minimum delay. 13980eb36f2SGeorge Wilson */ 14080eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 14180eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 142ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1431b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 144ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 145ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 146ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 147ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1482e0c549eSJonathan Adams }; 1492e0c549eSJonathan Adams 1503b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1513b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 15289a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1531195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 154*5cabbc6bSPrashanth Sreenivasa spa_load_state_t state, spa_import_type_t type, boolean_t trust_config, 1551195e687SMark J Musante char **ereport); 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 16635a5a358SJonathan Adams /* 16735a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 16835a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 16935a5a358SJonathan Adams */ 17035a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 17135a5a358SJonathan Adams 172990b4856Slling /* 173990b4856Slling * ========================================================================== 174990b4856Slling * SPA properties routines 175990b4856Slling * ========================================================================== 176990b4856Slling */ 177990b4856Slling 178990b4856Slling /* 179990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 180990b4856Slling */ 1819d82f4f6Slling static void 182990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 183990b4856Slling uint64_t intval, zprop_source_t src) 184990b4856Slling { 185990b4856Slling const char *propname = zpool_prop_to_name(prop); 186990b4856Slling nvlist_t *propval; 187990b4856Slling 1889d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1899d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 190990b4856Slling 1919d82f4f6Slling if (strval != NULL) 1929d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1939d82f4f6Slling else 1949d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 195990b4856Slling 1969d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 197990b4856Slling nvlist_free(propval); 198990b4856Slling } 199990b4856Slling 200990b4856Slling /* 201990b4856Slling * Get property values from the spa configuration. 202990b4856Slling */ 2039d82f4f6Slling static void 204990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 205990b4856Slling { 2064263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 207ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2082e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 209990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 210c5904d13Seschrock spa_config_dirent_t *dp; 2112e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 212990b4856Slling 213e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 214e14bb325SJeff Bonwick 2154263d13fSGeorge Wilson if (rvd != NULL) { 216485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 217b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 218379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 219379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 220485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 221485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 222485bbbf5SGeorge Wilson size - alloc, src); 2234263d13fSGeorge Wilson 2242e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2252e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2262e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2272e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 228f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 229f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 230379c004dSEric Schrock 231485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 232379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 233379c004dSEric Schrock 234b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 235b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 236b24ab676SJeff Bonwick 237379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2384263d13fSGeorge Wilson rvd->vdev_state, src); 239379c004dSEric Schrock 240379c004dSEric Schrock version = spa_version(spa); 241379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 242379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 243379c004dSEric Schrock else 244379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 245379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 246379c004dSEric Schrock } 247990b4856Slling 248ad135b5dSChristopher Siden if (pool != NULL) { 249ad135b5dSChristopher Siden /* 250ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 251ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 252ad135b5dSChristopher Siden */ 2537fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 254ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 255c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 256c1379625SJustin T. Gibbs src); 257ad135b5dSChristopher Siden } else { 258ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 259ad135b5dSChristopher Siden NULL, 0, src); 260ad135b5dSChristopher Siden } 2617fd05ac4SMatthew Ahrens 2627fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2637fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 264c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 265c1379625SJustin T. Gibbs src); 2667fd05ac4SMatthew Ahrens } else { 2677fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2687fd05ac4SMatthew Ahrens NULL, 0, src); 2697fd05ac4SMatthew Ahrens } 270ad135b5dSChristopher Siden } 271ad135b5dSChristopher Siden 2729d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 273990b4856Slling 2748704186eSDan McDonald if (spa->spa_comment != NULL) { 2758704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2768704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2778704186eSDan McDonald } 2788704186eSDan McDonald 2799d82f4f6Slling if (spa->spa_root != NULL) 2809d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2819d82f4f6Slling 0, ZPROP_SRC_LOCAL); 282990b4856Slling 283b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 284b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 285b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 286b5152584SMatthew Ahrens } else { 287b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 288b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 289b5152584SMatthew Ahrens } 290b5152584SMatthew Ahrens 291c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 292c5904d13Seschrock if (dp->scd_path == NULL) { 2939d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 294c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 295c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2969d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 297c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2982f8aaab3Seschrock } 2992f8aaab3Seschrock } 300990b4856Slling } 301990b4856Slling 302990b4856Slling /* 303990b4856Slling * Get zpool property values. 304990b4856Slling */ 305990b4856Slling int 306990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 307990b4856Slling { 308b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 309990b4856Slling zap_cursor_t zc; 310990b4856Slling zap_attribute_t za; 311990b4856Slling int err; 312990b4856Slling 3139d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 314990b4856Slling 315e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 316e14bb325SJeff Bonwick 317990b4856Slling /* 318990b4856Slling * Get properties from the spa config. 319990b4856Slling */ 3209d82f4f6Slling spa_prop_get_config(spa, nvp); 321990b4856Slling 322990b4856Slling /* If no pool property object, no more prop to get. */ 323afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 324990b4856Slling mutex_exit(&spa->spa_props_lock); 325990b4856Slling return (0); 326990b4856Slling } 327990b4856Slling 328990b4856Slling /* 329990b4856Slling * Get properties from the MOS pool property object. 330990b4856Slling */ 331990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 332990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 333990b4856Slling zap_cursor_advance(&zc)) { 334990b4856Slling uint64_t intval = 0; 335990b4856Slling char *strval = NULL; 336990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 337990b4856Slling zpool_prop_t prop; 338990b4856Slling 339990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 340990b4856Slling continue; 341990b4856Slling 342990b4856Slling switch (za.za_integer_length) { 343990b4856Slling case 8: 344990b4856Slling /* integer property */ 345990b4856Slling if (za.za_first_integer != 346990b4856Slling zpool_prop_default_numeric(prop)) 347990b4856Slling src = ZPROP_SRC_LOCAL; 348990b4856Slling 349990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 350990b4856Slling dsl_pool_t *dp; 351990b4856Slling dsl_dataset_t *ds = NULL; 352990b4856Slling 353990b4856Slling dp = spa_get_dsl(spa); 3543b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 355745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 356745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3573b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 358990b4856Slling break; 359990b4856Slling } 360990b4856Slling 3619adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 362990b4856Slling KM_SLEEP); 363990b4856Slling dsl_dataset_name(ds, strval); 364745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3653b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 366990b4856Slling } else { 367990b4856Slling strval = NULL; 368990b4856Slling intval = za.za_first_integer; 369990b4856Slling } 370990b4856Slling 3719d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 372990b4856Slling 373990b4856Slling if (strval != NULL) 3749adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 375990b4856Slling 376990b4856Slling break; 377990b4856Slling 378990b4856Slling case 1: 379990b4856Slling /* string property */ 380990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 381990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 382990b4856Slling za.za_name, 1, za.za_num_integers, strval); 383990b4856Slling if (err) { 384990b4856Slling kmem_free(strval, za.za_num_integers); 385990b4856Slling break; 386990b4856Slling } 3879d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 388990b4856Slling kmem_free(strval, za.za_num_integers); 389990b4856Slling break; 390990b4856Slling 391990b4856Slling default: 392990b4856Slling break; 393990b4856Slling } 394990b4856Slling } 395990b4856Slling zap_cursor_fini(&zc); 396990b4856Slling mutex_exit(&spa->spa_props_lock); 397990b4856Slling out: 398990b4856Slling if (err && err != ENOENT) { 399990b4856Slling nvlist_free(*nvp); 4009d82f4f6Slling *nvp = NULL; 401990b4856Slling return (err); 402990b4856Slling } 403990b4856Slling 404990b4856Slling return (0); 405990b4856Slling } 406990b4856Slling 407990b4856Slling /* 408990b4856Slling * Validate the given pool properties nvlist and modify the list 409990b4856Slling * for the property values to be set. 410990b4856Slling */ 411990b4856Slling static int 412990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 413990b4856Slling { 414990b4856Slling nvpair_t *elem; 415990b4856Slling int error = 0, reset_bootfs = 0; 416d5285caeSGeorge Wilson uint64_t objnum = 0; 417ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 418990b4856Slling 419990b4856Slling elem = NULL; 420990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 421990b4856Slling uint64_t intval; 422ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 423ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 424ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 425ad135b5dSChristopher Siden 426ad135b5dSChristopher Siden switch (prop) { 427ad135b5dSChristopher Siden case ZPROP_INVAL: 428ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 429be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 430ad135b5dSChristopher Siden break; 431ad135b5dSChristopher Siden } 432990b4856Slling 433ad135b5dSChristopher Siden /* 434ad135b5dSChristopher Siden * Sanitize the input. 435ad135b5dSChristopher Siden */ 436ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 437be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 438ad135b5dSChristopher Siden break; 439ad135b5dSChristopher Siden } 440990b4856Slling 441ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 442be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 443ad135b5dSChristopher Siden break; 444ad135b5dSChristopher Siden } 445ad135b5dSChristopher Siden 446ad135b5dSChristopher Siden if (intval != 0) { 447be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 448ad135b5dSChristopher Siden break; 449ad135b5dSChristopher Siden } 450ad135b5dSChristopher Siden 451ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 452ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 453be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 454ad135b5dSChristopher Siden break; 455ad135b5dSChristopher Siden } 456ad135b5dSChristopher Siden 457ad135b5dSChristopher Siden has_feature = B_TRUE; 458ad135b5dSChristopher Siden break; 459990b4856Slling 460990b4856Slling case ZPOOL_PROP_VERSION: 461990b4856Slling error = nvpair_value_uint64(elem, &intval); 462990b4856Slling if (!error && 463ad135b5dSChristopher Siden (intval < spa_version(spa) || 464ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 465ad135b5dSChristopher Siden has_feature)) 466be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 467990b4856Slling break; 468990b4856Slling 469990b4856Slling case ZPOOL_PROP_DELEGATION: 470990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 471d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 472573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 473990b4856Slling error = nvpair_value_uint64(elem, &intval); 474990b4856Slling if (!error && intval > 1) 475be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 476990b4856Slling break; 477990b4856Slling 478990b4856Slling case ZPOOL_PROP_BOOTFS: 47925f89ee2SJeff Bonwick /* 48025f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 48125f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 48225f89ee2SJeff Bonwick * the bootfs property cannot be set. 48325f89ee2SJeff Bonwick */ 484990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 485be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 486990b4856Slling break; 487990b4856Slling } 488990b4856Slling 489990b4856Slling /* 49015e6edf1Sgw * Make sure the vdev config is bootable 491990b4856Slling */ 49215e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 493be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 494990b4856Slling break; 495990b4856Slling } 496990b4856Slling 497990b4856Slling reset_bootfs = 1; 498990b4856Slling 499990b4856Slling error = nvpair_value_string(elem, &strval); 500990b4856Slling 501990b4856Slling if (!error) { 502ad135b5dSChristopher Siden objset_t *os; 503b5152584SMatthew Ahrens uint64_t propval; 50415e6edf1Sgw 505990b4856Slling if (strval == NULL || strval[0] == '\0') { 506990b4856Slling objnum = zpool_prop_default_numeric( 507990b4856Slling ZPOOL_PROP_BOOTFS); 508990b4856Slling break; 509990b4856Slling } 510990b4856Slling 511503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 512990b4856Slling break; 51315e6edf1Sgw 514b5152584SMatthew Ahrens /* 515b5152584SMatthew Ahrens * Must be ZPL, and its property settings 516b5152584SMatthew Ahrens * must be supported by GRUB (compression 517b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 518b5152584SMatthew Ahrens */ 519503ad85cSMatthew Ahrens 520503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 521be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5223b2aab18SMatthew Ahrens } else if ((error = 5233b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 52415e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 525b5152584SMatthew Ahrens &propval)) == 0 && 526b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 527b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 52815e6edf1Sgw } else { 52915e6edf1Sgw objnum = dmu_objset_id(os); 53015e6edf1Sgw } 531503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 532990b4856Slling } 533990b4856Slling break; 534e14bb325SJeff Bonwick 5350a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5360a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5370a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5380a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 539be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5400a4e9518Sgw 5410a4e9518Sgw /* 5420a4e9518Sgw * This is a special case which only occurs when 5430a4e9518Sgw * the pool has completely failed. This allows 5440a4e9518Sgw * the user to change the in-core failmode property 5450a4e9518Sgw * without syncing it out to disk (I/Os might 5460a4e9518Sgw * currently be blocked). We do this by returning 5470a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5480a4e9518Sgw * into thinking we encountered a property validation 5490a4e9518Sgw * error. 5500a4e9518Sgw */ 551e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5520a4e9518Sgw spa->spa_failmode = intval; 553be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5540a4e9518Sgw } 5550a4e9518Sgw break; 5562f8aaab3Seschrock 5572f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5582f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5592f8aaab3Seschrock break; 5602f8aaab3Seschrock 5612f8aaab3Seschrock if (strval[0] == '\0') 5622f8aaab3Seschrock break; 5632f8aaab3Seschrock 5642f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5652f8aaab3Seschrock break; 5662f8aaab3Seschrock 5672f8aaab3Seschrock if (strval[0] != '/') { 568be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5692f8aaab3Seschrock break; 5702f8aaab3Seschrock } 5712f8aaab3Seschrock 5722f8aaab3Seschrock slash = strrchr(strval, '/'); 5732f8aaab3Seschrock ASSERT(slash != NULL); 5742f8aaab3Seschrock 5752f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5762f8aaab3Seschrock strcmp(slash, "/..") == 0) 577be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5782f8aaab3Seschrock break; 579b24ab676SJeff Bonwick 5808704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5818704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5828704186eSDan McDonald break; 5838704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5848704186eSDan McDonald /* 5858704186eSDan McDonald * The kernel doesn't have an easy isprint() 5868704186eSDan McDonald * check. For this kernel check, we merely 5878704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5888704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5898704186eSDan McDonald */ 5908704186eSDan McDonald if (*check >= 0x7f) { 591be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5928704186eSDan McDonald break; 5938704186eSDan McDonald } 5948704186eSDan McDonald } 5958704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5968704186eSDan McDonald error = E2BIG; 5978704186eSDan McDonald break; 5988704186eSDan McDonald 599b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 600b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 601be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 602b24ab676SJeff Bonwick else 603b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 604b24ab676SJeff Bonwick if (error == 0 && 605b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 606be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 607b24ab676SJeff Bonwick break; 608990b4856Slling } 609990b4856Slling 610990b4856Slling if (error) 611990b4856Slling break; 612990b4856Slling } 613990b4856Slling 614990b4856Slling if (!error && reset_bootfs) { 615990b4856Slling error = nvlist_remove(props, 616990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 617990b4856Slling 618990b4856Slling if (!error) { 619990b4856Slling error = nvlist_add_uint64(props, 620990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 621990b4856Slling } 622990b4856Slling } 623990b4856Slling 624990b4856Slling return (error); 625990b4856Slling } 626990b4856Slling 627379c004dSEric Schrock void 628379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 629379c004dSEric Schrock { 630379c004dSEric Schrock char *cachefile; 631379c004dSEric Schrock spa_config_dirent_t *dp; 632379c004dSEric Schrock 633379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 634379c004dSEric Schrock &cachefile) != 0) 635379c004dSEric Schrock return; 636379c004dSEric Schrock 637379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 638379c004dSEric Schrock KM_SLEEP); 639379c004dSEric Schrock 640379c004dSEric Schrock if (cachefile[0] == '\0') 641379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 642379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 643379c004dSEric Schrock dp->scd_path = NULL; 644379c004dSEric Schrock else 645379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 646379c004dSEric Schrock 647379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 648379c004dSEric Schrock if (need_sync) 649379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 650379c004dSEric Schrock } 651379c004dSEric Schrock 652990b4856Slling int 653990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 654990b4856Slling { 655990b4856Slling int error; 656ad135b5dSChristopher Siden nvpair_t *elem = NULL; 657379c004dSEric Schrock boolean_t need_sync = B_FALSE; 658990b4856Slling 659990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 660990b4856Slling return (error); 661990b4856Slling 662379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 663ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 664379c004dSEric Schrock 665f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 666f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 667f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 668379c004dSEric Schrock continue; 669379c004dSEric Schrock 670ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 671ad135b5dSChristopher Siden uint64_t ver; 672ad135b5dSChristopher Siden 673ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 674ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 675ad135b5dSChristopher Siden } else { 676ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 677ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 678ad135b5dSChristopher Siden need_sync = B_TRUE; 679ad135b5dSChristopher Siden } 680ad135b5dSChristopher Siden 681ad135b5dSChristopher Siden /* Save time if the version is already set. */ 682ad135b5dSChristopher Siden if (ver == spa_version(spa)) 683ad135b5dSChristopher Siden continue; 684ad135b5dSChristopher Siden 685ad135b5dSChristopher Siden /* 686ad135b5dSChristopher Siden * In addition to the pool directory object, we might 687ad135b5dSChristopher Siden * create the pool properties object, the features for 688ad135b5dSChristopher Siden * read object, the features for write object, or the 689ad135b5dSChristopher Siden * feature descriptions object. 690ad135b5dSChristopher Siden */ 6913b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6927d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6937d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 694ad135b5dSChristopher Siden if (error) 695ad135b5dSChristopher Siden return (error); 696ad135b5dSChristopher Siden continue; 697ad135b5dSChristopher Siden } 698ad135b5dSChristopher Siden 699379c004dSEric Schrock need_sync = B_TRUE; 700379c004dSEric Schrock break; 701379c004dSEric Schrock } 702379c004dSEric Schrock 703ad135b5dSChristopher Siden if (need_sync) { 7043b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7057d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 706ad135b5dSChristopher Siden } 707ad135b5dSChristopher Siden 708ad135b5dSChristopher Siden return (0); 709990b4856Slling } 710990b4856Slling 711990b4856Slling /* 712990b4856Slling * If the bootfs property value is dsobj, clear it. 713990b4856Slling */ 714990b4856Slling void 715990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 716990b4856Slling { 717990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 718990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 719990b4856Slling spa->spa_pool_props_object, 720990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 721990b4856Slling spa->spa_bootfs = 0; 722990b4856Slling } 723990b4856Slling } 724990b4856Slling 725dfbb9432SGeorge Wilson /*ARGSUSED*/ 726dfbb9432SGeorge Wilson static int 7273b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 728dfbb9432SGeorge Wilson { 7293b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7303b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 731dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 732dfbb9432SGeorge Wilson uint64_t vdev_state; 733dfbb9432SGeorge Wilson 734dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 735dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 736dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 737dfbb9432SGeorge Wilson 738dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 739be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 740dfbb9432SGeorge Wilson 741dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 742dfbb9432SGeorge Wilson 743dfbb9432SGeorge Wilson return (0); 744dfbb9432SGeorge Wilson } 745dfbb9432SGeorge Wilson 746dfbb9432SGeorge Wilson static void 7473b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 748dfbb9432SGeorge Wilson { 7493b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7503b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 751dfbb9432SGeorge Wilson uint64_t oldguid; 752dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 753dfbb9432SGeorge Wilson 754dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 755dfbb9432SGeorge Wilson 756dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 757dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 758dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 759dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 760dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 761dfbb9432SGeorge Wilson 76220128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 763dfbb9432SGeorge Wilson oldguid, *newguid); 764dfbb9432SGeorge Wilson } 765dfbb9432SGeorge Wilson 766e9103aaeSGarrett D'Amore /* 767e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 768e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 769e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 770e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 771e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 772e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 773e9103aaeSGarrett D'Amore * sysevent to update any watchers. 774e9103aaeSGarrett D'Amore */ 775e9103aaeSGarrett D'Amore int 776e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 777e9103aaeSGarrett D'Amore { 778dfbb9432SGeorge Wilson int error; 779dfbb9432SGeorge Wilson uint64_t guid; 780e9103aaeSGarrett D'Amore 7812c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 782dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 783dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 784e9103aaeSGarrett D'Amore 7853b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7867d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 787e9103aaeSGarrett D'Amore 788dfbb9432SGeorge Wilson if (error == 0) { 789*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 790ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 791dfbb9432SGeorge Wilson } 792e9103aaeSGarrett D'Amore 793dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7942c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 795e9103aaeSGarrett D'Amore 796dfbb9432SGeorge Wilson return (error); 797e9103aaeSGarrett D'Amore } 798e9103aaeSGarrett D'Amore 799fa9e4066Sahrens /* 800fa9e4066Sahrens * ========================================================================== 801fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 802fa9e4066Sahrens * ========================================================================== 803fa9e4066Sahrens */ 804fa9e4066Sahrens 805ea8dc4b6Seschrock static int 806ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 807ea8dc4b6Seschrock { 808ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 809ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 810ea8dc4b6Seschrock int ret; 811ea8dc4b6Seschrock 812ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8137802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 814ea8dc4b6Seschrock 815ea8dc4b6Seschrock if (ret < 0) 816ea8dc4b6Seschrock return (-1); 817ea8dc4b6Seschrock else if (ret > 0) 818ea8dc4b6Seschrock return (1); 819ea8dc4b6Seschrock else 820ea8dc4b6Seschrock return (0); 821ea8dc4b6Seschrock } 822ea8dc4b6Seschrock 823ea8dc4b6Seschrock /* 824ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 825ea8dc4b6Seschrock * re-initializes them in the process. 826ea8dc4b6Seschrock */ 827ea8dc4b6Seschrock void 828ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 829ea8dc4b6Seschrock { 830ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 831ea8dc4b6Seschrock 832ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 833ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 834ea8dc4b6Seschrock 835ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 836ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 837ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 838ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 839ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 840ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 841ea8dc4b6Seschrock } 842ea8dc4b6Seschrock 843ec94d322SAdam Leventhal static void 844ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 845fa9e4066Sahrens { 846ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 847ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 848ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 849ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 850ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 851ec94d322SAdam Leventhal char name[32]; 8525aeb9474SGarrett D'Amore uint_t flags = 0; 85335a5a358SJonathan Adams boolean_t batch = B_FALSE; 854fa9e4066Sahrens 855ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 856ec94d322SAdam Leventhal tqs->stqs_count = 0; 857ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 858ec94d322SAdam Leventhal return; 859ec94d322SAdam Leventhal } 860fa9e4066Sahrens 861ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 862fa9e4066Sahrens 863ec94d322SAdam Leventhal tqs->stqs_count = count; 864ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86535a5a358SJonathan Adams 86669962b56SMatthew Ahrens switch (mode) { 86769962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86869962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86969962b56SMatthew Ahrens value = MAX(value, 1); 87069962b56SMatthew Ahrens break; 871ec94d322SAdam Leventhal 87269962b56SMatthew Ahrens case ZTI_MODE_BATCH: 87369962b56SMatthew Ahrens batch = B_TRUE; 87469962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87569962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87669962b56SMatthew Ahrens break; 877ec94d322SAdam Leventhal 87869962b56SMatthew Ahrens default: 87969962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 88069962b56SMatthew Ahrens "spa_activate()", 88169962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 88269962b56SMatthew Ahrens break; 88369962b56SMatthew Ahrens } 884ec94d322SAdam Leventhal 88569962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88669962b56SMatthew Ahrens taskq_t *tq; 887ec94d322SAdam Leventhal 888ec94d322SAdam Leventhal if (count > 1) { 889ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 890ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 891ec94d322SAdam Leventhal } else { 892ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 893ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 894ec94d322SAdam Leventhal } 895ec94d322SAdam Leventhal 896ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 897ec94d322SAdam Leventhal if (batch) 898ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 899ec94d322SAdam Leventhal 900ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 901ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 902ec94d322SAdam Leventhal } else { 90369962b56SMatthew Ahrens pri_t pri = maxclsyspri; 90469962b56SMatthew Ahrens /* 90569962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90669962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90769962b56SMatthew Ahrens * than the other taskqs. 90869962b56SMatthew Ahrens */ 90969962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 91069962b56SMatthew Ahrens pri--; 91169962b56SMatthew Ahrens 91269962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 913ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 914ec94d322SAdam Leventhal } 915ec94d322SAdam Leventhal 916ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 917ec94d322SAdam Leventhal } 918ec94d322SAdam Leventhal } 919ec94d322SAdam Leventhal 920ec94d322SAdam Leventhal static void 921ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 922ec94d322SAdam Leventhal { 923ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 924ec94d322SAdam Leventhal 925ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 926ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 927ec94d322SAdam Leventhal return; 928ec94d322SAdam Leventhal } 929ec94d322SAdam Leventhal 930ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 931ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 932ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 93335a5a358SJonathan Adams } 93435a5a358SJonathan Adams 935ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 936ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 937ec94d322SAdam Leventhal } 93835a5a358SJonathan Adams 939ec94d322SAdam Leventhal /* 940ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 941ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 942ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 943ec94d322SAdam Leventhal * the low bits of gethrtime(). 944ec94d322SAdam Leventhal */ 945ec94d322SAdam Leventhal void 946ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 947ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 948ec94d322SAdam Leventhal { 949ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 950ec94d322SAdam Leventhal taskq_t *tq; 951ec94d322SAdam Leventhal 952ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 953ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 954ec94d322SAdam Leventhal 955ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 956ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 957ec94d322SAdam Leventhal } else { 958ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95935a5a358SJonathan Adams } 960ec94d322SAdam Leventhal 961ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 96235a5a358SJonathan Adams } 96335a5a358SJonathan Adams 96435a5a358SJonathan Adams static void 96535a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96635a5a358SJonathan Adams { 967e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 968e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 969ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 97035a5a358SJonathan Adams } 97135a5a358SJonathan Adams } 97235a5a358SJonathan Adams } 97335a5a358SJonathan Adams 97435a5a358SJonathan Adams #ifdef _KERNEL 97535a5a358SJonathan Adams static void 97635a5a358SJonathan Adams spa_thread(void *arg) 97735a5a358SJonathan Adams { 97835a5a358SJonathan Adams callb_cpr_t cprinfo; 9792e0c549eSJonathan Adams 98035a5a358SJonathan Adams spa_t *spa = arg; 98135a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9822e0c549eSJonathan Adams 98335a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 98435a5a358SJonathan Adams spa->spa_name); 9852e0c549eSJonathan Adams 98635a5a358SJonathan Adams ASSERT(curproc != &p0); 98735a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98835a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98935a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9902e0c549eSJonathan Adams 99135a5a358SJonathan Adams /* bind this thread to the requested psrset */ 99235a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 99335a5a358SJonathan Adams pool_lock(); 99435a5a358SJonathan Adams mutex_enter(&cpu_lock); 99535a5a358SJonathan Adams mutex_enter(&pidlock); 99635a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99780eb36f2SGeorge Wilson 99835a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99935a5a358SJonathan Adams 0, NULL, NULL) == 0) { 100035a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 100135a5a358SJonathan Adams } else { 100235a5a358SJonathan Adams cmn_err(CE_WARN, 100335a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 100435a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100535a5a358SJonathan Adams } 100635a5a358SJonathan Adams 100735a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100835a5a358SJonathan Adams mutex_exit(&pidlock); 100935a5a358SJonathan Adams mutex_exit(&cpu_lock); 101035a5a358SJonathan Adams pool_unlock(); 101135a5a358SJonathan Adams } 101235a5a358SJonathan Adams 101335a5a358SJonathan Adams if (zio_taskq_sysdc) { 101435a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101535a5a358SJonathan Adams } 101635a5a358SJonathan Adams 101735a5a358SJonathan Adams spa->spa_proc = curproc; 101835a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101935a5a358SJonathan Adams 102035a5a358SJonathan Adams spa_create_zio_taskqs(spa); 102135a5a358SJonathan Adams 102235a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 102335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 102435a5a358SJonathan Adams 102535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102635a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102735a5a358SJonathan Adams 102835a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102935a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 103035a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 103135a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 103235a5a358SJonathan Adams 103335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 103435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103535a5a358SJonathan Adams spa->spa_proc = &p0; 103635a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103735a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103835a5a358SJonathan Adams 103935a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 104035a5a358SJonathan Adams lwp_exit(); 104135a5a358SJonathan Adams } 104235a5a358SJonathan Adams #endif 104335a5a358SJonathan Adams 104435a5a358SJonathan Adams /* 104535a5a358SJonathan Adams * Activate an uninitialized pool. 104635a5a358SJonathan Adams */ 104735a5a358SJonathan Adams static void 104835a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104935a5a358SJonathan Adams { 105035a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 105135a5a358SJonathan Adams 105235a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 105335a5a358SJonathan Adams spa->spa_mode = mode; 105435a5a358SJonathan Adams 105535a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105635a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105735a5a358SJonathan Adams 105835a5a358SJonathan Adams /* Try to create a covering process */ 105935a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 106035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 106135a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 106235a5a358SJonathan Adams spa->spa_did = 0; 106335a5a358SJonathan Adams 106435a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106535a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106635a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106735a5a358SJonathan Adams NULL, 0) == 0) { 106835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106935a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 107035a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 107135a5a358SJonathan Adams &spa->spa_proc_lock); 10722e0c549eSJonathan Adams } 107335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 107435a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107535a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107635a5a358SJonathan Adams } else { 107735a5a358SJonathan Adams #ifdef _KERNEL 107835a5a358SJonathan Adams cmn_err(CE_WARN, 107935a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 108035a5a358SJonathan Adams spa->spa_name); 108135a5a358SJonathan Adams #endif 1082e14bb325SJeff Bonwick } 1083fa9e4066Sahrens } 108435a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108535a5a358SJonathan Adams 108635a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108735a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108835a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108935a5a358SJonathan Adams } 1090fa9e4066Sahrens 1091*5cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) 1092*5cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, 0); 1093*5cabbc6bSPrashanth Sreenivasa 1094e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1095e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1096bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1097bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1098e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1099e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1100fa9e4066Sahrens 1101b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1102fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1103ea8dc4b6Seschrock 1104ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1105ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1106ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1107ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1108ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1109ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1110fa9e4066Sahrens } 1111fa9e4066Sahrens 1112fa9e4066Sahrens /* 1113fa9e4066Sahrens * Opposite of spa_activate(). 1114fa9e4066Sahrens */ 1115fa9e4066Sahrens static void 1116fa9e4066Sahrens spa_deactivate(spa_t *spa) 1117fa9e4066Sahrens { 1118fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1119fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1120fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 112125f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1122fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1123fa9e4066Sahrens 1124bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1125bc9014e6SJustin Gibbs 1126fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1127fa9e4066Sahrens 1128e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1129bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1130e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1131fa9e4066Sahrens 1132e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1133e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1134ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1135e14bb325SJeff Bonwick } 1136fa9e4066Sahrens } 1137fa9e4066Sahrens 1138*5cabbc6bSPrashanth Sreenivasa for (size_t i = 0; i < TXG_SIZE; i++) { 1139*5cabbc6bSPrashanth Sreenivasa ASSERT3P(spa->spa_txg_zio[i], !=, NULL); 1140*5cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[i])); 1141*5cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[i] = NULL; 1142*5cabbc6bSPrashanth Sreenivasa } 1143*5cabbc6bSPrashanth Sreenivasa 1144fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1145fa9e4066Sahrens spa->spa_normal_class = NULL; 1146fa9e4066Sahrens 11478654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11488654d025Sperrin spa->spa_log_class = NULL; 11498654d025Sperrin 1150ea8dc4b6Seschrock /* 1151ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1152ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1153ea8dc4b6Seschrock */ 1154ea8dc4b6Seschrock spa_errlog_drain(spa); 1155ea8dc4b6Seschrock 1156ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1157ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1158ea8dc4b6Seschrock 1159fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 116035a5a358SJonathan Adams 116135a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 116235a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 116335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 116435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 116535a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 116635a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 116735a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 116835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 116935a5a358SJonathan Adams } 117035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 117135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 117235a5a358SJonathan Adams } 117335a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 117435a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 117535a5a358SJonathan Adams 117635a5a358SJonathan Adams /* 117735a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 117835a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 117935a5a358SJonathan Adams * it. 118035a5a358SJonathan Adams */ 118135a5a358SJonathan Adams if (spa->spa_did != 0) { 118235a5a358SJonathan Adams thread_join(spa->spa_did); 118335a5a358SJonathan Adams spa->spa_did = 0; 118435a5a358SJonathan Adams } 1185fa9e4066Sahrens } 1186fa9e4066Sahrens 1187fa9e4066Sahrens /* 1188fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1189fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1190fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1191fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1192fa9e4066Sahrens */ 119399653d4eSeschrock static int 119499653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 119599653d4eSeschrock uint_t id, int atype) 1196fa9e4066Sahrens { 1197fa9e4066Sahrens nvlist_t **child; 1198573ca77eSGeorge Wilson uint_t children; 119999653d4eSeschrock int error; 1200fa9e4066Sahrens 120199653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 120299653d4eSeschrock return (error); 1203fa9e4066Sahrens 120499653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 120599653d4eSeschrock return (0); 1206fa9e4066Sahrens 1207e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1208e14bb325SJeff Bonwick &child, &children); 1209e14bb325SJeff Bonwick 1210e14bb325SJeff Bonwick if (error == ENOENT) 1211e14bb325SJeff Bonwick return (0); 1212e14bb325SJeff Bonwick 1213e14bb325SJeff Bonwick if (error) { 121499653d4eSeschrock vdev_free(*vdp); 121599653d4eSeschrock *vdp = NULL; 1216be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1217fa9e4066Sahrens } 1218fa9e4066Sahrens 1219573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 122099653d4eSeschrock vdev_t *vd; 122199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 122299653d4eSeschrock atype)) != 0) { 122399653d4eSeschrock vdev_free(*vdp); 122499653d4eSeschrock *vdp = NULL; 122599653d4eSeschrock return (error); 1226fa9e4066Sahrens } 1227fa9e4066Sahrens } 1228fa9e4066Sahrens 122999653d4eSeschrock ASSERT(*vdp != NULL); 123099653d4eSeschrock 123199653d4eSeschrock return (0); 1232fa9e4066Sahrens } 1233fa9e4066Sahrens 1234fa9e4066Sahrens /* 1235fa9e4066Sahrens * Opposite of spa_load(). 1236fa9e4066Sahrens */ 1237fa9e4066Sahrens static void 1238fa9e4066Sahrens spa_unload(spa_t *spa) 1239fa9e4066Sahrens { 124099653d4eSeschrock int i; 124199653d4eSeschrock 1242e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1243e14bb325SJeff Bonwick 1244ea8dc4b6Seschrock /* 1245ea8dc4b6Seschrock * Stop async tasks. 1246ea8dc4b6Seschrock */ 1247ea8dc4b6Seschrock spa_async_suspend(spa); 1248ea8dc4b6Seschrock 1249fa9e4066Sahrens /* 1250fa9e4066Sahrens * Stop syncing. 1251fa9e4066Sahrens */ 1252fa9e4066Sahrens if (spa->spa_sync_on) { 1253fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1254fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1255fa9e4066Sahrens } 1256fa9e4066Sahrens 12578363e80aSGeorge Wilson /* 12588363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 12598363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 12608363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 12618363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 12628363e80aSGeorge Wilson */ 12638363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 12648363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12658363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 12668363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 12678363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 12688363e80aSGeorge Wilson } 12698363e80aSGeorge Wilson 1270fa9e4066Sahrens /* 1271e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1272fa9e4066Sahrens */ 127354d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12746f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12756f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12766f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 127754d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 127854d692b7SGeorge Wilson } 1279fa9e4066Sahrens 1280*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 1281*5cabbc6bSPrashanth Sreenivasa spa_vdev_removal_destroy(spa->spa_vdev_removal); 1282*5cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal = NULL; 1283*5cabbc6bSPrashanth Sreenivasa } 1284*5cabbc6bSPrashanth Sreenivasa 1285*5cabbc6bSPrashanth Sreenivasa spa_condense_fini(spa); 1286*5cabbc6bSPrashanth Sreenivasa 1287cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1288cde58dbcSMatthew Ahrens 12890713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12900713e232SGeorge Wilson 12910713e232SGeorge Wilson /* 12920713e232SGeorge Wilson * Close all vdevs. 12930713e232SGeorge Wilson */ 12940713e232SGeorge Wilson if (spa->spa_root_vdev) 12950713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12960713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12970713e232SGeorge Wilson 1298fa9e4066Sahrens /* 1299fa9e4066Sahrens * Close the dsl pool. 1300fa9e4066Sahrens */ 1301fa9e4066Sahrens if (spa->spa_dsl_pool) { 1302fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1303fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1304afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1305fa9e4066Sahrens } 1306fa9e4066Sahrens 1307b24ab676SJeff Bonwick ddt_unload(spa); 1308b24ab676SJeff Bonwick 13098ad4d6ddSJeff Bonwick /* 13108ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 13118ad4d6ddSJeff Bonwick */ 13128ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 13138ad4d6ddSJeff Bonwick 1314fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1315fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1316fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1317fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1318fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1319fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 132099653d4eSeschrock } 1321fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1322fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1323fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1324fa94a07fSbrendan } 13252ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1326fa94a07fSbrendan 1327cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1328cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1329fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1330cd0837ccSGeorge Wilson } 1331fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1332fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1333fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1334fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1335fa94a07fSbrendan } 1336fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1337fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1338fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 133999653d4eSeschrock } 13402ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 134199653d4eSeschrock 1342ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13438ad4d6ddSJeff Bonwick 1344*5cabbc6bSPrashanth Sreenivasa spa->spa_indirect_vdevs_loaded = B_FALSE; 1345*5cabbc6bSPrashanth Sreenivasa 13468704186eSDan McDonald if (spa->spa_comment != NULL) { 13478704186eSDan McDonald spa_strfree(spa->spa_comment); 13488704186eSDan McDonald spa->spa_comment = NULL; 13498704186eSDan McDonald } 13508704186eSDan McDonald 13518ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1352fa9e4066Sahrens } 1353fa9e4066Sahrens 135499653d4eSeschrock /* 135599653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 135699653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1357fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1358fa94a07fSbrendan * then re-generate a more complete list including status information. 135999653d4eSeschrock */ 1360*5cabbc6bSPrashanth Sreenivasa void 136199653d4eSeschrock spa_load_spares(spa_t *spa) 136299653d4eSeschrock { 136399653d4eSeschrock nvlist_t **spares; 136499653d4eSeschrock uint_t nspares; 136599653d4eSeschrock int i; 136639c23413Seschrock vdev_t *vd, *tvd; 136799653d4eSeschrock 1368e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1369e14bb325SJeff Bonwick 137099653d4eSeschrock /* 137199653d4eSeschrock * First, close and free any existing spare vdevs. 137299653d4eSeschrock */ 1373fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1374fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 137539c23413Seschrock 137639c23413Seschrock /* Undo the call to spa_activate() below */ 1377c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1378c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 137939c23413Seschrock spa_spare_remove(tvd); 138039c23413Seschrock vdev_close(vd); 138139c23413Seschrock vdev_free(vd); 138299653d4eSeschrock } 138339c23413Seschrock 1384fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1385fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1386fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 138799653d4eSeschrock 1388fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 138999653d4eSeschrock nspares = 0; 139099653d4eSeschrock else 1391fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 139299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 139399653d4eSeschrock 1394fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1395fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 139699653d4eSeschrock 139799653d4eSeschrock if (nspares == 0) 139899653d4eSeschrock return; 139999653d4eSeschrock 140099653d4eSeschrock /* 140199653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 140239c23413Seschrock * process. For each spare, there is potentially two different vdev_t 140339c23413Seschrock * structures associated with it: one in the list of spares (used only 140439c23413Seschrock * for basic validation purposes) and one in the active vdev 140539c23413Seschrock * configuration (if it's spared in). During this phase we open and 140639c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 140739c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 140899653d4eSeschrock */ 1409fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1410fa94a07fSbrendan KM_SLEEP); 1411fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 141299653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 141399653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 141499653d4eSeschrock ASSERT(vd != NULL); 141599653d4eSeschrock 1416fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 141799653d4eSeschrock 1418c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1419c5904d13Seschrock B_FALSE)) != NULL) { 142039c23413Seschrock if (!tvd->vdev_isspare) 142139c23413Seschrock spa_spare_add(tvd); 142239c23413Seschrock 142339c23413Seschrock /* 142439c23413Seschrock * We only mark the spare active if we were successfully 142539c23413Seschrock * able to load the vdev. Otherwise, importing a pool 142639c23413Seschrock * with a bad active spare would result in strange 142739c23413Seschrock * behavior, because multiple pool would think the spare 142839c23413Seschrock * is actively in use. 142939c23413Seschrock * 143039c23413Seschrock * There is a vulnerability here to an equally bizarre 143139c23413Seschrock * circumstance, where a dead active spare is later 143239c23413Seschrock * brought back to life (onlined or otherwise). Given 143339c23413Seschrock * the rarity of this scenario, and the extra complexity 143439c23413Seschrock * it adds, we ignore the possibility. 143539c23413Seschrock */ 143639c23413Seschrock if (!vdev_is_dead(tvd)) 143739c23413Seschrock spa_spare_activate(tvd); 143839c23413Seschrock } 143939c23413Seschrock 1440e14bb325SJeff Bonwick vd->vdev_top = vd; 14416809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1442e14bb325SJeff Bonwick 144399653d4eSeschrock if (vdev_open(vd) != 0) 144499653d4eSeschrock continue; 144599653d4eSeschrock 1446fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1447fa94a07fSbrendan spa_spare_add(vd); 144899653d4eSeschrock } 144999653d4eSeschrock 145099653d4eSeschrock /* 145199653d4eSeschrock * Recompute the stashed list of spares, with status information 145299653d4eSeschrock * this time. 145399653d4eSeschrock */ 1454fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 145599653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 145699653d4eSeschrock 1457fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1458fa94a07fSbrendan KM_SLEEP); 1459fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1460fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14613f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1462fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1463fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1464fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 146599653d4eSeschrock nvlist_free(spares[i]); 1466fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1467fa94a07fSbrendan } 1468fa94a07fSbrendan 1469fa94a07fSbrendan /* 1470fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1471fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1472fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1473fa94a07fSbrendan * then re-generate a more complete list including status information. 1474fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1475fa94a07fSbrendan * not re-opened. 1476fa94a07fSbrendan */ 1477*5cabbc6bSPrashanth Sreenivasa void 1478fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1479fa94a07fSbrendan { 1480fa94a07fSbrendan nvlist_t **l2cache; 1481fa94a07fSbrendan uint_t nl2cache; 1482fa94a07fSbrendan int i, j, oldnvdevs; 1483573ca77eSGeorge Wilson uint64_t guid; 1484fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1485fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1486fa94a07fSbrendan 1487e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1488e14bb325SJeff Bonwick 1489fa94a07fSbrendan if (sav->sav_config != NULL) { 1490fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1491fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1492fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1493fa94a07fSbrendan } else { 1494fa94a07fSbrendan nl2cache = 0; 1495d5285caeSGeorge Wilson newvdevs = NULL; 1496fa94a07fSbrendan } 1497fa94a07fSbrendan 1498fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1499fa94a07fSbrendan oldnvdevs = sav->sav_count; 1500fa94a07fSbrendan sav->sav_vdevs = NULL; 1501fa94a07fSbrendan sav->sav_count = 0; 1502fa94a07fSbrendan 1503fa94a07fSbrendan /* 1504fa94a07fSbrendan * Process new nvlist of vdevs. 1505fa94a07fSbrendan */ 1506fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1507fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1508fa94a07fSbrendan &guid) == 0); 1509fa94a07fSbrendan 1510fa94a07fSbrendan newvdevs[i] = NULL; 1511fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1512fa94a07fSbrendan vd = oldvdevs[j]; 1513fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1514fa94a07fSbrendan /* 1515fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1516fa94a07fSbrendan */ 1517fa94a07fSbrendan newvdevs[i] = vd; 1518fa94a07fSbrendan oldvdevs[j] = NULL; 1519fa94a07fSbrendan break; 1520fa94a07fSbrendan } 1521fa94a07fSbrendan } 1522fa94a07fSbrendan 1523fa94a07fSbrendan if (newvdevs[i] == NULL) { 1524fa94a07fSbrendan /* 1525fa94a07fSbrendan * Create new vdev 1526fa94a07fSbrendan */ 1527fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1528fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1529fa94a07fSbrendan ASSERT(vd != NULL); 1530fa94a07fSbrendan newvdevs[i] = vd; 1531fa94a07fSbrendan 1532fa94a07fSbrendan /* 1533fa94a07fSbrendan * Commit this vdev as an l2cache device, 1534fa94a07fSbrendan * even if it fails to open. 1535fa94a07fSbrendan */ 1536fa94a07fSbrendan spa_l2cache_add(vd); 1537fa94a07fSbrendan 1538c5904d13Seschrock vd->vdev_top = vd; 1539c5904d13Seschrock vd->vdev_aux = sav; 1540c5904d13Seschrock 1541c5904d13Seschrock spa_l2cache_activate(vd); 1542c5904d13Seschrock 1543fa94a07fSbrendan if (vdev_open(vd) != 0) 1544fa94a07fSbrendan continue; 1545fa94a07fSbrendan 1546fa94a07fSbrendan (void) vdev_validate_aux(vd); 1547fa94a07fSbrendan 1548573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1549573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1550fa94a07fSbrendan } 1551fa94a07fSbrendan } 1552fa94a07fSbrendan 1553fa94a07fSbrendan /* 1554fa94a07fSbrendan * Purge vdevs that were dropped 1555fa94a07fSbrendan */ 1556fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1557fa94a07fSbrendan uint64_t pool; 1558fa94a07fSbrendan 1559fa94a07fSbrendan vd = oldvdevs[i]; 1560fa94a07fSbrendan if (vd != NULL) { 1561cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1562cd0837ccSGeorge Wilson 15638ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15648ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1565fa94a07fSbrendan l2arc_remove_vdev(vd); 1566cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1567cd0837ccSGeorge Wilson vdev_free(vd); 1568fa94a07fSbrendan } 1569fa94a07fSbrendan } 1570fa94a07fSbrendan 1571fa94a07fSbrendan if (oldvdevs) 1572fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1573fa94a07fSbrendan 1574fa94a07fSbrendan if (sav->sav_config == NULL) 1575fa94a07fSbrendan goto out; 1576fa94a07fSbrendan 1577fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1578fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1579fa94a07fSbrendan 1580fa94a07fSbrendan /* 1581fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1582fa94a07fSbrendan * information this time. 1583fa94a07fSbrendan */ 1584fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1585fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1586fa94a07fSbrendan 1587fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1588fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1589fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15903f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1591fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1592fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1593fa94a07fSbrendan out: 1594fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1595fa94a07fSbrendan nvlist_free(l2cache[i]); 1596fa94a07fSbrendan if (sav->sav_count) 1597fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 159899653d4eSeschrock } 159999653d4eSeschrock 160099653d4eSeschrock static int 160199653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 160299653d4eSeschrock { 160399653d4eSeschrock dmu_buf_t *db; 160499653d4eSeschrock char *packed = NULL; 160599653d4eSeschrock size_t nvsize = 0; 160699653d4eSeschrock int error; 160799653d4eSeschrock *value = NULL; 160899653d4eSeschrock 1609a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1610a45f1c3cSBrian Behlendorf if (error != 0) 1611a45f1c3cSBrian Behlendorf return (error); 1612a45f1c3cSBrian Behlendorf 161399653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 161499653d4eSeschrock dmu_buf_rele(db, FTAG); 161599653d4eSeschrock 161699653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 16177bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 16187bfdf011SNeil Perrin DMU_READ_PREFETCH); 161999653d4eSeschrock if (error == 0) 162099653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 162199653d4eSeschrock kmem_free(packed, nvsize); 162299653d4eSeschrock 162399653d4eSeschrock return (error); 162499653d4eSeschrock } 162599653d4eSeschrock 16263d7072f8Seschrock /* 16273d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 16283d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 16293d7072f8Seschrock */ 16303d7072f8Seschrock static void 16313d7072f8Seschrock spa_check_removed(vdev_t *vd) 16323d7072f8Seschrock { 1633573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 16343d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 16353d7072f8Seschrock 1636efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1637*5cabbc6bSPrashanth Sreenivasa vdev_is_concrete(vd)) { 16383d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 1639ce1577b0SDave Eddy spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 16403d7072f8Seschrock } 16413d7072f8Seschrock } 16423d7072f8Seschrock 1643215198a6SJoe Stein static void 1644215198a6SJoe Stein spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) 1645215198a6SJoe Stein { 1646215198a6SJoe Stein ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); 1647215198a6SJoe Stein 1648215198a6SJoe Stein vd->vdev_top_zap = mvd->vdev_top_zap; 1649215198a6SJoe Stein vd->vdev_leaf_zap = mvd->vdev_leaf_zap; 1650215198a6SJoe Stein 1651215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 1652215198a6SJoe Stein spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); 1653215198a6SJoe Stein } 1654215198a6SJoe Stein } 1655215198a6SJoe Stein 1656e6ca193dSGeorge Wilson /* 16574b964adaSGeorge Wilson * Validate the current config against the MOS config 1658e6ca193dSGeorge Wilson */ 16594b964adaSGeorge Wilson static boolean_t 16604b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1661e6ca193dSGeorge Wilson { 16624b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16634b964adaSGeorge Wilson nvlist_t *nv; 16644b964adaSGeorge Wilson 16654b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16664b964adaSGeorge Wilson 16674b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16684b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16694b964adaSGeorge Wilson 16704b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1671e6ca193dSGeorge Wilson 167288ecc943SGeorge Wilson /* 16734b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16744b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16754b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 167688ecc943SGeorge Wilson */ 16774b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16784b964adaSGeorge Wilson nvlist_t **child, *nv; 16794b964adaSGeorge Wilson uint64_t idx = 0; 16804b964adaSGeorge Wilson 16814b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16824b964adaSGeorge Wilson KM_SLEEP); 16834b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1684e6ca193dSGeorge Wilson 16854b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16864b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16874b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16884b964adaSGeorge Wilson 16894b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16904b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16914b964adaSGeorge Wilson mtvd->vdev_islog) 16924b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16934b964adaSGeorge Wilson B_FALSE, 0); 16944b964adaSGeorge Wilson } 16954b964adaSGeorge Wilson 16964b964adaSGeorge Wilson if (idx) { 16974b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16984b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16994b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 17004b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 17014b964adaSGeorge Wilson 17024b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 17034b964adaSGeorge Wilson nvlist_free(child[i]); 17044b964adaSGeorge Wilson } 17054b964adaSGeorge Wilson nvlist_free(nv); 17064b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 17074b964adaSGeorge Wilson } 17084b964adaSGeorge Wilson 17094b964adaSGeorge Wilson /* 17104b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 17114b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 17124b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 17134b964adaSGeorge Wilson */ 171488ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 17154b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 17164b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 17174b964adaSGeorge Wilson 17184b964adaSGeorge Wilson /* 17194b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 1720*5cabbc6bSPrashanth Sreenivasa * Also trust the MOS config about any "indirect" vdevs. 17214b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 17224b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 17234b964adaSGeorge Wilson */ 1724*5cabbc6bSPrashanth Sreenivasa if ((tvd->vdev_ops == &vdev_missing_ops && 1725*5cabbc6bSPrashanth Sreenivasa mtvd->vdev_ops != &vdev_missing_ops) || 1726*5cabbc6bSPrashanth Sreenivasa (mtvd->vdev_ops == &vdev_indirect_ops && 1727*5cabbc6bSPrashanth Sreenivasa tvd->vdev_ops != &vdev_indirect_ops)) { 17284b964adaSGeorge Wilson 17294b964adaSGeorge Wilson /* 17304b964adaSGeorge Wilson * Device specific actions. 17314b964adaSGeorge Wilson */ 17324b964adaSGeorge Wilson if (mtvd->vdev_islog) { 1733*5cabbc6bSPrashanth Sreenivasa if (!(spa->spa_import_flags & 1734*5cabbc6bSPrashanth Sreenivasa ZFS_IMPORT_MISSING_LOG)) { 1735*5cabbc6bSPrashanth Sreenivasa continue; 1736*5cabbc6bSPrashanth Sreenivasa } 1737*5cabbc6bSPrashanth Sreenivasa 17384b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 1739*5cabbc6bSPrashanth Sreenivasa } else if (mtvd->vdev_ops != &vdev_indirect_ops) { 17404b964adaSGeorge Wilson continue; 17414b964adaSGeorge Wilson } 17424b964adaSGeorge Wilson 17434b964adaSGeorge Wilson /* 17444b964adaSGeorge Wilson * Swap the missing vdev with the data we were 17454b964adaSGeorge Wilson * able to obtain from the MOS config. 17464b964adaSGeorge Wilson */ 17474b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 17484b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 17494b964adaSGeorge Wilson 17504b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 17514b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17524b964adaSGeorge Wilson 17534b964adaSGeorge Wilson vdev_reopen(rvd); 1754215198a6SJoe Stein } else { 1755215198a6SJoe Stein if (mtvd->vdev_islog) { 1756215198a6SJoe Stein /* 1757215198a6SJoe Stein * Load the slog device's state from the MOS 1758215198a6SJoe Stein * config since it's possible that the label 1759215198a6SJoe Stein * does not contain the most up-to-date 1760215198a6SJoe Stein * information. 1761215198a6SJoe Stein */ 1762215198a6SJoe Stein vdev_load_log_state(tvd, mtvd); 1763215198a6SJoe Stein vdev_reopen(tvd); 1764215198a6SJoe Stein } 1765215198a6SJoe Stein 17664b964adaSGeorge Wilson /* 1767215198a6SJoe Stein * Per-vdev ZAP info is stored exclusively in the MOS. 17684b964adaSGeorge Wilson */ 1769215198a6SJoe Stein spa_config_valid_zaps(tvd, mtvd); 17704b964adaSGeorge Wilson } 1771*5cabbc6bSPrashanth Sreenivasa 1772*5cabbc6bSPrashanth Sreenivasa /* 1773*5cabbc6bSPrashanth Sreenivasa * Never trust this info from userland; always use what's 1774*5cabbc6bSPrashanth Sreenivasa * in the MOS. This prevents it from getting out of sync 1775*5cabbc6bSPrashanth Sreenivasa * with the rest of the info in the MOS. 1776*5cabbc6bSPrashanth Sreenivasa */ 1777*5cabbc6bSPrashanth Sreenivasa tvd->vdev_removing = mtvd->vdev_removing; 1778*5cabbc6bSPrashanth Sreenivasa tvd->vdev_indirect_config = mtvd->vdev_indirect_config; 1779e6ca193dSGeorge Wilson } 1780215198a6SJoe Stein 17814b964adaSGeorge Wilson vdev_free(mrvd); 178288ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17834b964adaSGeorge Wilson 17844b964adaSGeorge Wilson /* 17854b964adaSGeorge Wilson * Ensure we were able to validate the config. 17864b964adaSGeorge Wilson */ 17874b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1788e6ca193dSGeorge Wilson } 1789e6ca193dSGeorge Wilson 1790b87f3af3Sperrin /* 1791b87f3af3Sperrin * Check for missing log devices 1792b87f3af3Sperrin */ 17933b2aab18SMatthew Ahrens static boolean_t 1794b87f3af3Sperrin spa_check_logs(spa_t *spa) 1795b87f3af3Sperrin { 17963b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 179712380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17983b2aab18SMatthew Ahrens 1799b87f3af3Sperrin switch (spa->spa_log_state) { 1800b87f3af3Sperrin case SPA_LOG_MISSING: 1801b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1802b87f3af3Sperrin case SPA_LOG_UNKNOWN: 180312380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 180412380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 18053b2aab18SMatthew Ahrens if (rv) 18061195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1807b87f3af3Sperrin break; 1808b87f3af3Sperrin } 18093b2aab18SMatthew Ahrens return (rv); 1810b87f3af3Sperrin } 1811b87f3af3Sperrin 18121195e687SMark J Musante static boolean_t 18131195e687SMark J Musante spa_passivate_log(spa_t *spa) 18141195e687SMark J Musante { 18151195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18161195e687SMark J Musante boolean_t slog_found = B_FALSE; 18171195e687SMark J Musante 18181195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18191195e687SMark J Musante 18201195e687SMark J Musante if (!spa_has_slogs(spa)) 18211195e687SMark J Musante return (B_FALSE); 18221195e687SMark J Musante 18231195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18241195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18251195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18261195e687SMark J Musante 18271195e687SMark J Musante if (tvd->vdev_islog) { 18281195e687SMark J Musante metaslab_group_passivate(mg); 18291195e687SMark J Musante slog_found = B_TRUE; 18301195e687SMark J Musante } 18311195e687SMark J Musante } 18321195e687SMark J Musante 18331195e687SMark J Musante return (slog_found); 18341195e687SMark J Musante } 18351195e687SMark J Musante 18361195e687SMark J Musante static void 18371195e687SMark J Musante spa_activate_log(spa_t *spa) 18381195e687SMark J Musante { 18391195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18401195e687SMark J Musante 18411195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18421195e687SMark J Musante 18431195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18441195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18451195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18461195e687SMark J Musante 18471195e687SMark J Musante if (tvd->vdev_islog) 18481195e687SMark J Musante metaslab_group_activate(mg); 18491195e687SMark J Musante } 18501195e687SMark J Musante } 18511195e687SMark J Musante 18521195e687SMark J Musante int 1853*5cabbc6bSPrashanth Sreenivasa spa_reset_logs(spa_t *spa) 18541195e687SMark J Musante { 18553b2aab18SMatthew Ahrens int error; 18561195e687SMark J Musante 1857*5cabbc6bSPrashanth Sreenivasa error = dmu_objset_find(spa_name(spa), zil_reset, 18583b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18593b2aab18SMatthew Ahrens if (error == 0) { 18601195e687SMark J Musante /* 18611195e687SMark J Musante * We successfully offlined the log device, sync out the 18621195e687SMark J Musante * current txg so that the "stubby" block can be removed 18631195e687SMark J Musante * by zil_sync(). 18641195e687SMark J Musante */ 18651195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18661195e687SMark J Musante } 18671195e687SMark J Musante return (error); 18681195e687SMark J Musante } 18691195e687SMark J Musante 1870b693757aSEric Schrock static void 1871b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1872b693757aSEric Schrock { 1873b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1874b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1875b693757aSEric Schrock } 1876b693757aSEric Schrock 1877b24ab676SJeff Bonwick void 1878b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1879b24ab676SJeff Bonwick { 1880b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1881b24ab676SJeff Bonwick 1882b24ab676SJeff Bonwick if (zio->io_error) 1883b24ab676SJeff Bonwick return; 1884b24ab676SJeff Bonwick 1885b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1886b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1887b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1888b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1889b24ab676SJeff Bonwick } 1890b24ab676SJeff Bonwick 1891468c413aSTim Haley typedef struct spa_load_error { 1892c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1893468c413aSTim Haley uint64_t sle_data_count; 1894468c413aSTim Haley } spa_load_error_t; 1895468c413aSTim Haley 1896468c413aSTim Haley static void 1897468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1898468c413aSTim Haley { 1899468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1900468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1901468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1902468c413aSTim Haley int error = zio->io_error; 1903e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1904468c413aSTim Haley 1905770499e1SDan Kimmel abd_free(zio->io_abd); 1906468c413aSTim Haley if (error) { 1907ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1908468c413aSTim Haley type != DMU_OT_INTENT_LOG) 19091a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1910468c413aSTim Haley else 19111a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1912468c413aSTim Haley } 1913e42d2059SMatthew Ahrens 1914e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1915e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1916e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1917e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1918468c413aSTim Haley } 1919468c413aSTim Haley 1920e42d2059SMatthew Ahrens /* 1921e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1922e42d2059SMatthew Ahrens * a pool while importing it. 1923e42d2059SMatthew Ahrens */ 1924e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1925e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1926e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1927e42d2059SMatthew Ahrens 1928468c413aSTim Haley /*ARGSUSED*/ 1929468c413aSTim Haley static int 1930b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19317802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1932468c413aSTim Haley { 1933a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1934e42d2059SMatthew Ahrens return (0); 1935e42d2059SMatthew Ahrens /* 1936e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1937e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1938e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1939e42d2059SMatthew Ahrens */ 1940e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1941e42d2059SMatthew Ahrens return (0); 1942770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1943e42d2059SMatthew Ahrens return (0); 1944468c413aSTim Haley 1945e42d2059SMatthew Ahrens zio_t *rio = arg; 1946e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1947e42d2059SMatthew Ahrens 1948e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1949e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1950e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1951e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1952e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1953e42d2059SMatthew Ahrens 1954770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 1955e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1956e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1957e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1958468c413aSTim Haley return (0); 1959468c413aSTim Haley } 1960468c413aSTim Haley 1961c971037bSPaul Dagnelie /* ARGSUSED */ 1962c971037bSPaul Dagnelie int 1963c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1964c971037bSPaul Dagnelie { 19659adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1966c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1967c971037bSPaul Dagnelie 1968c971037bSPaul Dagnelie return (0); 1969c971037bSPaul Dagnelie } 1970c971037bSPaul Dagnelie 1971468c413aSTim Haley static int 1972468c413aSTim Haley spa_load_verify(spa_t *spa) 1973468c413aSTim Haley { 1974468c413aSTim Haley zio_t *rio; 1975468c413aSTim Haley spa_load_error_t sle = { 0 }; 1976468c413aSTim Haley zpool_rewind_policy_t policy; 1977468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1978e42d2059SMatthew Ahrens int error = 0; 1979468c413aSTim Haley 1980c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1981c8ee1847SVictor Latushkin 1982c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1983c8ee1847SVictor Latushkin return (0); 1984c8ee1847SVictor Latushkin 1985c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1986c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1987c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1988c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1989c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1990c971037bSPaul Dagnelie if (error != 0) 1991c971037bSPaul Dagnelie return (error); 1992c971037bSPaul Dagnelie 1993468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1994468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1995468c413aSTim Haley 1996e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1997e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1998e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1999e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 2000e42d2059SMatthew Ahrens } 2001468c413aSTim Haley 2002468c413aSTim Haley (void) zio_wait(rio); 2003468c413aSTim Haley 2004c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 2005468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 2006468c413aSTim Haley 2007c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 2008468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 20094b964adaSGeorge Wilson int64_t loss = 0; 20104b964adaSGeorge Wilson 2011468c413aSTim Haley verify_ok = B_TRUE; 2012468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 2013468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 20144b964adaSGeorge Wilson 20154b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 20164b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20174b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 20184b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 20194b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 20204b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 20214b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 2022a33cae98STim Haley } else { 2023a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2024468c413aSTim Haley } 2025468c413aSTim Haley 2026468c413aSTim Haley if (error) { 2027468c413aSTim Haley if (error != ENXIO && error != EIO) 2028be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2029468c413aSTim Haley return (error); 2030468c413aSTim Haley } 2031468c413aSTim Haley 2032468c413aSTim Haley return (verify_ok ? 0 : EIO); 2033468c413aSTim Haley } 2034468c413aSTim Haley 20351195e687SMark J Musante /* 20361195e687SMark J Musante * Find a value in the pool props object. 20371195e687SMark J Musante */ 20381195e687SMark J Musante static void 20391195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20401195e687SMark J Musante { 20411195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20421195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20431195e687SMark J Musante } 20441195e687SMark J Musante 20451195e687SMark J Musante /* 20461195e687SMark J Musante * Find a value in the pool directory object. 20471195e687SMark J Musante */ 20481195e687SMark J Musante static int 20491195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 20501195e687SMark J Musante { 20511195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20521195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 20531195e687SMark J Musante } 20541195e687SMark J Musante 20551195e687SMark J Musante static int 20561195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20571195e687SMark J Musante { 20581195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 2059*5cabbc6bSPrashanth Sreenivasa return (SET_ERROR(err)); 20601195e687SMark J Musante } 20611195e687SMark J Musante 20621195e687SMark J Musante /* 20631195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20641195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20651195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20661195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20671195e687SMark J Musante * 20681195e687SMark J Musante * This function determines what to do with that list: either rejoin 20691195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20701195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20711195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20721195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20731195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20741195e687SMark J Musante * 2075d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2076d41c4376SMark J Musante * the original pool. 20771195e687SMark J Musante */ 20781195e687SMark J Musante static void 20791195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 20801195e687SMark J Musante { 20811195e687SMark J Musante uint_t extracted; 20821195e687SMark J Musante uint64_t *glist; 20831195e687SMark J Musante uint_t i, gcount; 20841195e687SMark J Musante nvlist_t *nvl; 20851195e687SMark J Musante vdev_t **vd; 20861195e687SMark J Musante boolean_t attempt_reopen; 20871195e687SMark J Musante 20881195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20891195e687SMark J Musante return; 20901195e687SMark J Musante 20911195e687SMark J Musante /* check that the config is complete */ 20921195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20931195e687SMark J Musante &glist, &gcount) != 0) 20941195e687SMark J Musante return; 20951195e687SMark J Musante 20961195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20971195e687SMark J Musante 20981195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20991195e687SMark J Musante attempt_reopen = B_TRUE; 21001195e687SMark J Musante for (i = 0; i < gcount; i++) { 21011195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 21021195e687SMark J Musante continue; 21031195e687SMark J Musante 21041195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 21051195e687SMark J Musante if (vd[i] == NULL) { 21061195e687SMark J Musante /* 21071195e687SMark J Musante * Don't bother attempting to reopen the disks; 21081195e687SMark J Musante * just do the split. 21091195e687SMark J Musante */ 21101195e687SMark J Musante attempt_reopen = B_FALSE; 21111195e687SMark J Musante } else { 21121195e687SMark J Musante /* attempt to re-online it */ 21131195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 21141195e687SMark J Musante } 21151195e687SMark J Musante } 21161195e687SMark J Musante 21171195e687SMark J Musante if (attempt_reopen) { 21181195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21191195e687SMark J Musante 21201195e687SMark J Musante /* check each device to see what state it's in */ 21211195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 21221195e687SMark J Musante if (vd[i] != NULL && 21231195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 21241195e687SMark J Musante break; 21251195e687SMark J Musante ++extracted; 21261195e687SMark J Musante } 21271195e687SMark J Musante } 21281195e687SMark J Musante 21291195e687SMark J Musante /* 21301195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 21311195e687SMark J Musante * even attempted to look at them, then we split them off for 21321195e687SMark J Musante * good. 21331195e687SMark J Musante */ 21341195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 21351195e687SMark J Musante for (i = 0; i < gcount; i++) 21361195e687SMark J Musante if (vd[i] != NULL) 21371195e687SMark J Musante vdev_split(vd[i]); 21381195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21391195e687SMark J Musante } 21401195e687SMark J Musante 21411195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21421195e687SMark J Musante } 21431195e687SMark J Musante 21441195e687SMark J Musante static int 21451195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 21461195e687SMark J Musante boolean_t mosconfig) 21471195e687SMark J Musante { 21481195e687SMark J Musante nvlist_t *config = spa->spa_config; 21491195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21508704186eSDan McDonald char *comment; 21511195e687SMark J Musante int error; 21521195e687SMark J Musante uint64_t pool_guid; 21531195e687SMark J Musante nvlist_t *nvl; 21541195e687SMark J Musante 21551195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2156be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21571195e687SMark J Musante 21588704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 21598704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 21608704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 21618704186eSDan McDonald 21621195e687SMark J Musante /* 21631195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 21641195e687SMark J Musante * it's not present treat it as the initial version. 21651195e687SMark J Musante */ 21661195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 21671195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 21681195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 21691195e687SMark J Musante 21701195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 21711195e687SMark J Musante &spa->spa_config_txg); 21721195e687SMark J Musante 21731195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 21741195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2175be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 21761195e687SMark J Musante } else { 2177e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 21781195e687SMark J Musante 21791195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 21801195e687SMark J Musante &nvl) == 0) { 21811195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 21821195e687SMark J Musante KM_SLEEP) == 0); 21831195e687SMark J Musante } 21841195e687SMark J Musante 2185ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2186ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2187ad135b5dSChristopher Siden 218811027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21891195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21901195e687SMark J Musante mosconfig, &ereport); 21911195e687SMark J Musante } 21921195e687SMark J Musante 2193bc9014e6SJustin Gibbs /* 2194bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2195bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2196bc9014e6SJustin Gibbs */ 2197bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21981195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 219911027bc7STim Haley if (error) { 220011027bc7STim Haley if (error != EEXIST) { 220111027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 220211027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 220311027bc7STim Haley } 220411027bc7STim Haley if (error != EBADF) { 220511027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 220611027bc7STim Haley } 220711027bc7STim Haley } 22081195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 22091195e687SMark J Musante spa->spa_ena = 0; 22101195e687SMark J Musante 22111195e687SMark J Musante return (error); 22121195e687SMark J Musante } 22131195e687SMark J Musante 2214215198a6SJoe Stein /* 2215215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2216215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2217215198a6SJoe Stein * spa's per-vdev ZAP list. 2218215198a6SJoe Stein */ 2219215198a6SJoe Stein static uint64_t 2220215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2221215198a6SJoe Stein { 2222215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2223215198a6SJoe Stein uint64_t total = 0; 2224215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2225215198a6SJoe Stein total++; 2226215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2227215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2228215198a6SJoe Stein } 2229215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2230215198a6SJoe Stein total++; 2231215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2232215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2233215198a6SJoe Stein } 2234215198a6SJoe Stein 2235215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2236215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2237215198a6SJoe Stein } 2238215198a6SJoe Stein 2239215198a6SJoe Stein return (total); 2240215198a6SJoe Stein } 2241215198a6SJoe Stein 2242fa9e4066Sahrens /* 2243fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2244ea8dc4b6Seschrock * source of configuration information. 2245fa9e4066Sahrens */ 2246fa9e4066Sahrens static int 22471195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 2248*5cabbc6bSPrashanth Sreenivasa spa_load_state_t state, spa_import_type_t type, boolean_t trust_config, 22491195e687SMark J Musante char **ereport) 2250fa9e4066Sahrens { 2251fa9e4066Sahrens int error = 0; 2252871a9500SMark J Musante nvlist_t *nvroot = NULL; 2253ad135b5dSChristopher Siden nvlist_t *label; 2254fa9e4066Sahrens vdev_t *rvd; 2255fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 22564b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 22578ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 22581195e687SMark J Musante int parse; 2259cde58dbcSMatthew Ahrens uint64_t obj; 2260ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2261fa9e4066Sahrens 22628ad4d6ddSJeff Bonwick /* 22638ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 22648ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 22658ad4d6ddSJeff Bonwick */ 2266*5cabbc6bSPrashanth Sreenivasa if (!trust_config) 22678ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 22688ad4d6ddSJeff Bonwick 2269e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2270e14bb325SJeff Bonwick 2271ea8dc4b6Seschrock spa->spa_load_state = state; 22720373e76bSbonwick 22731195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2274be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2275fa9e4066Sahrens 22761195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 22771195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2278b5989ec7Seschrock 227954d692b7SGeorge Wilson /* 228054d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 228154d692b7SGeorge Wilson */ 22826f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 22836f834bc1SMatthew Ahrens KM_SLEEP); 22846f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 22856f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 22866f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 22876f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 22886f834bc1SMatthew Ahrens } 228954d692b7SGeorge Wilson 2290fa9e4066Sahrens /* 229199653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 229299653d4eSeschrock * value that will be returned by spa_version() since parsing the 229399653d4eSeschrock * configuration requires knowing the version number. 2294fa9e4066Sahrens */ 2295e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22961195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2297e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2298fa9e4066Sahrens 229999653d4eSeschrock if (error != 0) 23001195e687SMark J Musante return (error); 2301fa9e4066Sahrens 23020e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 230381cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 230481cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 23051195e687SMark J Musante 23061195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23071195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 23081195e687SMark J Musante } 2309fa9e4066Sahrens 2310fa9e4066Sahrens /* 2311fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2312fa9e4066Sahrens */ 2313e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 23140bf246f5Smc error = vdev_open(rvd); 2315e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 23160bf246f5Smc if (error != 0) 23171195e687SMark J Musante return (error); 2318fa9e4066Sahrens 2319560e6e96Seschrock /* 232077e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 232177e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 232277e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 23231195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 232477e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 232577e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 232677e3a39cSMark J Musante * the vdev config. 23271195e687SMark J Musante * 23281195e687SMark J Musante * If we're assembling a new pool that's been split off from an 23291195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 23301195e687SMark J Musante * validation for now. 2331560e6e96Seschrock */ 23321195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23331195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2334*5cabbc6bSPrashanth Sreenivasa error = vdev_validate(rvd, trust_config); 23351195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2336560e6e96Seschrock 23371195e687SMark J Musante if (error != 0) 23381195e687SMark J Musante return (error); 23391195e687SMark J Musante 23401195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2341be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2342560e6e96Seschrock } 2343560e6e96Seschrock 2344fa9e4066Sahrens /* 2345fa9e4066Sahrens * Find the best uberblock. 2346fa9e4066Sahrens */ 2347ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2348fa9e4066Sahrens 2349fa9e4066Sahrens /* 2350fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2351fa9e4066Sahrens */ 2352ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2353ad135b5dSChristopher Siden nvlist_free(label); 23541195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2355ad135b5dSChristopher Siden } 2356ea8dc4b6Seschrock 2357ea8dc4b6Seschrock /* 2358ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2359ea8dc4b6Seschrock */ 2360ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2361ad135b5dSChristopher Siden nvlist_free(label); 23621195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2363ad135b5dSChristopher Siden } 2364ad135b5dSChristopher Siden 2365ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2366ad135b5dSChristopher Siden nvlist_t *features; 2367ad135b5dSChristopher Siden 2368ad135b5dSChristopher Siden /* 2369ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2370ad135b5dSChristopher Siden * MOS in the label, return failure. 2371ad135b5dSChristopher Siden */ 2372ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2373ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2374ad135b5dSChristopher Siden nvlist_free(label); 2375ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2376ad135b5dSChristopher Siden ENXIO)); 2377ad135b5dSChristopher Siden } 2378ad135b5dSChristopher Siden 2379ad135b5dSChristopher Siden /* 2380ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2381ad135b5dSChristopher Siden * from the label. 2382ad135b5dSChristopher Siden */ 2383ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2384ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2385ad135b5dSChristopher Siden } 2386ad135b5dSChristopher Siden 2387ad135b5dSChristopher Siden nvlist_free(label); 2388ad135b5dSChristopher Siden 2389ad135b5dSChristopher Siden /* 2390ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2391ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2392ad135b5dSChristopher Siden * cannot open a pool. 2393ad135b5dSChristopher Siden */ 2394ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2395ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2396ad135b5dSChristopher Siden 2397ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2398ad135b5dSChristopher Siden 0); 2399ad135b5dSChristopher Siden 2400ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2401ad135b5dSChristopher Siden NULL); nvp != NULL; 2402ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2403ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2404ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2405ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2406ad135b5dSChristopher Siden } 2407ad135b5dSChristopher Siden } 2408ad135b5dSChristopher Siden 2409ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2410ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2411ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2412ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2413ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2414ad135b5dSChristopher Siden ENOTSUP)); 2415ad135b5dSChristopher Siden } 2416ad135b5dSChristopher Siden 2417ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2418ad135b5dSChristopher Siden } 2419fa9e4066Sahrens 2420fa9e4066Sahrens /* 2421fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 24224b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 24234b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 24244b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 24254b964adaSGeorge Wilson * can handle missing vdevs. 2426fa9e4066Sahrens */ 24274b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 2428*5cabbc6bSPrashanth Sreenivasa &children) != 0 && trust_config && type != SPA_IMPORT_ASSEMBLE && 24291195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 24301195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 24311195e687SMark J Musante 24321195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 24331195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24341195e687SMark J Musante spa_try_repair(spa, config); 24351195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 24361195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 24371195e687SMark J Musante spa->spa_config_splitting = NULL; 2438fa9e4066Sahrens } 2439fa9e4066Sahrens 2440fa9e4066Sahrens /* 2441fa9e4066Sahrens * Initialize internal SPA structures. 2442fa9e4066Sahrens */ 2443fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2444fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2445468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2446c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2447468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2448468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2449b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 24503f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2451b24ab676SJeff Bonwick 2452*5cabbc6bSPrashanth Sreenivasa /* 2453*5cabbc6bSPrashanth Sreenivasa * Everything that we read before we do spa_remove_init() must 2454*5cabbc6bSPrashanth Sreenivasa * have been rewritten after the last device removal was initiated. 2455*5cabbc6bSPrashanth Sreenivasa * Otherwise we could be reading from indirect vdevs before 2456*5cabbc6bSPrashanth Sreenivasa * we have loaded their mappings. 2457*5cabbc6bSPrashanth Sreenivasa */ 2458*5cabbc6bSPrashanth Sreenivasa 2459ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 24601195e687SMark J Musante if (error) 24611195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2462fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2463fa9e4066Sahrens 24641195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 24651195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2466fa9e4066Sahrens 2467*5cabbc6bSPrashanth Sreenivasa /* 2468*5cabbc6bSPrashanth Sreenivasa * Validate the config, using the MOS config to fill in any 2469*5cabbc6bSPrashanth Sreenivasa * information which might be missing. If we fail to validate 2470*5cabbc6bSPrashanth Sreenivasa * the config then declare the pool unfit for use. If we're 2471*5cabbc6bSPrashanth Sreenivasa * assembling a pool from a split, the log is not transferred 2472*5cabbc6bSPrashanth Sreenivasa * over. 2473*5cabbc6bSPrashanth Sreenivasa */ 2474*5cabbc6bSPrashanth Sreenivasa if (type != SPA_IMPORT_ASSEMBLE) { 2475*5cabbc6bSPrashanth Sreenivasa nvlist_t *mos_config; 2476*5cabbc6bSPrashanth Sreenivasa if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2477*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2478*5cabbc6bSPrashanth Sreenivasa 2479*5cabbc6bSPrashanth Sreenivasa if (!spa_config_valid(spa, mos_config)) { 2480*5cabbc6bSPrashanth Sreenivasa nvlist_free(mos_config); 2481*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 2482*5cabbc6bSPrashanth Sreenivasa ENXIO)); 2483*5cabbc6bSPrashanth Sreenivasa } 2484*5cabbc6bSPrashanth Sreenivasa nvlist_free(mos_config); 2485*5cabbc6bSPrashanth Sreenivasa 2486*5cabbc6bSPrashanth Sreenivasa /* 2487*5cabbc6bSPrashanth Sreenivasa * Now that we've validated the config, check the state of the 2488*5cabbc6bSPrashanth Sreenivasa * root vdev. If it can't be opened, it indicates one or 2489*5cabbc6bSPrashanth Sreenivasa * more toplevel vdevs are faulted. 2490*5cabbc6bSPrashanth Sreenivasa */ 2491*5cabbc6bSPrashanth Sreenivasa if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2492*5cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENXIO)); 2493*5cabbc6bSPrashanth Sreenivasa } 2494*5cabbc6bSPrashanth Sreenivasa 2495*5cabbc6bSPrashanth Sreenivasa /* 2496*5cabbc6bSPrashanth Sreenivasa * Everything that we read before spa_remove_init() must be stored 2497*5cabbc6bSPrashanth Sreenivasa * on concreted vdevs. Therefore we do this as early as possible. 2498*5cabbc6bSPrashanth Sreenivasa */ 2499*5cabbc6bSPrashanth Sreenivasa if (spa_remove_init(spa) != 0) 2500*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2501*5cabbc6bSPrashanth Sreenivasa 2502ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2503ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 250457221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2505ad135b5dSChristopher Siden 2506ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2507ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2508ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2509ad135b5dSChristopher Siden } 2510ad135b5dSChristopher Siden 2511ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2512ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2513ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2514ad135b5dSChristopher Siden } 2515ad135b5dSChristopher Siden 2516ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2517ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2518ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2519ad135b5dSChristopher Siden } 2520ad135b5dSChristopher Siden 252157221772SChristopher Siden enabled_feat = fnvlist_alloc(); 252257221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2523ad135b5dSChristopher Siden 25242acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 252557221772SChristopher Siden unsup_feat, enabled_feat)) 2526ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2527ad135b5dSChristopher Siden 2528ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 25292acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 253057221772SChristopher Siden unsup_feat, enabled_feat)) { 2531ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 253257221772SChristopher Siden } 2533ad135b5dSChristopher Siden } 2534ad135b5dSChristopher Siden 253557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 253657221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 253757221772SChristopher Siden 2538ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 253957221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 254057221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2541ad135b5dSChristopher Siden } 2542ad135b5dSChristopher Siden 254357221772SChristopher Siden fnvlist_free(enabled_feat); 254457221772SChristopher Siden fnvlist_free(unsup_feat); 2545ad135b5dSChristopher Siden 2546ad135b5dSChristopher Siden if (!missing_feat_read) { 2547ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2548ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2549ad135b5dSChristopher Siden } 2550ad135b5dSChristopher Siden 2551ad135b5dSChristopher Siden /* 2552ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2553ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2554ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2555ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2556ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2557ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2558ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2559ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2560ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2561ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2562ad135b5dSChristopher Siden * available for open in read-only mode. 2563ad135b5dSChristopher Siden * 2564ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2565ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2566ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2567ad135b5dSChristopher Siden * userland in order to know whether to display the 2568ad135b5dSChristopher Siden * abovementioned note. 2569ad135b5dSChristopher Siden */ 2570ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2571ad135b5dSChristopher Siden spa_writeable(spa))) { 2572ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2573ad135b5dSChristopher Siden ENOTSUP)); 2574ad135b5dSChristopher Siden } 257543466aaeSMax Grossman 257643466aaeSMax Grossman /* 257743466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 257843466aaeSMax Grossman * cache during SPA initialization. 257943466aaeSMax Grossman */ 258043466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 258143466aaeSMax Grossman uint64_t refcount; 258243466aaeSMax Grossman 258343466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 258443466aaeSMax Grossman &spa_feature_table[i], &refcount); 258543466aaeSMax Grossman if (error == 0) { 258643466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 258743466aaeSMax Grossman } else if (error == ENOTSUP) { 258843466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 258943466aaeSMax Grossman SPA_FEATURE_DISABLED; 259043466aaeSMax Grossman } else { 259143466aaeSMax Grossman return (spa_vdev_err(rvd, 259243466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 259343466aaeSMax Grossman } 259443466aaeSMax Grossman } 259543466aaeSMax Grossman } 259643466aaeSMax Grossman 259743466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 259843466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 25995d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 260043466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2601ad135b5dSChristopher Siden } 2602ad135b5dSChristopher Siden 2603ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2604ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2605ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2606ad135b5dSChristopher Siden if (error != 0) 2607ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2608ad135b5dSChristopher Siden 2609*5cabbc6bSPrashanth Sreenivasa if (!trust_config) { 261095173954Sek uint64_t hostid; 2611*5cabbc6bSPrashanth Sreenivasa nvlist_t *policy = NULL; 2612*5cabbc6bSPrashanth Sreenivasa nvlist_t *mos_config; 2613871a9500SMark J Musante 2614*5cabbc6bSPrashanth Sreenivasa if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2615871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2616fa9e4066Sahrens 2617*5cabbc6bSPrashanth Sreenivasa if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, 261877650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 261995173954Sek char *hostname; 262095173954Sek unsigned long myhostid = 0; 262195173954Sek 2622*5cabbc6bSPrashanth Sreenivasa VERIFY(nvlist_lookup_string(mos_config, 262395173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 262495173954Sek 26255679c89fSjv #ifdef _KERNEL 26265679c89fSjv myhostid = zone_get_hostid(NULL); 26275679c89fSjv #else /* _KERNEL */ 26285679c89fSjv /* 26295679c89fSjv * We're emulating the system's hostid in userland, so 26305679c89fSjv * we can't use zone_get_hostid(). 26315679c89fSjv */ 263295173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 26335679c89fSjv #endif /* _KERNEL */ 263417194a52Slling if (hostid != 0 && myhostid != 0 && 26355679c89fSjv hostid != myhostid) { 2636*5cabbc6bSPrashanth Sreenivasa nvlist_free(mos_config); 263795173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 263895173954Sek "loaded as it was last accessed by " 263977650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2640654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2641e14bb325SJeff Bonwick spa_name(spa), hostname, 264295173954Sek (unsigned long)hostid); 2643be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 264495173954Sek } 264595173954Sek } 2646c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2647c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2648*5cabbc6bSPrashanth Sreenivasa VERIFY(nvlist_add_nvlist(mos_config, 2649c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 265095173954Sek 2651*5cabbc6bSPrashanth Sreenivasa spa_config_set(spa, mos_config); 2652fa9e4066Sahrens spa_unload(spa); 2653fa9e4066Sahrens spa_deactivate(spa); 26548ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2655fa9e4066Sahrens 26561195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2657fa9e4066Sahrens } 2658fa9e4066Sahrens 265945818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 266045818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 266145818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 266245818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 266345818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 266445818ee1SMatthew Ahrens if (error == ENOENT) { 266545818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 266645818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 266745818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 266845818ee1SMatthew Ahrens } else if (error != 0) { 266945818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 267045818ee1SMatthew Ahrens } 267145818ee1SMatthew Ahrens 2672cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2673cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2674cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2675cde58dbcSMatthew Ahrens if (error != 0) 26761195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2677fa9e4066Sahrens 267899653d4eSeschrock /* 267999653d4eSeschrock * Load the bit that tells us to use the new accounting function 268099653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 268199653d4eSeschrock * be present. 268299653d4eSeschrock */ 26831195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 26841195e687SMark J Musante if (error != 0 && error != ENOENT) 26851195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 268699653d4eSeschrock 26873f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 26883f9d6ad7SLin Ling &spa->spa_creation_version); 26893f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 26903f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26913f9d6ad7SLin Ling 2692fa9e4066Sahrens /* 2693ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2694ea8dc4b6Seschrock * not be present. 2695fa9e4066Sahrens */ 26961195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 26971195e687SMark J Musante if (error != 0 && error != ENOENT) 26981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2699ea8dc4b6Seschrock 27001195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 27011195e687SMark J Musante &spa->spa_errlog_scrub); 27021195e687SMark J Musante if (error != 0 && error != ENOENT) 27031195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2704ea8dc4b6Seschrock 270506eeb2adSek /* 270606eeb2adSek * Load the history object. If we have an older pool, this 270706eeb2adSek * will not be present. 270806eeb2adSek */ 27091195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 27101195e687SMark J Musante if (error != 0 && error != ENOENT) 27111195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27121195e687SMark J Musante 2713215198a6SJoe Stein /* 2714215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2715215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2716215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2717215198a6SJoe Stein * spa_sync_config_object. 2718215198a6SJoe Stein */ 2719215198a6SJoe Stein 2720215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2721215198a6SJoe Stein nvlist_t *mos_config; 2722215198a6SJoe Stein if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2723215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2724215198a6SJoe Stein 2725215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 2726215198a6SJoe Stein &spa->spa_all_vdev_zaps); 2727215198a6SJoe Stein 2728555da511SPaul Dagnelie if (error == ENOENT) { 2729555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 2730555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 2731555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 2732555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2733555da511SPaul Dagnelie } else if (error != 0) { 2734215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2735555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2736215198a6SJoe Stein /* 2737215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2738215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2739215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2740215198a6SJoe Stein */ 2741215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 2742215198a6SJoe Stein /* 2743215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 2744215198a6SJoe Stein * before this. Better be sure of it. 2745215198a6SJoe Stein */ 2746215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2747215198a6SJoe Stein } 2748215198a6SJoe Stein nvlist_free(mos_config); 2749215198a6SJoe Stein 27501195e687SMark J Musante /* 27511195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 27521195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 27531195e687SMark J Musante * devices. 27541195e687SMark J Musante */ 275506eeb2adSek 275699653d4eSeschrock /* 275799653d4eSeschrock * Load any hot spares for this pool. 275899653d4eSeschrock */ 27591195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 27601195e687SMark J Musante if (error != 0 && error != ENOENT) 27611195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27621195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2763e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2764fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 27651195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 27661195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 276799653d4eSeschrock 2768e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 276999653d4eSeschrock spa_load_spares(spa); 2770e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27711195e687SMark J Musante } else if (error == 0) { 27721195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 277399653d4eSeschrock } 277499653d4eSeschrock 2775fa94a07fSbrendan /* 2776fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2777fa94a07fSbrendan */ 27781195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2779fa94a07fSbrendan &spa->spa_l2cache.sav_object); 27801195e687SMark J Musante if (error != 0 && error != ENOENT) 27811195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27821195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2783fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2784fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 27851195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 27861195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2787fa94a07fSbrendan 2788e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2789fa94a07fSbrendan spa_load_l2cache(spa); 2790e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27911195e687SMark J Musante } else if (error == 0) { 27921195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2793fa94a07fSbrendan } 2794fa94a07fSbrendan 2795990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2796ecd6cf80Smarks 27971195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 27981195e687SMark J Musante if (error && error != ENOENT) 27991195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2800b1b8ab34Slling 2801b1b8ab34Slling if (error == 0) { 28021195e687SMark J Musante uint64_t autoreplace; 28031195e687SMark J Musante 28041195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 28051195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 28061195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 28071195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 28081195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 28097855d95bSToomas Soome spa_prop_find(spa, ZPOOL_PROP_BOOTSIZE, &spa->spa_bootsize); 28101195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 28111195e687SMark J Musante &spa->spa_dedup_ditto); 28121195e687SMark J Musante 2813b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2814b1b8ab34Slling } 2815b1b8ab34Slling 28163d7072f8Seschrock /* 28173d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 28183d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 28193d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 28203d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 28213d7072f8Seschrock * over. 28223d7072f8Seschrock */ 2823b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 28243d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2825b693757aSEric Schrock /* 2826b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2827b693757aSEric Schrock * at this point we're using the spare definitions from 2828b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2829b693757aSEric Schrock */ 2830b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2831b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2832b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2833b693757aSEric Schrock } 2834b693757aSEric Schrock } 28353d7072f8Seschrock 2836ea8dc4b6Seschrock /* 2837560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2838ea8dc4b6Seschrock */ 2839*5cabbc6bSPrashanth Sreenivasa error = vdev_load(rvd); 2840*5cabbc6bSPrashanth Sreenivasa if (error != 0) { 2841*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 2842*5cabbc6bSPrashanth Sreenivasa } 2843*5cabbc6bSPrashanth Sreenivasa 2844*5cabbc6bSPrashanth Sreenivasa error = spa_condense_init(spa); 2845*5cabbc6bSPrashanth Sreenivasa if (error != 0) { 2846*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 2847*5cabbc6bSPrashanth Sreenivasa } 28480373e76bSbonwick 2849fa9e4066Sahrens /* 2850fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2851fa9e4066Sahrens */ 2852e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2853fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2854e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2855fa9e4066Sahrens 2856b24ab676SJeff Bonwick /* 2857b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2858b24ab676SJeff Bonwick */ 2859b24ab676SJeff Bonwick error = ddt_load(spa); 28601195e687SMark J Musante if (error != 0) 28611195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2862b24ab676SJeff Bonwick 2863485bbbf5SGeorge Wilson spa_update_dspace(spa); 2864485bbbf5SGeorge Wilson 2865*5cabbc6bSPrashanth Sreenivasa if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa) && 2866*5cabbc6bSPrashanth Sreenivasa spa_check_logs(spa)) { 2867*5cabbc6bSPrashanth Sreenivasa *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 2868*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 2869b24ab676SJeff Bonwick } 2870b24ab676SJeff Bonwick 2871ad135b5dSChristopher Siden if (missing_feat_write) { 2872ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2873ad135b5dSChristopher Siden 2874ad135b5dSChristopher Siden /* 2875ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2876ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2877ad135b5dSChristopher Siden * information and can return to userland. 2878ad135b5dSChristopher Siden */ 2879ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2880ad135b5dSChristopher Siden } 2881ad135b5dSChristopher Siden 28824b964adaSGeorge Wilson /* 28834b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 28844b964adaSGeorge Wilson * to start pushing transactions. 28854b964adaSGeorge Wilson */ 28864b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 28874b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 28884b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 28894b964adaSGeorge Wilson error)); 28904b964adaSGeorge Wilson } 28914b964adaSGeorge Wilson 2892468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2893468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 28945dabedeeSbonwick dmu_tx_t *tx; 28950373e76bSbonwick int need_update = B_FALSE; 289612380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 28978ad4d6ddSJeff Bonwick 2898*5cabbc6bSPrashanth Sreenivasa /* 2899*5cabbc6bSPrashanth Sreenivasa * We must check this before we start the sync thread, because 2900*5cabbc6bSPrashanth Sreenivasa * we only want to start a condense thread for condense 2901*5cabbc6bSPrashanth Sreenivasa * operations that were in progress when the pool was 2902*5cabbc6bSPrashanth Sreenivasa * imported. Once we start syncing, spa_sync() could 2903*5cabbc6bSPrashanth Sreenivasa * initiate a condense (and start a thread for it). In 2904*5cabbc6bSPrashanth Sreenivasa * that case it would be wrong to start a second 2905*5cabbc6bSPrashanth Sreenivasa * condense thread. 2906*5cabbc6bSPrashanth Sreenivasa */ 2907*5cabbc6bSPrashanth Sreenivasa boolean_t condense_in_progress = 2908*5cabbc6bSPrashanth Sreenivasa (spa->spa_condensing_indirect != NULL); 2909*5cabbc6bSPrashanth Sreenivasa 29108ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 29115dabedeeSbonwick 29120373e76bSbonwick /* 29130373e76bSbonwick * Claim log blocks that haven't been committed yet. 29140373e76bSbonwick * This must all happen in a single txg. 2915b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2916b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2917468c413aSTim Haley * Price of rollback is that we abandon the log. 29180373e76bSbonwick */ 2919b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2920b24ab676SJeff Bonwick 292112380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 292212380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 29230b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2924fa9e4066Sahrens dmu_tx_commit(tx); 2925fa9e4066Sahrens 2926b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2927b24ab676SJeff Bonwick 29281195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2929fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2930fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2931fa9e4066Sahrens 2932fa9e4066Sahrens /* 2933b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2934b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2935b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2936b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2937b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2938fa9e4066Sahrens */ 2939b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 29400e34b6a7Sbonwick 29410e34b6a7Sbonwick /* 29420373e76bSbonwick * If the config cache is stale, or we have uninitialized 29430373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2944bc758434SLin Ling * 29454b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2946bc758434SLin Ling * in-core spa_config and update the disk labels. 29470e34b6a7Sbonwick */ 29480373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 29494b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 29504b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 29514b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 29520373e76bSbonwick need_update = B_TRUE; 29530373e76bSbonwick 29548ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 29550373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 29560373e76bSbonwick need_update = B_TRUE; 29570e34b6a7Sbonwick 29580e34b6a7Sbonwick /* 29590373e76bSbonwick * Update the config cache asychronously in case we're the 29600373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 29610e34b6a7Sbonwick */ 29620373e76bSbonwick if (need_update) 29630373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 29648ad4d6ddSJeff Bonwick 29658ad4d6ddSJeff Bonwick /* 29668ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 29678ad4d6ddSJeff Bonwick */ 29683f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 29693f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 29708ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2971503ad85cSMatthew Ahrens 29724445fffbSMatthew Ahrens /* 29734445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 29744445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 29754445fffbSMatthew Ahrens */ 29764445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 29774445fffbSMatthew Ahrens 2978503ad85cSMatthew Ahrens /* 2979503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2980503ad85cSMatthew Ahrens */ 2981503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2982503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2983ca45db41SChris Kirby 2984ca45db41SChris Kirby /* 2985ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2986ca45db41SChris Kirby */ 2987ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2988*5cabbc6bSPrashanth Sreenivasa 2989*5cabbc6bSPrashanth Sreenivasa /* 2990*5cabbc6bSPrashanth Sreenivasa * Note: unlike condensing, we don't need an analogous 2991*5cabbc6bSPrashanth Sreenivasa * "removal_in_progress" dance because no other thread 2992*5cabbc6bSPrashanth Sreenivasa * can start a removal while we hold the spa_namespace_lock. 2993*5cabbc6bSPrashanth Sreenivasa */ 2994*5cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 2995*5cabbc6bSPrashanth Sreenivasa 2996*5cabbc6bSPrashanth Sreenivasa if (condense_in_progress) 2997*5cabbc6bSPrashanth Sreenivasa spa_condense_indirect_restart(spa); 2998fa9e4066Sahrens } 2999fa9e4066Sahrens 30001195e687SMark J Musante return (0); 3001fa9e4066Sahrens } 3002fa9e4066Sahrens 3003468c413aSTim Haley static int 3004468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 3005468c413aSTim Haley { 3006f9af39baSGeorge Wilson int mode = spa->spa_mode; 3007f9af39baSGeorge Wilson 3008468c413aSTim Haley spa_unload(spa); 3009468c413aSTim Haley spa_deactivate(spa); 3010468c413aSTim Haley 3011e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 3012468c413aSTim Haley 3013f9af39baSGeorge Wilson spa_activate(spa, mode); 3014468c413aSTim Haley spa_async_suspend(spa); 3015468c413aSTim Haley 30161195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 3017468c413aSTim Haley } 3018468c413aSTim Haley 3019ad135b5dSChristopher Siden /* 3020ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 3021ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 3022ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 3023ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 3024ad135b5dSChristopher Siden * spa_load(). 3025ad135b5dSChristopher Siden */ 3026468c413aSTim Haley static int 3027468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 3028c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 3029468c413aSTim Haley { 3030ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 3031468c413aSTim Haley nvlist_t *config = NULL; 3032468c413aSTim Haley int load_error, rewind_error; 3033c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 3034468c413aSTim Haley uint64_t min_txg; 3035468c413aSTim Haley 3036a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 3037468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 30381195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3039a33cae98STim Haley } else { 3040468c413aSTim Haley spa->spa_load_max_txg = max_request; 3041e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 3042e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 3043a33cae98STim Haley } 3044468c413aSTim Haley 30451195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 30461195e687SMark J Musante mosconfig); 3047468c413aSTim Haley if (load_error == 0) 3048468c413aSTim Haley return (0); 3049468c413aSTim Haley 3050468c413aSTim Haley if (spa->spa_root_vdev != NULL) 3051468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3052468c413aSTim Haley 3053468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 3054468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 3055468c413aSTim Haley 3056c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 3057468c413aSTim Haley nvlist_free(config); 3058468c413aSTim Haley return (load_error); 3059468c413aSTim Haley } 3060468c413aSTim Haley 3061ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3062ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 30631195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 3064ad135b5dSChristopher Siden } else { 3065ad135b5dSChristopher Siden /* 3066ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 3067ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 3068ad135b5dSChristopher Siden * to rewind. 3069ad135b5dSChristopher Siden */ 3070ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3071ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3072ad135b5dSChristopher Siden } 3073468c413aSTim Haley 3074c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3075c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3076c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3077c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3078468c413aSTim Haley 3079c8ee1847SVictor Latushkin /* 3080c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3081c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3082c8ee1847SVictor Latushkin */ 3083c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3084c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3085c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3086468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 3087468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 3088468c413aSTim Haley } 3089468c413aSTim Haley 3090468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3091468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3092468c413aSTim Haley 3093468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3094468c413aSTim Haley spa_config_set(spa, config); 30957d3000f7SPavel Zakharov else 30967d3000f7SPavel Zakharov nvlist_free(config); 3097468c413aSTim Haley 3098ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3099ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3100ad135b5dSChristopher Siden return (rewind_error); 3101ad135b5dSChristopher Siden } else { 3102ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3103ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3104ad135b5dSChristopher Siden spa->spa_load_info); 3105ad135b5dSChristopher Siden 3106ad135b5dSChristopher Siden /* Restore the initial load info */ 3107ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3108ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3109ad135b5dSChristopher Siden 3110ad135b5dSChristopher Siden return (load_error); 3111ad135b5dSChristopher Siden } 3112468c413aSTim Haley } 3113468c413aSTim Haley 3114fa9e4066Sahrens /* 3115fa9e4066Sahrens * Pool Open/Import 3116fa9e4066Sahrens * 3117fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3118fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3119fa9e4066Sahrens * case of an open, the pool configuration will exist in the 31203d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3121fa9e4066Sahrens * 3122fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3123fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3124fa9e4066Sahrens * ambiguous state. 3125fa9e4066Sahrens */ 3126fa9e4066Sahrens static int 3127468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3128468c413aSTim Haley nvlist_t **config) 3129fa9e4066Sahrens { 3130fa9e4066Sahrens spa_t *spa; 31314b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3132fa9e4066Sahrens int error; 3133fa9e4066Sahrens int locked = B_FALSE; 3134fa9e4066Sahrens 3135fa9e4066Sahrens *spapp = NULL; 3136fa9e4066Sahrens 3137fa9e4066Sahrens /* 3138fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3139fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3140fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3141fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3142fa9e4066Sahrens */ 3143fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3144fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3145fa9e4066Sahrens locked = B_TRUE; 3146fa9e4066Sahrens } 3147fa9e4066Sahrens 3148fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3149fa9e4066Sahrens if (locked) 3150fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3151be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3152fa9e4066Sahrens } 3153468c413aSTim Haley 3154fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 31554b44c88cSTim Haley zpool_rewind_policy_t policy; 31564b44c88cSTim Haley 31574b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 31584b44c88cSTim Haley &policy); 31594b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 31604b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3161fa9e4066Sahrens 31628ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3163fa9e4066Sahrens 3164468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3165468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3166468c413aSTim Haley 3167468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 3168c8ee1847SVictor Latushkin policy.zrp_request); 3169fa9e4066Sahrens 3170fa9e4066Sahrens if (error == EBADF) { 3171fa9e4066Sahrens /* 3172560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3173560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3174560e6e96Seschrock * that the pool has been exported or destroyed. If 3175560e6e96Seschrock * this is the case, the config cache is out of sync and 3176560e6e96Seschrock * we should remove the pool from the namespace. 3177fa9e4066Sahrens */ 3178fa9e4066Sahrens spa_unload(spa); 3179fa9e4066Sahrens spa_deactivate(spa); 3180*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 3181fa9e4066Sahrens spa_remove(spa); 3182fa9e4066Sahrens if (locked) 3183fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3184be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3185ea8dc4b6Seschrock } 3186ea8dc4b6Seschrock 3187ea8dc4b6Seschrock if (error) { 3188fa9e4066Sahrens /* 3189fa9e4066Sahrens * We can't open the pool, but we still have useful 3190fa9e4066Sahrens * information: the state of each vdev after the 3191fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3192fa9e4066Sahrens */ 31934b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3194468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3195468c413aSTim Haley KM_SLEEP) == 0); 31964b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 31974b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 31984b964adaSGeorge Wilson spa->spa_load_info) == 0); 31994b964adaSGeorge Wilson } 3200fa9e4066Sahrens spa_unload(spa); 3201fa9e4066Sahrens spa_deactivate(spa); 3202468c413aSTim Haley spa->spa_last_open_failed = error; 3203fa9e4066Sahrens if (locked) 3204fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3205fa9e4066Sahrens *spapp = NULL; 3206fa9e4066Sahrens return (error); 3207fa9e4066Sahrens } 3208fa9e4066Sahrens } 3209fa9e4066Sahrens 3210fa9e4066Sahrens spa_open_ref(spa, tag); 32113d7072f8Seschrock 3212468c413aSTim Haley if (config != NULL) 3213468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3214468c413aSTim Haley 32154b964adaSGeorge Wilson /* 32164b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 32174b964adaSGeorge Wilson * gathered while doing the load. 32184b964adaSGeorge Wilson */ 32194b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 32204b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 32214b964adaSGeorge Wilson spa->spa_load_info) == 0); 32224b964adaSGeorge Wilson } 32234b964adaSGeorge Wilson 3224a33cae98STim Haley if (locked) { 3225a33cae98STim Haley spa->spa_last_open_failed = 0; 3226a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3227a33cae98STim Haley spa->spa_load_txg = 0; 3228fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3229a33cae98STim Haley } 3230fa9e4066Sahrens 3231fa9e4066Sahrens *spapp = spa; 3232fa9e4066Sahrens 3233fa9e4066Sahrens return (0); 3234fa9e4066Sahrens } 3235fa9e4066Sahrens 3236468c413aSTim Haley int 3237468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3238468c413aSTim Haley nvlist_t **config) 3239468c413aSTim Haley { 3240468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3241468c413aSTim Haley } 3242468c413aSTim Haley 3243fa9e4066Sahrens int 3244fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3245fa9e4066Sahrens { 3246468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3247fa9e4066Sahrens } 3248fa9e4066Sahrens 3249ea8dc4b6Seschrock /* 3250ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3251ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3252ea8dc4b6Seschrock */ 3253ea8dc4b6Seschrock spa_t * 3254ea8dc4b6Seschrock spa_inject_addref(char *name) 3255ea8dc4b6Seschrock { 3256ea8dc4b6Seschrock spa_t *spa; 3257ea8dc4b6Seschrock 3258ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3259ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3260ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3261ea8dc4b6Seschrock return (NULL); 3262ea8dc4b6Seschrock } 3263ea8dc4b6Seschrock spa->spa_inject_ref++; 3264ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3265ea8dc4b6Seschrock 3266ea8dc4b6Seschrock return (spa); 3267ea8dc4b6Seschrock } 3268ea8dc4b6Seschrock 3269ea8dc4b6Seschrock void 3270ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3271ea8dc4b6Seschrock { 3272ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3273ea8dc4b6Seschrock spa->spa_inject_ref--; 3274ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3275ea8dc4b6Seschrock } 3276ea8dc4b6Seschrock 3277fa94a07fSbrendan /* 3278fa94a07fSbrendan * Add spares device information to the nvlist. 3279fa94a07fSbrendan */ 328099653d4eSeschrock static void 328199653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 328299653d4eSeschrock { 328399653d4eSeschrock nvlist_t **spares; 328499653d4eSeschrock uint_t i, nspares; 328599653d4eSeschrock nvlist_t *nvroot; 328699653d4eSeschrock uint64_t guid; 328799653d4eSeschrock vdev_stat_t *vs; 328899653d4eSeschrock uint_t vsc; 328939c23413Seschrock uint64_t pool; 329099653d4eSeschrock 32916809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32926809eb4eSEric Schrock 3293fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 329499653d4eSeschrock return; 329599653d4eSeschrock 329699653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 329799653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3298fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 329999653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 330099653d4eSeschrock if (nspares != 0) { 330199653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 330299653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 330399653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 330499653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 330599653d4eSeschrock 330699653d4eSeschrock /* 330799653d4eSeschrock * Go through and find any spares which have since been 330899653d4eSeschrock * repurposed as an active spare. If this is the case, update 330999653d4eSeschrock * their status appropriately. 331099653d4eSeschrock */ 331199653d4eSeschrock for (i = 0; i < nspares; i++) { 331299653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 331399653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 331489a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 331589a89ebfSlling pool != 0ULL) { 331699653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 33173f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 331899653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 331999653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 332099653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 332199653d4eSeschrock } 332299653d4eSeschrock } 332399653d4eSeschrock } 332499653d4eSeschrock } 332599653d4eSeschrock 3326fa94a07fSbrendan /* 3327fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3328fa94a07fSbrendan */ 3329fa94a07fSbrendan static void 3330fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3331fa94a07fSbrendan { 3332fa94a07fSbrendan nvlist_t **l2cache; 3333fa94a07fSbrendan uint_t i, j, nl2cache; 3334fa94a07fSbrendan nvlist_t *nvroot; 3335fa94a07fSbrendan uint64_t guid; 3336fa94a07fSbrendan vdev_t *vd; 3337fa94a07fSbrendan vdev_stat_t *vs; 3338fa94a07fSbrendan uint_t vsc; 3339fa94a07fSbrendan 33406809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 33416809eb4eSEric Schrock 3342fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3343fa94a07fSbrendan return; 3344fa94a07fSbrendan 3345fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3346fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3347fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3348fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3349fa94a07fSbrendan if (nl2cache != 0) { 3350fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3351fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3352fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3353fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3354fa94a07fSbrendan 3355fa94a07fSbrendan /* 3356fa94a07fSbrendan * Update level 2 cache device stats. 3357fa94a07fSbrendan */ 3358fa94a07fSbrendan 3359fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3360fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3361fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3362fa94a07fSbrendan 3363fa94a07fSbrendan vd = NULL; 3364fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3365fa94a07fSbrendan if (guid == 3366fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3367fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3368fa94a07fSbrendan break; 3369fa94a07fSbrendan } 3370fa94a07fSbrendan } 3371fa94a07fSbrendan ASSERT(vd != NULL); 3372fa94a07fSbrendan 3373fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 33743f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 33753f9d6ad7SLin Ling == 0); 3376fa94a07fSbrendan vdev_get_stats(vd, vs); 3377fa94a07fSbrendan } 3378fa94a07fSbrendan } 3379fa94a07fSbrendan } 3380fa94a07fSbrendan 3381ad135b5dSChristopher Siden static void 3382ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3383ad135b5dSChristopher Siden { 3384ad135b5dSChristopher Siden nvlist_t *features; 3385ad135b5dSChristopher Siden zap_cursor_t zc; 3386ad135b5dSChristopher Siden zap_attribute_t za; 3387ad135b5dSChristopher Siden 3388ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3389ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3390ad135b5dSChristopher Siden 3391ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3392ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3393ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3394ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3395ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3396ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3397ad135b5dSChristopher Siden za.za_num_integers == 1); 3398b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3399ad135b5dSChristopher Siden za.za_first_integer)); 3400ad135b5dSChristopher Siden } 3401ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3402ad135b5dSChristopher Siden } 3403ad135b5dSChristopher Siden 3404ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3405ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3406ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3407ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3408ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3409ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3410ad135b5dSChristopher Siden za.za_num_integers == 1); 3411b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3412ad135b5dSChristopher Siden za.za_first_integer)); 3413ad135b5dSChristopher Siden } 3414ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3415ad135b5dSChristopher Siden } 3416ad135b5dSChristopher Siden 3417ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3418ad135b5dSChristopher Siden features) == 0); 3419ad135b5dSChristopher Siden nvlist_free(features); 3420ad135b5dSChristopher Siden } 3421ad135b5dSChristopher Siden 3422fa9e4066Sahrens int 3423ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3424ad135b5dSChristopher Siden char *altroot, size_t buflen) 3425fa9e4066Sahrens { 3426fa9e4066Sahrens int error; 3427fa9e4066Sahrens spa_t *spa; 3428fa9e4066Sahrens 3429fa9e4066Sahrens *config = NULL; 3430468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3431fa9e4066Sahrens 34326809eb4eSEric Schrock if (spa != NULL) { 34336809eb4eSEric Schrock /* 34346809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 34356809eb4eSEric Schrock * or l2cache devices could change and the config would be 34366809eb4eSEric Schrock * self-inconsistent. 34376809eb4eSEric Schrock */ 34386809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3439ea8dc4b6Seschrock 34406809eb4eSEric Schrock if (*config != NULL) { 344111027bc7STim Haley uint64_t loadtimes[2]; 344211027bc7STim Haley 344311027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 344411027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 344511027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 344611027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 344711027bc7STim Haley 3448e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 34496809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 34506809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3451e14bb325SJeff Bonwick 34526809eb4eSEric Schrock if (spa_suspended(spa)) 34536809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 34546809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 34556809eb4eSEric Schrock spa->spa_failmode) == 0); 34566809eb4eSEric Schrock 34576809eb4eSEric Schrock spa_add_spares(spa, *config); 34586809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3459ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 34606809eb4eSEric Schrock } 346199653d4eSeschrock } 346299653d4eSeschrock 3463ea8dc4b6Seschrock /* 3464ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3465ea8dc4b6Seschrock * and call spa_lookup() directly. 3466ea8dc4b6Seschrock */ 3467ea8dc4b6Seschrock if (altroot) { 3468ea8dc4b6Seschrock if (spa == NULL) { 3469ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3470ea8dc4b6Seschrock spa = spa_lookup(name); 3471ea8dc4b6Seschrock if (spa) 3472ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3473ea8dc4b6Seschrock else 3474ea8dc4b6Seschrock altroot[0] = '\0'; 3475ea8dc4b6Seschrock spa = NULL; 3476ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3477ea8dc4b6Seschrock } else { 3478ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3479ea8dc4b6Seschrock } 3480ea8dc4b6Seschrock } 3481ea8dc4b6Seschrock 34826809eb4eSEric Schrock if (spa != NULL) { 34836809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3484fa9e4066Sahrens spa_close(spa, FTAG); 34856809eb4eSEric Schrock } 3486fa9e4066Sahrens 3487fa9e4066Sahrens return (error); 3488fa9e4066Sahrens } 3489fa9e4066Sahrens 349099653d4eSeschrock /* 3491fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3492fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3493fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3494fa94a07fSbrendan * specified, as long as they are well-formed. 349599653d4eSeschrock */ 349699653d4eSeschrock static int 3497fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3498fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3499fa94a07fSbrendan vdev_labeltype_t label) 350099653d4eSeschrock { 3501fa94a07fSbrendan nvlist_t **dev; 3502fa94a07fSbrendan uint_t i, ndev; 350399653d4eSeschrock vdev_t *vd; 350499653d4eSeschrock int error; 350599653d4eSeschrock 3506e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3507e14bb325SJeff Bonwick 350899653d4eSeschrock /* 3509fa94a07fSbrendan * It's acceptable to have no devs specified. 351099653d4eSeschrock */ 3511fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 351299653d4eSeschrock return (0); 351399653d4eSeschrock 3514fa94a07fSbrendan if (ndev == 0) 3515be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 351699653d4eSeschrock 351799653d4eSeschrock /* 3518fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3519fa94a07fSbrendan * device type. 352099653d4eSeschrock */ 3521fa94a07fSbrendan if (spa_version(spa) < version) 3522be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 352399653d4eSeschrock 352439c23413Seschrock /* 3525fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 352639c23413Seschrock * checking. 352739c23413Seschrock */ 3528fa94a07fSbrendan sav->sav_pending = dev; 3529fa94a07fSbrendan sav->sav_npending = ndev; 353039c23413Seschrock 3531fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3532fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 353399653d4eSeschrock mode)) != 0) 353439c23413Seschrock goto out; 353599653d4eSeschrock 353699653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 353799653d4eSeschrock vdev_free(vd); 3538be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 353939c23413Seschrock goto out; 354099653d4eSeschrock } 354199653d4eSeschrock 3542fa94a07fSbrendan /* 3543e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3544e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3545fa94a07fSbrendan */ 3546e14bb325SJeff Bonwick #ifdef _KERNEL 3547fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3548fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3549be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3550cd0837ccSGeorge Wilson vdev_free(vd); 3551fa94a07fSbrendan goto out; 3552fa94a07fSbrendan } 3553e14bb325SJeff Bonwick #endif 355499653d4eSeschrock vd->vdev_top = vd; 355599653d4eSeschrock 355639c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3557fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3558fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 355939c23413Seschrock vd->vdev_guid) == 0); 356039c23413Seschrock } 356199653d4eSeschrock 356299653d4eSeschrock vdev_free(vd); 356339c23413Seschrock 3564fa94a07fSbrendan if (error && 3565fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 356639c23413Seschrock goto out; 356739c23413Seschrock else 356839c23413Seschrock error = 0; 356999653d4eSeschrock } 357099653d4eSeschrock 357139c23413Seschrock out: 3572fa94a07fSbrendan sav->sav_pending = NULL; 3573fa94a07fSbrendan sav->sav_npending = 0; 357439c23413Seschrock return (error); 357599653d4eSeschrock } 357699653d4eSeschrock 3577fa94a07fSbrendan static int 3578fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3579fa94a07fSbrendan { 3580fa94a07fSbrendan int error; 3581fa94a07fSbrendan 3582e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3583e14bb325SJeff Bonwick 3584fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3585fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3586fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3587fa94a07fSbrendan return (error); 3588fa94a07fSbrendan } 3589fa94a07fSbrendan 3590fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3591fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3592fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3593fa94a07fSbrendan } 3594fa94a07fSbrendan 3595fa94a07fSbrendan static void 3596fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3597fa94a07fSbrendan const char *config) 3598fa94a07fSbrendan { 3599fa94a07fSbrendan int i; 3600fa94a07fSbrendan 3601fa94a07fSbrendan if (sav->sav_config != NULL) { 3602fa94a07fSbrendan nvlist_t **olddevs; 3603fa94a07fSbrendan uint_t oldndevs; 3604fa94a07fSbrendan nvlist_t **newdevs; 3605fa94a07fSbrendan 3606fa94a07fSbrendan /* 3607fa94a07fSbrendan * Generate new dev list by concatentating with the 3608fa94a07fSbrendan * current dev list. 3609fa94a07fSbrendan */ 3610fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3611fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3612fa94a07fSbrendan 3613fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3614fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3615fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3616fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3617fa94a07fSbrendan KM_SLEEP) == 0); 3618fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3619fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3620fa94a07fSbrendan KM_SLEEP) == 0); 3621fa94a07fSbrendan 3622fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3623fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3624fa94a07fSbrendan 3625fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3626fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3627fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3628fa94a07fSbrendan nvlist_free(newdevs[i]); 3629fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3630fa94a07fSbrendan } else { 3631fa94a07fSbrendan /* 3632fa94a07fSbrendan * Generate a new dev list. 3633fa94a07fSbrendan */ 3634fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3635fa94a07fSbrendan KM_SLEEP) == 0); 3636fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3637fa94a07fSbrendan devs, ndevs) == 0); 3638fa94a07fSbrendan } 3639fa94a07fSbrendan } 3640fa94a07fSbrendan 3641fa94a07fSbrendan /* 3642fa94a07fSbrendan * Stop and drop level 2 ARC devices 3643fa94a07fSbrendan */ 3644fa94a07fSbrendan void 3645fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3646fa94a07fSbrendan { 3647fa94a07fSbrendan vdev_t *vd; 3648fa94a07fSbrendan int i; 3649fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3650fa94a07fSbrendan 3651fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3652fa94a07fSbrendan uint64_t pool; 3653fa94a07fSbrendan 3654fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3655fa94a07fSbrendan ASSERT(vd != NULL); 3656fa94a07fSbrendan 36578ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 36588ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3659fa94a07fSbrendan l2arc_remove_vdev(vd); 3660fa94a07fSbrendan } 3661fa94a07fSbrendan } 3662fa94a07fSbrendan 3663fa9e4066Sahrens /* 3664fa9e4066Sahrens * Pool Creation 3665fa9e4066Sahrens */ 3666fa9e4066Sahrens int 3667990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 36684445fffbSMatthew Ahrens nvlist_t *zplprops) 3669fa9e4066Sahrens { 3670fa9e4066Sahrens spa_t *spa; 3671990b4856Slling char *altroot = NULL; 36720373e76bSbonwick vdev_t *rvd; 3673fa9e4066Sahrens dsl_pool_t *dp; 3674fa9e4066Sahrens dmu_tx_t *tx; 3675573ca77eSGeorge Wilson int error = 0; 3676fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3677fa94a07fSbrendan nvlist_t **spares, **l2cache; 3678fa94a07fSbrendan uint_t nspares, nl2cache; 3679cde58dbcSMatthew Ahrens uint64_t version, obj; 3680ad135b5dSChristopher Siden boolean_t has_features; 3681fa9e4066Sahrens 3682fa9e4066Sahrens /* 3683fa9e4066Sahrens * If this pool already exists, return failure. 3684fa9e4066Sahrens */ 3685fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3686fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3687fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3688be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3689fa9e4066Sahrens } 3690fa9e4066Sahrens 3691fa9e4066Sahrens /* 3692fa9e4066Sahrens * Allocate a new spa_t structure. 3693fa9e4066Sahrens */ 3694990b4856Slling (void) nvlist_lookup_string(props, 3695990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3696468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 36978ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3698fa9e4066Sahrens 3699990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3700990b4856Slling spa_deactivate(spa); 3701990b4856Slling spa_remove(spa); 3702c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3703990b4856Slling return (error); 3704990b4856Slling } 3705990b4856Slling 3706ad135b5dSChristopher Siden has_features = B_FALSE; 3707ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3708ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3709ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3710ad135b5dSChristopher Siden has_features = B_TRUE; 3711ad135b5dSChristopher Siden } 3712ad135b5dSChristopher Siden 3713ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3714ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3715990b4856Slling version = SPA_VERSION; 3716ad135b5dSChristopher Siden } 3717ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3718b24ab676SJeff Bonwick 3719b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3720b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3721990b4856Slling spa->spa_uberblock.ub_version = version; 3722fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 37230f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 3724*5cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_state = DSS_NONE; 3725*5cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_removing_vdev = -1; 3726*5cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev = -1; 3727fa9e4066Sahrens 372854d692b7SGeorge Wilson /* 372954d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 373054d692b7SGeorge Wilson */ 37316f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 37326f834bc1SMatthew Ahrens KM_SLEEP); 37336f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 37346f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 37356f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 37366f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 37376f834bc1SMatthew Ahrens } 373854d692b7SGeorge Wilson 37390373e76bSbonwick /* 37400373e76bSbonwick * Create the root vdev. 37410373e76bSbonwick */ 3742e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 37430373e76bSbonwick 374499653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 37450373e76bSbonwick 374699653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 374799653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 37480373e76bSbonwick 3749b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3750be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 375199653d4eSeschrock 375299653d4eSeschrock if (error == 0 && 375399653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3754fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 375599653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3756573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3757573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3758573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3759573ca77eSGeorge Wilson } 37600373e76bSbonwick } 37610373e76bSbonwick 3762e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3763fa9e4066Sahrens 376499653d4eSeschrock if (error != 0) { 3765fa9e4066Sahrens spa_unload(spa); 3766fa9e4066Sahrens spa_deactivate(spa); 3767fa9e4066Sahrens spa_remove(spa); 3768fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3769fa9e4066Sahrens return (error); 3770fa9e4066Sahrens } 3771fa9e4066Sahrens 377299653d4eSeschrock /* 377399653d4eSeschrock * Get the list of spares, if specified. 377499653d4eSeschrock */ 377599653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 377699653d4eSeschrock &spares, &nspares) == 0) { 3777fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 377899653d4eSeschrock KM_SLEEP) == 0); 3779fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 378099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3781e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 378299653d4eSeschrock spa_load_spares(spa); 3783e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3784fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3785fa94a07fSbrendan } 3786fa94a07fSbrendan 3787fa94a07fSbrendan /* 3788fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3789fa94a07fSbrendan */ 3790fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3791fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3792fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3793fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3794fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3795fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3796e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3797fa94a07fSbrendan spa_load_l2cache(spa); 3798e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3799fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 380099653d4eSeschrock } 380199653d4eSeschrock 3802ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 38030a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3804fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3805ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3806fa9e4066Sahrens 3807485bbbf5SGeorge Wilson /* 3808485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3809485bbbf5SGeorge Wilson */ 3810485bbbf5SGeorge Wilson ddt_create(spa); 3811485bbbf5SGeorge Wilson 3812485bbbf5SGeorge Wilson spa_update_dspace(spa); 3813485bbbf5SGeorge Wilson 3814fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3815fa9e4066Sahrens 3816fa9e4066Sahrens /* 3817fa9e4066Sahrens * Create the pool config object. 3818fa9e4066Sahrens */ 3819fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3820f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3821fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3822fa9e4066Sahrens 3823ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3824fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3825ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3826ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3827ea8dc4b6Seschrock } 3828fa9e4066Sahrens 3829ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3830ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3831ad135b5dSChristopher Siden 38323f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 38333f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 38343f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 38353f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 38363f9d6ad7SLin Ling } 38373f9d6ad7SLin Ling 3838990b4856Slling /* Newly created pools with the right version are always deflated. */ 3839990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3840990b4856Slling spa->spa_deflate = TRUE; 3841990b4856Slling if (zap_add(spa->spa_meta_objset, 3842990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3843990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3844990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3845990b4856Slling } 384699653d4eSeschrock } 384799653d4eSeschrock 3848fa9e4066Sahrens /* 3849cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3850fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3851fa9e4066Sahrens * keeps changing. 3852fa9e4066Sahrens */ 3853cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3854cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3855cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3856ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3857cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3858cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3859cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3860ea8dc4b6Seschrock } 3861b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3862cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3863fa9e4066Sahrens 386406eeb2adSek /* 386506eeb2adSek * Create the pool's history object. 386606eeb2adSek */ 3867990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3868990b4856Slling spa_history_create_obj(spa, tx); 3869990b4856Slling 387045818ee1SMatthew Ahrens /* 387145818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 387245818ee1SMatthew Ahrens */ 387345818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 387445818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 387545818ee1SMatthew Ahrens 3876990b4856Slling /* 3877990b4856Slling * Set pool properties. 3878990b4856Slling */ 3879990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3880990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 38810a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3882573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3883b24ab676SJeff Bonwick 3884379c004dSEric Schrock if (props != NULL) { 3885379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38863b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3887379c004dSEric Schrock } 388806eeb2adSek 3889fa9e4066Sahrens dmu_tx_commit(tx); 3890fa9e4066Sahrens 3891fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3892fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3893fa9e4066Sahrens 3894fa9e4066Sahrens /* 3895fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3896fa9e4066Sahrens * bean counters are appropriately updated. 3897fa9e4066Sahrens */ 3898fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3899fa9e4066Sahrens 3900*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 3901ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 3902fa9e4066Sahrens 39034445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3904228975ccSek 3905bc9014e6SJustin Gibbs /* 3906bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 3907bc9014e6SJustin Gibbs * and are making their way through the eviction process. 3908bc9014e6SJustin Gibbs */ 3909bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 3910088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 39110f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 3912088f3894Sahrens 3913daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3914daaa36a7SGeorge Wilson 3915fa9e4066Sahrens return (0); 3916fa9e4066Sahrens } 3917fa9e4066Sahrens 3918e7cbe64fSgw #ifdef _KERNEL 3919e7cbe64fSgw /* 392021ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 392121ecdf64SLin Ling * during the system boot up time. 3922e7cbe64fSgw */ 392321ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 392421ecdf64SLin Ling 392521ecdf64SLin Ling static nvlist_t * 392621ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3927e7cbe64fSgw { 392821ecdf64SLin Ling nvlist_t *config; 3929e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3930e7cbe64fSgw uint64_t pgid; 3931e7cbe64fSgw 393221ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 393321ecdf64SLin Ling return (NULL); 393421ecdf64SLin Ling 3935e7cbe64fSgw /* 3936e7cbe64fSgw * Add this top-level vdev to the child array. 3937e7cbe64fSgw */ 393821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 393921ecdf64SLin Ling &nvtop) == 0); 394021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 394121ecdf64SLin Ling &pgid) == 0); 394221ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3943e7cbe64fSgw 3944e7cbe64fSgw /* 3945e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3946e7cbe64fSgw */ 3947e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 394821ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 394921ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3950e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3951e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3952e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3953e7cbe64fSgw &nvtop, 1) == 0); 3954e7cbe64fSgw 3955e7cbe64fSgw /* 3956e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3957e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3958e7cbe64fSgw */ 3959e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3960e7cbe64fSgw nvlist_free(nvroot); 396121ecdf64SLin Ling return (config); 3962e7cbe64fSgw } 3963e7cbe64fSgw 3964e7cbe64fSgw /* 396521ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 396621ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 396721ecdf64SLin Ling * device has a more recent txg. 3968051aabe6Staylor */ 396921ecdf64SLin Ling static void 397021ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3971051aabe6Staylor { 3972573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 397321ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3974051aabe6Staylor 397521ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 397621ecdf64SLin Ling nvlist_t *label; 397721ecdf64SLin Ling uint64_t label_txg; 3978051aabe6Staylor 397921ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 398021ecdf64SLin Ling &label) != 0) 398121ecdf64SLin Ling return; 3982051aabe6Staylor 398321ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 398421ecdf64SLin Ling &label_txg) == 0); 3985051aabe6Staylor 398621ecdf64SLin Ling /* 398721ecdf64SLin Ling * Do we have a better boot device? 398821ecdf64SLin Ling */ 398921ecdf64SLin Ling if (label_txg > *txg) { 399021ecdf64SLin Ling *txg = label_txg; 399121ecdf64SLin Ling *avd = vd; 3992051aabe6Staylor } 399321ecdf64SLin Ling nvlist_free(label); 3994051aabe6Staylor } 3995051aabe6Staylor } 3996051aabe6Staylor 3997e7cbe64fSgw /* 3998e7cbe64fSgw * Import a root pool. 3999e7cbe64fSgw * 4000051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 4001051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 4002051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 4003e7cbe64fSgw * 4004e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 4005e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 4006e7cbe64fSgw * e.g. 4007e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 4008e7cbe64fSgw */ 4009e7cbe64fSgw int 4010051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 4011e7cbe64fSgw { 401221ecdf64SLin Ling spa_t *spa; 401321ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 401421ecdf64SLin Ling nvlist_t *config, *nvtop; 401521ecdf64SLin Ling uint64_t guid, txg; 4016e7cbe64fSgw char *pname; 4017e7cbe64fSgw int error; 4018e7cbe64fSgw 4019e7cbe64fSgw /* 402021ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 4021e7cbe64fSgw */ 4022dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4023dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 4024dedec472SJack Meng if (config == NULL) { 4025dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 4026dedec472SJack Meng /* iscsi boot */ 4027dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 4028dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 4029dedec472SJack Meng } 4030dedec472SJack Meng } 4031dedec472SJack Meng #endif 4032dedec472SJack Meng if (config == NULL) { 4033ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 403421ecdf64SLin Ling devpath); 4035be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 403621ecdf64SLin Ling } 4037e7cbe64fSgw 403821ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 403921ecdf64SLin Ling &pname) == 0); 404021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 4041e7cbe64fSgw 40426809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 40436809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 40446809eb4eSEric Schrock /* 40456809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 40466809eb4eSEric Schrock * can replace it with the correct config we just read in. 40476809eb4eSEric Schrock */ 40486809eb4eSEric Schrock spa_remove(spa); 40496809eb4eSEric Schrock } 40506809eb4eSEric Schrock 4051468c413aSTim Haley spa = spa_add(pname, config, NULL); 40526809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 40534b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 4054e7cbe64fSgw 405521ecdf64SLin Ling /* 405621ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 405721ecdf64SLin Ling */ 405821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 405921ecdf64SLin Ling &nvtop) == 0); 406021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 406121ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 406221ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 406321ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 406421ecdf64SLin Ling if (error) { 406521ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 406621ecdf64SLin Ling nvlist_free(config); 406721ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 406821ecdf64SLin Ling pname); 406921ecdf64SLin Ling return (error); 407021ecdf64SLin Ling } 407121ecdf64SLin Ling 407221ecdf64SLin Ling /* 407321ecdf64SLin Ling * Get the boot vdev. 407421ecdf64SLin Ling */ 407521ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 407621ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 407721ecdf64SLin Ling (u_longlong_t)guid); 4078be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 407921ecdf64SLin Ling goto out; 408021ecdf64SLin Ling } 4081e7cbe64fSgw 408221ecdf64SLin Ling /* 408321ecdf64SLin Ling * Determine if there is a better boot device. 408421ecdf64SLin Ling */ 408521ecdf64SLin Ling avd = bvd; 408621ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 408721ecdf64SLin Ling if (avd != bvd) { 408821ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 408921ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4090be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 409121ecdf64SLin Ling goto out; 409221ecdf64SLin Ling } 4093e7cbe64fSgw 409421ecdf64SLin Ling /* 409521ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 409621ecdf64SLin Ling * we're booting off the active spare. 409721ecdf64SLin Ling */ 409821ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 409921ecdf64SLin Ling !bvd->vdev_isspare) { 410021ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 410121ecdf64SLin Ling "try booting from '%s'", 4102cb04b873SMark J Musante bvd->vdev_parent-> 4103cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4104be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 410521ecdf64SLin Ling goto out; 410621ecdf64SLin Ling } 410721ecdf64SLin Ling 410821ecdf64SLin Ling error = 0; 410921ecdf64SLin Ling out: 411021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 411121ecdf64SLin Ling vdev_free(rvd); 411221ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 411321ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 411421ecdf64SLin Ling 411521ecdf64SLin Ling nvlist_free(config); 4116e7cbe64fSgw return (error); 4117e7cbe64fSgw } 411821ecdf64SLin Ling 4119e7cbe64fSgw #endif 4120e7cbe64fSgw 41216809eb4eSEric Schrock /* 41226809eb4eSEric Schrock * Import a non-root pool into the system. 41236809eb4eSEric Schrock */ 4124c5904d13Seschrock int 41254b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4126c5904d13Seschrock { 41276809eb4eSEric Schrock spa_t *spa; 41286809eb4eSEric Schrock char *altroot = NULL; 4129468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4130468c413aSTim Haley zpool_rewind_policy_t policy; 4131f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4132f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 41336809eb4eSEric Schrock int error; 41346809eb4eSEric Schrock nvlist_t *nvroot; 41356809eb4eSEric Schrock nvlist_t **spares, **l2cache; 41366809eb4eSEric Schrock uint_t nspares, nl2cache; 41376809eb4eSEric Schrock 41386809eb4eSEric Schrock /* 41396809eb4eSEric Schrock * If a pool with this name exists, return failure. 41406809eb4eSEric Schrock */ 41416809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 41421195e687SMark J Musante if (spa_lookup(pool) != NULL) { 41436809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4144be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 41456809eb4eSEric Schrock } 41466809eb4eSEric Schrock 41476809eb4eSEric Schrock /* 41486809eb4eSEric Schrock * Create and initialize the spa structure. 41496809eb4eSEric Schrock */ 41506809eb4eSEric Schrock (void) nvlist_lookup_string(props, 41516809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4152f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4153f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4154f9af39baSGeorge Wilson if (readonly) 4155f9af39baSGeorge Wilson mode = FREAD; 4156468c413aSTim Haley spa = spa_add(pool, config, altroot); 41574b964adaSGeorge Wilson spa->spa_import_flags = flags; 41584b964adaSGeorge Wilson 41594b964adaSGeorge Wilson /* 41604b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 41614b964adaSGeorge Wilson * as if it had been loaded at boot. 41624b964adaSGeorge Wilson */ 41634b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 41644b964adaSGeorge Wilson if (props != NULL) 41654b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 41664b964adaSGeorge Wilson 4167*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 4168ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 41694b964adaSGeorge Wilson 41704b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 41714b964adaSGeorge Wilson return (0); 41724b964adaSGeorge Wilson } 41734b964adaSGeorge Wilson 4174f9af39baSGeorge Wilson spa_activate(spa, mode); 41756809eb4eSEric Schrock 417625f89ee2SJeff Bonwick /* 417725f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 417825f89ee2SJeff Bonwick */ 417925f89ee2SJeff Bonwick spa_async_suspend(spa); 418025f89ee2SJeff Bonwick 41814b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 41824b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 41834b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 41844b964adaSGeorge Wilson 41856809eb4eSEric Schrock /* 41866809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 41876809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 41886809eb4eSEric Schrock * doing an import. 41896809eb4eSEric Schrock */ 4190468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4191468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 41924b964adaSGeorge Wilson 4193468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 4194c8ee1847SVictor Latushkin policy.zrp_request); 4195468c413aSTim Haley 4196468c413aSTim Haley /* 41974b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 41984b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4199468c413aSTim Haley */ 42004b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 42014b964adaSGeorge Wilson spa->spa_load_info) == 0); 42026809eb4eSEric Schrock 42036809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42046809eb4eSEric Schrock /* 42056809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 42066809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 42076809eb4eSEric Schrock */ 42086809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 42096809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 42106809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 42116809eb4eSEric Schrock spa_load_spares(spa); 42126809eb4eSEric Schrock } 42136809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 42146809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 42156809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 42166809eb4eSEric Schrock spa_load_l2cache(spa); 42176809eb4eSEric Schrock } 42186809eb4eSEric Schrock 42196809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 42206809eb4eSEric Schrock &nvroot) == 0); 42216809eb4eSEric Schrock if (error == 0) 42226809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 42236809eb4eSEric Schrock VDEV_ALLOC_SPARE); 42246809eb4eSEric Schrock if (error == 0) 42256809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 42266809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 42276809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42286809eb4eSEric Schrock 42296809eb4eSEric Schrock if (props != NULL) 42306809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 42316809eb4eSEric Schrock 42326809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 42336809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 42346809eb4eSEric Schrock spa_unload(spa); 42356809eb4eSEric Schrock spa_deactivate(spa); 42366809eb4eSEric Schrock spa_remove(spa); 42376809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 42386809eb4eSEric Schrock return (error); 42396809eb4eSEric Schrock } 42406809eb4eSEric Schrock 4241955ef359SLin Ling spa_async_resume(spa); 4242955ef359SLin Ling 42436809eb4eSEric Schrock /* 42446809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 42456809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 42466809eb4eSEric Schrock */ 42476809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 42486809eb4eSEric Schrock &spares, &nspares) == 0) { 42496809eb4eSEric Schrock if (spa->spa_spares.sav_config) 42506809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 42516809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 42526809eb4eSEric Schrock else 42536809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 42546809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 42556809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 42566809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 42576809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42586809eb4eSEric Schrock spa_load_spares(spa); 42596809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42606809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 42616809eb4eSEric Schrock } 42626809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 42636809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 42646809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 42656809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 42666809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 42676809eb4eSEric Schrock else 42686809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 42696809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 42706809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 42716809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 42726809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42736809eb4eSEric Schrock spa_load_l2cache(spa); 42746809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42756809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 42766809eb4eSEric Schrock } 42776809eb4eSEric Schrock 4278b693757aSEric Schrock /* 4279b693757aSEric Schrock * Check for any removed devices. 4280b693757aSEric Schrock */ 4281b693757aSEric Schrock if (spa->spa_autoreplace) { 4282b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4283b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4284b693757aSEric Schrock } 4285b693757aSEric Schrock 42866809eb4eSEric Schrock if (spa_writeable(spa)) { 42876809eb4eSEric Schrock /* 42886809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 42896809eb4eSEric Schrock */ 4290bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 42916809eb4eSEric Schrock } 42926809eb4eSEric Schrock 4293573ca77eSGeorge Wilson /* 4294573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4295573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4296573ca77eSGeorge Wilson */ 4297573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4298573ca77eSGeorge Wilson 42994445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 43006809eb4eSEric Schrock 4301ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 430214372834SHans Rosenfeld 430314372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 430414372834SHans Rosenfeld 43056809eb4eSEric Schrock return (0); 4306c5904d13Seschrock } 4307c5904d13Seschrock 4308fa9e4066Sahrens nvlist_t * 4309fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4310fa9e4066Sahrens { 4311fa9e4066Sahrens nvlist_t *config = NULL; 4312fa9e4066Sahrens char *poolname; 4313fa9e4066Sahrens spa_t *spa; 4314fa9e4066Sahrens uint64_t state; 43157b7154beSLin Ling int error; 4316fa9e4066Sahrens 4317fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4318fa9e4066Sahrens return (NULL); 4319fa9e4066Sahrens 4320fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4321fa9e4066Sahrens return (NULL); 4322fa9e4066Sahrens 4323fa9e4066Sahrens /* 43240373e76bSbonwick * Create and initialize the spa structure. 4325fa9e4066Sahrens */ 43260373e76bSbonwick mutex_enter(&spa_namespace_lock); 4327468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 43288ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4329fa9e4066Sahrens 4330fa9e4066Sahrens /* 43310373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4332ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4333ecc2d604Sbonwick * is actually the one to trust when doing an import. 4334fa9e4066Sahrens */ 43351195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4336fa9e4066Sahrens 4337fa9e4066Sahrens /* 4338fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4339fa9e4066Sahrens */ 4340fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4341fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4342fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4343fa9e4066Sahrens poolname) == 0); 4344fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4345fa9e4066Sahrens state) == 0); 434695173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 434795173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4348ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4349ad135b5dSChristopher Siden spa->spa_load_info) == 0); 435099653d4eSeschrock 4351e7cbe64fSgw /* 4352e7cbe64fSgw * If the bootfs property exists on this pool then we 4353e7cbe64fSgw * copy it out so that external consumers can tell which 4354e7cbe64fSgw * pools are bootable. 4355e7cbe64fSgw */ 43567b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4357e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4358e7cbe64fSgw 4359e7cbe64fSgw /* 4360e7cbe64fSgw * We have to play games with the name since the 4361e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4362e7cbe64fSgw */ 4363e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4364e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4365e7cbe64fSgw char *cp; 4366e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4367e7cbe64fSgw 4368e7cbe64fSgw cp = strchr(tmpname, '/'); 4369e7cbe64fSgw if (cp == NULL) { 4370e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4371e7cbe64fSgw MAXPATHLEN); 4372e7cbe64fSgw } else { 4373e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4374e7cbe64fSgw "%s/%s", poolname, ++cp); 4375e7cbe64fSgw } 4376e7cbe64fSgw VERIFY(nvlist_add_string(config, 4377e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4378e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4379e7cbe64fSgw } 4380e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4381e7cbe64fSgw } 4382e7cbe64fSgw 438399653d4eSeschrock /* 4384fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 438599653d4eSeschrock */ 43866809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 438799653d4eSeschrock spa_add_spares(spa, config); 4388fa94a07fSbrendan spa_add_l2cache(spa, config); 43896809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4390fa9e4066Sahrens } 4391fa9e4066Sahrens 4392fa9e4066Sahrens spa_unload(spa); 4393fa9e4066Sahrens spa_deactivate(spa); 4394fa9e4066Sahrens spa_remove(spa); 4395fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4396fa9e4066Sahrens 4397fa9e4066Sahrens return (config); 4398fa9e4066Sahrens } 4399fa9e4066Sahrens 4400fa9e4066Sahrens /* 4401fa9e4066Sahrens * Pool export/destroy 4402fa9e4066Sahrens * 4403fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4404fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4405fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4406394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4407394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4408fa9e4066Sahrens */ 4409fa9e4066Sahrens static int 441089a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4411394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4412fa9e4066Sahrens { 4413fa9e4066Sahrens spa_t *spa; 4414fa9e4066Sahrens 441544cd46caSbillm if (oldconfig) 441644cd46caSbillm *oldconfig = NULL; 441744cd46caSbillm 44188ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4419be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4420fa9e4066Sahrens 4421fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4422fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4423fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4424be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4425fa9e4066Sahrens } 4426fa9e4066Sahrens 4427ea8dc4b6Seschrock /* 4428ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4429ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4430ea8dc4b6Seschrock */ 4431ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4432ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4433ea8dc4b6Seschrock spa_async_suspend(spa); 4434ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4435ea8dc4b6Seschrock spa_close(spa, FTAG); 4436ea8dc4b6Seschrock 4437fa9e4066Sahrens /* 4438fa9e4066Sahrens * The pool will be in core if it's openable, 4439fa9e4066Sahrens * in which case we can modify its state. 4440fa9e4066Sahrens */ 4441fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4442fa9e4066Sahrens /* 4443fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4444fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4445fa9e4066Sahrens */ 4446fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4447bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4448fa9e4066Sahrens 4449ea8dc4b6Seschrock /* 4450ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4451ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4452ea8dc4b6Seschrock * fault injection handlers. 4453ea8dc4b6Seschrock */ 4454ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4455ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4456ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4457ea8dc4b6Seschrock spa_async_resume(spa); 4458fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4459be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4460fa9e4066Sahrens } 4461fa9e4066Sahrens 446289a89ebfSlling /* 446389a89ebfSlling * A pool cannot be exported if it has an active shared spare. 446489a89ebfSlling * This is to prevent other pools stealing the active spare 446589a89ebfSlling * from an exported pool. At user's own will, such pool can 446689a89ebfSlling * be forcedly exported. 446789a89ebfSlling */ 446889a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 446989a89ebfSlling spa_has_active_shared_spare(spa)) { 447089a89ebfSlling spa_async_resume(spa); 447189a89ebfSlling mutex_exit(&spa_namespace_lock); 4472be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 447389a89ebfSlling } 447489a89ebfSlling 4475fa9e4066Sahrens /* 4476fa9e4066Sahrens * We want this to be reflected on every label, 4477fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4478fa9e4066Sahrens * final sync that pushes these changes out. 4479fa9e4066Sahrens */ 4480394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4481e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4482ea8dc4b6Seschrock spa->spa_state = new_state; 44833f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 44843f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4485ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4486e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4487ea8dc4b6Seschrock } 4488fa9e4066Sahrens } 4489fa9e4066Sahrens 4490ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 44913d7072f8Seschrock 4492fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4493fa9e4066Sahrens spa_unload(spa); 4494fa9e4066Sahrens spa_deactivate(spa); 4495fa9e4066Sahrens } 4496fa9e4066Sahrens 449744cd46caSbillm if (oldconfig && spa->spa_config) 449844cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 449944cd46caSbillm 4500ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4501394ab0cbSGeorge Wilson if (!hardforce) 4502*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_TRUE, B_TRUE); 4503ea8dc4b6Seschrock spa_remove(spa); 4504ea8dc4b6Seschrock } 4505fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4506fa9e4066Sahrens 4507fa9e4066Sahrens return (0); 4508fa9e4066Sahrens } 4509fa9e4066Sahrens 4510fa9e4066Sahrens /* 4511fa9e4066Sahrens * Destroy a storage pool. 4512fa9e4066Sahrens */ 4513fa9e4066Sahrens int 4514fa9e4066Sahrens spa_destroy(char *pool) 4515fa9e4066Sahrens { 4516394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4517394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4518fa9e4066Sahrens } 4519fa9e4066Sahrens 4520fa9e4066Sahrens /* 4521fa9e4066Sahrens * Export a storage pool. 4522fa9e4066Sahrens */ 4523fa9e4066Sahrens int 4524394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4525394ab0cbSGeorge Wilson boolean_t hardforce) 4526fa9e4066Sahrens { 4527394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4528394ab0cbSGeorge Wilson force, hardforce)); 4529fa9e4066Sahrens } 4530fa9e4066Sahrens 4531ea8dc4b6Seschrock /* 4532ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4533ea8dc4b6Seschrock * from the namespace in any way. 4534ea8dc4b6Seschrock */ 4535ea8dc4b6Seschrock int 4536ea8dc4b6Seschrock spa_reset(char *pool) 4537ea8dc4b6Seschrock { 453889a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4539394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4540ea8dc4b6Seschrock } 4541ea8dc4b6Seschrock 4542fa9e4066Sahrens /* 4543fa9e4066Sahrens * ========================================================================== 4544fa9e4066Sahrens * Device manipulation 4545fa9e4066Sahrens * ========================================================================== 4546fa9e4066Sahrens */ 4547fa9e4066Sahrens 4548fa9e4066Sahrens /* 45498654d025Sperrin * Add a device to a storage pool. 4550fa9e4066Sahrens */ 4551fa9e4066Sahrens int 4552fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4553fa9e4066Sahrens { 455488ecc943SGeorge Wilson uint64_t txg, id; 45558ad4d6ddSJeff Bonwick int error; 4556fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 45570e34b6a7Sbonwick vdev_t *vd, *tvd; 4558fa94a07fSbrendan nvlist_t **spares, **l2cache; 4559fa94a07fSbrendan uint_t nspares, nl2cache; 4560fa9e4066Sahrens 4561f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4562f9af39baSGeorge Wilson 4563fa9e4066Sahrens txg = spa_vdev_enter(spa); 4564fa9e4066Sahrens 456599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 456699653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 456799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4568fa9e4066Sahrens 4569e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 457099653d4eSeschrock 4571fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4572fa94a07fSbrendan &nspares) != 0) 457399653d4eSeschrock nspares = 0; 457499653d4eSeschrock 4575fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4576fa94a07fSbrendan &nl2cache) != 0) 4577fa94a07fSbrendan nl2cache = 0; 4578fa94a07fSbrendan 4579e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4580fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4581fa9e4066Sahrens 4582e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4583e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4584e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 458599653d4eSeschrock 458639c23413Seschrock /* 4587fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4588fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 458939c23413Seschrock */ 4590e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 459139c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 459239c23413Seschrock 459339c23413Seschrock /* 4594*5cabbc6bSPrashanth Sreenivasa * If we are in the middle of a device removal, we can only add 4595*5cabbc6bSPrashanth Sreenivasa * devices which match the existing devices in the pool. 4596*5cabbc6bSPrashanth Sreenivasa * If we are in the middle of a removal, or have some indirect 4597*5cabbc6bSPrashanth Sreenivasa * vdevs, we can not add raidz toplevels. 459839c23413Seschrock */ 4599*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 4600*5cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 4601*5cabbc6bSPrashanth Sreenivasa for (int c = 0; c < vd->vdev_children; c++) { 4602*5cabbc6bSPrashanth Sreenivasa tvd = vd->vdev_child[c]; 4603*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL && 4604*5cabbc6bSPrashanth Sreenivasa tvd->vdev_ashift != 4605*5cabbc6bSPrashanth Sreenivasa spa->spa_vdev_removal->svr_vdev->vdev_ashift) { 4606*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4607*5cabbc6bSPrashanth Sreenivasa } 4608*5cabbc6bSPrashanth Sreenivasa /* Fail if top level vdev is raidz */ 4609*5cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_raidz_ops) { 4610*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4611*5cabbc6bSPrashanth Sreenivasa } 4612*5cabbc6bSPrashanth Sreenivasa /* 4613*5cabbc6bSPrashanth Sreenivasa * Need the top level mirror to be 4614*5cabbc6bSPrashanth Sreenivasa * a mirror of leaf vdevs only 4615*5cabbc6bSPrashanth Sreenivasa */ 4616*5cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ops == &vdev_mirror_ops) { 4617*5cabbc6bSPrashanth Sreenivasa for (uint64_t cid = 0; 4618*5cabbc6bSPrashanth Sreenivasa cid < tvd->vdev_children; cid++) { 4619*5cabbc6bSPrashanth Sreenivasa vdev_t *cvd = tvd->vdev_child[cid]; 4620*5cabbc6bSPrashanth Sreenivasa if (!cvd->vdev_ops->vdev_op_leaf) { 4621*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, vd, 4622*5cabbc6bSPrashanth Sreenivasa txg, EINVAL)); 4623*5cabbc6bSPrashanth Sreenivasa } 4624*5cabbc6bSPrashanth Sreenivasa } 4625*5cabbc6bSPrashanth Sreenivasa } 4626*5cabbc6bSPrashanth Sreenivasa } 4627*5cabbc6bSPrashanth Sreenivasa } 4628*5cabbc6bSPrashanth Sreenivasa 46298ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 463088ecc943SGeorge Wilson 463188ecc943SGeorge Wilson /* 463288ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 463388ecc943SGeorge Wilson */ 463488ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 463588ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 463688ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 463788ecc943SGeorge Wilson break; 463888ecc943SGeorge Wilson } 463988ecc943SGeorge Wilson } 464039c23413Seschrock tvd = vd->vdev_child[c]; 464139c23413Seschrock vdev_remove_child(vd, tvd); 464288ecc943SGeorge Wilson tvd->vdev_id = id; 464339c23413Seschrock vdev_add_child(rvd, tvd); 464439c23413Seschrock vdev_config_dirty(tvd); 464539c23413Seschrock } 464639c23413Seschrock 464799653d4eSeschrock if (nspares != 0) { 4648fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4649fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 465099653d4eSeschrock spa_load_spares(spa); 4651fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4652fa94a07fSbrendan } 4653fa94a07fSbrendan 4654fa94a07fSbrendan if (nl2cache != 0) { 4655fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4656fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4657fa94a07fSbrendan spa_load_l2cache(spa); 4658fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4659fa9e4066Sahrens } 4660fa9e4066Sahrens 4661fa9e4066Sahrens /* 46620e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 46630e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 46640e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 46650e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 46660e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 46670e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 46680373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 46690e34b6a7Sbonwick * 46700e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 46710e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 46720e34b6a7Sbonwick * steps will be completed the next time we load the pool. 46730e34b6a7Sbonwick */ 46740373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 46750e34b6a7Sbonwick 46760373e76bSbonwick mutex_enter(&spa_namespace_lock); 46770373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 4678ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 46790373e76bSbonwick mutex_exit(&spa_namespace_lock); 4680fa9e4066Sahrens 46810373e76bSbonwick return (0); 4682fa9e4066Sahrens } 4683fa9e4066Sahrens 4684fa9e4066Sahrens /* 4685fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4686fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4687fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4688fa9e4066Sahrens * 4689fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4690fa9e4066Sahrens * existing device; in this case the two devices are made into their own 46913d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4692fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4693fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4694fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4695fa9e4066Sahrens * is automatically detached. 4696fa9e4066Sahrens */ 4697fa9e4066Sahrens int 4698ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4699fa9e4066Sahrens { 47003f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4701fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4702fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 470399653d4eSeschrock vdev_ops_t *pvops; 47049b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 47059b3f6b42SEric Kustarz int newvd_isspare; 47069b3f6b42SEric Kustarz int error; 4707fa9e4066Sahrens 4708f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4709f9af39baSGeorge Wilson 4710fa9e4066Sahrens txg = spa_vdev_enter(spa); 4711fa9e4066Sahrens 4712c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4713fa9e4066Sahrens 4714*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL || 4715*5cabbc6bSPrashanth Sreenivasa spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 4716*5cabbc6bSPrashanth Sreenivasa return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4717*5cabbc6bSPrashanth Sreenivasa } 4718*5cabbc6bSPrashanth Sreenivasa 4719fa9e4066Sahrens if (oldvd == NULL) 4720fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4721fa9e4066Sahrens 47220e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 47230e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 47240e34b6a7Sbonwick 4725fa9e4066Sahrens pvd = oldvd->vdev_parent; 4726fa9e4066Sahrens 472799653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4728cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 47293d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47303d7072f8Seschrock 47313d7072f8Seschrock if (newrootvd->vdev_children != 1) 4732fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4733fa9e4066Sahrens 4734fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4735fa9e4066Sahrens 4736fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4737fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4738fa9e4066Sahrens 473999653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4740fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4741fa9e4066Sahrens 47428654d025Sperrin /* 47438654d025Sperrin * Spares can't replace logs 47448654d025Sperrin */ 4745ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 47468654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 47478654d025Sperrin 474899653d4eSeschrock if (!replacing) { 474999653d4eSeschrock /* 475099653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 475199653d4eSeschrock * vdev. 475299653d4eSeschrock */ 475399653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 475499653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 475599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 475699653d4eSeschrock 475799653d4eSeschrock pvops = &vdev_mirror_ops; 475899653d4eSeschrock } else { 475999653d4eSeschrock /* 476099653d4eSeschrock * Active hot spares can only be replaced by inactive hot 476199653d4eSeschrock * spares. 476299653d4eSeschrock */ 476399653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4764cb04b873SMark J Musante oldvd->vdev_isspare && 476599653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 476699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 476799653d4eSeschrock 476899653d4eSeschrock /* 476999653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 477099653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 477139c23413Seschrock * want to create a replacing vdev. The user is not allowed to 477239c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 477339c23413Seschrock * the same (spare replaces spare, non-spare replaces 477439c23413Seschrock * non-spare). 477599653d4eSeschrock */ 4776cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4777cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 477899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4779cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4780cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 478139c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4782cb04b873SMark J Musante } 4783cb04b873SMark J Musante 4784cb04b873SMark J Musante if (newvd->vdev_isspare) 478599653d4eSeschrock pvops = &vdev_spare_ops; 478699653d4eSeschrock else 478799653d4eSeschrock pvops = &vdev_replacing_ops; 478899653d4eSeschrock } 478999653d4eSeschrock 47902a79c5feSlling /* 4791573ca77eSGeorge Wilson * Make sure the new device is big enough. 47922a79c5feSlling */ 4793573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4794fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4795fa9e4066Sahrens 4796ecc2d604Sbonwick /* 4797ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4798ecc2d604Sbonwick * than the top-level vdev. 4799ecc2d604Sbonwick */ 4800ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4801fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4802fa9e4066Sahrens 4803fa9e4066Sahrens /* 4804fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4805fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4806fa9e4066Sahrens */ 4807fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4808fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4809fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4810fa9e4066Sahrens KM_SLEEP); 4811fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4812fa9e4066Sahrens newvd->vdev_path, "old"); 4813fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4814fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4815fa9e4066Sahrens oldvd->vdev_devid = NULL; 4816fa9e4066Sahrens } 4817fa9e4066Sahrens } 4818fa9e4066Sahrens 4819cb04b873SMark J Musante /* mark the device being resilvered */ 4820b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4821cb04b873SMark J Musante 4822fa9e4066Sahrens /* 482399653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 482499653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4825fa9e4066Sahrens */ 4826fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4827fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4828fa9e4066Sahrens 4829fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4830fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4831fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4832fa9e4066Sahrens 4833fa9e4066Sahrens /* 4834fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4835fa9e4066Sahrens */ 4836fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4837fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 483888ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4839fa9e4066Sahrens vdev_add_child(pvd, newvd); 4840fa9e4066Sahrens 4841fa9e4066Sahrens tvd = newvd->vdev_top; 4842fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4843fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4844fa9e4066Sahrens 4845fa9e4066Sahrens vdev_config_dirty(tvd); 4846fa9e4066Sahrens 4847fa9e4066Sahrens /* 48483f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 48493f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 48503f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4851fa9e4066Sahrens */ 48523f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4853fa9e4066Sahrens 48543f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 48553f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4856fa9e4066Sahrens 48576809eb4eSEric Schrock if (newvd->vdev_isspare) { 485839c23413Seschrock spa_spare_activate(newvd); 4859ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 48606809eb4eSEric Schrock } 48616809eb4eSEric Schrock 4862e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4863e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 48649b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4865ea8dc4b6Seschrock 4866fa9e4066Sahrens /* 4867fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4868fa9e4066Sahrens */ 4869ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4870fa9e4066Sahrens 48713f9d6ad7SLin Ling /* 48720713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 48730713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 48740713e232SGeorge Wilson * respective datasets. 48753f9d6ad7SLin Ling */ 48763f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 48773f9d6ad7SLin Ling 487814372834SHans Rosenfeld if (spa->spa_bootfs) 4879ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 488014372834SHans Rosenfeld 4881ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 488214372834SHans Rosenfeld 48833f9d6ad7SLin Ling /* 48843f9d6ad7SLin Ling * Commit the config 48853f9d6ad7SLin Ling */ 48863f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4887fa9e4066Sahrens 48884445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 48893f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4890c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4891c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4892c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 48939b3f6b42SEric Kustarz 48949b3f6b42SEric Kustarz spa_strfree(oldvdpath); 48959b3f6b42SEric Kustarz spa_strfree(newvdpath); 48969b3f6b42SEric Kustarz 4897fa9e4066Sahrens return (0); 4898fa9e4066Sahrens } 4899fa9e4066Sahrens 4900fa9e4066Sahrens /* 4901fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4902f7170741SWill Andrews * 4903fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4904fa9e4066Sahrens * is a replacing vdev. 4905fa9e4066Sahrens */ 4906fa9e4066Sahrens int 49078ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4908fa9e4066Sahrens { 4909fa9e4066Sahrens uint64_t txg; 49108ad4d6ddSJeff Bonwick int error; 4911fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4912fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 491399653d4eSeschrock boolean_t unspare = B_FALSE; 4914d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 49151195e687SMark J Musante char *vdpath; 4916fa9e4066Sahrens 4917f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4918f9af39baSGeorge Wilson 4919fa9e4066Sahrens txg = spa_vdev_enter(spa); 4920fa9e4066Sahrens 4921c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4922fa9e4066Sahrens 4923fa9e4066Sahrens if (vd == NULL) 4924fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4925fa9e4066Sahrens 49260e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 49270e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 49280e34b6a7Sbonwick 4929fa9e4066Sahrens pvd = vd->vdev_parent; 4930fa9e4066Sahrens 49318ad4d6ddSJeff Bonwick /* 49328ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 49338ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 49348ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 49358ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 49368ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 49378ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 49388ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 49398ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 49408ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 49418ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 49428ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 49438ad4d6ddSJeff Bonwick */ 49448ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 49458ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 49468ad4d6ddSJeff Bonwick 4947fa9e4066Sahrens /* 4948cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 494999653d4eSeschrock */ 4950cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4951cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4952cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 495399653d4eSeschrock 495499653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4955e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4956fa9e4066Sahrens 4957fa9e4066Sahrens /* 495899653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4959fa9e4066Sahrens */ 4960fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 496199653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 496299653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4963fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4964fa9e4066Sahrens 4965fa9e4066Sahrens /* 49668ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 49678ad4d6ddSJeff Bonwick * we cannot safely detach it. 4968fa9e4066Sahrens */ 49698ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4970fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4971fa9e4066Sahrens 49728ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4973fa9e4066Sahrens 4974bf82a41bSeschrock /* 4975bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4976bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4977bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4978bf82a41bSeschrock */ 4979cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4980cb04b873SMark J Musante vd->vdev_path != NULL) { 4981cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4982cb04b873SMark J Musante 4983cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4984cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4985cb04b873SMark J Musante 4986cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4987cb04b873SMark J Musante continue; 4988cb04b873SMark J Musante 4989cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4990cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4991cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4992cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4993cb04b873SMark J Musante break; 4994cb04b873SMark J Musante } 4995bf82a41bSeschrock } 4996bf82a41bSeschrock } 4997bf82a41bSeschrock 499899653d4eSeschrock /* 499999653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 500099653d4eSeschrock * that the spare should become a real disk, and be removed from the 500199653d4eSeschrock * active spare list for the pool. 500299653d4eSeschrock */ 500399653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 5004cb04b873SMark J Musante vd->vdev_id == 0 && 5005cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 500699653d4eSeschrock unspare = B_TRUE; 500799653d4eSeschrock 5008fa9e4066Sahrens /* 5009fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 5010fa9e4066Sahrens * This must be done after all other error cases are handled, 5011fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 5012fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 5013fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 5014fa9e4066Sahrens * it's being detached! 5015fa9e4066Sahrens */ 501639c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5017fa9e4066Sahrens 5018fa9e4066Sahrens /* 5019fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 5020fa9e4066Sahrens */ 5021fa9e4066Sahrens vdev_remove_child(pvd, vd); 5022fa9e4066Sahrens vdev_compact_children(pvd); 5023fa9e4066Sahrens 5024fa9e4066Sahrens /* 5025fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 5026fa9e4066Sahrens */ 5027cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 5028fa9e4066Sahrens 502999653d4eSeschrock /* 503099653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 50318ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 50328ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 50338ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 50348ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 50358ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 50368ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 503799653d4eSeschrock */ 503899653d4eSeschrock if (unspare) { 503999653d4eSeschrock ASSERT(cvd->vdev_isspare); 504039c23413Seschrock spa_spare_remove(cvd); 504199653d4eSeschrock unspare_guid = cvd->vdev_guid; 50428ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 5043cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 504499653d4eSeschrock } 504599653d4eSeschrock 5046fa9e4066Sahrens /* 5047fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 5048fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 5049fa9e4066Sahrens */ 5050cb04b873SMark J Musante if (pvd->vdev_children == 1) { 5051cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 5052cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 5053fa9e4066Sahrens vdev_remove_parent(cvd); 5054cb04b873SMark J Musante } 5055cb04b873SMark J Musante 5056fa9e4066Sahrens 5057fa9e4066Sahrens /* 5058fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 5059fa9e4066Sahrens * may have been the previous top-level vdev. 5060fa9e4066Sahrens */ 5061fa9e4066Sahrens tvd = cvd->vdev_top; 5062fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 5063fa9e4066Sahrens 5064fa9e4066Sahrens /* 506539c23413Seschrock * Reevaluate the parent vdev state. 5066fa9e4066Sahrens */ 50673d7072f8Seschrock vdev_propagate_state(cvd); 5068fa9e4066Sahrens 5069fa9e4066Sahrens /* 5070573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 5071573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 5072573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 5073573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 5074573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 5075fa9e4066Sahrens */ 5076573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 5077573ca77eSGeorge Wilson vdev_reopen(tvd); 5078573ca77eSGeorge Wilson vdev_expand(tvd, txg); 5079573ca77eSGeorge Wilson } 5080fa9e4066Sahrens 5081fa9e4066Sahrens vdev_config_dirty(tvd); 5082fa9e4066Sahrens 5083fa9e4066Sahrens /* 508439c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 508539c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 508639c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 508739c23413Seschrock * prevent vd from being accessed after it's freed. 5088fa9e4066Sahrens */ 50891195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 50908ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 5091fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 5092ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 5093ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 5094fa9e4066Sahrens 5095ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 50963d7072f8Seschrock 5097cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 5098cb04b873SMark J Musante spa_open_ref(spa, FTAG); 5099cb04b873SMark J Musante 510099653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 510199653d4eSeschrock 51024445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 51031195e687SMark J Musante "vdev=%s", vdpath); 51041195e687SMark J Musante spa_strfree(vdpath); 51051195e687SMark J Musante 510699653d4eSeschrock /* 510739c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 510839c23413Seschrock * then we want to go through and remove the device from the hot spare 510939c23413Seschrock * list of every other pool. 511099653d4eSeschrock */ 511199653d4eSeschrock if (unspare) { 5112cb04b873SMark J Musante spa_t *altspa = NULL; 5113cb04b873SMark J Musante 511499653d4eSeschrock mutex_enter(&spa_namespace_lock); 5115cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 5116cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 5117cb04b873SMark J Musante altspa == spa) 511899653d4eSeschrock continue; 5119cb04b873SMark J Musante 5120cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 51219af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 5122cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 51239af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 5124cb04b873SMark J Musante spa_close(altspa, FTAG); 512599653d4eSeschrock } 512699653d4eSeschrock mutex_exit(&spa_namespace_lock); 5127cb04b873SMark J Musante 5128cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 5129cb04b873SMark J Musante spa_vdev_resilver_done(spa); 513099653d4eSeschrock } 513199653d4eSeschrock 5132cb04b873SMark J Musante /* all done with the spa; OK to release */ 5133cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5134cb04b873SMark J Musante spa_close(spa, FTAG); 5135cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5136cb04b873SMark J Musante 513799653d4eSeschrock return (error); 513899653d4eSeschrock } 513999653d4eSeschrock 51401195e687SMark J Musante /* 51411195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 51421195e687SMark J Musante */ 51431195e687SMark J Musante int 51441195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 51451195e687SMark J Musante nvlist_t *props, boolean_t exp) 51461195e687SMark J Musante { 51471195e687SMark J Musante int error = 0; 51481195e687SMark J Musante uint64_t txg, *glist; 51491195e687SMark J Musante spa_t *newspa; 51501195e687SMark J Musante uint_t c, children, lastlog; 51511195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 51521195e687SMark J Musante dmu_tx_t *tx; 51531195e687SMark J Musante char *altroot = NULL; 51541195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 51551195e687SMark J Musante boolean_t activate_slog; 51561195e687SMark J Musante 5157f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 51581195e687SMark J Musante 51591195e687SMark J Musante txg = spa_vdev_enter(spa); 51601195e687SMark J Musante 51611195e687SMark J Musante /* clear the log and flush everything up to now */ 51621195e687SMark J Musante activate_slog = spa_passivate_log(spa); 51631195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 5164*5cabbc6bSPrashanth Sreenivasa error = spa_reset_logs(spa); 51651195e687SMark J Musante txg = spa_vdev_config_enter(spa); 51661195e687SMark J Musante 51671195e687SMark J Musante if (activate_slog) 51681195e687SMark J Musante spa_activate_log(spa); 51691195e687SMark J Musante 51701195e687SMark J Musante if (error != 0) 51711195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 51721195e687SMark J Musante 51731195e687SMark J Musante /* check new spa name before going any further */ 51741195e687SMark J Musante if (spa_lookup(newname) != NULL) 51751195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 51761195e687SMark J Musante 51771195e687SMark J Musante /* 51781195e687SMark J Musante * scan through all the children to ensure they're all mirrors 51791195e687SMark J Musante */ 51801195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 51811195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 51821195e687SMark J Musante &children) != 0) 51831195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 51841195e687SMark J Musante 51851195e687SMark J Musante /* first, check to ensure we've got the right child count */ 51861195e687SMark J Musante rvd = spa->spa_root_vdev; 51871195e687SMark J Musante lastlog = 0; 51881195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 51891195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 51901195e687SMark J Musante 51911195e687SMark J Musante /* don't count the holes & logs as children */ 5192*5cabbc6bSPrashanth Sreenivasa if (vd->vdev_islog || !vdev_is_concrete(vd)) { 51931195e687SMark J Musante if (lastlog == 0) 51941195e687SMark J Musante lastlog = c; 51951195e687SMark J Musante continue; 51961195e687SMark J Musante } 51971195e687SMark J Musante 51981195e687SMark J Musante lastlog = 0; 51991195e687SMark J Musante } 52001195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 52011195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 52021195e687SMark J Musante 52031195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 52041195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 52051195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 52061195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 52071195e687SMark J Musante 52081195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 52091195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 52101195e687SMark J Musante 52111195e687SMark J Musante /* then, loop over each vdev and validate it */ 52121195e687SMark J Musante for (c = 0; c < children; c++) { 52131195e687SMark J Musante uint64_t is_hole = 0; 52141195e687SMark J Musante 52151195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 52161195e687SMark J Musante &is_hole); 52171195e687SMark J Musante 52181195e687SMark J Musante if (is_hole != 0) { 52191195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 52201195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 52211195e687SMark J Musante continue; 52221195e687SMark J Musante } else { 5223be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 52241195e687SMark J Musante break; 52251195e687SMark J Musante } 52261195e687SMark J Musante } 52271195e687SMark J Musante 52281195e687SMark J Musante /* which disk is going to be split? */ 52291195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 52301195e687SMark J Musante &glist[c]) != 0) { 5231be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 52321195e687SMark J Musante break; 52331195e687SMark J Musante } 52341195e687SMark J Musante 52351195e687SMark J Musante /* look it up in the spa */ 52361195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 52371195e687SMark J Musante if (vml[c] == NULL) { 5238be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 52391195e687SMark J Musante break; 52401195e687SMark J Musante } 52411195e687SMark J Musante 52421195e687SMark J Musante /* make sure there's nothing stopping the split */ 52431195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 52441195e687SMark J Musante vml[c]->vdev_islog || 5245*5cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vml[c]) || 52461195e687SMark J Musante vml[c]->vdev_isspare || 52471195e687SMark J Musante vml[c]->vdev_isl2cache || 52481195e687SMark J Musante !vdev_writeable(vml[c]) || 5249d41c4376SMark J Musante vml[c]->vdev_children != 0 || 52501195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 52511195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5252be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 52531195e687SMark J Musante break; 52541195e687SMark J Musante } 52551195e687SMark J Musante 52561195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5257be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 52581195e687SMark J Musante break; 52591195e687SMark J Musante } 52601195e687SMark J Musante 52611195e687SMark J Musante /* we need certain info from the top level */ 52621195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 52631195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 52641195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 52651195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 52661195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 52671195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 52681195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 52691195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5270215198a6SJoe Stein 5271215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5272215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5273215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5274215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5275215198a6SJoe Stein 5276215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5277215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5278215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5279215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 52801195e687SMark J Musante } 52811195e687SMark J Musante 52821195e687SMark J Musante if (error != 0) { 52831195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 52841195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 52851195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 52861195e687SMark J Musante } 52871195e687SMark J Musante 52881195e687SMark J Musante /* stop writers from using the disks */ 52891195e687SMark J Musante for (c = 0; c < children; c++) { 52901195e687SMark J Musante if (vml[c] != NULL) 52911195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 52921195e687SMark J Musante } 52931195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 52941195e687SMark J Musante 52951195e687SMark J Musante /* 52961195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 52971195e687SMark J Musante * will disappear once the config is regenerated. 52981195e687SMark J Musante */ 52991195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 53001195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 53011195e687SMark J Musante glist, children) == 0); 53021195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 53031195e687SMark J Musante 530498295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 53051195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 53061195e687SMark J Musante nvl) == 0); 530798295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 53081195e687SMark J Musante spa->spa_config_splitting = nvl; 53091195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 53101195e687SMark J Musante 53111195e687SMark J Musante /* configure and create the new pool */ 53121195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 53131195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 53141195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 53151195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 53161195e687SMark J Musante spa_version(spa)) == 0); 53171195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 53181195e687SMark J Musante spa->spa_config_txg) == 0); 53191195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 53201195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5321215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 53221195e687SMark J Musante (void) nvlist_lookup_string(props, 53231195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 53241195e687SMark J Musante 5325d41c4376SMark J Musante /* add the new pool to the namespace */ 53261195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5327215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 53281195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 53291195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 53301195e687SMark J Musante 53311195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 53321195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 53331195e687SMark J Musante 53341195e687SMark J Musante if (zio_injection_enabled) 53351195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 53361195e687SMark J Musante 53371195e687SMark J Musante spa_activate(newspa, spa_mode_global); 53381195e687SMark J Musante spa_async_suspend(newspa); 53391195e687SMark J Musante 53401195e687SMark J Musante /* create the new pool from the disks of the original pool */ 53411195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 53421195e687SMark J Musante if (error) 53431195e687SMark J Musante goto out; 53441195e687SMark J Musante 53451195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 53461195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 53471195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 53481195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 53491195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 53501195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 53511195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 53521195e687SMark J Musante B_TRUE)); 53531195e687SMark J Musante } 53541195e687SMark J Musante 53551195e687SMark J Musante /* set the props */ 53561195e687SMark J Musante if (props != NULL) { 53571195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 53581195e687SMark J Musante error = spa_prop_set(newspa, props); 53591195e687SMark J Musante if (error) 53601195e687SMark J Musante goto out; 53611195e687SMark J Musante } 53621195e687SMark J Musante 53631195e687SMark J Musante /* flush everything */ 53641195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 53651195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 53661195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 53671195e687SMark J Musante 53681195e687SMark J Musante if (zio_injection_enabled) 53691195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 53701195e687SMark J Musante 53711195e687SMark J Musante spa_async_resume(newspa); 53721195e687SMark J Musante 53731195e687SMark J Musante /* finally, update the original pool's config */ 53741195e687SMark J Musante txg = spa_vdev_config_enter(spa); 53751195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 53761195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 53771195e687SMark J Musante if (error != 0) 53781195e687SMark J Musante dmu_tx_abort(tx); 53791195e687SMark J Musante for (c = 0; c < children; c++) { 53801195e687SMark J Musante if (vml[c] != NULL) { 53811195e687SMark J Musante vdev_split(vml[c]); 53821195e687SMark J Musante if (error == 0) 53834445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 53844445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5385215198a6SJoe Stein 53861195e687SMark J Musante vdev_free(vml[c]); 53871195e687SMark J Musante } 53881195e687SMark J Musante } 5389215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 53901195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 53911195e687SMark J Musante spa->spa_config_splitting = NULL; 53921195e687SMark J Musante nvlist_free(nvl); 53931195e687SMark J Musante if (error == 0) 53941195e687SMark J Musante dmu_tx_commit(tx); 53951195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 53961195e687SMark J Musante 53971195e687SMark J Musante if (zio_injection_enabled) 53981195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 53991195e687SMark J Musante 54001195e687SMark J Musante /* split is complete; log a history record */ 54014445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 54024445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 54031195e687SMark J Musante 54041195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 54051195e687SMark J Musante 54061195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 54071195e687SMark J Musante if (exp) 54081195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 54091195e687SMark J Musante B_FALSE, B_FALSE); 54101195e687SMark J Musante 54111195e687SMark J Musante return (error); 54121195e687SMark J Musante 54131195e687SMark J Musante out: 54141195e687SMark J Musante spa_unload(newspa); 54151195e687SMark J Musante spa_deactivate(newspa); 54161195e687SMark J Musante spa_remove(newspa); 54171195e687SMark J Musante 54181195e687SMark J Musante txg = spa_vdev_config_enter(spa); 541998295d61SMark J Musante 542098295d61SMark J Musante /* re-online all offlined disks */ 542198295d61SMark J Musante for (c = 0; c < children; c++) { 542298295d61SMark J Musante if (vml[c] != NULL) 542398295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 542498295d61SMark J Musante } 542598295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 542698295d61SMark J Musante 54271195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 54281195e687SMark J Musante spa->spa_config_splitting = NULL; 5429d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 54301195e687SMark J Musante 54311195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 54321195e687SMark J Musante return (error); 54331195e687SMark J Musante } 54341195e687SMark J Musante 5435fa9e4066Sahrens /* 54363d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5437f7170741SWill Andrews * currently spared, so we can detach it. 5438fa9e4066Sahrens */ 5439ea8dc4b6Seschrock static vdev_t * 54403d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5441fa9e4066Sahrens { 5442ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5443fa9e4066Sahrens 5444573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 54453d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5446ea8dc4b6Seschrock if (oldvd != NULL) 5447ea8dc4b6Seschrock return (oldvd); 5448ea8dc4b6Seschrock } 5449fa9e4066Sahrens 54503d7072f8Seschrock /* 5451cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5452cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5453cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5454cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5455cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5456cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 54573d7072f8Seschrock */ 5458cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5459cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5460cb04b873SMark J Musante 5461cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5462ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5463ea8dc4b6Seschrock 54648ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5465e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 54668ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5467ea8dc4b6Seschrock return (oldvd); 5468fa9e4066Sahrens } 5469ea8dc4b6Seschrock 54703d7072f8Seschrock /* 54713d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 54723d7072f8Seschrock */ 5473cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5474cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5475cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5476cb04b873SMark J Musante 5477cb04b873SMark J Musante if (last->vdev_unspare) { 5478cb04b873SMark J Musante oldvd = first; 5479cb04b873SMark J Musante newvd = last; 5480cb04b873SMark J Musante } else if (first->vdev_unspare) { 5481cb04b873SMark J Musante oldvd = last; 5482cb04b873SMark J Musante newvd = first; 5483cb04b873SMark J Musante } else { 5484cb04b873SMark J Musante oldvd = NULL; 5485cb04b873SMark J Musante } 54863d7072f8Seschrock 5487cb04b873SMark J Musante if (oldvd != NULL && 54888ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5489e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5490cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 54913d7072f8Seschrock return (oldvd); 5492cb04b873SMark J Musante 5493cb04b873SMark J Musante /* 5494cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5495cb04b873SMark J Musante * and those spares are not required, then we want to 5496cb04b873SMark J Musante * attempt to free them up now so that they can be used 5497cb04b873SMark J Musante * by other pools. Once we're back down to a single 5498cb04b873SMark J Musante * disk+spare, we stop removing them. 5499cb04b873SMark J Musante */ 5500cb04b873SMark J Musante if (vd->vdev_children > 2) { 5501cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5502cb04b873SMark J Musante 5503cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5504cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5505cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5506cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5507cb04b873SMark J Musante return (newvd); 55083d7072f8Seschrock } 55093d7072f8Seschrock } 55103d7072f8Seschrock 5511ea8dc4b6Seschrock return (NULL); 5512fa9e4066Sahrens } 5513fa9e4066Sahrens 5514ea8dc4b6Seschrock static void 55153d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5516fa9e4066Sahrens { 55178ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 55188ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5519ea8dc4b6Seschrock 55208ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5521ea8dc4b6Seschrock 55223d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 55238ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 55248ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5525ea8dc4b6Seschrock guid = vd->vdev_guid; 55268ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 55278ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 55288ad4d6ddSJeff Bonwick sguid = 0; 552999653d4eSeschrock /* 553099653d4eSeschrock * If we have just finished replacing a hot spared device, then 553199653d4eSeschrock * we need to detach the parent's first child (the original hot 553299653d4eSeschrock * spare) as well. 553399653d4eSeschrock */ 5534cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5535cb04b873SMark J Musante ppvd->vdev_children == 2) { 553699653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 55378ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 553899653d4eSeschrock } 5539b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5540b4952e17SGeorge Wilson 55418ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 55428ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5543ea8dc4b6Seschrock return; 55448ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 554599653d4eSeschrock return; 55468ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5547fa9e4066Sahrens } 5548fa9e4066Sahrens 55498ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5550fa9e4066Sahrens } 5551fa9e4066Sahrens 5552c67d9675Seschrock /* 5553b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5554c67d9675Seschrock */ 5555c67d9675Seschrock int 55566809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 55576809eb4eSEric Schrock boolean_t ispath) 5558c67d9675Seschrock { 5559c5904d13Seschrock vdev_t *vd; 5560208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5561c67d9675Seschrock 5562f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5563f9af39baSGeorge Wilson 5564b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5565c67d9675Seschrock 55666809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5567b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5568c67d9675Seschrock 55690e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5570b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 55710e34b6a7Sbonwick 55726809eb4eSEric Schrock if (ispath) { 5573208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5574208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5575208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5576208044b8SGeorge Wilson sync = B_TRUE; 5577208044b8SGeorge Wilson } 55786809eb4eSEric Schrock } else { 5579208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5580208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5581208044b8SGeorge Wilson sync = B_TRUE; 5582208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 55836809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5584208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5585208044b8SGeorge Wilson sync = B_TRUE; 5586208044b8SGeorge Wilson } 55876809eb4eSEric Schrock } 5588c67d9675Seschrock 5589208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5590c67d9675Seschrock } 5591c67d9675Seschrock 55926809eb4eSEric Schrock int 55936809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 55946809eb4eSEric Schrock { 55956809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 55966809eb4eSEric Schrock } 55976809eb4eSEric Schrock 55986809eb4eSEric Schrock int 55996809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 56006809eb4eSEric Schrock { 56016809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 56026809eb4eSEric Schrock } 56036809eb4eSEric Schrock 5604fa9e4066Sahrens /* 5605fa9e4066Sahrens * ========================================================================== 56063f9d6ad7SLin Ling * SPA Scanning 5607fa9e4066Sahrens * ========================================================================== 5608fa9e4066Sahrens */ 56091702cce7SAlek Pinchuk int 56101702cce7SAlek Pinchuk spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) 56111702cce7SAlek Pinchuk { 56121702cce7SAlek Pinchuk ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 56131702cce7SAlek Pinchuk 56141702cce7SAlek Pinchuk if (dsl_scan_resilvering(spa->spa_dsl_pool)) 56151702cce7SAlek Pinchuk return (SET_ERROR(EBUSY)); 56161702cce7SAlek Pinchuk 56171702cce7SAlek Pinchuk return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); 56181702cce7SAlek Pinchuk } 5619fa9e4066Sahrens 5620ea8dc4b6Seschrock int 56213f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5622fa9e4066Sahrens { 5623e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 56243f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5625be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 56263f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 56273f9d6ad7SLin Ling } 5628bb8b5132Sek 56293f9d6ad7SLin Ling int 56303f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 56313f9d6ad7SLin Ling { 56323f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 56333f9d6ad7SLin Ling 56343f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5635be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5636fa9e4066Sahrens 5637fa9e4066Sahrens /* 5638088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5639088f3894Sahrens * writeable leaf device, we have nothing to do. 5640fa9e4066Sahrens */ 56413f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5642088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5643088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5644ea8dc4b6Seschrock return (0); 5645ea8dc4b6Seschrock } 5646fa9e4066Sahrens 56473f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5648fa9e4066Sahrens } 5649fa9e4066Sahrens 5650ea8dc4b6Seschrock /* 5651ea8dc4b6Seschrock * ========================================================================== 5652ea8dc4b6Seschrock * SPA async task processing 5653ea8dc4b6Seschrock * ========================================================================== 5654ea8dc4b6Seschrock */ 5655ea8dc4b6Seschrock 5656ea8dc4b6Seschrock static void 56573d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5658fa9e4066Sahrens { 565949cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 566098d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 566198d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 566249cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 56631d713200SEric Schrock 56641d713200SEric Schrock /* 56651d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 56661d713200SEric Schrock * vdev_clear() as that will cause us to throw away 56671d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 56681d713200SEric Schrock * device, all of which is a waste. 56691d713200SEric Schrock */ 56701d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 56711d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 56721d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 56731d713200SEric Schrock 5674e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5675ea8dc4b6Seschrock } 567649cf58c0SBrendan Gregg - Sun Microsystems 5677e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 567849cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5679ea8dc4b6Seschrock } 5680fa9e4066Sahrens 5681e14bb325SJeff Bonwick static void 5682e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5683e14bb325SJeff Bonwick { 5684e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 568598d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5686e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5687e14bb325SJeff Bonwick } 5688e14bb325SJeff Bonwick 5689e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5690e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5691e14bb325SJeff Bonwick } 5692e14bb325SJeff Bonwick 5693573ca77eSGeorge Wilson static void 5694573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5695573ca77eSGeorge Wilson { 5696573ca77eSGeorge Wilson sysevent_id_t eid; 5697573ca77eSGeorge Wilson nvlist_t *attr; 5698573ca77eSGeorge Wilson char *physpath; 5699573ca77eSGeorge Wilson 5700573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5701573ca77eSGeorge Wilson return; 5702573ca77eSGeorge Wilson 5703573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5704573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5705573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5706573ca77eSGeorge Wilson } 5707573ca77eSGeorge Wilson 5708573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5709573ca77eSGeorge Wilson return; 5710573ca77eSGeorge Wilson 5711573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5712573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5713573ca77eSGeorge Wilson 5714573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5715573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5716573ca77eSGeorge Wilson 5717573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5718573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5719573ca77eSGeorge Wilson 5720573ca77eSGeorge Wilson nvlist_free(attr); 5721573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5722573ca77eSGeorge Wilson } 5723573ca77eSGeorge Wilson 5724ea8dc4b6Seschrock static void 57253f7978d0SAlan Somers spa_async_thread(void *arg) 5726ea8dc4b6Seschrock { 57273f7978d0SAlan Somers spa_t *spa = (spa_t *)arg; 5728e14bb325SJeff Bonwick int tasks; 5729ea8dc4b6Seschrock 5730ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5731ea8dc4b6Seschrock 5732ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5733ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5734ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5735ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5736ea8dc4b6Seschrock 57370373e76bSbonwick /* 57380373e76bSbonwick * See if the config needs to be updated. 57390373e76bSbonwick */ 57400373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5741b24ab676SJeff Bonwick uint64_t old_space, new_space; 5742573ca77eSGeorge Wilson 57430373e76bSbonwick mutex_enter(&spa_namespace_lock); 5744b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 57450373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5746b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 57470373e76bSbonwick mutex_exit(&spa_namespace_lock); 5748573ca77eSGeorge Wilson 5749573ca77eSGeorge Wilson /* 5750573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5751573ca77eSGeorge Wilson * then log an internal history event. 5752573ca77eSGeorge Wilson */ 5753b24ab676SJeff Bonwick if (new_space != old_space) { 57544445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5755c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5756b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5757573ca77eSGeorge Wilson } 57580373e76bSbonwick } 57590373e76bSbonwick 5760ea8dc4b6Seschrock /* 57613d7072f8Seschrock * See if any devices need to be marked REMOVED. 5762ea8dc4b6Seschrock */ 5763e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 57648f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 57653d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5766e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 576749cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5768e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 576949cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5770e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5771e14bb325SJeff Bonwick } 5772e14bb325SJeff Bonwick 5773573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5774573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5775573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5776573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5777573ca77eSGeorge Wilson } 5778573ca77eSGeorge Wilson 5779e14bb325SJeff Bonwick /* 5780e14bb325SJeff Bonwick * See if any devices need to be probed. 5781e14bb325SJeff Bonwick */ 5782e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 57838f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5784e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5785e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 57863d7072f8Seschrock } 5787ea8dc4b6Seschrock 5788ea8dc4b6Seschrock /* 5789ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5790ea8dc4b6Seschrock */ 57913d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 57923d7072f8Seschrock spa_vdev_resilver_done(spa); 5793fa9e4066Sahrens 5794ea8dc4b6Seschrock /* 5795ea8dc4b6Seschrock * Kick off a resilver. 5796ea8dc4b6Seschrock */ 5797088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 57983f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5799ea8dc4b6Seschrock 5800ea8dc4b6Seschrock /* 5801ea8dc4b6Seschrock * Let the world know that we're done. 5802ea8dc4b6Seschrock */ 5803ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5804ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5805ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5806ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5807ea8dc4b6Seschrock thread_exit(); 5808ea8dc4b6Seschrock } 5809ea8dc4b6Seschrock 5810ea8dc4b6Seschrock void 5811ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5812ea8dc4b6Seschrock { 5813ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5814ea8dc4b6Seschrock spa->spa_async_suspended++; 5815*5cabbc6bSPrashanth Sreenivasa while (spa->spa_async_thread != NULL || 5816*5cabbc6bSPrashanth Sreenivasa spa->spa_condense_thread != NULL) 5817ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5818ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5819*5cabbc6bSPrashanth Sreenivasa 5820*5cabbc6bSPrashanth Sreenivasa spa_vdev_remove_suspend(spa); 5821ea8dc4b6Seschrock } 5822ea8dc4b6Seschrock 5823ea8dc4b6Seschrock void 5824ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5825ea8dc4b6Seschrock { 5826ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5827ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5828ea8dc4b6Seschrock spa->spa_async_suspended--; 5829ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5830*5cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa); 5831ea8dc4b6Seschrock } 5832ea8dc4b6Seschrock 58333cb69f73SWill Andrews static boolean_t 58343cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 58353cb69f73SWill Andrews { 58363cb69f73SWill Andrews uint_t non_config_tasks; 58373cb69f73SWill Andrews uint_t config_task; 58383cb69f73SWill Andrews boolean_t config_task_suspended; 58393cb69f73SWill Andrews 58403cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 58413cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 58423cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 58433cb69f73SWill Andrews config_task_suspended = B_FALSE; 58443cb69f73SWill Andrews } else { 58453cb69f73SWill Andrews config_task_suspended = 58463cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 58473cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 58483cb69f73SWill Andrews } 58493cb69f73SWill Andrews 58503cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 58513cb69f73SWill Andrews } 58523cb69f73SWill Andrews 5853ea8dc4b6Seschrock static void 5854ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5855ea8dc4b6Seschrock { 5856ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 58573cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 58583cb69f73SWill Andrews !spa->spa_async_suspended && 58590373e76bSbonwick spa->spa_async_thread == NULL && 58603cb69f73SWill Andrews rootdir != NULL) 5861ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5862ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5863ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5864ea8dc4b6Seschrock } 5865ea8dc4b6Seschrock 5866ea8dc4b6Seschrock void 5867ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5868ea8dc4b6Seschrock { 58693f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5870ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5871ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5872ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5873fa9e4066Sahrens } 5874fa9e4066Sahrens 5875fa9e4066Sahrens /* 5876fa9e4066Sahrens * ========================================================================== 5877fa9e4066Sahrens * SPA syncing routines 5878fa9e4066Sahrens * ========================================================================== 5879fa9e4066Sahrens */ 5880fa9e4066Sahrens 5881cde58dbcSMatthew Ahrens static int 5882cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5883cde58dbcSMatthew Ahrens { 5884cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5885cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5886cde58dbcSMatthew Ahrens return (0); 5887b24ab676SJeff Bonwick } 5888b24ab676SJeff Bonwick 5889cde58dbcSMatthew Ahrens static int 5890cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5891b24ab676SJeff Bonwick { 5892b24ab676SJeff Bonwick zio_t *zio = arg; 5893b24ab676SJeff Bonwick 5894b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5895b24ab676SJeff Bonwick zio->io_flags)); 5896cde58dbcSMatthew Ahrens return (0); 5897fa9e4066Sahrens } 5898fa9e4066Sahrens 589969962b56SMatthew Ahrens /* 590069962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 590169962b56SMatthew Ahrens * amount of time spent syncing frees. 590269962b56SMatthew Ahrens */ 590369962b56SMatthew Ahrens static void 590469962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 590569962b56SMatthew Ahrens { 590669962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 590769962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 590869962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 590969962b56SMatthew Ahrens } 591069962b56SMatthew Ahrens 591169962b56SMatthew Ahrens /* 591269962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 591369962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 591469962b56SMatthew Ahrens */ 591569962b56SMatthew Ahrens static void 591669962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 591769962b56SMatthew Ahrens { 591869962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 591969962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 592069962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 592169962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 592269962b56SMatthew Ahrens } 592369962b56SMatthew Ahrens 592469962b56SMatthew Ahrens 5925fa9e4066Sahrens static void 592699653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5927fa9e4066Sahrens { 5928fa9e4066Sahrens char *packed = NULL; 5929f7991ba4STim Haley size_t bufsize; 5930fa9e4066Sahrens size_t nvsize = 0; 5931fa9e4066Sahrens dmu_buf_t *db; 5932fa9e4066Sahrens 593399653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5934fa9e4066Sahrens 5935f7991ba4STim Haley /* 5936f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 593743466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 5938f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5939f7991ba4STim Haley */ 5940ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 5941f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5942fa9e4066Sahrens 594399653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5944ea8dc4b6Seschrock KM_SLEEP) == 0); 5945f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5946fa9e4066Sahrens 5947f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5948fa9e4066Sahrens 5949f7991ba4STim Haley kmem_free(packed, bufsize); 5950fa9e4066Sahrens 595199653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5952fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5953fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5954ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5955fa9e4066Sahrens } 5956fa9e4066Sahrens 595799653d4eSeschrock static void 5958fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5959fa94a07fSbrendan const char *config, const char *entry) 596099653d4eSeschrock { 596199653d4eSeschrock nvlist_t *nvroot; 5962fa94a07fSbrendan nvlist_t **list; 596399653d4eSeschrock int i; 596499653d4eSeschrock 5965fa94a07fSbrendan if (!sav->sav_sync) 596699653d4eSeschrock return; 596799653d4eSeschrock 596899653d4eSeschrock /* 5969fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5970fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 59713d7072f8Seschrock * valid and the vdevs are labeled appropriately. 597299653d4eSeschrock */ 5973fa94a07fSbrendan if (sav->sav_object == 0) { 5974fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5975fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5976fa94a07fSbrendan sizeof (uint64_t), tx); 597799653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5978fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5979fa94a07fSbrendan &sav->sav_object, tx) == 0); 598099653d4eSeschrock } 598199653d4eSeschrock 598299653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5983fa94a07fSbrendan if (sav->sav_count == 0) { 5984fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 598599653d4eSeschrock } else { 5986fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5987fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5988fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 59893f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5990fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5991fa94a07fSbrendan sav->sav_count) == 0); 5992fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5993fa94a07fSbrendan nvlist_free(list[i]); 5994fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 599599653d4eSeschrock } 599699653d4eSeschrock 5997fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 599806eeb2adSek nvlist_free(nvroot); 599999653d4eSeschrock 6000fa94a07fSbrendan sav->sav_sync = B_FALSE; 600199653d4eSeschrock } 600299653d4eSeschrock 6003215198a6SJoe Stein /* 6004215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6005215198a6SJoe Stein * The all-vdev ZAP must be empty. 6006215198a6SJoe Stein */ 6007215198a6SJoe Stein static void 6008215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6009215198a6SJoe Stein { 6010215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6011215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6012215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6013215198a6SJoe Stein vd->vdev_top_zap, tx)); 6014215198a6SJoe Stein } 6015215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6016215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6017215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6018215198a6SJoe Stein } 6019215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6020215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6021215198a6SJoe Stein } 6022215198a6SJoe Stein } 6023215198a6SJoe Stein 602499653d4eSeschrock static void 602599653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 602699653d4eSeschrock { 602799653d4eSeschrock nvlist_t *config; 602899653d4eSeschrock 6029215198a6SJoe Stein /* 6030215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6031215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6032215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6033215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6034215198a6SJoe Stein */ 6035215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6036215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 603799653d4eSeschrock return; 603899653d4eSeschrock 6039e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6040e14bb325SJeff Bonwick 6041215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6042555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 6043215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6044215198a6SJoe Stein 6045215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6046215198a6SJoe Stein /* Make and build the new AVZ */ 6047215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6048215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6049215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6050215198a6SJoe Stein 6051215198a6SJoe Stein /* Diff old AVZ with new one */ 6052215198a6SJoe Stein zap_cursor_t zc; 6053215198a6SJoe Stein zap_attribute_t za; 6054215198a6SJoe Stein 6055215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6056215198a6SJoe Stein spa->spa_all_vdev_zaps); 6057215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6058215198a6SJoe Stein zap_cursor_advance(&zc)) { 6059215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6060215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6061215198a6SJoe Stein vdzap) == ENOENT) { 6062215198a6SJoe Stein /* 6063215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6064215198a6SJoe Stein * destroy it 6065215198a6SJoe Stein */ 6066215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6067215198a6SJoe Stein tx)); 6068215198a6SJoe Stein } 6069215198a6SJoe Stein } 6070215198a6SJoe Stein 6071215198a6SJoe Stein zap_cursor_fini(&zc); 6072215198a6SJoe Stein 6073215198a6SJoe Stein /* Destroy the old AVZ */ 6074215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6075215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6076215198a6SJoe Stein 6077215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6078215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6079215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6080215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6081215198a6SJoe Stein 6082215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6083215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6084215198a6SJoe Stein zap_cursor_t zc; 6085215198a6SJoe Stein zap_attribute_t za; 6086215198a6SJoe Stein 6087215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6088215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6089215198a6SJoe Stein spa->spa_all_vdev_zaps); 6090215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6091215198a6SJoe Stein zap_cursor_advance(&zc)) { 6092215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6093215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6094215198a6SJoe Stein } 6095215198a6SJoe Stein 6096215198a6SJoe Stein zap_cursor_fini(&zc); 6097215198a6SJoe Stein 6098215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6099215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6100215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6101215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6102215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6103215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6104215198a6SJoe Stein } 6105215198a6SJoe Stein 6106215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6107215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6108215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6109215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6110215198a6SJoe Stein } 6111215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6112215198a6SJoe Stein 6113215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6114215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6115215198a6SJoe Stein 6116e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6117e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6118e14bb325SJeff Bonwick 611925345e46SGeorge Wilson /* 612025345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 612125345e46SGeorge Wilson * the config object gets updated with the correct version. 612225345e46SGeorge Wilson */ 612325345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 612425345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 612525345e46SGeorge Wilson spa->spa_uberblock.ub_version); 612625345e46SGeorge Wilson 6127e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 612899653d4eSeschrock 6129aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 613099653d4eSeschrock spa->spa_config_syncing = config; 613199653d4eSeschrock 613299653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 613399653d4eSeschrock } 613499653d4eSeschrock 6135ad135b5dSChristopher Siden static void 61363b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6137ad135b5dSChristopher Siden { 61383b2aab18SMatthew Ahrens uint64_t *versionp = arg; 61393b2aab18SMatthew Ahrens uint64_t version = *versionp; 61403b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6141ad135b5dSChristopher Siden 6142ad135b5dSChristopher Siden /* 6143ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6144ad135b5dSChristopher Siden */ 6145ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6146ad135b5dSChristopher Siden 614762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6148ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6149ad135b5dSChristopher Siden 6150ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6151ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 61524445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6153ad135b5dSChristopher Siden } 6154ad135b5dSChristopher Siden 6155990b4856Slling /* 6156990b4856Slling * Set zpool properties. 6157990b4856Slling */ 6158b1b8ab34Slling static void 61593b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6160b1b8ab34Slling { 61613b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 61623b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6163b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6164ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6165b1b8ab34Slling 6166e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6167e14bb325SJeff Bonwick 6168990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6169ad135b5dSChristopher Siden uint64_t intval; 6170ad135b5dSChristopher Siden char *strval, *fname; 6171ad135b5dSChristopher Siden zpool_prop_t prop; 6172ad135b5dSChristopher Siden const char *propname; 6173ad135b5dSChristopher Siden zprop_type_t proptype; 61742acef22dSMatthew Ahrens spa_feature_t fid; 6175ad135b5dSChristopher Siden 6176990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6177ad135b5dSChristopher Siden case ZPROP_INVAL: 6178ad135b5dSChristopher Siden /* 6179ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6180ad135b5dSChristopher Siden */ 6181ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6182ad135b5dSChristopher Siden 6183ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 61842acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6185ad135b5dSChristopher Siden 61862acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 61874445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 61884445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6189ad135b5dSChristopher Siden break; 6190ad135b5dSChristopher Siden 6191990b4856Slling case ZPOOL_PROP_VERSION: 61920713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6193990b4856Slling /* 6194ad135b5dSChristopher Siden * The version is synced seperatly before other 6195ad135b5dSChristopher Siden * properties and should be correct by now. 6196990b4856Slling */ 6197ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6198ecd6cf80Smarks break; 6199990b4856Slling 6200990b4856Slling case ZPOOL_PROP_ALTROOT: 6201990b4856Slling /* 6202990b4856Slling * 'altroot' is a non-persistent property. It should 6203990b4856Slling * have been set temporarily at creation or import time. 6204990b4856Slling */ 6205990b4856Slling ASSERT(spa->spa_root != NULL); 6206b1b8ab34Slling break; 62073d7072f8Seschrock 6208f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 62092f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6210990b4856Slling /* 6211f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6212f9af39baSGeorge Wilson * properties. 6213990b4856Slling */ 62143d7072f8Seschrock break; 62158704186eSDan McDonald case ZPOOL_PROP_COMMENT: 62160713e232SGeorge Wilson strval = fnvpair_value_string(elem); 62178704186eSDan McDonald if (spa->spa_comment != NULL) 62188704186eSDan McDonald spa_strfree(spa->spa_comment); 62198704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 62208704186eSDan McDonald /* 62218704186eSDan McDonald * We need to dirty the configuration on all the vdevs 62228704186eSDan McDonald * so that their labels get updated. It's unnecessary 62238704186eSDan McDonald * to do this for pool creation since the vdev's 62248704186eSDan McDonald * configuratoin has already been dirtied. 62258704186eSDan McDonald */ 62268704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 62278704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 62284445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 62294445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 62308704186eSDan McDonald break; 6231990b4856Slling default: 6232990b4856Slling /* 6233990b4856Slling * Set pool property values in the poolprops mos object. 6234990b4856Slling */ 6235990b4856Slling if (spa->spa_pool_props_object == 0) { 6236ad135b5dSChristopher Siden spa->spa_pool_props_object = 6237ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6238990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6239ad135b5dSChristopher Siden tx); 6240990b4856Slling } 6241990b4856Slling 6242990b4856Slling /* normalize the property name */ 6243990b4856Slling propname = zpool_prop_to_name(prop); 6244990b4856Slling proptype = zpool_prop_get_type(prop); 6245990b4856Slling 6246990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6247990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 62480713e232SGeorge Wilson strval = fnvpair_value_string(elem); 62490713e232SGeorge Wilson VERIFY0(zap_update(mos, 6250990b4856Slling spa->spa_pool_props_object, propname, 62510713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 62524445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 62534445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6254990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 62550713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6256990b4856Slling 6257990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6258990b4856Slling const char *unused; 62590713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 62600713e232SGeorge Wilson prop, intval, &unused)); 6261990b4856Slling } 62620713e232SGeorge Wilson VERIFY0(zap_update(mos, 6263990b4856Slling spa->spa_pool_props_object, propname, 62640713e232SGeorge Wilson 8, 1, &intval, tx)); 62654445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 62664445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6267990b4856Slling } else { 6268990b4856Slling ASSERT(0); /* not allowed */ 6269990b4856Slling } 6270990b4856Slling 62710a4e9518Sgw switch (prop) { 62720a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6273990b4856Slling spa->spa_delegation = intval; 62740a4e9518Sgw break; 62750a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6276990b4856Slling spa->spa_bootfs = intval; 62770a4e9518Sgw break; 62780a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 62790a4e9518Sgw spa->spa_failmode = intval; 62800a4e9518Sgw break; 6281573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6282573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6283b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6284b98131cfSEric Taylor spa_async_request(spa, 6285b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6286573ca77eSGeorge Wilson break; 6287b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6288b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6289b24ab676SJeff Bonwick break; 62900a4e9518Sgw default: 62910a4e9518Sgw break; 62920a4e9518Sgw } 6293990b4856Slling } 6294990b4856Slling 6295b1b8ab34Slling } 6296e14bb325SJeff Bonwick 6297e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6298b1b8ab34Slling } 6299b1b8ab34Slling 6300cde58dbcSMatthew Ahrens /* 6301cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6302cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6303cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6304cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6305cde58dbcSMatthew Ahrens * tasks. 6306cde58dbcSMatthew Ahrens */ 6307cde58dbcSMatthew Ahrens static void 6308cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6309cde58dbcSMatthew Ahrens { 6310cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6311cde58dbcSMatthew Ahrens 6312cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6313cde58dbcSMatthew Ahrens 63143b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 63153b2aab18SMatthew Ahrens 6316cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6317cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6318cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6319cde58dbcSMatthew Ahrens 6320cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6321cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6322cde58dbcSMatthew Ahrens } 6323cde58dbcSMatthew Ahrens 6324cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6325cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6326cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6327cde58dbcSMatthew Ahrens } 6328cde58dbcSMatthew Ahrens 6329cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6330cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6331cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6332cde58dbcSMatthew Ahrens 6333cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6334cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6335cde58dbcSMatthew Ahrens } 6336ad135b5dSChristopher Siden 6337ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6338ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6339ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6340ad135b5dSChristopher Siden } 6341b8289d24SDaniil Lunev 6342b8289d24SDaniil Lunev /* 6343b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6344b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6345b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6346b8289d24SDaniil Lunev * this feature active 6347b8289d24SDaniil Lunev */ 6348b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6349b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6350b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6351b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6352b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6353b8289d24SDaniil Lunev 6354b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6355b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6356b8289d24SDaniil Lunev } 635745818ee1SMatthew Ahrens 635845818ee1SMatthew Ahrens /* 635945818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 636045818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 636145818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 636245818ee1SMatthew Ahrens */ 636345818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 636445818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 636545818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 636645818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 636745818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 636845818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 636945818ee1SMatthew Ahrens } 637045818ee1SMatthew Ahrens 63713b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6372cde58dbcSMatthew Ahrens } 6373cde58dbcSMatthew Ahrens 6374*5cabbc6bSPrashanth Sreenivasa static void 6375*5cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vdev_t *vd) 6376*5cabbc6bSPrashanth Sreenivasa { 6377*5cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 6378*5cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 6379*5cabbc6bSPrashanth Sreenivasa 6380*5cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops == &vdev_indirect_ops) { 6381*5cabbc6bSPrashanth Sreenivasa ASSERT(vim != NULL); 6382*5cabbc6bSPrashanth Sreenivasa ASSERT(vib != NULL); 6383*5cabbc6bSPrashanth Sreenivasa } 6384*5cabbc6bSPrashanth Sreenivasa 6385*5cabbc6bSPrashanth Sreenivasa if (vdev_obsolete_sm_object(vd) != 0) { 6386*5cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_sm != NULL); 6387*5cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_removing || 6388*5cabbc6bSPrashanth Sreenivasa vd->vdev_ops == &vdev_indirect_ops); 6389*5cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); 6390*5cabbc6bSPrashanth Sreenivasa ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); 6391*5cabbc6bSPrashanth Sreenivasa 6392*5cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_obsolete_sm_object(vd), ==, 6393*5cabbc6bSPrashanth Sreenivasa space_map_object(vd->vdev_obsolete_sm)); 6394*5cabbc6bSPrashanth Sreenivasa ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, 6395*5cabbc6bSPrashanth Sreenivasa space_map_allocated(vd->vdev_obsolete_sm)); 6396*5cabbc6bSPrashanth Sreenivasa } 6397*5cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_obsolete_segments != NULL); 6398*5cabbc6bSPrashanth Sreenivasa 6399*5cabbc6bSPrashanth Sreenivasa /* 6400*5cabbc6bSPrashanth Sreenivasa * Since frees / remaps to an indirect vdev can only 6401*5cabbc6bSPrashanth Sreenivasa * happen in syncing context, the obsolete segments 6402*5cabbc6bSPrashanth Sreenivasa * tree must be empty when we start syncing. 6403*5cabbc6bSPrashanth Sreenivasa */ 6404*5cabbc6bSPrashanth Sreenivasa ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); 6405*5cabbc6bSPrashanth Sreenivasa } 6406*5cabbc6bSPrashanth Sreenivasa 6407fa9e4066Sahrens /* 6408fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6409fa9e4066Sahrens * part of the process, so we iterate until it converges. 6410fa9e4066Sahrens */ 6411fa9e4066Sahrens void 6412fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6413fa9e4066Sahrens { 6414fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6415fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6416b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 64170373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6418fa9e4066Sahrens vdev_t *vd; 6419fa9e4066Sahrens dmu_tx_t *tx; 6420e14bb325SJeff Bonwick int error; 64210f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 64220f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 6423fa9e4066Sahrens 6424f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6425f9af39baSGeorge Wilson 6426*5cabbc6bSPrashanth Sreenivasa /* 6427*5cabbc6bSPrashanth Sreenivasa * Wait for i/os issued in open context that need to complete 6428*5cabbc6bSPrashanth Sreenivasa * before this txg syncs. 6429*5cabbc6bSPrashanth Sreenivasa */ 6430*5cabbc6bSPrashanth Sreenivasa VERIFY0(zio_wait(spa->spa_txg_zio[txg & TXG_MASK])); 6431*5cabbc6bSPrashanth Sreenivasa spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, 0); 6432*5cabbc6bSPrashanth Sreenivasa 6433fa9e4066Sahrens /* 6434fa9e4066Sahrens * Lock out configuration changes. 6435fa9e4066Sahrens */ 6436e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6437fa9e4066Sahrens 6438fa9e4066Sahrens spa->spa_syncing_txg = txg; 6439fa9e4066Sahrens spa->spa_sync_pass = 0; 6440fa9e4066Sahrens 64410f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 64420f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 64430f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 64440f7643c7SGeorge Wilson 6445e14bb325SJeff Bonwick /* 6446e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6447e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6448e14bb325SJeff Bonwick */ 6449e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 64508ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 64518ad4d6ddSJeff Bonwick /* 64528ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 64538ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 64548ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 64558ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 64568ad4d6ddSJeff Bonwick * into the root vdev tree. 64578ad4d6ddSJeff Bonwick */ 64588ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 64598ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 64608ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 64618ad4d6ddSJeff Bonwick vdev_state_clean(vd); 64628ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 64638ad4d6ddSJeff Bonwick } 64648ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 64658ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6466e14bb325SJeff Bonwick } 6467e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6468e14bb325SJeff Bonwick 646999653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 647099653d4eSeschrock 6471283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6472283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6473283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6474283b8460SGeorge.Wilson 647599653d4eSeschrock /* 6476e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 647799653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 647899653d4eSeschrock */ 6479e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6480e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 648199653d4eSeschrock int i; 648299653d4eSeschrock 648399653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 648499653d4eSeschrock vd = rvd->vdev_child[i]; 648599653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 648699653d4eSeschrock break; 648799653d4eSeschrock } 648899653d4eSeschrock if (i == rvd->vdev_children) { 648999653d4eSeschrock spa->spa_deflate = TRUE; 649099653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 649199653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 649299653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 649399653d4eSeschrock } 649499653d4eSeschrock } 649599653d4eSeschrock 64960f7643c7SGeorge Wilson /* 64970f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 64980f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 64990f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 65000f7643c7SGeorge Wilson * out this txg. 65010f7643c7SGeorge Wilson */ 65020f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 65030f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 65040f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 65050f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 65060f7643c7SGeorge Wilson 65070f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 65080f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 65090f7643c7SGeorge Wilson continue; 65100f7643c7SGeorge Wilson 65110f7643c7SGeorge Wilson /* 65120f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 65130f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 65140f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 65150f7643c7SGeorge Wilson */ 65160f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 65170f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 65180f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 65190f7643c7SGeorge Wilson } 65200f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 65210f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 65220f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 65230f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 65240f7643c7SGeorge Wilson 65250f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 65260f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 65270f7643c7SGeorge Wilson 6528*5cabbc6bSPrashanth Sreenivasa for (int c = 0; c < rvd->vdev_children; c++) { 6529*5cabbc6bSPrashanth Sreenivasa vdev_t *vd = rvd->vdev_child[c]; 6530*5cabbc6bSPrashanth Sreenivasa vdev_indirect_state_sync_verify(vd); 6531*5cabbc6bSPrashanth Sreenivasa 6532*5cabbc6bSPrashanth Sreenivasa if (vdev_indirect_should_condense(vd)) { 6533*5cabbc6bSPrashanth Sreenivasa spa_condense_indirect_start_sync(vd, tx); 6534*5cabbc6bSPrashanth Sreenivasa break; 6535*5cabbc6bSPrashanth Sreenivasa } 6536*5cabbc6bSPrashanth Sreenivasa } 6537*5cabbc6bSPrashanth Sreenivasa 6538fa9e4066Sahrens /* 6539fa9e4066Sahrens * Iterate to convergence. 6540fa9e4066Sahrens */ 6541fa9e4066Sahrens do { 6542b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6543fa9e4066Sahrens 6544fa9e4066Sahrens spa_sync_config_object(spa, tx); 6545fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6546fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6547fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6548fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6549ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6550fa9e4066Sahrens dsl_pool_sync(dp, txg); 6551fa9e4066Sahrens 655201f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 655369962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6554b24ab676SJeff Bonwick } else { 6555231aab85SMatthew Ahrens /* 6556231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6557231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6558231aab85SMatthew Ahrens */ 6559231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6560cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 656169962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6562fa9e4066Sahrens } 6563fa9e4066Sahrens 6564b24ab676SJeff Bonwick ddt_sync(spa, txg); 65653f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6566afee20e4SGeorge Wilson 6567*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) 6568*5cabbc6bSPrashanth Sreenivasa svr_sync(spa, tx); 6569*5cabbc6bSPrashanth Sreenivasa 6570*5cabbc6bSPrashanth Sreenivasa while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6571*5cabbc6bSPrashanth Sreenivasa != NULL) 6572b24ab676SJeff Bonwick vdev_sync(vd, txg); 6573b24ab676SJeff Bonwick 6574231aab85SMatthew Ahrens if (pass == 1) { 6575cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6576231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6577231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6578231aab85SMatthew Ahrens /* 6579231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6580231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6581231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6582231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6583231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6584231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6585231aab85SMatthew Ahrens * data (though in this case the MOS will most 6586231aab85SMatthew Ahrens * likely also be dirty due to second order 6587231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6588231aab85SMatthew Ahrens */ 6589231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6590231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6591231aab85SMatthew Ahrens /* 6592231aab85SMatthew Ahrens * Nothing changed on the first pass, 6593231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6594231aab85SMatthew Ahrens * syncing deferred frees, so that we 6595231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6596231aab85SMatthew Ahrens */ 6597231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6598231aab85SMatthew Ahrens txg)); 6599231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6600231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6601231aab85SMatthew Ahrens break; 6602231aab85SMatthew Ahrens } 6603231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6604231aab85SMatthew Ahrens } 6605fa9e4066Sahrens 6606cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6607fa9e4066Sahrens 6608215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 6609215198a6SJoe Stein /* 6610215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 6611215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 6612215198a6SJoe Stein * called if the config is dirty; otherwise there may be 6613215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 6614215198a6SJoe Stein * spa_sync_config_object. 6615215198a6SJoe Stein */ 6616215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 6617215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 6618215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 6619215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 6620215198a6SJoe Stein all_vdev_zap_entry_count); 6621215198a6SJoe Stein } 6622215198a6SJoe Stein 6623*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 6624*5cabbc6bSPrashanth Sreenivasa ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); 6625*5cabbc6bSPrashanth Sreenivasa } 6626*5cabbc6bSPrashanth Sreenivasa 6627fa9e4066Sahrens /* 6628fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6629fa9e4066Sahrens * to commit the transaction group. 66300373e76bSbonwick * 663117f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 663217f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6633e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6634e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 66350373e76bSbonwick */ 6636e14bb325SJeff Bonwick for (;;) { 6637e14bb325SJeff Bonwick /* 6638e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6639e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6640e14bb325SJeff Bonwick */ 6641e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6642e14bb325SJeff Bonwick 6643e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6644e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6645e14bb325SJeff Bonwick int svdcount = 0; 6646e14bb325SJeff Bonwick int children = rvd->vdev_children; 6647e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6648e14bb325SJeff Bonwick 6649573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6650e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6651*5cabbc6bSPrashanth Sreenivasa if (vd->vdev_ms_array == 0 || vd->vdev_islog || 6652*5cabbc6bSPrashanth Sreenivasa !vdev_is_concrete(vd)) 6653e14bb325SJeff Bonwick continue; 6654e14bb325SJeff Bonwick svd[svdcount++] = vd; 6655e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6656e14bb325SJeff Bonwick break; 6657e14bb325SJeff Bonwick } 6658eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 6659e14bb325SJeff Bonwick } else { 6660e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 6661eb5bb584SWill Andrews rvd->vdev_children, txg); 66620373e76bSbonwick } 6663e14bb325SJeff Bonwick 6664dfbb9432SGeorge Wilson if (error == 0) 6665dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6666dfbb9432SGeorge Wilson 6667e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6668e14bb325SJeff Bonwick 6669e14bb325SJeff Bonwick if (error == 0) 6670e14bb325SJeff Bonwick break; 6671e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6672e14bb325SJeff Bonwick zio_resume_wait(spa); 66730373e76bSbonwick } 667499653d4eSeschrock dmu_tx_commit(tx); 667599653d4eSeschrock 6676283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6677283b8460SGeorge.Wilson 66780373e76bSbonwick /* 66790373e76bSbonwick * Clear the dirty config list. 6680fa9e4066Sahrens */ 6681e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 66820373e76bSbonwick vdev_config_clean(vd); 66830373e76bSbonwick 66840373e76bSbonwick /* 66850373e76bSbonwick * Now that the new config has synced transactionally, 66860373e76bSbonwick * let it become visible to the config cache. 66870373e76bSbonwick */ 66880373e76bSbonwick if (spa->spa_config_syncing != NULL) { 66890373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 66900373e76bSbonwick spa->spa_config_txg = txg; 66910373e76bSbonwick spa->spa_config_syncing = NULL; 66920373e76bSbonwick } 6693fa9e4066Sahrens 6694b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6695fa9e4066Sahrens 66960f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 66970f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 66980f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 66990f7643c7SGeorge Wilson 6700fa9e4066Sahrens /* 6701fa9e4066Sahrens * Update usable space statistics. 6702fa9e4066Sahrens */ 6703fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6704fa9e4066Sahrens vdev_sync_done(vd, txg); 6705fa9e4066Sahrens 6706485bbbf5SGeorge Wilson spa_update_dspace(spa); 6707485bbbf5SGeorge Wilson 6708fa9e4066Sahrens /* 6709fa9e4066Sahrens * It had better be the case that we didn't dirty anything 671099653d4eSeschrock * since vdev_config_sync(). 6711fa9e4066Sahrens */ 6712fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6713fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6714fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6715b24ab676SJeff Bonwick 6716b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6717fa9e4066Sahrens 671843297f97SGeorge Wilson /* 671943297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 672043297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 672143297f97SGeorge Wilson * will be guaranteed that all the processing associated with 672243297f97SGeorge Wilson * that txg has been completed. 672343297f97SGeorge Wilson */ 672443297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 6725e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6726ea8dc4b6Seschrock 6727468c413aSTim Haley spa_handle_ignored_writes(spa); 6728468c413aSTim Haley 6729ea8dc4b6Seschrock /* 6730ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6731ea8dc4b6Seschrock */ 6732ea8dc4b6Seschrock spa_async_dispatch(spa); 6733fa9e4066Sahrens } 6734fa9e4066Sahrens 6735fa9e4066Sahrens /* 6736fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6737fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6738fa9e4066Sahrens * sync. 6739fa9e4066Sahrens */ 6740fa9e4066Sahrens void 6741fa9e4066Sahrens spa_sync_allpools(void) 6742fa9e4066Sahrens { 6743fa9e4066Sahrens spa_t *spa = NULL; 6744fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6745fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6746f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6747f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6748fa9e4066Sahrens continue; 6749fa9e4066Sahrens spa_open_ref(spa, FTAG); 6750fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6751fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6752fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6753fa9e4066Sahrens spa_close(spa, FTAG); 6754fa9e4066Sahrens } 6755fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6756fa9e4066Sahrens } 6757fa9e4066Sahrens 6758fa9e4066Sahrens /* 6759fa9e4066Sahrens * ========================================================================== 6760fa9e4066Sahrens * Miscellaneous routines 6761fa9e4066Sahrens * ========================================================================== 6762fa9e4066Sahrens */ 6763fa9e4066Sahrens 6764fa9e4066Sahrens /* 6765fa9e4066Sahrens * Remove all pools in the system. 6766fa9e4066Sahrens */ 6767fa9e4066Sahrens void 6768fa9e4066Sahrens spa_evict_all(void) 6769fa9e4066Sahrens { 6770fa9e4066Sahrens spa_t *spa; 6771fa9e4066Sahrens 6772fa9e4066Sahrens /* 6773fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6774fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6775fa9e4066Sahrens */ 6776fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6777fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6778fa9e4066Sahrens /* 6779ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6780ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6781ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6782fa9e4066Sahrens */ 6783fa9e4066Sahrens spa_open_ref(spa, FTAG); 6784fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6785ea8dc4b6Seschrock spa_async_suspend(spa); 6786fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6787fa9e4066Sahrens spa_close(spa, FTAG); 6788fa9e4066Sahrens 6789fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6790fa9e4066Sahrens spa_unload(spa); 6791fa9e4066Sahrens spa_deactivate(spa); 6792fa9e4066Sahrens } 6793fa9e4066Sahrens spa_remove(spa); 6794fa9e4066Sahrens } 6795fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6796fa9e4066Sahrens } 6797ea8dc4b6Seschrock 6798ea8dc4b6Seschrock vdev_t * 67996809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6800ea8dc4b6Seschrock { 6801c5904d13Seschrock vdev_t *vd; 6802c5904d13Seschrock int i; 6803c5904d13Seschrock 6804c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6805c5904d13Seschrock return (vd); 6806c5904d13Seschrock 68076809eb4eSEric Schrock if (aux) { 6808c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6809c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 68106809eb4eSEric Schrock if (vd->vdev_guid == guid) 68116809eb4eSEric Schrock return (vd); 68126809eb4eSEric Schrock } 68136809eb4eSEric Schrock 68146809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 68156809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6816c5904d13Seschrock if (vd->vdev_guid == guid) 6817c5904d13Seschrock return (vd); 6818c5904d13Seschrock } 6819c5904d13Seschrock } 6820c5904d13Seschrock 6821c5904d13Seschrock return (NULL); 6822ea8dc4b6Seschrock } 6823eaca9bbdSeschrock 6824eaca9bbdSeschrock void 6825990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6826eaca9bbdSeschrock { 6827f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6828f9af39baSGeorge Wilson 6829e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6830eaca9bbdSeschrock 6831eaca9bbdSeschrock /* 6832eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6833eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6834eaca9bbdSeschrock * possible. 6835eaca9bbdSeschrock */ 683662eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 68375d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6838eaca9bbdSeschrock 6839990b4856Slling spa->spa_uberblock.ub_version = version; 6840eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6841eaca9bbdSeschrock 6842e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 684399653d4eSeschrock 684499653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 684599653d4eSeschrock } 684699653d4eSeschrock 684799653d4eSeschrock boolean_t 684899653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 684999653d4eSeschrock { 685099653d4eSeschrock int i; 685139c23413Seschrock uint64_t spareguid; 6852fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 685399653d4eSeschrock 6854fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6855fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 685699653d4eSeschrock return (B_TRUE); 685799653d4eSeschrock 6858fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6859fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6860fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 686139c23413Seschrock return (B_TRUE); 686239c23413Seschrock } 686339c23413Seschrock 686499653d4eSeschrock return (B_FALSE); 6865eaca9bbdSeschrock } 6866b1b8ab34Slling 686789a89ebfSlling /* 686889a89ebfSlling * Check if a pool has an active shared spare device. 686989a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 687089a89ebfSlling */ 687189a89ebfSlling static boolean_t 687289a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 687389a89ebfSlling { 687489a89ebfSlling int i, refcnt; 687589a89ebfSlling uint64_t pool; 687689a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 687789a89ebfSlling 687889a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 687989a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 688089a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 688189a89ebfSlling refcnt > 2) 688289a89ebfSlling return (B_TRUE); 688389a89ebfSlling } 688489a89ebfSlling 688589a89ebfSlling return (B_FALSE); 688689a89ebfSlling } 688789a89ebfSlling 6888*5cabbc6bSPrashanth Sreenivasa sysevent_t * 6889ce1577b0SDave Eddy spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 68903d7072f8Seschrock { 6891b72b6bb1SAlan Somers sysevent_t *ev = NULL; 68923d7072f8Seschrock #ifdef _KERNEL 68933d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 68943d7072f8Seschrock sysevent_value_t value; 68953d7072f8Seschrock 68963d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 68973d7072f8Seschrock SE_SLEEP); 6898b72b6bb1SAlan Somers ASSERT(ev != NULL); 68993d7072f8Seschrock 69003d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69013d7072f8Seschrock value.value.sv_string = spa_name(spa); 69023d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 69033d7072f8Seschrock goto done; 69043d7072f8Seschrock 69053d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69063d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 69073d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 69083d7072f8Seschrock goto done; 69093d7072f8Seschrock 69103d7072f8Seschrock if (vd) { 69113d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69123d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 69133d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 69143d7072f8Seschrock SE_SLEEP) != 0) 69153d7072f8Seschrock goto done; 69163d7072f8Seschrock 69173d7072f8Seschrock if (vd->vdev_path) { 69183d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69193d7072f8Seschrock value.value.sv_string = vd->vdev_path; 69203d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 69213d7072f8Seschrock &value, SE_SLEEP) != 0) 69223d7072f8Seschrock goto done; 69233d7072f8Seschrock } 69243d7072f8Seschrock } 69253d7072f8Seschrock 6926ce1577b0SDave Eddy if (hist_nvl != NULL) { 6927ce1577b0SDave Eddy fnvlist_merge((nvlist_t *)attr, hist_nvl); 6928ce1577b0SDave Eddy } 6929ce1577b0SDave Eddy 6930b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6931b01c3b58Seschrock goto done; 6932b01c3b58Seschrock attr = NULL; 6933b01c3b58Seschrock 69343d7072f8Seschrock done: 69353d7072f8Seschrock if (attr) 69363d7072f8Seschrock sysevent_free_attr(attr); 6937b72b6bb1SAlan Somers 6938b72b6bb1SAlan Somers #endif 6939b72b6bb1SAlan Somers return (ev); 6940b72b6bb1SAlan Somers } 6941b72b6bb1SAlan Somers 6942*5cabbc6bSPrashanth Sreenivasa void 6943b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 6944b72b6bb1SAlan Somers { 6945b72b6bb1SAlan Somers #ifdef _KERNEL 6946b72b6bb1SAlan Somers sysevent_id_t eid; 6947b72b6bb1SAlan Somers 6948b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 69493d7072f8Seschrock sysevent_free(ev); 69503d7072f8Seschrock #endif 69513d7072f8Seschrock } 6952b72b6bb1SAlan Somers 6953*5cabbc6bSPrashanth Sreenivasa void 6954*5cabbc6bSPrashanth Sreenivasa spa_event_discard(sysevent_t *ev) 6955*5cabbc6bSPrashanth Sreenivasa { 6956*5cabbc6bSPrashanth Sreenivasa #ifdef _KERNEL 6957*5cabbc6bSPrashanth Sreenivasa sysevent_free(ev); 6958*5cabbc6bSPrashanth Sreenivasa #endif 6959*5cabbc6bSPrashanth Sreenivasa } 6960*5cabbc6bSPrashanth Sreenivasa 6961b72b6bb1SAlan Somers /* 6962b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 6963b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 6964ce1577b0SDave Eddy * filled in from the spa and (optionally) the vdev and history nvl. This 6965ce1577b0SDave Eddy * doesn't do anything in the userland libzpool, as we don't want consumers to 6966ce1577b0SDave Eddy * misinterpret ztest or zdb as real changes. 6967b72b6bb1SAlan Somers */ 6968b72b6bb1SAlan Somers void 6969ce1577b0SDave Eddy spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 6970b72b6bb1SAlan Somers { 6971ce1577b0SDave Eddy spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 6972b72b6bb1SAlan Somers } 6973