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. 24b7b2590dSMatthew Ahrens * Copyright (c) 2011, 2017 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> 305aeb9474SGarrett D'Amore */ 31fa9e4066Sahrens 32fa9e4066Sahrens /* 333e30c24aSWill Andrews * SPA: Storage Pool Allocator 343e30c24aSWill Andrews * 35fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 36fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 37fa9e4066Sahrens * pool. 38fa9e4066Sahrens */ 39fa9e4066Sahrens 40fa9e4066Sahrens #include <sys/zfs_context.h> 41ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 42fa9e4066Sahrens #include <sys/spa_impl.h> 43fa9e4066Sahrens #include <sys/zio.h> 44fa9e4066Sahrens #include <sys/zio_checksum.h> 45fa9e4066Sahrens #include <sys/dmu.h> 46fa9e4066Sahrens #include <sys/dmu_tx.h> 47fa9e4066Sahrens #include <sys/zap.h> 48fa9e4066Sahrens #include <sys/zil.h> 49b24ab676SJeff Bonwick #include <sys/ddt.h> 50fa9e4066Sahrens #include <sys/vdev_impl.h> 51fa9e4066Sahrens #include <sys/metaslab.h> 5288ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 53fa9e4066Sahrens #include <sys/uberblock_impl.h> 54fa9e4066Sahrens #include <sys/txg.h> 55fa9e4066Sahrens #include <sys/avl.h> 56fa9e4066Sahrens #include <sys/dmu_traverse.h> 57b1b8ab34Slling #include <sys/dmu_objset.h> 58fa9e4066Sahrens #include <sys/unique.h> 59fa9e4066Sahrens #include <sys/dsl_pool.h> 60b1b8ab34Slling #include <sys/dsl_dataset.h> 61fa9e4066Sahrens #include <sys/dsl_dir.h> 62fa9e4066Sahrens #include <sys/dsl_prop.h> 63b1b8ab34Slling #include <sys/dsl_synctask.h> 64fa9e4066Sahrens #include <sys/fs/zfs.h> 65fa94a07fSbrendan #include <sys/arc.h> 66fa9e4066Sahrens #include <sys/callb.h> 6795173954Sek #include <sys/systeminfo.h> 68e7cbe64fSgw #include <sys/spa_boot.h> 69573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 703f9d6ad7SLin Ling #include <sys/dsl_scan.h> 71ad135b5dSChristopher Siden #include <sys/zfeature.h> 723b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 73*770499e1SDan Kimmel #include <sys/abd.h> 74fa9e4066Sahrens 755679c89fSjv #ifdef _KERNEL 76dedec472SJack Meng #include <sys/bootprops.h> 7735a5a358SJonathan Adams #include <sys/callb.h> 7835a5a358SJonathan Adams #include <sys/cpupart.h> 7935a5a358SJonathan Adams #include <sys/pool.h> 8035a5a358SJonathan Adams #include <sys/sysdc.h> 8135a5a358SJonathan Adams #include <sys/zone.h> 825679c89fSjv #endif /* _KERNEL */ 835679c89fSjv 84990b4856Slling #include "zfs_prop.h" 85b7b97454Sperrin #include "zfs_comutil.h" 86990b4856Slling 873cb69f73SWill Andrews /* 883cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 893cb69f73SWill Andrews * should be retried. 903cb69f73SWill Andrews */ 913cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300; 923cb69f73SWill Andrews 9335a5a358SJonathan Adams typedef enum zti_modes { 94ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 95ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 96ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 97ec94d322SAdam Leventhal ZTI_NMODES 9835a5a358SJonathan Adams } zti_modes_t; 99416e0cd8Sek 100ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 101ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 102ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1032e0c549eSJonathan Adams 104ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 105ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1062e0c549eSJonathan Adams 1072e0c549eSJonathan Adams typedef struct zio_taskq_info { 108ec94d322SAdam Leventhal zti_modes_t zti_mode; 10980eb36f2SGeorge Wilson uint_t zti_value; 110ec94d322SAdam Leventhal uint_t zti_count; 1112e0c549eSJonathan Adams } zio_taskq_info_t; 1122e0c549eSJonathan Adams 1132e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 11435a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1152e0c549eSJonathan Adams }; 1162e0c549eSJonathan Adams 11780eb36f2SGeorge Wilson /* 118ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 119ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 120ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 121ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 122ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 123ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 124ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 125ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 126ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 127ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 128ec94d322SAdam Leventhal * particular taskq is chosen at random. 129ec94d322SAdam Leventhal * 130ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 131ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 132ec94d322SAdam Leventhal * need to be handled with minimum delay. 13380eb36f2SGeorge Wilson */ 13480eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 13580eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 136ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1371b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 138ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 139ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 140ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 141ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1422e0c549eSJonathan Adams }; 1432e0c549eSJonathan Adams 144b72b6bb1SAlan Somers static sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, const char *name); 145b72b6bb1SAlan Somers static void spa_event_post(sysevent_t *ev); 1463b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1473b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 14889a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1491195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1501195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1511195e687SMark J Musante char **ereport); 152cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 153990b4856Slling 15469962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15535a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 15635a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 15735a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 15835a5a358SJonathan Adams 15935a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 16001f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16135a5a358SJonathan Adams 16235a5a358SJonathan Adams /* 16335a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 16435a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 16535a5a358SJonathan Adams */ 16635a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 16735a5a358SJonathan Adams 168990b4856Slling /* 169990b4856Slling * ========================================================================== 170990b4856Slling * SPA properties routines 171990b4856Slling * ========================================================================== 172990b4856Slling */ 173990b4856Slling 174990b4856Slling /* 175990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 176990b4856Slling */ 1779d82f4f6Slling static void 178990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 179990b4856Slling uint64_t intval, zprop_source_t src) 180990b4856Slling { 181990b4856Slling const char *propname = zpool_prop_to_name(prop); 182990b4856Slling nvlist_t *propval; 183990b4856Slling 1849d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1859d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 186990b4856Slling 1879d82f4f6Slling if (strval != NULL) 1889d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1899d82f4f6Slling else 1909d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 191990b4856Slling 1929d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 193990b4856Slling nvlist_free(propval); 194990b4856Slling } 195990b4856Slling 196990b4856Slling /* 197990b4856Slling * Get property values from the spa configuration. 198990b4856Slling */ 1999d82f4f6Slling static void 200990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 201990b4856Slling { 2024263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 203ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2042e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 205990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 206c5904d13Seschrock spa_config_dirent_t *dp; 2072e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 208990b4856Slling 209e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 210e14bb325SJeff Bonwick 2114263d13fSGeorge Wilson if (rvd != NULL) { 212485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 213b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 214379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 215379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 216485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 217485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 218485bbbf5SGeorge Wilson size - alloc, src); 2194263d13fSGeorge Wilson 2202e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2212e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2222e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2232e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 224f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 225f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 226379c004dSEric Schrock 227485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 228379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 229379c004dSEric Schrock 230b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 231b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 232b24ab676SJeff Bonwick 233379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2344263d13fSGeorge Wilson rvd->vdev_state, src); 235379c004dSEric Schrock 236379c004dSEric Schrock version = spa_version(spa); 237379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 238379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 239379c004dSEric Schrock else 240379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 241379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 242379c004dSEric Schrock } 243990b4856Slling 244ad135b5dSChristopher Siden if (pool != NULL) { 245ad135b5dSChristopher Siden /* 246ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 247ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 248ad135b5dSChristopher Siden */ 2497fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 250ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 251c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 252c1379625SJustin T. Gibbs src); 253ad135b5dSChristopher Siden } else { 254ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 255ad135b5dSChristopher Siden NULL, 0, src); 256ad135b5dSChristopher Siden } 2577fd05ac4SMatthew Ahrens 2587fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2597fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 260c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 261c1379625SJustin T. Gibbs src); 2627fd05ac4SMatthew Ahrens } else { 2637fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2647fd05ac4SMatthew Ahrens NULL, 0, src); 2657fd05ac4SMatthew Ahrens } 266ad135b5dSChristopher Siden } 267ad135b5dSChristopher Siden 2689d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 269990b4856Slling 2708704186eSDan McDonald if (spa->spa_comment != NULL) { 2718704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2728704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2738704186eSDan McDonald } 2748704186eSDan McDonald 2759d82f4f6Slling if (spa->spa_root != NULL) 2769d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2779d82f4f6Slling 0, ZPROP_SRC_LOCAL); 278990b4856Slling 279b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 280b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 281b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 282b5152584SMatthew Ahrens } else { 283b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 284b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 285b5152584SMatthew Ahrens } 286b5152584SMatthew Ahrens 287c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 288c5904d13Seschrock if (dp->scd_path == NULL) { 2899d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 290c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 291c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2929d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 293c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2942f8aaab3Seschrock } 2952f8aaab3Seschrock } 296990b4856Slling } 297990b4856Slling 298990b4856Slling /* 299990b4856Slling * Get zpool property values. 300990b4856Slling */ 301990b4856Slling int 302990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 303990b4856Slling { 304b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 305990b4856Slling zap_cursor_t zc; 306990b4856Slling zap_attribute_t za; 307990b4856Slling int err; 308990b4856Slling 3099d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 310990b4856Slling 311e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 312e14bb325SJeff Bonwick 313990b4856Slling /* 314990b4856Slling * Get properties from the spa config. 315990b4856Slling */ 3169d82f4f6Slling spa_prop_get_config(spa, nvp); 317990b4856Slling 318990b4856Slling /* If no pool property object, no more prop to get. */ 319afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 320990b4856Slling mutex_exit(&spa->spa_props_lock); 321990b4856Slling return (0); 322990b4856Slling } 323990b4856Slling 324990b4856Slling /* 325990b4856Slling * Get properties from the MOS pool property object. 326990b4856Slling */ 327990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 328990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 329990b4856Slling zap_cursor_advance(&zc)) { 330990b4856Slling uint64_t intval = 0; 331990b4856Slling char *strval = NULL; 332990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 333990b4856Slling zpool_prop_t prop; 334990b4856Slling 335990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 336990b4856Slling continue; 337990b4856Slling 338990b4856Slling switch (za.za_integer_length) { 339990b4856Slling case 8: 340990b4856Slling /* integer property */ 341990b4856Slling if (za.za_first_integer != 342990b4856Slling zpool_prop_default_numeric(prop)) 343990b4856Slling src = ZPROP_SRC_LOCAL; 344990b4856Slling 345990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 346990b4856Slling dsl_pool_t *dp; 347990b4856Slling dsl_dataset_t *ds = NULL; 348990b4856Slling 349990b4856Slling dp = spa_get_dsl(spa); 3503b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 351745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 352745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3533b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 354990b4856Slling break; 355990b4856Slling } 356990b4856Slling 3579adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 358990b4856Slling KM_SLEEP); 359990b4856Slling dsl_dataset_name(ds, strval); 360745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3613b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 362990b4856Slling } else { 363990b4856Slling strval = NULL; 364990b4856Slling intval = za.za_first_integer; 365990b4856Slling } 366990b4856Slling 3679d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 368990b4856Slling 369990b4856Slling if (strval != NULL) 3709adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 371990b4856Slling 372990b4856Slling break; 373990b4856Slling 374990b4856Slling case 1: 375990b4856Slling /* string property */ 376990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 377990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 378990b4856Slling za.za_name, 1, za.za_num_integers, strval); 379990b4856Slling if (err) { 380990b4856Slling kmem_free(strval, za.za_num_integers); 381990b4856Slling break; 382990b4856Slling } 3839d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 384990b4856Slling kmem_free(strval, za.za_num_integers); 385990b4856Slling break; 386990b4856Slling 387990b4856Slling default: 388990b4856Slling break; 389990b4856Slling } 390990b4856Slling } 391990b4856Slling zap_cursor_fini(&zc); 392990b4856Slling mutex_exit(&spa->spa_props_lock); 393990b4856Slling out: 394990b4856Slling if (err && err != ENOENT) { 395990b4856Slling nvlist_free(*nvp); 3969d82f4f6Slling *nvp = NULL; 397990b4856Slling return (err); 398990b4856Slling } 399990b4856Slling 400990b4856Slling return (0); 401990b4856Slling } 402990b4856Slling 403990b4856Slling /* 404990b4856Slling * Validate the given pool properties nvlist and modify the list 405990b4856Slling * for the property values to be set. 406990b4856Slling */ 407990b4856Slling static int 408990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 409990b4856Slling { 410990b4856Slling nvpair_t *elem; 411990b4856Slling int error = 0, reset_bootfs = 0; 412d5285caeSGeorge Wilson uint64_t objnum = 0; 413ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 414990b4856Slling 415990b4856Slling elem = NULL; 416990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 417990b4856Slling uint64_t intval; 418ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 419ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 420ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 421ad135b5dSChristopher Siden 422ad135b5dSChristopher Siden switch (prop) { 423ad135b5dSChristopher Siden case ZPROP_INVAL: 424ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 425be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 426ad135b5dSChristopher Siden break; 427ad135b5dSChristopher Siden } 428990b4856Slling 429ad135b5dSChristopher Siden /* 430ad135b5dSChristopher Siden * Sanitize the input. 431ad135b5dSChristopher Siden */ 432ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 433be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 434ad135b5dSChristopher Siden break; 435ad135b5dSChristopher Siden } 436990b4856Slling 437ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 438be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 439ad135b5dSChristopher Siden break; 440ad135b5dSChristopher Siden } 441ad135b5dSChristopher Siden 442ad135b5dSChristopher Siden if (intval != 0) { 443be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 444ad135b5dSChristopher Siden break; 445ad135b5dSChristopher Siden } 446ad135b5dSChristopher Siden 447ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 448ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 449be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 450ad135b5dSChristopher Siden break; 451ad135b5dSChristopher Siden } 452ad135b5dSChristopher Siden 453ad135b5dSChristopher Siden has_feature = B_TRUE; 454ad135b5dSChristopher Siden break; 455990b4856Slling 456990b4856Slling case ZPOOL_PROP_VERSION: 457990b4856Slling error = nvpair_value_uint64(elem, &intval); 458990b4856Slling if (!error && 459ad135b5dSChristopher Siden (intval < spa_version(spa) || 460ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 461ad135b5dSChristopher Siden has_feature)) 462be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 463990b4856Slling break; 464990b4856Slling 465990b4856Slling case ZPOOL_PROP_DELEGATION: 466990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 467d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 468573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 469990b4856Slling error = nvpair_value_uint64(elem, &intval); 470990b4856Slling if (!error && intval > 1) 471be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 472990b4856Slling break; 473990b4856Slling 474990b4856Slling case ZPOOL_PROP_BOOTFS: 47525f89ee2SJeff Bonwick /* 47625f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 47725f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 47825f89ee2SJeff Bonwick * the bootfs property cannot be set. 47925f89ee2SJeff Bonwick */ 480990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 481be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 482990b4856Slling break; 483990b4856Slling } 484990b4856Slling 485990b4856Slling /* 48615e6edf1Sgw * Make sure the vdev config is bootable 487990b4856Slling */ 48815e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 489be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 490990b4856Slling break; 491990b4856Slling } 492990b4856Slling 493990b4856Slling reset_bootfs = 1; 494990b4856Slling 495990b4856Slling error = nvpair_value_string(elem, &strval); 496990b4856Slling 497990b4856Slling if (!error) { 498ad135b5dSChristopher Siden objset_t *os; 499b5152584SMatthew Ahrens uint64_t propval; 50015e6edf1Sgw 501990b4856Slling if (strval == NULL || strval[0] == '\0') { 502990b4856Slling objnum = zpool_prop_default_numeric( 503990b4856Slling ZPOOL_PROP_BOOTFS); 504990b4856Slling break; 505990b4856Slling } 506990b4856Slling 507503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 508990b4856Slling break; 50915e6edf1Sgw 510b5152584SMatthew Ahrens /* 511b5152584SMatthew Ahrens * Must be ZPL, and its property settings 512b5152584SMatthew Ahrens * must be supported by GRUB (compression 513b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 514b5152584SMatthew Ahrens */ 515503ad85cSMatthew Ahrens 516503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 517be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5183b2aab18SMatthew Ahrens } else if ((error = 5193b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 52015e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 521b5152584SMatthew Ahrens &propval)) == 0 && 522b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 523b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 52415e6edf1Sgw } else { 52515e6edf1Sgw objnum = dmu_objset_id(os); 52615e6edf1Sgw } 527503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 528990b4856Slling } 529990b4856Slling break; 530e14bb325SJeff Bonwick 5310a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5320a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5330a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5340a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 535be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5360a4e9518Sgw 5370a4e9518Sgw /* 5380a4e9518Sgw * This is a special case which only occurs when 5390a4e9518Sgw * the pool has completely failed. This allows 5400a4e9518Sgw * the user to change the in-core failmode property 5410a4e9518Sgw * without syncing it out to disk (I/Os might 5420a4e9518Sgw * currently be blocked). We do this by returning 5430a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5440a4e9518Sgw * into thinking we encountered a property validation 5450a4e9518Sgw * error. 5460a4e9518Sgw */ 547e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5480a4e9518Sgw spa->spa_failmode = intval; 549be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5500a4e9518Sgw } 5510a4e9518Sgw break; 5522f8aaab3Seschrock 5532f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5542f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5552f8aaab3Seschrock break; 5562f8aaab3Seschrock 5572f8aaab3Seschrock if (strval[0] == '\0') 5582f8aaab3Seschrock break; 5592f8aaab3Seschrock 5602f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5612f8aaab3Seschrock break; 5622f8aaab3Seschrock 5632f8aaab3Seschrock if (strval[0] != '/') { 564be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5652f8aaab3Seschrock break; 5662f8aaab3Seschrock } 5672f8aaab3Seschrock 5682f8aaab3Seschrock slash = strrchr(strval, '/'); 5692f8aaab3Seschrock ASSERT(slash != NULL); 5702f8aaab3Seschrock 5712f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5722f8aaab3Seschrock strcmp(slash, "/..") == 0) 573be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5742f8aaab3Seschrock break; 575b24ab676SJeff Bonwick 5768704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5778704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5788704186eSDan McDonald break; 5798704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5808704186eSDan McDonald /* 5818704186eSDan McDonald * The kernel doesn't have an easy isprint() 5828704186eSDan McDonald * check. For this kernel check, we merely 5838704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5848704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5858704186eSDan McDonald */ 5868704186eSDan McDonald if (*check >= 0x7f) { 587be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5888704186eSDan McDonald break; 5898704186eSDan McDonald } 5908704186eSDan McDonald } 5918704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5928704186eSDan McDonald error = E2BIG; 5938704186eSDan McDonald break; 5948704186eSDan McDonald 595b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 596b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 597be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 598b24ab676SJeff Bonwick else 599b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 600b24ab676SJeff Bonwick if (error == 0 && 601b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 602be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 603b24ab676SJeff Bonwick break; 604990b4856Slling } 605990b4856Slling 606990b4856Slling if (error) 607990b4856Slling break; 608990b4856Slling } 609990b4856Slling 610990b4856Slling if (!error && reset_bootfs) { 611990b4856Slling error = nvlist_remove(props, 612990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 613990b4856Slling 614990b4856Slling if (!error) { 615990b4856Slling error = nvlist_add_uint64(props, 616990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 617990b4856Slling } 618990b4856Slling } 619990b4856Slling 620990b4856Slling return (error); 621990b4856Slling } 622990b4856Slling 623379c004dSEric Schrock void 624379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 625379c004dSEric Schrock { 626379c004dSEric Schrock char *cachefile; 627379c004dSEric Schrock spa_config_dirent_t *dp; 628379c004dSEric Schrock 629379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 630379c004dSEric Schrock &cachefile) != 0) 631379c004dSEric Schrock return; 632379c004dSEric Schrock 633379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 634379c004dSEric Schrock KM_SLEEP); 635379c004dSEric Schrock 636379c004dSEric Schrock if (cachefile[0] == '\0') 637379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 638379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 639379c004dSEric Schrock dp->scd_path = NULL; 640379c004dSEric Schrock else 641379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 642379c004dSEric Schrock 643379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 644379c004dSEric Schrock if (need_sync) 645379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 646379c004dSEric Schrock } 647379c004dSEric Schrock 648990b4856Slling int 649990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 650990b4856Slling { 651990b4856Slling int error; 652ad135b5dSChristopher Siden nvpair_t *elem = NULL; 653379c004dSEric Schrock boolean_t need_sync = B_FALSE; 654990b4856Slling 655990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 656990b4856Slling return (error); 657990b4856Slling 658379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 659ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 660379c004dSEric Schrock 661f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 662f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 663f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 664379c004dSEric Schrock continue; 665379c004dSEric Schrock 666ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 667ad135b5dSChristopher Siden uint64_t ver; 668ad135b5dSChristopher Siden 669ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 670ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 671ad135b5dSChristopher Siden } else { 672ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 673ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 674ad135b5dSChristopher Siden need_sync = B_TRUE; 675ad135b5dSChristopher Siden } 676ad135b5dSChristopher Siden 677ad135b5dSChristopher Siden /* Save time if the version is already set. */ 678ad135b5dSChristopher Siden if (ver == spa_version(spa)) 679ad135b5dSChristopher Siden continue; 680ad135b5dSChristopher Siden 681ad135b5dSChristopher Siden /* 682ad135b5dSChristopher Siden * In addition to the pool directory object, we might 683ad135b5dSChristopher Siden * create the pool properties object, the features for 684ad135b5dSChristopher Siden * read object, the features for write object, or the 685ad135b5dSChristopher Siden * feature descriptions object. 686ad135b5dSChristopher Siden */ 6873b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6887d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6897d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 690ad135b5dSChristopher Siden if (error) 691ad135b5dSChristopher Siden return (error); 692ad135b5dSChristopher Siden continue; 693ad135b5dSChristopher Siden } 694ad135b5dSChristopher Siden 695379c004dSEric Schrock need_sync = B_TRUE; 696379c004dSEric Schrock break; 697379c004dSEric Schrock } 698379c004dSEric Schrock 699ad135b5dSChristopher Siden if (need_sync) { 7003b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7017d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 702ad135b5dSChristopher Siden } 703ad135b5dSChristopher Siden 704ad135b5dSChristopher Siden return (0); 705990b4856Slling } 706990b4856Slling 707990b4856Slling /* 708990b4856Slling * If the bootfs property value is dsobj, clear it. 709990b4856Slling */ 710990b4856Slling void 711990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 712990b4856Slling { 713990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 714990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 715990b4856Slling spa->spa_pool_props_object, 716990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 717990b4856Slling spa->spa_bootfs = 0; 718990b4856Slling } 719990b4856Slling } 720990b4856Slling 721dfbb9432SGeorge Wilson /*ARGSUSED*/ 722dfbb9432SGeorge Wilson static int 7233b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 724dfbb9432SGeorge Wilson { 7253b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7263b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 727dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 728dfbb9432SGeorge Wilson uint64_t vdev_state; 729dfbb9432SGeorge Wilson 730dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 731dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 732dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 733dfbb9432SGeorge Wilson 734dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 735be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 736dfbb9432SGeorge Wilson 737dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 738dfbb9432SGeorge Wilson 739dfbb9432SGeorge Wilson return (0); 740dfbb9432SGeorge Wilson } 741dfbb9432SGeorge Wilson 742dfbb9432SGeorge Wilson static void 7433b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 744dfbb9432SGeorge Wilson { 7453b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7463b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 747dfbb9432SGeorge Wilson uint64_t oldguid; 748dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 749dfbb9432SGeorge Wilson 750dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 751dfbb9432SGeorge Wilson 752dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 753dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 754dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 755dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 756dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 757dfbb9432SGeorge Wilson 75820128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 759dfbb9432SGeorge Wilson oldguid, *newguid); 760dfbb9432SGeorge Wilson } 761dfbb9432SGeorge Wilson 762e9103aaeSGarrett D'Amore /* 763e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 764e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 765e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 766e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 767e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 768e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 769e9103aaeSGarrett D'Amore * sysevent to update any watchers. 770e9103aaeSGarrett D'Amore */ 771e9103aaeSGarrett D'Amore int 772e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 773e9103aaeSGarrett D'Amore { 774dfbb9432SGeorge Wilson int error; 775dfbb9432SGeorge Wilson uint64_t guid; 776e9103aaeSGarrett D'Amore 7772c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 778dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 779dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 780e9103aaeSGarrett D'Amore 7813b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7827d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 783e9103aaeSGarrett D'Amore 784dfbb9432SGeorge Wilson if (error == 0) { 785dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 786dfbb9432SGeorge Wilson spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); 787dfbb9432SGeorge Wilson } 788e9103aaeSGarrett D'Amore 789dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7902c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 791e9103aaeSGarrett D'Amore 792dfbb9432SGeorge Wilson return (error); 793e9103aaeSGarrett D'Amore } 794e9103aaeSGarrett D'Amore 795fa9e4066Sahrens /* 796fa9e4066Sahrens * ========================================================================== 797fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 798fa9e4066Sahrens * ========================================================================== 799fa9e4066Sahrens */ 800fa9e4066Sahrens 801ea8dc4b6Seschrock static int 802ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 803ea8dc4b6Seschrock { 804ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 805ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 806ea8dc4b6Seschrock int ret; 807ea8dc4b6Seschrock 808ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8097802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 810ea8dc4b6Seschrock 811ea8dc4b6Seschrock if (ret < 0) 812ea8dc4b6Seschrock return (-1); 813ea8dc4b6Seschrock else if (ret > 0) 814ea8dc4b6Seschrock return (1); 815ea8dc4b6Seschrock else 816ea8dc4b6Seschrock return (0); 817ea8dc4b6Seschrock } 818ea8dc4b6Seschrock 819ea8dc4b6Seschrock /* 820ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 821ea8dc4b6Seschrock * re-initializes them in the process. 822ea8dc4b6Seschrock */ 823ea8dc4b6Seschrock void 824ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 825ea8dc4b6Seschrock { 826ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 827ea8dc4b6Seschrock 828ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 829ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 830ea8dc4b6Seschrock 831ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 832ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 833ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 834ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 835ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 836ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 837ea8dc4b6Seschrock } 838ea8dc4b6Seschrock 839ec94d322SAdam Leventhal static void 840ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 841fa9e4066Sahrens { 842ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 843ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 844ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 845ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 846ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 847ec94d322SAdam Leventhal char name[32]; 8485aeb9474SGarrett D'Amore uint_t flags = 0; 84935a5a358SJonathan Adams boolean_t batch = B_FALSE; 850fa9e4066Sahrens 851ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 852ec94d322SAdam Leventhal tqs->stqs_count = 0; 853ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 854ec94d322SAdam Leventhal return; 855ec94d322SAdam Leventhal } 856fa9e4066Sahrens 857ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 858fa9e4066Sahrens 859ec94d322SAdam Leventhal tqs->stqs_count = count; 860ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86135a5a358SJonathan Adams 86269962b56SMatthew Ahrens switch (mode) { 86369962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86469962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86569962b56SMatthew Ahrens value = MAX(value, 1); 86669962b56SMatthew Ahrens break; 867ec94d322SAdam Leventhal 86869962b56SMatthew Ahrens case ZTI_MODE_BATCH: 86969962b56SMatthew Ahrens batch = B_TRUE; 87069962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87169962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87269962b56SMatthew Ahrens break; 873ec94d322SAdam Leventhal 87469962b56SMatthew Ahrens default: 87569962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 87669962b56SMatthew Ahrens "spa_activate()", 87769962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 87869962b56SMatthew Ahrens break; 87969962b56SMatthew Ahrens } 880ec94d322SAdam Leventhal 88169962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88269962b56SMatthew Ahrens taskq_t *tq; 883ec94d322SAdam Leventhal 884ec94d322SAdam Leventhal if (count > 1) { 885ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 886ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 887ec94d322SAdam Leventhal } else { 888ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 889ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 890ec94d322SAdam Leventhal } 891ec94d322SAdam Leventhal 892ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 893ec94d322SAdam Leventhal if (batch) 894ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 895ec94d322SAdam Leventhal 896ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 897ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 898ec94d322SAdam Leventhal } else { 89969962b56SMatthew Ahrens pri_t pri = maxclsyspri; 90069962b56SMatthew Ahrens /* 90169962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90269962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90369962b56SMatthew Ahrens * than the other taskqs. 90469962b56SMatthew Ahrens */ 90569962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 90669962b56SMatthew Ahrens pri--; 90769962b56SMatthew Ahrens 90869962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 909ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 910ec94d322SAdam Leventhal } 911ec94d322SAdam Leventhal 912ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 913ec94d322SAdam Leventhal } 914ec94d322SAdam Leventhal } 915ec94d322SAdam Leventhal 916ec94d322SAdam Leventhal static void 917ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 918ec94d322SAdam Leventhal { 919ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 920ec94d322SAdam Leventhal 921ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 922ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 923ec94d322SAdam Leventhal return; 924ec94d322SAdam Leventhal } 925ec94d322SAdam Leventhal 926ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 927ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 928ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 92935a5a358SJonathan Adams } 93035a5a358SJonathan Adams 931ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 932ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 933ec94d322SAdam Leventhal } 93435a5a358SJonathan Adams 935ec94d322SAdam Leventhal /* 936ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 937ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 938ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 939ec94d322SAdam Leventhal * the low bits of gethrtime(). 940ec94d322SAdam Leventhal */ 941ec94d322SAdam Leventhal void 942ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 943ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 944ec94d322SAdam Leventhal { 945ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 946ec94d322SAdam Leventhal taskq_t *tq; 947ec94d322SAdam Leventhal 948ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 949ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 950ec94d322SAdam Leventhal 951ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 952ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 953ec94d322SAdam Leventhal } else { 954ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95535a5a358SJonathan Adams } 956ec94d322SAdam Leventhal 957ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 95835a5a358SJonathan Adams } 95935a5a358SJonathan Adams 96035a5a358SJonathan Adams static void 96135a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96235a5a358SJonathan Adams { 963e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 964e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 965ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 96635a5a358SJonathan Adams } 96735a5a358SJonathan Adams } 96835a5a358SJonathan Adams } 96935a5a358SJonathan Adams 97035a5a358SJonathan Adams #ifdef _KERNEL 97135a5a358SJonathan Adams static void 97235a5a358SJonathan Adams spa_thread(void *arg) 97335a5a358SJonathan Adams { 97435a5a358SJonathan Adams callb_cpr_t cprinfo; 9752e0c549eSJonathan Adams 97635a5a358SJonathan Adams spa_t *spa = arg; 97735a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9782e0c549eSJonathan Adams 97935a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 98035a5a358SJonathan Adams spa->spa_name); 9812e0c549eSJonathan Adams 98235a5a358SJonathan Adams ASSERT(curproc != &p0); 98335a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98435a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98535a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9862e0c549eSJonathan Adams 98735a5a358SJonathan Adams /* bind this thread to the requested psrset */ 98835a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 98935a5a358SJonathan Adams pool_lock(); 99035a5a358SJonathan Adams mutex_enter(&cpu_lock); 99135a5a358SJonathan Adams mutex_enter(&pidlock); 99235a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99380eb36f2SGeorge Wilson 99435a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99535a5a358SJonathan Adams 0, NULL, NULL) == 0) { 99635a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 99735a5a358SJonathan Adams } else { 99835a5a358SJonathan Adams cmn_err(CE_WARN, 99935a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 100035a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100135a5a358SJonathan Adams } 100235a5a358SJonathan Adams 100335a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100435a5a358SJonathan Adams mutex_exit(&pidlock); 100535a5a358SJonathan Adams mutex_exit(&cpu_lock); 100635a5a358SJonathan Adams pool_unlock(); 100735a5a358SJonathan Adams } 100835a5a358SJonathan Adams 100935a5a358SJonathan Adams if (zio_taskq_sysdc) { 101035a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101135a5a358SJonathan Adams } 101235a5a358SJonathan Adams 101335a5a358SJonathan Adams spa->spa_proc = curproc; 101435a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101535a5a358SJonathan Adams 101635a5a358SJonathan Adams spa_create_zio_taskqs(spa); 101735a5a358SJonathan Adams 101835a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 101935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 102035a5a358SJonathan Adams 102135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102335a5a358SJonathan Adams 102435a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 102635a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 102735a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 102835a5a358SJonathan Adams 102935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 103035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103135a5a358SJonathan Adams spa->spa_proc = &p0; 103235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103335a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103435a5a358SJonathan Adams 103535a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 103635a5a358SJonathan Adams lwp_exit(); 103735a5a358SJonathan Adams } 103835a5a358SJonathan Adams #endif 103935a5a358SJonathan Adams 104035a5a358SJonathan Adams /* 104135a5a358SJonathan Adams * Activate an uninitialized pool. 104235a5a358SJonathan Adams */ 104335a5a358SJonathan Adams static void 104435a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104535a5a358SJonathan Adams { 104635a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 104735a5a358SJonathan Adams 104835a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 104935a5a358SJonathan Adams spa->spa_mode = mode; 105035a5a358SJonathan Adams 105135a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105235a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105335a5a358SJonathan Adams 105435a5a358SJonathan Adams /* Try to create a covering process */ 105535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 105635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 105735a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 105835a5a358SJonathan Adams spa->spa_did = 0; 105935a5a358SJonathan Adams 106035a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106135a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106235a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106335a5a358SJonathan Adams NULL, 0) == 0) { 106435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 106635a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 106735a5a358SJonathan Adams &spa->spa_proc_lock); 10682e0c549eSJonathan Adams } 106935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 107035a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107135a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107235a5a358SJonathan Adams } else { 107335a5a358SJonathan Adams #ifdef _KERNEL 107435a5a358SJonathan Adams cmn_err(CE_WARN, 107535a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 107635a5a358SJonathan Adams spa->spa_name); 107735a5a358SJonathan Adams #endif 1078e14bb325SJeff Bonwick } 1079fa9e4066Sahrens } 108035a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108135a5a358SJonathan Adams 108235a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108335a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108435a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108535a5a358SJonathan Adams } 1086fa9e4066Sahrens 1087e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1088e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1089bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1090bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1091e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1092e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1093fa9e4066Sahrens 1094b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1095fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1096ea8dc4b6Seschrock 1097ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1098ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1099ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1100ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1101ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1102ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1103fa9e4066Sahrens } 1104fa9e4066Sahrens 1105fa9e4066Sahrens /* 1106fa9e4066Sahrens * Opposite of spa_activate(). 1107fa9e4066Sahrens */ 1108fa9e4066Sahrens static void 1109fa9e4066Sahrens spa_deactivate(spa_t *spa) 1110fa9e4066Sahrens { 1111fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1112fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1113fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 111425f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1115fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1116fa9e4066Sahrens 1117bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1118bc9014e6SJustin Gibbs 1119fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1120fa9e4066Sahrens 1121e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1122bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1123e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1124fa9e4066Sahrens 1125e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1126e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1127ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1128e14bb325SJeff Bonwick } 1129fa9e4066Sahrens } 1130fa9e4066Sahrens 1131fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1132fa9e4066Sahrens spa->spa_normal_class = NULL; 1133fa9e4066Sahrens 11348654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11358654d025Sperrin spa->spa_log_class = NULL; 11368654d025Sperrin 1137ea8dc4b6Seschrock /* 1138ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1139ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1140ea8dc4b6Seschrock */ 1141ea8dc4b6Seschrock spa_errlog_drain(spa); 1142ea8dc4b6Seschrock 1143ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1144ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1145ea8dc4b6Seschrock 1146fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 114735a5a358SJonathan Adams 114835a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 114935a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 115035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 115135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 115235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 115335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 115435a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 115535a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 115635a5a358SJonathan Adams } 115735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 115835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 115935a5a358SJonathan Adams } 116035a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 116135a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 116235a5a358SJonathan Adams 116335a5a358SJonathan Adams /* 116435a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 116535a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 116635a5a358SJonathan Adams * it. 116735a5a358SJonathan Adams */ 116835a5a358SJonathan Adams if (spa->spa_did != 0) { 116935a5a358SJonathan Adams thread_join(spa->spa_did); 117035a5a358SJonathan Adams spa->spa_did = 0; 117135a5a358SJonathan Adams } 1172fa9e4066Sahrens } 1173fa9e4066Sahrens 1174fa9e4066Sahrens /* 1175fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1176fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1177fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1178fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1179fa9e4066Sahrens */ 118099653d4eSeschrock static int 118199653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 118299653d4eSeschrock uint_t id, int atype) 1183fa9e4066Sahrens { 1184fa9e4066Sahrens nvlist_t **child; 1185573ca77eSGeorge Wilson uint_t children; 118699653d4eSeschrock int error; 1187fa9e4066Sahrens 118899653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 118999653d4eSeschrock return (error); 1190fa9e4066Sahrens 119199653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 119299653d4eSeschrock return (0); 1193fa9e4066Sahrens 1194e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1195e14bb325SJeff Bonwick &child, &children); 1196e14bb325SJeff Bonwick 1197e14bb325SJeff Bonwick if (error == ENOENT) 1198e14bb325SJeff Bonwick return (0); 1199e14bb325SJeff Bonwick 1200e14bb325SJeff Bonwick if (error) { 120199653d4eSeschrock vdev_free(*vdp); 120299653d4eSeschrock *vdp = NULL; 1203be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1204fa9e4066Sahrens } 1205fa9e4066Sahrens 1206573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 120799653d4eSeschrock vdev_t *vd; 120899653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 120999653d4eSeschrock atype)) != 0) { 121099653d4eSeschrock vdev_free(*vdp); 121199653d4eSeschrock *vdp = NULL; 121299653d4eSeschrock return (error); 1213fa9e4066Sahrens } 1214fa9e4066Sahrens } 1215fa9e4066Sahrens 121699653d4eSeschrock ASSERT(*vdp != NULL); 121799653d4eSeschrock 121899653d4eSeschrock return (0); 1219fa9e4066Sahrens } 1220fa9e4066Sahrens 1221fa9e4066Sahrens /* 1222fa9e4066Sahrens * Opposite of spa_load(). 1223fa9e4066Sahrens */ 1224fa9e4066Sahrens static void 1225fa9e4066Sahrens spa_unload(spa_t *spa) 1226fa9e4066Sahrens { 122799653d4eSeschrock int i; 122899653d4eSeschrock 1229e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1230e14bb325SJeff Bonwick 1231ea8dc4b6Seschrock /* 1232ea8dc4b6Seschrock * Stop async tasks. 1233ea8dc4b6Seschrock */ 1234ea8dc4b6Seschrock spa_async_suspend(spa); 1235ea8dc4b6Seschrock 1236fa9e4066Sahrens /* 1237fa9e4066Sahrens * Stop syncing. 1238fa9e4066Sahrens */ 1239fa9e4066Sahrens if (spa->spa_sync_on) { 1240fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1241fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1242fa9e4066Sahrens } 1243fa9e4066Sahrens 12448363e80aSGeorge Wilson /* 12458363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 12468363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 12478363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 12488363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 12498363e80aSGeorge Wilson */ 12508363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 12518363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12528363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 12538363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 12548363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 12558363e80aSGeorge Wilson } 12568363e80aSGeorge Wilson 1257fa9e4066Sahrens /* 1258e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1259fa9e4066Sahrens */ 126054d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12616f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12626f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12636f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 126454d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 126554d692b7SGeorge Wilson } 1266fa9e4066Sahrens 1267cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1268cde58dbcSMatthew Ahrens 12690713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12700713e232SGeorge Wilson 12710713e232SGeorge Wilson /* 12720713e232SGeorge Wilson * Close all vdevs. 12730713e232SGeorge Wilson */ 12740713e232SGeorge Wilson if (spa->spa_root_vdev) 12750713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12760713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12770713e232SGeorge Wilson 1278fa9e4066Sahrens /* 1279fa9e4066Sahrens * Close the dsl pool. 1280fa9e4066Sahrens */ 1281fa9e4066Sahrens if (spa->spa_dsl_pool) { 1282fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1283fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1284afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1285fa9e4066Sahrens } 1286fa9e4066Sahrens 1287b24ab676SJeff Bonwick ddt_unload(spa); 1288b24ab676SJeff Bonwick 12898ad4d6ddSJeff Bonwick /* 12908ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12918ad4d6ddSJeff Bonwick */ 12928ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12938ad4d6ddSJeff Bonwick 1294fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1295fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1296fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1297fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1298fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1299fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 130099653d4eSeschrock } 1301fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1302fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1303fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1304fa94a07fSbrendan } 13052ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1306fa94a07fSbrendan 1307cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1308cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1309fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1310cd0837ccSGeorge Wilson } 1311fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1312fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1313fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1314fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1315fa94a07fSbrendan } 1316fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1317fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1318fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 131999653d4eSeschrock } 13202ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 132199653d4eSeschrock 1322ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13238ad4d6ddSJeff Bonwick 13248704186eSDan McDonald if (spa->spa_comment != NULL) { 13258704186eSDan McDonald spa_strfree(spa->spa_comment); 13268704186eSDan McDonald spa->spa_comment = NULL; 13278704186eSDan McDonald } 13288704186eSDan McDonald 13298ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1330fa9e4066Sahrens } 1331fa9e4066Sahrens 133299653d4eSeschrock /* 133399653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 133499653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1335fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1336fa94a07fSbrendan * then re-generate a more complete list including status information. 133799653d4eSeschrock */ 133899653d4eSeschrock static void 133999653d4eSeschrock spa_load_spares(spa_t *spa) 134099653d4eSeschrock { 134199653d4eSeschrock nvlist_t **spares; 134299653d4eSeschrock uint_t nspares; 134399653d4eSeschrock int i; 134439c23413Seschrock vdev_t *vd, *tvd; 134599653d4eSeschrock 1346e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1347e14bb325SJeff Bonwick 134899653d4eSeschrock /* 134999653d4eSeschrock * First, close and free any existing spare vdevs. 135099653d4eSeschrock */ 1351fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1352fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 135339c23413Seschrock 135439c23413Seschrock /* Undo the call to spa_activate() below */ 1355c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1356c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 135739c23413Seschrock spa_spare_remove(tvd); 135839c23413Seschrock vdev_close(vd); 135939c23413Seschrock vdev_free(vd); 136099653d4eSeschrock } 136139c23413Seschrock 1362fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1363fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1364fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 136599653d4eSeschrock 1366fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 136799653d4eSeschrock nspares = 0; 136899653d4eSeschrock else 1369fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 137099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 137199653d4eSeschrock 1372fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1373fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 137499653d4eSeschrock 137599653d4eSeschrock if (nspares == 0) 137699653d4eSeschrock return; 137799653d4eSeschrock 137899653d4eSeschrock /* 137999653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 138039c23413Seschrock * process. For each spare, there is potentially two different vdev_t 138139c23413Seschrock * structures associated with it: one in the list of spares (used only 138239c23413Seschrock * for basic validation purposes) and one in the active vdev 138339c23413Seschrock * configuration (if it's spared in). During this phase we open and 138439c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 138539c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 138699653d4eSeschrock */ 1387fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1388fa94a07fSbrendan KM_SLEEP); 1389fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 139099653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 139199653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 139299653d4eSeschrock ASSERT(vd != NULL); 139399653d4eSeschrock 1394fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 139599653d4eSeschrock 1396c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1397c5904d13Seschrock B_FALSE)) != NULL) { 139839c23413Seschrock if (!tvd->vdev_isspare) 139939c23413Seschrock spa_spare_add(tvd); 140039c23413Seschrock 140139c23413Seschrock /* 140239c23413Seschrock * We only mark the spare active if we were successfully 140339c23413Seschrock * able to load the vdev. Otherwise, importing a pool 140439c23413Seschrock * with a bad active spare would result in strange 140539c23413Seschrock * behavior, because multiple pool would think the spare 140639c23413Seschrock * is actively in use. 140739c23413Seschrock * 140839c23413Seschrock * There is a vulnerability here to an equally bizarre 140939c23413Seschrock * circumstance, where a dead active spare is later 141039c23413Seschrock * brought back to life (onlined or otherwise). Given 141139c23413Seschrock * the rarity of this scenario, and the extra complexity 141239c23413Seschrock * it adds, we ignore the possibility. 141339c23413Seschrock */ 141439c23413Seschrock if (!vdev_is_dead(tvd)) 141539c23413Seschrock spa_spare_activate(tvd); 141639c23413Seschrock } 141739c23413Seschrock 1418e14bb325SJeff Bonwick vd->vdev_top = vd; 14196809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1420e14bb325SJeff Bonwick 142199653d4eSeschrock if (vdev_open(vd) != 0) 142299653d4eSeschrock continue; 142399653d4eSeschrock 1424fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1425fa94a07fSbrendan spa_spare_add(vd); 142699653d4eSeschrock } 142799653d4eSeschrock 142899653d4eSeschrock /* 142999653d4eSeschrock * Recompute the stashed list of spares, with status information 143099653d4eSeschrock * this time. 143199653d4eSeschrock */ 1432fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 143399653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 143499653d4eSeschrock 1435fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1436fa94a07fSbrendan KM_SLEEP); 1437fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1438fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14393f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1440fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1441fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1442fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 144399653d4eSeschrock nvlist_free(spares[i]); 1444fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1445fa94a07fSbrendan } 1446fa94a07fSbrendan 1447fa94a07fSbrendan /* 1448fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1449fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1450fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1451fa94a07fSbrendan * then re-generate a more complete list including status information. 1452fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1453fa94a07fSbrendan * not re-opened. 1454fa94a07fSbrendan */ 1455fa94a07fSbrendan static void 1456fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1457fa94a07fSbrendan { 1458fa94a07fSbrendan nvlist_t **l2cache; 1459fa94a07fSbrendan uint_t nl2cache; 1460fa94a07fSbrendan int i, j, oldnvdevs; 1461573ca77eSGeorge Wilson uint64_t guid; 1462fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1463fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1464fa94a07fSbrendan 1465e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1466e14bb325SJeff Bonwick 1467fa94a07fSbrendan if (sav->sav_config != NULL) { 1468fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1469fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1470fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1471fa94a07fSbrendan } else { 1472fa94a07fSbrendan nl2cache = 0; 1473d5285caeSGeorge Wilson newvdevs = NULL; 1474fa94a07fSbrendan } 1475fa94a07fSbrendan 1476fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1477fa94a07fSbrendan oldnvdevs = sav->sav_count; 1478fa94a07fSbrendan sav->sav_vdevs = NULL; 1479fa94a07fSbrendan sav->sav_count = 0; 1480fa94a07fSbrendan 1481fa94a07fSbrendan /* 1482fa94a07fSbrendan * Process new nvlist of vdevs. 1483fa94a07fSbrendan */ 1484fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1485fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1486fa94a07fSbrendan &guid) == 0); 1487fa94a07fSbrendan 1488fa94a07fSbrendan newvdevs[i] = NULL; 1489fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1490fa94a07fSbrendan vd = oldvdevs[j]; 1491fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1492fa94a07fSbrendan /* 1493fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1494fa94a07fSbrendan */ 1495fa94a07fSbrendan newvdevs[i] = vd; 1496fa94a07fSbrendan oldvdevs[j] = NULL; 1497fa94a07fSbrendan break; 1498fa94a07fSbrendan } 1499fa94a07fSbrendan } 1500fa94a07fSbrendan 1501fa94a07fSbrendan if (newvdevs[i] == NULL) { 1502fa94a07fSbrendan /* 1503fa94a07fSbrendan * Create new vdev 1504fa94a07fSbrendan */ 1505fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1506fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1507fa94a07fSbrendan ASSERT(vd != NULL); 1508fa94a07fSbrendan newvdevs[i] = vd; 1509fa94a07fSbrendan 1510fa94a07fSbrendan /* 1511fa94a07fSbrendan * Commit this vdev as an l2cache device, 1512fa94a07fSbrendan * even if it fails to open. 1513fa94a07fSbrendan */ 1514fa94a07fSbrendan spa_l2cache_add(vd); 1515fa94a07fSbrendan 1516c5904d13Seschrock vd->vdev_top = vd; 1517c5904d13Seschrock vd->vdev_aux = sav; 1518c5904d13Seschrock 1519c5904d13Seschrock spa_l2cache_activate(vd); 1520c5904d13Seschrock 1521fa94a07fSbrendan if (vdev_open(vd) != 0) 1522fa94a07fSbrendan continue; 1523fa94a07fSbrendan 1524fa94a07fSbrendan (void) vdev_validate_aux(vd); 1525fa94a07fSbrendan 1526573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1527573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1528fa94a07fSbrendan } 1529fa94a07fSbrendan } 1530fa94a07fSbrendan 1531fa94a07fSbrendan /* 1532fa94a07fSbrendan * Purge vdevs that were dropped 1533fa94a07fSbrendan */ 1534fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1535fa94a07fSbrendan uint64_t pool; 1536fa94a07fSbrendan 1537fa94a07fSbrendan vd = oldvdevs[i]; 1538fa94a07fSbrendan if (vd != NULL) { 1539cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1540cd0837ccSGeorge Wilson 15418ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15428ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1543fa94a07fSbrendan l2arc_remove_vdev(vd); 1544cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1545cd0837ccSGeorge Wilson vdev_free(vd); 1546fa94a07fSbrendan } 1547fa94a07fSbrendan } 1548fa94a07fSbrendan 1549fa94a07fSbrendan if (oldvdevs) 1550fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1551fa94a07fSbrendan 1552fa94a07fSbrendan if (sav->sav_config == NULL) 1553fa94a07fSbrendan goto out; 1554fa94a07fSbrendan 1555fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1556fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1557fa94a07fSbrendan 1558fa94a07fSbrendan /* 1559fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1560fa94a07fSbrendan * information this time. 1561fa94a07fSbrendan */ 1562fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1563fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1564fa94a07fSbrendan 1565fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1566fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1567fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15683f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1569fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1570fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1571fa94a07fSbrendan out: 1572fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1573fa94a07fSbrendan nvlist_free(l2cache[i]); 1574fa94a07fSbrendan if (sav->sav_count) 1575fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 157699653d4eSeschrock } 157799653d4eSeschrock 157899653d4eSeschrock static int 157999653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 158099653d4eSeschrock { 158199653d4eSeschrock dmu_buf_t *db; 158299653d4eSeschrock char *packed = NULL; 158399653d4eSeschrock size_t nvsize = 0; 158499653d4eSeschrock int error; 158599653d4eSeschrock *value = NULL; 158699653d4eSeschrock 1587a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1588a45f1c3cSBrian Behlendorf if (error != 0) 1589a45f1c3cSBrian Behlendorf return (error); 1590a45f1c3cSBrian Behlendorf 159199653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 159299653d4eSeschrock dmu_buf_rele(db, FTAG); 159399653d4eSeschrock 159499653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15957bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15967bfdf011SNeil Perrin DMU_READ_PREFETCH); 159799653d4eSeschrock if (error == 0) 159899653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 159999653d4eSeschrock kmem_free(packed, nvsize); 160099653d4eSeschrock 160199653d4eSeschrock return (error); 160299653d4eSeschrock } 160399653d4eSeschrock 16043d7072f8Seschrock /* 16053d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 16063d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 16073d7072f8Seschrock */ 16083d7072f8Seschrock static void 16093d7072f8Seschrock spa_check_removed(vdev_t *vd) 16103d7072f8Seschrock { 1611573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 16123d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 16133d7072f8Seschrock 1614efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1615efb4a871SYuri Pankov !vd->vdev_ishole) { 16163d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 16173d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 16183d7072f8Seschrock } 16193d7072f8Seschrock } 16203d7072f8Seschrock 1621215198a6SJoe Stein static void 1622215198a6SJoe Stein spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) 1623215198a6SJoe Stein { 1624215198a6SJoe Stein ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); 1625215198a6SJoe Stein 1626215198a6SJoe Stein vd->vdev_top_zap = mvd->vdev_top_zap; 1627215198a6SJoe Stein vd->vdev_leaf_zap = mvd->vdev_leaf_zap; 1628215198a6SJoe Stein 1629215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 1630215198a6SJoe Stein spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); 1631215198a6SJoe Stein } 1632215198a6SJoe Stein } 1633215198a6SJoe Stein 1634e6ca193dSGeorge Wilson /* 16354b964adaSGeorge Wilson * Validate the current config against the MOS config 1636e6ca193dSGeorge Wilson */ 16374b964adaSGeorge Wilson static boolean_t 16384b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1639e6ca193dSGeorge Wilson { 16404b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16414b964adaSGeorge Wilson nvlist_t *nv; 16424b964adaSGeorge Wilson 16434b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16444b964adaSGeorge Wilson 16454b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16464b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16474b964adaSGeorge Wilson 16484b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1649e6ca193dSGeorge Wilson 165088ecc943SGeorge Wilson /* 16514b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16524b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16534b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 165488ecc943SGeorge Wilson */ 16554b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16564b964adaSGeorge Wilson nvlist_t **child, *nv; 16574b964adaSGeorge Wilson uint64_t idx = 0; 16584b964adaSGeorge Wilson 16594b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16604b964adaSGeorge Wilson KM_SLEEP); 16614b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1662e6ca193dSGeorge Wilson 16634b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16644b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16654b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16664b964adaSGeorge Wilson 16674b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16684b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16694b964adaSGeorge Wilson mtvd->vdev_islog) 16704b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16714b964adaSGeorge Wilson B_FALSE, 0); 16724b964adaSGeorge Wilson } 16734b964adaSGeorge Wilson 16744b964adaSGeorge Wilson if (idx) { 16754b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16764b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16774b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16784b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16794b964adaSGeorge Wilson 16804b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16814b964adaSGeorge Wilson nvlist_free(child[i]); 16824b964adaSGeorge Wilson } 16834b964adaSGeorge Wilson nvlist_free(nv); 16844b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16854b964adaSGeorge Wilson } 16864b964adaSGeorge Wilson 16874b964adaSGeorge Wilson /* 16884b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16894b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16904b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16914b964adaSGeorge Wilson */ 169288ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16934b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16944b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16954b964adaSGeorge Wilson 16964b964adaSGeorge Wilson /* 16974b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 16984b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 16994b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 17004b964adaSGeorge Wilson */ 17014b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 17024b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 17034b964adaSGeorge Wilson 17044b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 17054b964adaSGeorge Wilson continue; 17064b964adaSGeorge Wilson 17074b964adaSGeorge Wilson /* 17084b964adaSGeorge Wilson * Device specific actions. 17094b964adaSGeorge Wilson */ 17104b964adaSGeorge Wilson if (mtvd->vdev_islog) { 17114b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 17124b964adaSGeorge Wilson } else { 17134b964adaSGeorge Wilson /* 17144b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 17154b964adaSGeorge Wilson * support we should be able to handle 17164b964adaSGeorge Wilson * missing data devices by transitioning 17174b964adaSGeorge Wilson * the pool to readonly. 17184b964adaSGeorge Wilson */ 17194b964adaSGeorge Wilson continue; 17204b964adaSGeorge Wilson } 17214b964adaSGeorge Wilson 17224b964adaSGeorge Wilson /* 17234b964adaSGeorge Wilson * Swap the missing vdev with the data we were 17244b964adaSGeorge Wilson * able to obtain from the MOS config. 17254b964adaSGeorge Wilson */ 17264b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 17274b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 17284b964adaSGeorge Wilson 17294b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 17304b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17314b964adaSGeorge Wilson 17324b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17334b964adaSGeorge Wilson vdev_load(mtvd); 17344b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17354b964adaSGeorge Wilson 17364b964adaSGeorge Wilson vdev_reopen(rvd); 1737215198a6SJoe Stein } else { 1738215198a6SJoe Stein if (mtvd->vdev_islog) { 1739215198a6SJoe Stein /* 1740215198a6SJoe Stein * Load the slog device's state from the MOS 1741215198a6SJoe Stein * config since it's possible that the label 1742215198a6SJoe Stein * does not contain the most up-to-date 1743215198a6SJoe Stein * information. 1744215198a6SJoe Stein */ 1745215198a6SJoe Stein vdev_load_log_state(tvd, mtvd); 1746215198a6SJoe Stein vdev_reopen(tvd); 1747215198a6SJoe Stein } 1748215198a6SJoe Stein 17494b964adaSGeorge Wilson /* 1750215198a6SJoe Stein * Per-vdev ZAP info is stored exclusively in the MOS. 17514b964adaSGeorge Wilson */ 1752215198a6SJoe Stein spa_config_valid_zaps(tvd, mtvd); 17534b964adaSGeorge Wilson } 1754e6ca193dSGeorge Wilson } 1755215198a6SJoe Stein 17564b964adaSGeorge Wilson vdev_free(mrvd); 175788ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17584b964adaSGeorge Wilson 17594b964adaSGeorge Wilson /* 17604b964adaSGeorge Wilson * Ensure we were able to validate the config. 17614b964adaSGeorge Wilson */ 17624b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1763e6ca193dSGeorge Wilson } 1764e6ca193dSGeorge Wilson 1765b87f3af3Sperrin /* 1766b87f3af3Sperrin * Check for missing log devices 1767b87f3af3Sperrin */ 17683b2aab18SMatthew Ahrens static boolean_t 1769b87f3af3Sperrin spa_check_logs(spa_t *spa) 1770b87f3af3Sperrin { 17713b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 177212380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17733b2aab18SMatthew Ahrens 1774b87f3af3Sperrin switch (spa->spa_log_state) { 1775b87f3af3Sperrin case SPA_LOG_MISSING: 1776b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1777b87f3af3Sperrin case SPA_LOG_UNKNOWN: 177812380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 177912380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 17803b2aab18SMatthew Ahrens if (rv) 17811195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1782b87f3af3Sperrin break; 1783b87f3af3Sperrin } 17843b2aab18SMatthew Ahrens return (rv); 1785b87f3af3Sperrin } 1786b87f3af3Sperrin 17871195e687SMark J Musante static boolean_t 17881195e687SMark J Musante spa_passivate_log(spa_t *spa) 17891195e687SMark J Musante { 17901195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17911195e687SMark J Musante boolean_t slog_found = B_FALSE; 17921195e687SMark J Musante 17931195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17941195e687SMark J Musante 17951195e687SMark J Musante if (!spa_has_slogs(spa)) 17961195e687SMark J Musante return (B_FALSE); 17971195e687SMark J Musante 17981195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17991195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18001195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18011195e687SMark J Musante 18021195e687SMark J Musante if (tvd->vdev_islog) { 18031195e687SMark J Musante metaslab_group_passivate(mg); 18041195e687SMark J Musante slog_found = B_TRUE; 18051195e687SMark J Musante } 18061195e687SMark J Musante } 18071195e687SMark J Musante 18081195e687SMark J Musante return (slog_found); 18091195e687SMark J Musante } 18101195e687SMark J Musante 18111195e687SMark J Musante static void 18121195e687SMark J Musante spa_activate_log(spa_t *spa) 18131195e687SMark J Musante { 18141195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18151195e687SMark J Musante 18161195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18171195e687SMark J Musante 18181195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18191195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18201195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18211195e687SMark J Musante 18221195e687SMark J Musante if (tvd->vdev_islog) 18231195e687SMark J Musante metaslab_group_activate(mg); 18241195e687SMark J Musante } 18251195e687SMark J Musante } 18261195e687SMark J Musante 18271195e687SMark J Musante int 18281195e687SMark J Musante spa_offline_log(spa_t *spa) 18291195e687SMark J Musante { 18303b2aab18SMatthew Ahrens int error; 18311195e687SMark J Musante 18323b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 18333b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18343b2aab18SMatthew Ahrens if (error == 0) { 18351195e687SMark J Musante /* 18361195e687SMark J Musante * We successfully offlined the log device, sync out the 18371195e687SMark J Musante * current txg so that the "stubby" block can be removed 18381195e687SMark J Musante * by zil_sync(). 18391195e687SMark J Musante */ 18401195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18411195e687SMark J Musante } 18421195e687SMark J Musante return (error); 18431195e687SMark J Musante } 18441195e687SMark J Musante 1845b693757aSEric Schrock static void 1846b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1847b693757aSEric Schrock { 1848b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1849b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1850b693757aSEric Schrock } 1851b693757aSEric Schrock 1852b24ab676SJeff Bonwick void 1853b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1854b24ab676SJeff Bonwick { 1855b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1856b24ab676SJeff Bonwick 1857b24ab676SJeff Bonwick if (zio->io_error) 1858b24ab676SJeff Bonwick return; 1859b24ab676SJeff Bonwick 1860b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1861b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1862b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1863b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1864b24ab676SJeff Bonwick } 1865b24ab676SJeff Bonwick 1866468c413aSTim Haley typedef struct spa_load_error { 1867c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1868468c413aSTim Haley uint64_t sle_data_count; 1869468c413aSTim Haley } spa_load_error_t; 1870468c413aSTim Haley 1871468c413aSTim Haley static void 1872468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1873468c413aSTim Haley { 1874468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1875468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1876468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1877468c413aSTim Haley int error = zio->io_error; 1878e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1879468c413aSTim Haley 1880*770499e1SDan Kimmel abd_free(zio->io_abd); 1881468c413aSTim Haley if (error) { 1882ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1883468c413aSTim Haley type != DMU_OT_INTENT_LOG) 18841a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1885468c413aSTim Haley else 18861a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1887468c413aSTim Haley } 1888e42d2059SMatthew Ahrens 1889e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1890e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1891e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1892e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1893468c413aSTim Haley } 1894468c413aSTim Haley 1895e42d2059SMatthew Ahrens /* 1896e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1897e42d2059SMatthew Ahrens * a pool while importing it. 1898e42d2059SMatthew Ahrens */ 1899e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1900e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1901e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1902e42d2059SMatthew Ahrens 1903468c413aSTim Haley /*ARGSUSED*/ 1904468c413aSTim Haley static int 1905b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19067802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1907468c413aSTim Haley { 1908a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1909e42d2059SMatthew Ahrens return (0); 1910e42d2059SMatthew Ahrens /* 1911e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1912e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1913e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1914e42d2059SMatthew Ahrens */ 1915e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1916e42d2059SMatthew Ahrens return (0); 1917*770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1918e42d2059SMatthew Ahrens return (0); 1919468c413aSTim Haley 1920e42d2059SMatthew Ahrens zio_t *rio = arg; 1921e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1922e42d2059SMatthew Ahrens 1923e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1924e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1925e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1926e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1927e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1928e42d2059SMatthew Ahrens 1929*770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 1930e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1931e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1932e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1933468c413aSTim Haley return (0); 1934468c413aSTim Haley } 1935468c413aSTim Haley 1936c971037bSPaul Dagnelie /* ARGSUSED */ 1937c971037bSPaul Dagnelie int 1938c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1939c971037bSPaul Dagnelie { 19409adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1941c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1942c971037bSPaul Dagnelie 1943c971037bSPaul Dagnelie return (0); 1944c971037bSPaul Dagnelie } 1945c971037bSPaul Dagnelie 1946468c413aSTim Haley static int 1947468c413aSTim Haley spa_load_verify(spa_t *spa) 1948468c413aSTim Haley { 1949468c413aSTim Haley zio_t *rio; 1950468c413aSTim Haley spa_load_error_t sle = { 0 }; 1951468c413aSTim Haley zpool_rewind_policy_t policy; 1952468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1953e42d2059SMatthew Ahrens int error = 0; 1954468c413aSTim Haley 1955c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1956c8ee1847SVictor Latushkin 1957c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1958c8ee1847SVictor Latushkin return (0); 1959c8ee1847SVictor Latushkin 1960c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1961c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1962c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1963c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1964c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1965c971037bSPaul Dagnelie if (error != 0) 1966c971037bSPaul Dagnelie return (error); 1967c971037bSPaul Dagnelie 1968468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1969468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1970468c413aSTim Haley 1971e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1972e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1973e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1974e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 1975e42d2059SMatthew Ahrens } 1976468c413aSTim Haley 1977468c413aSTim Haley (void) zio_wait(rio); 1978468c413aSTim Haley 1979c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1980468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1981468c413aSTim Haley 1982c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1983468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 19844b964adaSGeorge Wilson int64_t loss = 0; 19854b964adaSGeorge Wilson 1986468c413aSTim Haley verify_ok = B_TRUE; 1987468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1988468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 19894b964adaSGeorge Wilson 19904b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 19914b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19924b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 19934b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 19944b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 19954b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19964b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1997a33cae98STim Haley } else { 1998a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1999468c413aSTim Haley } 2000468c413aSTim Haley 2001468c413aSTim Haley if (error) { 2002468c413aSTim Haley if (error != ENXIO && error != EIO) 2003be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2004468c413aSTim Haley return (error); 2005468c413aSTim Haley } 2006468c413aSTim Haley 2007468c413aSTim Haley return (verify_ok ? 0 : EIO); 2008468c413aSTim Haley } 2009468c413aSTim Haley 20101195e687SMark J Musante /* 20111195e687SMark J Musante * Find a value in the pool props object. 20121195e687SMark J Musante */ 20131195e687SMark J Musante static void 20141195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20151195e687SMark J Musante { 20161195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20171195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20181195e687SMark J Musante } 20191195e687SMark J Musante 20201195e687SMark J Musante /* 20211195e687SMark J Musante * Find a value in the pool directory object. 20221195e687SMark J Musante */ 20231195e687SMark J Musante static int 20241195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 20251195e687SMark J Musante { 20261195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20271195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 20281195e687SMark J Musante } 20291195e687SMark J Musante 20301195e687SMark J Musante static int 20311195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20321195e687SMark J Musante { 20331195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 20341195e687SMark J Musante return (err); 20351195e687SMark J Musante } 20361195e687SMark J Musante 20371195e687SMark J Musante /* 20381195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20391195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20401195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20411195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20421195e687SMark J Musante * 20431195e687SMark J Musante * This function determines what to do with that list: either rejoin 20441195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20451195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20461195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20471195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20481195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20491195e687SMark J Musante * 2050d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2051d41c4376SMark J Musante * the original pool. 20521195e687SMark J Musante */ 20531195e687SMark J Musante static void 20541195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 20551195e687SMark J Musante { 20561195e687SMark J Musante uint_t extracted; 20571195e687SMark J Musante uint64_t *glist; 20581195e687SMark J Musante uint_t i, gcount; 20591195e687SMark J Musante nvlist_t *nvl; 20601195e687SMark J Musante vdev_t **vd; 20611195e687SMark J Musante boolean_t attempt_reopen; 20621195e687SMark J Musante 20631195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20641195e687SMark J Musante return; 20651195e687SMark J Musante 20661195e687SMark J Musante /* check that the config is complete */ 20671195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20681195e687SMark J Musante &glist, &gcount) != 0) 20691195e687SMark J Musante return; 20701195e687SMark J Musante 20711195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20721195e687SMark J Musante 20731195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20741195e687SMark J Musante attempt_reopen = B_TRUE; 20751195e687SMark J Musante for (i = 0; i < gcount; i++) { 20761195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 20771195e687SMark J Musante continue; 20781195e687SMark J Musante 20791195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 20801195e687SMark J Musante if (vd[i] == NULL) { 20811195e687SMark J Musante /* 20821195e687SMark J Musante * Don't bother attempting to reopen the disks; 20831195e687SMark J Musante * just do the split. 20841195e687SMark J Musante */ 20851195e687SMark J Musante attempt_reopen = B_FALSE; 20861195e687SMark J Musante } else { 20871195e687SMark J Musante /* attempt to re-online it */ 20881195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 20891195e687SMark J Musante } 20901195e687SMark J Musante } 20911195e687SMark J Musante 20921195e687SMark J Musante if (attempt_reopen) { 20931195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20941195e687SMark J Musante 20951195e687SMark J Musante /* check each device to see what state it's in */ 20961195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 20971195e687SMark J Musante if (vd[i] != NULL && 20981195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 20991195e687SMark J Musante break; 21001195e687SMark J Musante ++extracted; 21011195e687SMark J Musante } 21021195e687SMark J Musante } 21031195e687SMark J Musante 21041195e687SMark J Musante /* 21051195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 21061195e687SMark J Musante * even attempted to look at them, then we split them off for 21071195e687SMark J Musante * good. 21081195e687SMark J Musante */ 21091195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 21101195e687SMark J Musante for (i = 0; i < gcount; i++) 21111195e687SMark J Musante if (vd[i] != NULL) 21121195e687SMark J Musante vdev_split(vd[i]); 21131195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21141195e687SMark J Musante } 21151195e687SMark J Musante 21161195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21171195e687SMark J Musante } 21181195e687SMark J Musante 21191195e687SMark J Musante static int 21201195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 21211195e687SMark J Musante boolean_t mosconfig) 21221195e687SMark J Musante { 21231195e687SMark J Musante nvlist_t *config = spa->spa_config; 21241195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21258704186eSDan McDonald char *comment; 21261195e687SMark J Musante int error; 21271195e687SMark J Musante uint64_t pool_guid; 21281195e687SMark J Musante nvlist_t *nvl; 21291195e687SMark J Musante 21301195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2131be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21321195e687SMark J Musante 21338704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 21348704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 21358704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 21368704186eSDan McDonald 21371195e687SMark J Musante /* 21381195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 21391195e687SMark J Musante * it's not present treat it as the initial version. 21401195e687SMark J Musante */ 21411195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 21421195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 21431195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 21441195e687SMark J Musante 21451195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 21461195e687SMark J Musante &spa->spa_config_txg); 21471195e687SMark J Musante 21481195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 21491195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2150be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 21511195e687SMark J Musante } else { 2152e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 21531195e687SMark J Musante 21541195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 21551195e687SMark J Musante &nvl) == 0) { 21561195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 21571195e687SMark J Musante KM_SLEEP) == 0); 21581195e687SMark J Musante } 21591195e687SMark J Musante 2160ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2161ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2162ad135b5dSChristopher Siden 216311027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21641195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21651195e687SMark J Musante mosconfig, &ereport); 21661195e687SMark J Musante } 21671195e687SMark J Musante 2168bc9014e6SJustin Gibbs /* 2169bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2170bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2171bc9014e6SJustin Gibbs */ 2172bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21731195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 217411027bc7STim Haley if (error) { 217511027bc7STim Haley if (error != EEXIST) { 217611027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 217711027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 217811027bc7STim Haley } 217911027bc7STim Haley if (error != EBADF) { 218011027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 218111027bc7STim Haley } 218211027bc7STim Haley } 21831195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21841195e687SMark J Musante spa->spa_ena = 0; 21851195e687SMark J Musante 21861195e687SMark J Musante return (error); 21871195e687SMark J Musante } 21881195e687SMark J Musante 2189215198a6SJoe Stein /* 2190215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2191215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2192215198a6SJoe Stein * spa's per-vdev ZAP list. 2193215198a6SJoe Stein */ 2194215198a6SJoe Stein static uint64_t 2195215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2196215198a6SJoe Stein { 2197215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2198215198a6SJoe Stein uint64_t total = 0; 2199215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2200215198a6SJoe Stein total++; 2201215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2202215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2203215198a6SJoe Stein } 2204215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2205215198a6SJoe Stein total++; 2206215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2207215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2208215198a6SJoe Stein } 2209215198a6SJoe Stein 2210215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2211215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2212215198a6SJoe Stein } 2213215198a6SJoe Stein 2214215198a6SJoe Stein return (total); 2215215198a6SJoe Stein } 2216215198a6SJoe Stein 2217fa9e4066Sahrens /* 2218fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2219ea8dc4b6Seschrock * source of configuration information. 2220fa9e4066Sahrens */ 2221fa9e4066Sahrens static int 22221195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 22231195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 22241195e687SMark J Musante char **ereport) 2225fa9e4066Sahrens { 2226fa9e4066Sahrens int error = 0; 2227871a9500SMark J Musante nvlist_t *nvroot = NULL; 2228ad135b5dSChristopher Siden nvlist_t *label; 2229fa9e4066Sahrens vdev_t *rvd; 2230fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 22314b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 22328ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 22331195e687SMark J Musante int parse; 2234cde58dbcSMatthew Ahrens uint64_t obj; 2235ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2236fa9e4066Sahrens 22378ad4d6ddSJeff Bonwick /* 22388ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 22398ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 22408ad4d6ddSJeff Bonwick */ 22418ad4d6ddSJeff Bonwick if (!mosconfig) 22428ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 22438ad4d6ddSJeff Bonwick 2244e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2245e14bb325SJeff Bonwick 2246ea8dc4b6Seschrock spa->spa_load_state = state; 22470373e76bSbonwick 22481195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2249be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2250fa9e4066Sahrens 22511195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 22521195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2253b5989ec7Seschrock 225454d692b7SGeorge Wilson /* 225554d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 225654d692b7SGeorge Wilson */ 22576f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 22586f834bc1SMatthew Ahrens KM_SLEEP); 22596f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 22606f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 22616f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 22626f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 22636f834bc1SMatthew Ahrens } 226454d692b7SGeorge Wilson 2265fa9e4066Sahrens /* 226699653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 226799653d4eSeschrock * value that will be returned by spa_version() since parsing the 226899653d4eSeschrock * configuration requires knowing the version number. 2269fa9e4066Sahrens */ 2270e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22711195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2272e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2273fa9e4066Sahrens 227499653d4eSeschrock if (error != 0) 22751195e687SMark J Musante return (error); 2276fa9e4066Sahrens 22770e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 227881cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 227981cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 22801195e687SMark J Musante 22811195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22821195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 22831195e687SMark J Musante } 2284fa9e4066Sahrens 2285fa9e4066Sahrens /* 2286fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2287fa9e4066Sahrens */ 2288e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22890bf246f5Smc error = vdev_open(rvd); 2290e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 22910bf246f5Smc if (error != 0) 22921195e687SMark J Musante return (error); 2293fa9e4066Sahrens 2294560e6e96Seschrock /* 229577e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 229677e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 229777e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 22981195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 229977e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 230077e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 230177e3a39cSMark J Musante * the vdev config. 23021195e687SMark J Musante * 23031195e687SMark J Musante * If we're assembling a new pool that's been split off from an 23041195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 23051195e687SMark J Musante * validation for now. 2306560e6e96Seschrock */ 23071195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23081195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2309d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 23101195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2311560e6e96Seschrock 23121195e687SMark J Musante if (error != 0) 23131195e687SMark J Musante return (error); 23141195e687SMark J Musante 23151195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2316be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2317560e6e96Seschrock } 2318560e6e96Seschrock 2319fa9e4066Sahrens /* 2320fa9e4066Sahrens * Find the best uberblock. 2321fa9e4066Sahrens */ 2322ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2323fa9e4066Sahrens 2324fa9e4066Sahrens /* 2325fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2326fa9e4066Sahrens */ 2327ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2328ad135b5dSChristopher Siden nvlist_free(label); 23291195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2330ad135b5dSChristopher Siden } 2331ea8dc4b6Seschrock 2332ea8dc4b6Seschrock /* 2333ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2334ea8dc4b6Seschrock */ 2335ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2336ad135b5dSChristopher Siden nvlist_free(label); 23371195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2338ad135b5dSChristopher Siden } 2339ad135b5dSChristopher Siden 2340ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2341ad135b5dSChristopher Siden nvlist_t *features; 2342ad135b5dSChristopher Siden 2343ad135b5dSChristopher Siden /* 2344ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2345ad135b5dSChristopher Siden * MOS in the label, return failure. 2346ad135b5dSChristopher Siden */ 2347ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2348ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2349ad135b5dSChristopher Siden nvlist_free(label); 2350ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2351ad135b5dSChristopher Siden ENXIO)); 2352ad135b5dSChristopher Siden } 2353ad135b5dSChristopher Siden 2354ad135b5dSChristopher Siden /* 2355ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2356ad135b5dSChristopher Siden * from the label. 2357ad135b5dSChristopher Siden */ 2358ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2359ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2360ad135b5dSChristopher Siden } 2361ad135b5dSChristopher Siden 2362ad135b5dSChristopher Siden nvlist_free(label); 2363ad135b5dSChristopher Siden 2364ad135b5dSChristopher Siden /* 2365ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2366ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2367ad135b5dSChristopher Siden * cannot open a pool. 2368ad135b5dSChristopher Siden */ 2369ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2370ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2371ad135b5dSChristopher Siden 2372ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2373ad135b5dSChristopher Siden 0); 2374ad135b5dSChristopher Siden 2375ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2376ad135b5dSChristopher Siden NULL); nvp != NULL; 2377ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2378ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2379ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2380ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2381ad135b5dSChristopher Siden } 2382ad135b5dSChristopher Siden } 2383ad135b5dSChristopher Siden 2384ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2385ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2386ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2387ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2388ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2389ad135b5dSChristopher Siden ENOTSUP)); 2390ad135b5dSChristopher Siden } 2391ad135b5dSChristopher Siden 2392ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2393ad135b5dSChristopher Siden } 2394fa9e4066Sahrens 2395fa9e4066Sahrens /* 2396fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 23974b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 23984b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 23994b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 24004b964adaSGeorge Wilson * can handle missing vdevs. 2401fa9e4066Sahrens */ 24024b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 24034b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 24041195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 24051195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 24061195e687SMark J Musante 24071195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 24081195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24091195e687SMark J Musante spa_try_repair(spa, config); 24101195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 24111195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 24121195e687SMark J Musante spa->spa_config_splitting = NULL; 2413fa9e4066Sahrens } 2414fa9e4066Sahrens 2415fa9e4066Sahrens /* 2416fa9e4066Sahrens * Initialize internal SPA structures. 2417fa9e4066Sahrens */ 2418fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2419fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2420468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2421c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2422468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2423468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2424b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 24253f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2426b24ab676SJeff Bonwick 2427ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 24281195e687SMark J Musante if (error) 24291195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2430fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2431fa9e4066Sahrens 24321195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 24331195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2434fa9e4066Sahrens 2435ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2436ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 243757221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2438ad135b5dSChristopher Siden 2439ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2440ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2441ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2442ad135b5dSChristopher Siden } 2443ad135b5dSChristopher Siden 2444ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2445ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2446ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2447ad135b5dSChristopher Siden } 2448ad135b5dSChristopher Siden 2449ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2450ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2451ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2452ad135b5dSChristopher Siden } 2453ad135b5dSChristopher Siden 245457221772SChristopher Siden enabled_feat = fnvlist_alloc(); 245557221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2456ad135b5dSChristopher Siden 24572acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 245857221772SChristopher Siden unsup_feat, enabled_feat)) 2459ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2460ad135b5dSChristopher Siden 2461ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 24622acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 246357221772SChristopher Siden unsup_feat, enabled_feat)) { 2464ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 246557221772SChristopher Siden } 2466ad135b5dSChristopher Siden } 2467ad135b5dSChristopher Siden 246857221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 246957221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 247057221772SChristopher Siden 2471ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 247257221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 247357221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2474ad135b5dSChristopher Siden } 2475ad135b5dSChristopher Siden 247657221772SChristopher Siden fnvlist_free(enabled_feat); 247757221772SChristopher Siden fnvlist_free(unsup_feat); 2478ad135b5dSChristopher Siden 2479ad135b5dSChristopher Siden if (!missing_feat_read) { 2480ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2481ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2482ad135b5dSChristopher Siden } 2483ad135b5dSChristopher Siden 2484ad135b5dSChristopher Siden /* 2485ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2486ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2487ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2488ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2489ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2490ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2491ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2492ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2493ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2494ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2495ad135b5dSChristopher Siden * available for open in read-only mode. 2496ad135b5dSChristopher Siden * 2497ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2498ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2499ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2500ad135b5dSChristopher Siden * userland in order to know whether to display the 2501ad135b5dSChristopher Siden * abovementioned note. 2502ad135b5dSChristopher Siden */ 2503ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2504ad135b5dSChristopher Siden spa_writeable(spa))) { 2505ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2506ad135b5dSChristopher Siden ENOTSUP)); 2507ad135b5dSChristopher Siden } 250843466aaeSMax Grossman 250943466aaeSMax Grossman /* 251043466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 251143466aaeSMax Grossman * cache during SPA initialization. 251243466aaeSMax Grossman */ 251343466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 251443466aaeSMax Grossman uint64_t refcount; 251543466aaeSMax Grossman 251643466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 251743466aaeSMax Grossman &spa_feature_table[i], &refcount); 251843466aaeSMax Grossman if (error == 0) { 251943466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 252043466aaeSMax Grossman } else if (error == ENOTSUP) { 252143466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 252243466aaeSMax Grossman SPA_FEATURE_DISABLED; 252343466aaeSMax Grossman } else { 252443466aaeSMax Grossman return (spa_vdev_err(rvd, 252543466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 252643466aaeSMax Grossman } 252743466aaeSMax Grossman } 252843466aaeSMax Grossman } 252943466aaeSMax Grossman 253043466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 253143466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 25325d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 253343466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2534ad135b5dSChristopher Siden } 2535ad135b5dSChristopher Siden 2536ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2537ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2538ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2539ad135b5dSChristopher Siden if (error != 0) 2540ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2541ad135b5dSChristopher Siden 2542fa9e4066Sahrens if (!mosconfig) { 254395173954Sek uint64_t hostid; 2544871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2545871a9500SMark J Musante 2546871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2547871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2548fa9e4066Sahrens 254988ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 255077650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 255195173954Sek char *hostname; 255295173954Sek unsigned long myhostid = 0; 255395173954Sek 255488ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 255595173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 255695173954Sek 25575679c89fSjv #ifdef _KERNEL 25585679c89fSjv myhostid = zone_get_hostid(NULL); 25595679c89fSjv #else /* _KERNEL */ 25605679c89fSjv /* 25615679c89fSjv * We're emulating the system's hostid in userland, so 25625679c89fSjv * we can't use zone_get_hostid(). 25635679c89fSjv */ 256495173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 25655679c89fSjv #endif /* _KERNEL */ 256617194a52Slling if (hostid != 0 && myhostid != 0 && 25675679c89fSjv hostid != myhostid) { 2568871a9500SMark J Musante nvlist_free(nvconfig); 256995173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 257095173954Sek "loaded as it was last accessed by " 257177650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2572654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2573e14bb325SJeff Bonwick spa_name(spa), hostname, 257495173954Sek (unsigned long)hostid); 2575be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 257695173954Sek } 257795173954Sek } 2578c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2579c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2580c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2581c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 258295173954Sek 258388ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2584fa9e4066Sahrens spa_unload(spa); 2585fa9e4066Sahrens spa_deactivate(spa); 25868ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2587fa9e4066Sahrens 25881195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2589fa9e4066Sahrens } 2590fa9e4066Sahrens 259145818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 259245818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 259345818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 259445818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 259545818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 259645818ee1SMatthew Ahrens if (error == ENOENT) { 259745818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 259845818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 259945818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 260045818ee1SMatthew Ahrens } else if (error != 0) { 260145818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 260245818ee1SMatthew Ahrens } 260345818ee1SMatthew Ahrens 2604cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2605cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2606cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2607cde58dbcSMatthew Ahrens if (error != 0) 26081195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2609fa9e4066Sahrens 261099653d4eSeschrock /* 261199653d4eSeschrock * Load the bit that tells us to use the new accounting function 261299653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 261399653d4eSeschrock * be present. 261499653d4eSeschrock */ 26151195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 26161195e687SMark J Musante if (error != 0 && error != ENOENT) 26171195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 261899653d4eSeschrock 26193f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 26203f9d6ad7SLin Ling &spa->spa_creation_version); 26213f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 26223f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26233f9d6ad7SLin Ling 2624fa9e4066Sahrens /* 2625ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2626ea8dc4b6Seschrock * not be present. 2627fa9e4066Sahrens */ 26281195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 26291195e687SMark J Musante if (error != 0 && error != ENOENT) 26301195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2631ea8dc4b6Seschrock 26321195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 26331195e687SMark J Musante &spa->spa_errlog_scrub); 26341195e687SMark J Musante if (error != 0 && error != ENOENT) 26351195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2636ea8dc4b6Seschrock 263706eeb2adSek /* 263806eeb2adSek * Load the history object. If we have an older pool, this 263906eeb2adSek * will not be present. 264006eeb2adSek */ 26411195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 26421195e687SMark J Musante if (error != 0 && error != ENOENT) 26431195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26441195e687SMark J Musante 2645215198a6SJoe Stein /* 2646215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2647215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2648215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2649215198a6SJoe Stein * spa_sync_config_object. 2650215198a6SJoe Stein */ 2651215198a6SJoe Stein 2652215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2653215198a6SJoe Stein nvlist_t *mos_config; 2654215198a6SJoe Stein if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2655215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2656215198a6SJoe Stein 2657215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 2658215198a6SJoe Stein &spa->spa_all_vdev_zaps); 2659215198a6SJoe Stein 2660555da511SPaul Dagnelie if (error == ENOENT) { 2661555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 2662555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 2663555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 2664555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2665555da511SPaul Dagnelie } else if (error != 0) { 2666215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2667555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2668215198a6SJoe Stein /* 2669215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2670215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2671215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2672215198a6SJoe Stein */ 2673215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 2674215198a6SJoe Stein /* 2675215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 2676215198a6SJoe Stein * before this. Better be sure of it. 2677215198a6SJoe Stein */ 2678215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2679215198a6SJoe Stein } 2680215198a6SJoe Stein nvlist_free(mos_config); 2681215198a6SJoe Stein 26821195e687SMark J Musante /* 26831195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 26841195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 26851195e687SMark J Musante * devices. 26861195e687SMark J Musante */ 268706eeb2adSek 268899653d4eSeschrock /* 268999653d4eSeschrock * Load any hot spares for this pool. 269099653d4eSeschrock */ 26911195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 26921195e687SMark J Musante if (error != 0 && error != ENOENT) 26931195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26941195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2695e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2696fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 26971195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 26981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 269999653d4eSeschrock 2700e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 270199653d4eSeschrock spa_load_spares(spa); 2702e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27031195e687SMark J Musante } else if (error == 0) { 27041195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 270599653d4eSeschrock } 270699653d4eSeschrock 2707fa94a07fSbrendan /* 2708fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2709fa94a07fSbrendan */ 27101195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2711fa94a07fSbrendan &spa->spa_l2cache.sav_object); 27121195e687SMark J Musante if (error != 0 && error != ENOENT) 27131195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27141195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2715fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2716fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 27171195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 27181195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2719fa94a07fSbrendan 2720e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2721fa94a07fSbrendan spa_load_l2cache(spa); 2722e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27231195e687SMark J Musante } else if (error == 0) { 27241195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2725fa94a07fSbrendan } 2726fa94a07fSbrendan 2727990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2728ecd6cf80Smarks 27291195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 27301195e687SMark J Musante if (error && error != ENOENT) 27311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2732b1b8ab34Slling 2733b1b8ab34Slling if (error == 0) { 27341195e687SMark J Musante uint64_t autoreplace; 27351195e687SMark J Musante 27361195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 27371195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 27381195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 27391195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 27401195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 27417855d95bSToomas Soome spa_prop_find(spa, ZPOOL_PROP_BOOTSIZE, &spa->spa_bootsize); 27421195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 27431195e687SMark J Musante &spa->spa_dedup_ditto); 27441195e687SMark J Musante 2745b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2746b1b8ab34Slling } 2747b1b8ab34Slling 27483d7072f8Seschrock /* 27493d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 27503d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 27513d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 27523d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 27533d7072f8Seschrock * over. 27543d7072f8Seschrock */ 2755b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 27563d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2757b693757aSEric Schrock /* 2758b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2759b693757aSEric Schrock * at this point we're using the spare definitions from 2760b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2761b693757aSEric Schrock */ 2762b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2763b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2764b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2765b693757aSEric Schrock } 2766b693757aSEric Schrock } 27673d7072f8Seschrock 2768ea8dc4b6Seschrock /* 2769560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2770ea8dc4b6Seschrock */ 2771560e6e96Seschrock vdev_load(rvd); 27720373e76bSbonwick 2773fa9e4066Sahrens /* 2774fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2775fa9e4066Sahrens */ 2776e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2777fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2778e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2779fa9e4066Sahrens 2780b24ab676SJeff Bonwick /* 2781b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2782b24ab676SJeff Bonwick */ 2783b24ab676SJeff Bonwick error = ddt_load(spa); 27841195e687SMark J Musante if (error != 0) 27851195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2786b24ab676SJeff Bonwick 2787485bbbf5SGeorge Wilson spa_update_dspace(spa); 2788485bbbf5SGeorge Wilson 2789b24ab676SJeff Bonwick /* 27904b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 27914b964adaSGeorge Wilson * information which might be missing. If we fail to validate 27924b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 27934b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 27944b964adaSGeorge Wilson * over. 2795b24ab676SJeff Bonwick */ 27961195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2797871a9500SMark J Musante nvlist_t *nvconfig; 2798871a9500SMark J Musante 2799871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2800871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2801871a9500SMark J Musante 28024b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 28034b964adaSGeorge Wilson nvlist_free(nvconfig); 28044b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 28054b964adaSGeorge Wilson ENXIO)); 28064b964adaSGeorge Wilson } 28071195e687SMark J Musante nvlist_free(nvconfig); 28081195e687SMark J Musante 28094b964adaSGeorge Wilson /* 2810ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 28114b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 28124b964adaSGeorge Wilson * more toplevel vdevs are faulted. 28134b964adaSGeorge Wilson */ 28144b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2815be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 28164b964adaSGeorge Wilson 281723367a2fSMatthew Ahrens if (spa_writeable(spa) && spa_check_logs(spa)) { 28181195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 28191195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 28201195e687SMark J Musante } 2821b24ab676SJeff Bonwick } 2822b24ab676SJeff Bonwick 2823ad135b5dSChristopher Siden if (missing_feat_write) { 2824ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2825ad135b5dSChristopher Siden 2826ad135b5dSChristopher Siden /* 2827ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2828ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2829ad135b5dSChristopher Siden * information and can return to userland. 2830ad135b5dSChristopher Siden */ 2831ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2832ad135b5dSChristopher Siden } 2833ad135b5dSChristopher Siden 28344b964adaSGeorge Wilson /* 28354b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 28364b964adaSGeorge Wilson * to start pushing transactions. 28374b964adaSGeorge Wilson */ 28384b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 28394b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 28404b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 28414b964adaSGeorge Wilson error)); 28424b964adaSGeorge Wilson } 28434b964adaSGeorge Wilson 2844468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2845468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 28465dabedeeSbonwick dmu_tx_t *tx; 28470373e76bSbonwick int need_update = B_FALSE; 284812380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 28498ad4d6ddSJeff Bonwick 28508ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 28515dabedeeSbonwick 28520373e76bSbonwick /* 28530373e76bSbonwick * Claim log blocks that haven't been committed yet. 28540373e76bSbonwick * This must all happen in a single txg. 2855b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2856b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2857468c413aSTim Haley * Price of rollback is that we abandon the log. 28580373e76bSbonwick */ 2859b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2860b24ab676SJeff Bonwick 286112380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 286212380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 28630b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2864fa9e4066Sahrens dmu_tx_commit(tx); 2865fa9e4066Sahrens 2866b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2867b24ab676SJeff Bonwick 28681195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2869fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2870fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2871fa9e4066Sahrens 2872fa9e4066Sahrens /* 2873b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2874b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2875b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2876b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2877b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2878fa9e4066Sahrens */ 2879b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 28800e34b6a7Sbonwick 28810e34b6a7Sbonwick /* 28820373e76bSbonwick * If the config cache is stale, or we have uninitialized 28830373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2884bc758434SLin Ling * 28854b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2886bc758434SLin Ling * in-core spa_config and update the disk labels. 28870e34b6a7Sbonwick */ 28880373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 28894b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 28904b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 28914b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 28920373e76bSbonwick need_update = B_TRUE; 28930373e76bSbonwick 28948ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 28950373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 28960373e76bSbonwick need_update = B_TRUE; 28970e34b6a7Sbonwick 28980e34b6a7Sbonwick /* 28990373e76bSbonwick * Update the config cache asychronously in case we're the 29000373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 29010e34b6a7Sbonwick */ 29020373e76bSbonwick if (need_update) 29030373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 29048ad4d6ddSJeff Bonwick 29058ad4d6ddSJeff Bonwick /* 29068ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 29078ad4d6ddSJeff Bonwick */ 29083f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 29093f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 29108ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2911503ad85cSMatthew Ahrens 29124445fffbSMatthew Ahrens /* 29134445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 29144445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 29154445fffbSMatthew Ahrens */ 29164445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 29174445fffbSMatthew Ahrens 2918503ad85cSMatthew Ahrens /* 2919503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2920503ad85cSMatthew Ahrens */ 2921503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2922503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2923ca45db41SChris Kirby 2924ca45db41SChris Kirby /* 2925ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2926ca45db41SChris Kirby */ 2927ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2928fa9e4066Sahrens } 2929fa9e4066Sahrens 29301195e687SMark J Musante return (0); 2931fa9e4066Sahrens } 2932fa9e4066Sahrens 2933468c413aSTim Haley static int 2934468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2935468c413aSTim Haley { 2936f9af39baSGeorge Wilson int mode = spa->spa_mode; 2937f9af39baSGeorge Wilson 2938468c413aSTim Haley spa_unload(spa); 2939468c413aSTim Haley spa_deactivate(spa); 2940468c413aSTim Haley 2941e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 2942468c413aSTim Haley 2943f9af39baSGeorge Wilson spa_activate(spa, mode); 2944468c413aSTim Haley spa_async_suspend(spa); 2945468c413aSTim Haley 29461195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2947468c413aSTim Haley } 2948468c413aSTim Haley 2949ad135b5dSChristopher Siden /* 2950ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2951ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2952ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2953ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2954ad135b5dSChristopher Siden * spa_load(). 2955ad135b5dSChristopher Siden */ 2956468c413aSTim Haley static int 2957468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2958c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2959468c413aSTim Haley { 2960ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2961468c413aSTim Haley nvlist_t *config = NULL; 2962468c413aSTim Haley int load_error, rewind_error; 2963c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2964468c413aSTim Haley uint64_t min_txg; 2965468c413aSTim Haley 2966a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2967468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 29681195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2969a33cae98STim Haley } else { 2970468c413aSTim Haley spa->spa_load_max_txg = max_request; 2971e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 2972e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 2973a33cae98STim Haley } 2974468c413aSTim Haley 29751195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 29761195e687SMark J Musante mosconfig); 2977468c413aSTim Haley if (load_error == 0) 2978468c413aSTim Haley return (0); 2979468c413aSTim Haley 2980468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2981468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2982468c413aSTim Haley 2983468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2984468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2985468c413aSTim Haley 2986c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2987468c413aSTim Haley nvlist_free(config); 2988468c413aSTim Haley return (load_error); 2989468c413aSTim Haley } 2990468c413aSTim Haley 2991ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2992ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 29931195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2994ad135b5dSChristopher Siden } else { 2995ad135b5dSChristopher Siden /* 2996ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2997ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2998ad135b5dSChristopher Siden * to rewind. 2999ad135b5dSChristopher Siden */ 3000ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3001ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3002ad135b5dSChristopher Siden } 3003468c413aSTim Haley 3004c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3005c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3006c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3007c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3008468c413aSTim Haley 3009c8ee1847SVictor Latushkin /* 3010c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3011c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3012c8ee1847SVictor Latushkin */ 3013c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3014c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3015c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3016468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 3017468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 3018468c413aSTim Haley } 3019468c413aSTim Haley 3020468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3021468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3022468c413aSTim Haley 3023468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3024468c413aSTim Haley spa_config_set(spa, config); 3025468c413aSTim Haley 3026ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3027ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3028ad135b5dSChristopher Siden return (rewind_error); 3029ad135b5dSChristopher Siden } else { 3030ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3031ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3032ad135b5dSChristopher Siden spa->spa_load_info); 3033ad135b5dSChristopher Siden 3034ad135b5dSChristopher Siden /* Restore the initial load info */ 3035ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3036ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3037ad135b5dSChristopher Siden 3038ad135b5dSChristopher Siden return (load_error); 3039ad135b5dSChristopher Siden } 3040468c413aSTim Haley } 3041468c413aSTim Haley 3042fa9e4066Sahrens /* 3043fa9e4066Sahrens * Pool Open/Import 3044fa9e4066Sahrens * 3045fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3046fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3047fa9e4066Sahrens * case of an open, the pool configuration will exist in the 30483d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3049fa9e4066Sahrens * 3050fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3051fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3052fa9e4066Sahrens * ambiguous state. 3053fa9e4066Sahrens */ 3054fa9e4066Sahrens static int 3055468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3056468c413aSTim Haley nvlist_t **config) 3057fa9e4066Sahrens { 3058fa9e4066Sahrens spa_t *spa; 30594b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3060fa9e4066Sahrens int error; 3061fa9e4066Sahrens int locked = B_FALSE; 3062fa9e4066Sahrens 3063fa9e4066Sahrens *spapp = NULL; 3064fa9e4066Sahrens 3065fa9e4066Sahrens /* 3066fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3067fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3068fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3069fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3070fa9e4066Sahrens */ 3071fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3072fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3073fa9e4066Sahrens locked = B_TRUE; 3074fa9e4066Sahrens } 3075fa9e4066Sahrens 3076fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3077fa9e4066Sahrens if (locked) 3078fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3079be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3080fa9e4066Sahrens } 3081468c413aSTim Haley 3082fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 30834b44c88cSTim Haley zpool_rewind_policy_t policy; 30844b44c88cSTim Haley 30854b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 30864b44c88cSTim Haley &policy); 30874b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 30884b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3089fa9e4066Sahrens 30908ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3091fa9e4066Sahrens 3092468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3093468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3094468c413aSTim Haley 3095468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 3096c8ee1847SVictor Latushkin policy.zrp_request); 3097fa9e4066Sahrens 3098fa9e4066Sahrens if (error == EBADF) { 3099fa9e4066Sahrens /* 3100560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3101560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3102560e6e96Seschrock * that the pool has been exported or destroyed. If 3103560e6e96Seschrock * this is the case, the config cache is out of sync and 3104560e6e96Seschrock * we should remove the pool from the namespace. 3105fa9e4066Sahrens */ 3106fa9e4066Sahrens spa_unload(spa); 3107fa9e4066Sahrens spa_deactivate(spa); 3108c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 3109fa9e4066Sahrens spa_remove(spa); 3110fa9e4066Sahrens if (locked) 3111fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3112be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3113ea8dc4b6Seschrock } 3114ea8dc4b6Seschrock 3115ea8dc4b6Seschrock if (error) { 3116fa9e4066Sahrens /* 3117fa9e4066Sahrens * We can't open the pool, but we still have useful 3118fa9e4066Sahrens * information: the state of each vdev after the 3119fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3120fa9e4066Sahrens */ 31214b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3122468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3123468c413aSTim Haley KM_SLEEP) == 0); 31244b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 31254b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 31264b964adaSGeorge Wilson spa->spa_load_info) == 0); 31274b964adaSGeorge Wilson } 3128fa9e4066Sahrens spa_unload(spa); 3129fa9e4066Sahrens spa_deactivate(spa); 3130468c413aSTim Haley spa->spa_last_open_failed = error; 3131fa9e4066Sahrens if (locked) 3132fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3133fa9e4066Sahrens *spapp = NULL; 3134fa9e4066Sahrens return (error); 3135fa9e4066Sahrens } 3136fa9e4066Sahrens } 3137fa9e4066Sahrens 3138fa9e4066Sahrens spa_open_ref(spa, tag); 31393d7072f8Seschrock 3140468c413aSTim Haley if (config != NULL) 3141468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3142468c413aSTim Haley 31434b964adaSGeorge Wilson /* 31444b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 31454b964adaSGeorge Wilson * gathered while doing the load. 31464b964adaSGeorge Wilson */ 31474b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 31484b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 31494b964adaSGeorge Wilson spa->spa_load_info) == 0); 31504b964adaSGeorge Wilson } 31514b964adaSGeorge Wilson 3152a33cae98STim Haley if (locked) { 3153a33cae98STim Haley spa->spa_last_open_failed = 0; 3154a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3155a33cae98STim Haley spa->spa_load_txg = 0; 3156fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3157a33cae98STim Haley } 3158fa9e4066Sahrens 3159fa9e4066Sahrens *spapp = spa; 3160fa9e4066Sahrens 3161fa9e4066Sahrens return (0); 3162fa9e4066Sahrens } 3163fa9e4066Sahrens 3164468c413aSTim Haley int 3165468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3166468c413aSTim Haley nvlist_t **config) 3167468c413aSTim Haley { 3168468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3169468c413aSTim Haley } 3170468c413aSTim Haley 3171fa9e4066Sahrens int 3172fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3173fa9e4066Sahrens { 3174468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3175fa9e4066Sahrens } 3176fa9e4066Sahrens 3177ea8dc4b6Seschrock /* 3178ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3179ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3180ea8dc4b6Seschrock */ 3181ea8dc4b6Seschrock spa_t * 3182ea8dc4b6Seschrock spa_inject_addref(char *name) 3183ea8dc4b6Seschrock { 3184ea8dc4b6Seschrock spa_t *spa; 3185ea8dc4b6Seschrock 3186ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3187ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3188ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3189ea8dc4b6Seschrock return (NULL); 3190ea8dc4b6Seschrock } 3191ea8dc4b6Seschrock spa->spa_inject_ref++; 3192ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3193ea8dc4b6Seschrock 3194ea8dc4b6Seschrock return (spa); 3195ea8dc4b6Seschrock } 3196ea8dc4b6Seschrock 3197ea8dc4b6Seschrock void 3198ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3199ea8dc4b6Seschrock { 3200ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3201ea8dc4b6Seschrock spa->spa_inject_ref--; 3202ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3203ea8dc4b6Seschrock } 3204ea8dc4b6Seschrock 3205fa94a07fSbrendan /* 3206fa94a07fSbrendan * Add spares device information to the nvlist. 3207fa94a07fSbrendan */ 320899653d4eSeschrock static void 320999653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 321099653d4eSeschrock { 321199653d4eSeschrock nvlist_t **spares; 321299653d4eSeschrock uint_t i, nspares; 321399653d4eSeschrock nvlist_t *nvroot; 321499653d4eSeschrock uint64_t guid; 321599653d4eSeschrock vdev_stat_t *vs; 321699653d4eSeschrock uint_t vsc; 321739c23413Seschrock uint64_t pool; 321899653d4eSeschrock 32196809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32206809eb4eSEric Schrock 3221fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 322299653d4eSeschrock return; 322399653d4eSeschrock 322499653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 322599653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3226fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 322799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 322899653d4eSeschrock if (nspares != 0) { 322999653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 323099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 323199653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 323299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 323399653d4eSeschrock 323499653d4eSeschrock /* 323599653d4eSeschrock * Go through and find any spares which have since been 323699653d4eSeschrock * repurposed as an active spare. If this is the case, update 323799653d4eSeschrock * their status appropriately. 323899653d4eSeschrock */ 323999653d4eSeschrock for (i = 0; i < nspares; i++) { 324099653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 324199653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 324289a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 324389a89ebfSlling pool != 0ULL) { 324499653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 32453f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 324699653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 324799653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 324899653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 324999653d4eSeschrock } 325099653d4eSeschrock } 325199653d4eSeschrock } 325299653d4eSeschrock } 325399653d4eSeschrock 3254fa94a07fSbrendan /* 3255fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3256fa94a07fSbrendan */ 3257fa94a07fSbrendan static void 3258fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3259fa94a07fSbrendan { 3260fa94a07fSbrendan nvlist_t **l2cache; 3261fa94a07fSbrendan uint_t i, j, nl2cache; 3262fa94a07fSbrendan nvlist_t *nvroot; 3263fa94a07fSbrendan uint64_t guid; 3264fa94a07fSbrendan vdev_t *vd; 3265fa94a07fSbrendan vdev_stat_t *vs; 3266fa94a07fSbrendan uint_t vsc; 3267fa94a07fSbrendan 32686809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32696809eb4eSEric Schrock 3270fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3271fa94a07fSbrendan return; 3272fa94a07fSbrendan 3273fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3274fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3275fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3276fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3277fa94a07fSbrendan if (nl2cache != 0) { 3278fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3279fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3280fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3281fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3282fa94a07fSbrendan 3283fa94a07fSbrendan /* 3284fa94a07fSbrendan * Update level 2 cache device stats. 3285fa94a07fSbrendan */ 3286fa94a07fSbrendan 3287fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3288fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3289fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3290fa94a07fSbrendan 3291fa94a07fSbrendan vd = NULL; 3292fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3293fa94a07fSbrendan if (guid == 3294fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3295fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3296fa94a07fSbrendan break; 3297fa94a07fSbrendan } 3298fa94a07fSbrendan } 3299fa94a07fSbrendan ASSERT(vd != NULL); 3300fa94a07fSbrendan 3301fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 33023f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 33033f9d6ad7SLin Ling == 0); 3304fa94a07fSbrendan vdev_get_stats(vd, vs); 3305fa94a07fSbrendan } 3306fa94a07fSbrendan } 3307fa94a07fSbrendan } 3308fa94a07fSbrendan 3309ad135b5dSChristopher Siden static void 3310ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3311ad135b5dSChristopher Siden { 3312ad135b5dSChristopher Siden nvlist_t *features; 3313ad135b5dSChristopher Siden zap_cursor_t zc; 3314ad135b5dSChristopher Siden zap_attribute_t za; 3315ad135b5dSChristopher Siden 3316ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3317ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3318ad135b5dSChristopher Siden 3319ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3320ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3321ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3322ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3323ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3324ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3325ad135b5dSChristopher Siden za.za_num_integers == 1); 3326b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3327ad135b5dSChristopher Siden za.za_first_integer)); 3328ad135b5dSChristopher Siden } 3329ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3330ad135b5dSChristopher Siden } 3331ad135b5dSChristopher Siden 3332ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3333ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3334ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3335ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3336ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3337ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3338ad135b5dSChristopher Siden za.za_num_integers == 1); 3339b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3340ad135b5dSChristopher Siden za.za_first_integer)); 3341ad135b5dSChristopher Siden } 3342ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3343ad135b5dSChristopher Siden } 3344ad135b5dSChristopher Siden 3345ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3346ad135b5dSChristopher Siden features) == 0); 3347ad135b5dSChristopher Siden nvlist_free(features); 3348ad135b5dSChristopher Siden } 3349ad135b5dSChristopher Siden 3350fa9e4066Sahrens int 3351ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3352ad135b5dSChristopher Siden char *altroot, size_t buflen) 3353fa9e4066Sahrens { 3354fa9e4066Sahrens int error; 3355fa9e4066Sahrens spa_t *spa; 3356fa9e4066Sahrens 3357fa9e4066Sahrens *config = NULL; 3358468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3359fa9e4066Sahrens 33606809eb4eSEric Schrock if (spa != NULL) { 33616809eb4eSEric Schrock /* 33626809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 33636809eb4eSEric Schrock * or l2cache devices could change and the config would be 33646809eb4eSEric Schrock * self-inconsistent. 33656809eb4eSEric Schrock */ 33666809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3367ea8dc4b6Seschrock 33686809eb4eSEric Schrock if (*config != NULL) { 336911027bc7STim Haley uint64_t loadtimes[2]; 337011027bc7STim Haley 337111027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 337211027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 337311027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 337411027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 337511027bc7STim Haley 3376e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 33776809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 33786809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3379e14bb325SJeff Bonwick 33806809eb4eSEric Schrock if (spa_suspended(spa)) 33816809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 33826809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 33836809eb4eSEric Schrock spa->spa_failmode) == 0); 33846809eb4eSEric Schrock 33856809eb4eSEric Schrock spa_add_spares(spa, *config); 33866809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3387ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 33886809eb4eSEric Schrock } 338999653d4eSeschrock } 339099653d4eSeschrock 3391ea8dc4b6Seschrock /* 3392ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3393ea8dc4b6Seschrock * and call spa_lookup() directly. 3394ea8dc4b6Seschrock */ 3395ea8dc4b6Seschrock if (altroot) { 3396ea8dc4b6Seschrock if (spa == NULL) { 3397ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3398ea8dc4b6Seschrock spa = spa_lookup(name); 3399ea8dc4b6Seschrock if (spa) 3400ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3401ea8dc4b6Seschrock else 3402ea8dc4b6Seschrock altroot[0] = '\0'; 3403ea8dc4b6Seschrock spa = NULL; 3404ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3405ea8dc4b6Seschrock } else { 3406ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3407ea8dc4b6Seschrock } 3408ea8dc4b6Seschrock } 3409ea8dc4b6Seschrock 34106809eb4eSEric Schrock if (spa != NULL) { 34116809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3412fa9e4066Sahrens spa_close(spa, FTAG); 34136809eb4eSEric Schrock } 3414fa9e4066Sahrens 3415fa9e4066Sahrens return (error); 3416fa9e4066Sahrens } 3417fa9e4066Sahrens 341899653d4eSeschrock /* 3419fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3420fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3421fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3422fa94a07fSbrendan * specified, as long as they are well-formed. 342399653d4eSeschrock */ 342499653d4eSeschrock static int 3425fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3426fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3427fa94a07fSbrendan vdev_labeltype_t label) 342899653d4eSeschrock { 3429fa94a07fSbrendan nvlist_t **dev; 3430fa94a07fSbrendan uint_t i, ndev; 343199653d4eSeschrock vdev_t *vd; 343299653d4eSeschrock int error; 343399653d4eSeschrock 3434e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3435e14bb325SJeff Bonwick 343699653d4eSeschrock /* 3437fa94a07fSbrendan * It's acceptable to have no devs specified. 343899653d4eSeschrock */ 3439fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 344099653d4eSeschrock return (0); 344199653d4eSeschrock 3442fa94a07fSbrendan if (ndev == 0) 3443be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 344499653d4eSeschrock 344599653d4eSeschrock /* 3446fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3447fa94a07fSbrendan * device type. 344899653d4eSeschrock */ 3449fa94a07fSbrendan if (spa_version(spa) < version) 3450be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 345199653d4eSeschrock 345239c23413Seschrock /* 3453fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 345439c23413Seschrock * checking. 345539c23413Seschrock */ 3456fa94a07fSbrendan sav->sav_pending = dev; 3457fa94a07fSbrendan sav->sav_npending = ndev; 345839c23413Seschrock 3459fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3460fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 346199653d4eSeschrock mode)) != 0) 346239c23413Seschrock goto out; 346399653d4eSeschrock 346499653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 346599653d4eSeschrock vdev_free(vd); 3466be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 346739c23413Seschrock goto out; 346899653d4eSeschrock } 346999653d4eSeschrock 3470fa94a07fSbrendan /* 3471e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3472e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3473fa94a07fSbrendan */ 3474e14bb325SJeff Bonwick #ifdef _KERNEL 3475fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3476fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3477be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3478cd0837ccSGeorge Wilson vdev_free(vd); 3479fa94a07fSbrendan goto out; 3480fa94a07fSbrendan } 3481e14bb325SJeff Bonwick #endif 348299653d4eSeschrock vd->vdev_top = vd; 348399653d4eSeschrock 348439c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3485fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3486fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 348739c23413Seschrock vd->vdev_guid) == 0); 348839c23413Seschrock } 348999653d4eSeschrock 349099653d4eSeschrock vdev_free(vd); 349139c23413Seschrock 3492fa94a07fSbrendan if (error && 3493fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 349439c23413Seschrock goto out; 349539c23413Seschrock else 349639c23413Seschrock error = 0; 349799653d4eSeschrock } 349899653d4eSeschrock 349939c23413Seschrock out: 3500fa94a07fSbrendan sav->sav_pending = NULL; 3501fa94a07fSbrendan sav->sav_npending = 0; 350239c23413Seschrock return (error); 350399653d4eSeschrock } 350499653d4eSeschrock 3505fa94a07fSbrendan static int 3506fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3507fa94a07fSbrendan { 3508fa94a07fSbrendan int error; 3509fa94a07fSbrendan 3510e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3511e14bb325SJeff Bonwick 3512fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3513fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3514fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3515fa94a07fSbrendan return (error); 3516fa94a07fSbrendan } 3517fa94a07fSbrendan 3518fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3519fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3520fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3521fa94a07fSbrendan } 3522fa94a07fSbrendan 3523fa94a07fSbrendan static void 3524fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3525fa94a07fSbrendan const char *config) 3526fa94a07fSbrendan { 3527fa94a07fSbrendan int i; 3528fa94a07fSbrendan 3529fa94a07fSbrendan if (sav->sav_config != NULL) { 3530fa94a07fSbrendan nvlist_t **olddevs; 3531fa94a07fSbrendan uint_t oldndevs; 3532fa94a07fSbrendan nvlist_t **newdevs; 3533fa94a07fSbrendan 3534fa94a07fSbrendan /* 3535fa94a07fSbrendan * Generate new dev list by concatentating with the 3536fa94a07fSbrendan * current dev list. 3537fa94a07fSbrendan */ 3538fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3539fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3540fa94a07fSbrendan 3541fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3542fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3543fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3544fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3545fa94a07fSbrendan KM_SLEEP) == 0); 3546fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3547fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3548fa94a07fSbrendan KM_SLEEP) == 0); 3549fa94a07fSbrendan 3550fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3551fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3552fa94a07fSbrendan 3553fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3554fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3555fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3556fa94a07fSbrendan nvlist_free(newdevs[i]); 3557fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3558fa94a07fSbrendan } else { 3559fa94a07fSbrendan /* 3560fa94a07fSbrendan * Generate a new dev list. 3561fa94a07fSbrendan */ 3562fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3563fa94a07fSbrendan KM_SLEEP) == 0); 3564fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3565fa94a07fSbrendan devs, ndevs) == 0); 3566fa94a07fSbrendan } 3567fa94a07fSbrendan } 3568fa94a07fSbrendan 3569fa94a07fSbrendan /* 3570fa94a07fSbrendan * Stop and drop level 2 ARC devices 3571fa94a07fSbrendan */ 3572fa94a07fSbrendan void 3573fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3574fa94a07fSbrendan { 3575fa94a07fSbrendan vdev_t *vd; 3576fa94a07fSbrendan int i; 3577fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3578fa94a07fSbrendan 3579fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3580fa94a07fSbrendan uint64_t pool; 3581fa94a07fSbrendan 3582fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3583fa94a07fSbrendan ASSERT(vd != NULL); 3584fa94a07fSbrendan 35858ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 35868ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3587fa94a07fSbrendan l2arc_remove_vdev(vd); 3588fa94a07fSbrendan } 3589fa94a07fSbrendan } 3590fa94a07fSbrendan 3591fa9e4066Sahrens /* 3592fa9e4066Sahrens * Pool Creation 3593fa9e4066Sahrens */ 3594fa9e4066Sahrens int 3595990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 35964445fffbSMatthew Ahrens nvlist_t *zplprops) 3597fa9e4066Sahrens { 3598fa9e4066Sahrens spa_t *spa; 3599990b4856Slling char *altroot = NULL; 36000373e76bSbonwick vdev_t *rvd; 3601fa9e4066Sahrens dsl_pool_t *dp; 3602fa9e4066Sahrens dmu_tx_t *tx; 3603573ca77eSGeorge Wilson int error = 0; 3604fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3605fa94a07fSbrendan nvlist_t **spares, **l2cache; 3606fa94a07fSbrendan uint_t nspares, nl2cache; 3607cde58dbcSMatthew Ahrens uint64_t version, obj; 3608ad135b5dSChristopher Siden boolean_t has_features; 3609fa9e4066Sahrens 3610fa9e4066Sahrens /* 3611fa9e4066Sahrens * If this pool already exists, return failure. 3612fa9e4066Sahrens */ 3613fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3614fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3615fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3616be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3617fa9e4066Sahrens } 3618fa9e4066Sahrens 3619fa9e4066Sahrens /* 3620fa9e4066Sahrens * Allocate a new spa_t structure. 3621fa9e4066Sahrens */ 3622990b4856Slling (void) nvlist_lookup_string(props, 3623990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3624468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 36258ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3626fa9e4066Sahrens 3627990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3628990b4856Slling spa_deactivate(spa); 3629990b4856Slling spa_remove(spa); 3630c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3631990b4856Slling return (error); 3632990b4856Slling } 3633990b4856Slling 3634ad135b5dSChristopher Siden has_features = B_FALSE; 3635ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3636ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3637ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3638ad135b5dSChristopher Siden has_features = B_TRUE; 3639ad135b5dSChristopher Siden } 3640ad135b5dSChristopher Siden 3641ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3642ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3643990b4856Slling version = SPA_VERSION; 3644ad135b5dSChristopher Siden } 3645ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3646b24ab676SJeff Bonwick 3647b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3648b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3649990b4856Slling spa->spa_uberblock.ub_version = version; 3650fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 36510f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 3652fa9e4066Sahrens 365354d692b7SGeorge Wilson /* 365454d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 365554d692b7SGeorge Wilson */ 36566f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 36576f834bc1SMatthew Ahrens KM_SLEEP); 36586f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 36596f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 36606f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 36616f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 36626f834bc1SMatthew Ahrens } 366354d692b7SGeorge Wilson 36640373e76bSbonwick /* 36650373e76bSbonwick * Create the root vdev. 36660373e76bSbonwick */ 3667e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 36680373e76bSbonwick 366999653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 36700373e76bSbonwick 367199653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 367299653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 36730373e76bSbonwick 3674b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3675be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 367699653d4eSeschrock 367799653d4eSeschrock if (error == 0 && 367899653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3679fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 368099653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3681573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3682573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3683573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3684573ca77eSGeorge Wilson } 36850373e76bSbonwick } 36860373e76bSbonwick 3687e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3688fa9e4066Sahrens 368999653d4eSeschrock if (error != 0) { 3690fa9e4066Sahrens spa_unload(spa); 3691fa9e4066Sahrens spa_deactivate(spa); 3692fa9e4066Sahrens spa_remove(spa); 3693fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3694fa9e4066Sahrens return (error); 3695fa9e4066Sahrens } 3696fa9e4066Sahrens 369799653d4eSeschrock /* 369899653d4eSeschrock * Get the list of spares, if specified. 369999653d4eSeschrock */ 370099653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 370199653d4eSeschrock &spares, &nspares) == 0) { 3702fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 370399653d4eSeschrock KM_SLEEP) == 0); 3704fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 370599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3706e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 370799653d4eSeschrock spa_load_spares(spa); 3708e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3709fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3710fa94a07fSbrendan } 3711fa94a07fSbrendan 3712fa94a07fSbrendan /* 3713fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3714fa94a07fSbrendan */ 3715fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3716fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3717fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3718fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3719fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3720fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3721e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3722fa94a07fSbrendan spa_load_l2cache(spa); 3723e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3724fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 372599653d4eSeschrock } 372699653d4eSeschrock 3727ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 37280a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3729fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3730ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3731fa9e4066Sahrens 3732485bbbf5SGeorge Wilson /* 3733485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3734485bbbf5SGeorge Wilson */ 3735485bbbf5SGeorge Wilson ddt_create(spa); 3736485bbbf5SGeorge Wilson 3737485bbbf5SGeorge Wilson spa_update_dspace(spa); 3738485bbbf5SGeorge Wilson 3739fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3740fa9e4066Sahrens 3741fa9e4066Sahrens /* 3742fa9e4066Sahrens * Create the pool config object. 3743fa9e4066Sahrens */ 3744fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3745f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3746fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3747fa9e4066Sahrens 3748ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3749fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3750ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3751ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3752ea8dc4b6Seschrock } 3753fa9e4066Sahrens 3754ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3755ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3756ad135b5dSChristopher Siden 37573f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 37583f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 37593f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 37603f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 37613f9d6ad7SLin Ling } 37623f9d6ad7SLin Ling 3763990b4856Slling /* Newly created pools with the right version are always deflated. */ 3764990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3765990b4856Slling spa->spa_deflate = TRUE; 3766990b4856Slling if (zap_add(spa->spa_meta_objset, 3767990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3768990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3769990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3770990b4856Slling } 377199653d4eSeschrock } 377299653d4eSeschrock 3773fa9e4066Sahrens /* 3774cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3775fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3776fa9e4066Sahrens * keeps changing. 3777fa9e4066Sahrens */ 3778cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3779cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3780cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3781ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3782cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3783cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3784cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3785ea8dc4b6Seschrock } 3786b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3787cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3788fa9e4066Sahrens 378906eeb2adSek /* 379006eeb2adSek * Create the pool's history object. 379106eeb2adSek */ 3792990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3793990b4856Slling spa_history_create_obj(spa, tx); 3794990b4856Slling 379545818ee1SMatthew Ahrens /* 379645818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 379745818ee1SMatthew Ahrens */ 379845818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 379945818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 380045818ee1SMatthew Ahrens 3801990b4856Slling /* 3802990b4856Slling * Set pool properties. 3803990b4856Slling */ 3804990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3805990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 38060a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3807573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3808b24ab676SJeff Bonwick 3809379c004dSEric Schrock if (props != NULL) { 3810379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38113b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3812379c004dSEric Schrock } 381306eeb2adSek 3814fa9e4066Sahrens dmu_tx_commit(tx); 3815fa9e4066Sahrens 3816fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3817fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3818fa9e4066Sahrens 3819fa9e4066Sahrens /* 3820fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3821fa9e4066Sahrens * bean counters are appropriately updated. 3822fa9e4066Sahrens */ 3823fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3824fa9e4066Sahrens 3825c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 382614372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE); 3827fa9e4066Sahrens 38284445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3829228975ccSek 3830bc9014e6SJustin Gibbs /* 3831bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 3832bc9014e6SJustin Gibbs * and are making their way through the eviction process. 3833bc9014e6SJustin Gibbs */ 3834bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 3835088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 38360f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 3837088f3894Sahrens 3838daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3839daaa36a7SGeorge Wilson 3840fa9e4066Sahrens return (0); 3841fa9e4066Sahrens } 3842fa9e4066Sahrens 3843e7cbe64fSgw #ifdef _KERNEL 3844e7cbe64fSgw /* 384521ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 384621ecdf64SLin Ling * during the system boot up time. 3847e7cbe64fSgw */ 384821ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 384921ecdf64SLin Ling 385021ecdf64SLin Ling static nvlist_t * 385121ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3852e7cbe64fSgw { 385321ecdf64SLin Ling nvlist_t *config; 3854e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3855e7cbe64fSgw uint64_t pgid; 3856e7cbe64fSgw 385721ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 385821ecdf64SLin Ling return (NULL); 385921ecdf64SLin Ling 3860e7cbe64fSgw /* 3861e7cbe64fSgw * Add this top-level vdev to the child array. 3862e7cbe64fSgw */ 386321ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 386421ecdf64SLin Ling &nvtop) == 0); 386521ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 386621ecdf64SLin Ling &pgid) == 0); 386721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3868e7cbe64fSgw 3869e7cbe64fSgw /* 3870e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3871e7cbe64fSgw */ 3872e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 387321ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 387421ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3875e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3876e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3877e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3878e7cbe64fSgw &nvtop, 1) == 0); 3879e7cbe64fSgw 3880e7cbe64fSgw /* 3881e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3882e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3883e7cbe64fSgw */ 3884e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3885e7cbe64fSgw nvlist_free(nvroot); 388621ecdf64SLin Ling return (config); 3887e7cbe64fSgw } 3888e7cbe64fSgw 3889e7cbe64fSgw /* 389021ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 389121ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 389221ecdf64SLin Ling * device has a more recent txg. 3893051aabe6Staylor */ 389421ecdf64SLin Ling static void 389521ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3896051aabe6Staylor { 3897573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 389821ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3899051aabe6Staylor 390021ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 390121ecdf64SLin Ling nvlist_t *label; 390221ecdf64SLin Ling uint64_t label_txg; 3903051aabe6Staylor 390421ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 390521ecdf64SLin Ling &label) != 0) 390621ecdf64SLin Ling return; 3907051aabe6Staylor 390821ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 390921ecdf64SLin Ling &label_txg) == 0); 3910051aabe6Staylor 391121ecdf64SLin Ling /* 391221ecdf64SLin Ling * Do we have a better boot device? 391321ecdf64SLin Ling */ 391421ecdf64SLin Ling if (label_txg > *txg) { 391521ecdf64SLin Ling *txg = label_txg; 391621ecdf64SLin Ling *avd = vd; 3917051aabe6Staylor } 391821ecdf64SLin Ling nvlist_free(label); 3919051aabe6Staylor } 3920051aabe6Staylor } 3921051aabe6Staylor 3922e7cbe64fSgw /* 3923e7cbe64fSgw * Import a root pool. 3924e7cbe64fSgw * 3925051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3926051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3927051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3928e7cbe64fSgw * 3929e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3930e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3931e7cbe64fSgw * e.g. 3932e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3933e7cbe64fSgw */ 3934e7cbe64fSgw int 3935051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3936e7cbe64fSgw { 393721ecdf64SLin Ling spa_t *spa; 393821ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 393921ecdf64SLin Ling nvlist_t *config, *nvtop; 394021ecdf64SLin Ling uint64_t guid, txg; 3941e7cbe64fSgw char *pname; 3942e7cbe64fSgw int error; 3943e7cbe64fSgw 3944e7cbe64fSgw /* 394521ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3946e7cbe64fSgw */ 3947dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3948dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3949dedec472SJack Meng if (config == NULL) { 3950dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3951dedec472SJack Meng /* iscsi boot */ 3952dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3953dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3954dedec472SJack Meng } 3955dedec472SJack Meng } 3956dedec472SJack Meng #endif 3957dedec472SJack Meng if (config == NULL) { 3958ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 395921ecdf64SLin Ling devpath); 3960be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 396121ecdf64SLin Ling } 3962e7cbe64fSgw 396321ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 396421ecdf64SLin Ling &pname) == 0); 396521ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3966e7cbe64fSgw 39676809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 39686809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 39696809eb4eSEric Schrock /* 39706809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 39716809eb4eSEric Schrock * can replace it with the correct config we just read in. 39726809eb4eSEric Schrock */ 39736809eb4eSEric Schrock spa_remove(spa); 39746809eb4eSEric Schrock } 39756809eb4eSEric Schrock 3976468c413aSTim Haley spa = spa_add(pname, config, NULL); 39776809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 39784b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3979e7cbe64fSgw 398021ecdf64SLin Ling /* 398121ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 398221ecdf64SLin Ling */ 398321ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 398421ecdf64SLin Ling &nvtop) == 0); 398521ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 398621ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 398721ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 398821ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 398921ecdf64SLin Ling if (error) { 399021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 399121ecdf64SLin Ling nvlist_free(config); 399221ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 399321ecdf64SLin Ling pname); 399421ecdf64SLin Ling return (error); 399521ecdf64SLin Ling } 399621ecdf64SLin Ling 399721ecdf64SLin Ling /* 399821ecdf64SLin Ling * Get the boot vdev. 399921ecdf64SLin Ling */ 400021ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 400121ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 400221ecdf64SLin Ling (u_longlong_t)guid); 4003be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 400421ecdf64SLin Ling goto out; 400521ecdf64SLin Ling } 4006e7cbe64fSgw 400721ecdf64SLin Ling /* 400821ecdf64SLin Ling * Determine if there is a better boot device. 400921ecdf64SLin Ling */ 401021ecdf64SLin Ling avd = bvd; 401121ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 401221ecdf64SLin Ling if (avd != bvd) { 401321ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 401421ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4015be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 401621ecdf64SLin Ling goto out; 401721ecdf64SLin Ling } 4018e7cbe64fSgw 401921ecdf64SLin Ling /* 402021ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 402121ecdf64SLin Ling * we're booting off the active spare. 402221ecdf64SLin Ling */ 402321ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 402421ecdf64SLin Ling !bvd->vdev_isspare) { 402521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 402621ecdf64SLin Ling "try booting from '%s'", 4027cb04b873SMark J Musante bvd->vdev_parent-> 4028cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4029be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 403021ecdf64SLin Ling goto out; 403121ecdf64SLin Ling } 403221ecdf64SLin Ling 403321ecdf64SLin Ling error = 0; 403421ecdf64SLin Ling out: 403521ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 403621ecdf64SLin Ling vdev_free(rvd); 403721ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 403821ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 403921ecdf64SLin Ling 404021ecdf64SLin Ling nvlist_free(config); 4041e7cbe64fSgw return (error); 4042e7cbe64fSgw } 404321ecdf64SLin Ling 4044e7cbe64fSgw #endif 4045e7cbe64fSgw 40466809eb4eSEric Schrock /* 40476809eb4eSEric Schrock * Import a non-root pool into the system. 40486809eb4eSEric Schrock */ 4049c5904d13Seschrock int 40504b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4051c5904d13Seschrock { 40526809eb4eSEric Schrock spa_t *spa; 40536809eb4eSEric Schrock char *altroot = NULL; 4054468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4055468c413aSTim Haley zpool_rewind_policy_t policy; 4056f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4057f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 40586809eb4eSEric Schrock int error; 40596809eb4eSEric Schrock nvlist_t *nvroot; 40606809eb4eSEric Schrock nvlist_t **spares, **l2cache; 40616809eb4eSEric Schrock uint_t nspares, nl2cache; 40626809eb4eSEric Schrock 40636809eb4eSEric Schrock /* 40646809eb4eSEric Schrock * If a pool with this name exists, return failure. 40656809eb4eSEric Schrock */ 40666809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 40671195e687SMark J Musante if (spa_lookup(pool) != NULL) { 40686809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4069be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 40706809eb4eSEric Schrock } 40716809eb4eSEric Schrock 40726809eb4eSEric Schrock /* 40736809eb4eSEric Schrock * Create and initialize the spa structure. 40746809eb4eSEric Schrock */ 40756809eb4eSEric Schrock (void) nvlist_lookup_string(props, 40766809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4077f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4078f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4079f9af39baSGeorge Wilson if (readonly) 4080f9af39baSGeorge Wilson mode = FREAD; 4081468c413aSTim Haley spa = spa_add(pool, config, altroot); 40824b964adaSGeorge Wilson spa->spa_import_flags = flags; 40834b964adaSGeorge Wilson 40844b964adaSGeorge Wilson /* 40854b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 40864b964adaSGeorge Wilson * as if it had been loaded at boot. 40874b964adaSGeorge Wilson */ 40884b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 40894b964adaSGeorge Wilson if (props != NULL) 40904b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 40914b964adaSGeorge Wilson 40924b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 409314372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); 40944b964adaSGeorge Wilson 40954b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 40964b964adaSGeorge Wilson return (0); 40974b964adaSGeorge Wilson } 40984b964adaSGeorge Wilson 4099f9af39baSGeorge Wilson spa_activate(spa, mode); 41006809eb4eSEric Schrock 410125f89ee2SJeff Bonwick /* 410225f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 410325f89ee2SJeff Bonwick */ 410425f89ee2SJeff Bonwick spa_async_suspend(spa); 410525f89ee2SJeff Bonwick 41064b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 41074b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 41084b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 41094b964adaSGeorge Wilson 41106809eb4eSEric Schrock /* 41116809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 41126809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 41136809eb4eSEric Schrock * doing an import. 41146809eb4eSEric Schrock */ 4115468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4116468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 41174b964adaSGeorge Wilson 4118468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 4119c8ee1847SVictor Latushkin policy.zrp_request); 4120468c413aSTim Haley 4121468c413aSTim Haley /* 41224b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 41234b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4124468c413aSTim Haley */ 41254b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 41264b964adaSGeorge Wilson spa->spa_load_info) == 0); 41276809eb4eSEric Schrock 41286809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41296809eb4eSEric Schrock /* 41306809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 41316809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 41326809eb4eSEric Schrock */ 41336809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 41346809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 41356809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 41366809eb4eSEric Schrock spa_load_spares(spa); 41376809eb4eSEric Schrock } 41386809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 41396809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 41406809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 41416809eb4eSEric Schrock spa_load_l2cache(spa); 41426809eb4eSEric Schrock } 41436809eb4eSEric Schrock 41446809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 41456809eb4eSEric Schrock &nvroot) == 0); 41466809eb4eSEric Schrock if (error == 0) 41476809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41486809eb4eSEric Schrock VDEV_ALLOC_SPARE); 41496809eb4eSEric Schrock if (error == 0) 41506809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41516809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 41526809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41536809eb4eSEric Schrock 41546809eb4eSEric Schrock if (props != NULL) 41556809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 41566809eb4eSEric Schrock 41576809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 41586809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 41596809eb4eSEric Schrock spa_unload(spa); 41606809eb4eSEric Schrock spa_deactivate(spa); 41616809eb4eSEric Schrock spa_remove(spa); 41626809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 41636809eb4eSEric Schrock return (error); 41646809eb4eSEric Schrock } 41656809eb4eSEric Schrock 4166955ef359SLin Ling spa_async_resume(spa); 4167955ef359SLin Ling 41686809eb4eSEric Schrock /* 41696809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 41706809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 41716809eb4eSEric Schrock */ 41726809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 41736809eb4eSEric Schrock &spares, &nspares) == 0) { 41746809eb4eSEric Schrock if (spa->spa_spares.sav_config) 41756809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 41766809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 41776809eb4eSEric Schrock else 41786809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 41796809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41806809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 41816809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 41826809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41836809eb4eSEric Schrock spa_load_spares(spa); 41846809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41856809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 41866809eb4eSEric Schrock } 41876809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 41886809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 41896809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 41906809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 41916809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 41926809eb4eSEric Schrock else 41936809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 41946809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41956809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 41966809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 41976809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41986809eb4eSEric Schrock spa_load_l2cache(spa); 41996809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42006809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 42016809eb4eSEric Schrock } 42026809eb4eSEric Schrock 4203b693757aSEric Schrock /* 4204b693757aSEric Schrock * Check for any removed devices. 4205b693757aSEric Schrock */ 4206b693757aSEric Schrock if (spa->spa_autoreplace) { 4207b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4208b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4209b693757aSEric Schrock } 4210b693757aSEric Schrock 42116809eb4eSEric Schrock if (spa_writeable(spa)) { 42126809eb4eSEric Schrock /* 42136809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 42146809eb4eSEric Schrock */ 4215bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 42166809eb4eSEric Schrock } 42176809eb4eSEric Schrock 4218573ca77eSGeorge Wilson /* 4219573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4220573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4221573ca77eSGeorge Wilson */ 4222573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4223573ca77eSGeorge Wilson 42244445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 42256809eb4eSEric Schrock 422614372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); 422714372834SHans Rosenfeld 422814372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 422914372834SHans Rosenfeld 42306809eb4eSEric Schrock return (0); 4231c5904d13Seschrock } 4232c5904d13Seschrock 4233fa9e4066Sahrens nvlist_t * 4234fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4235fa9e4066Sahrens { 4236fa9e4066Sahrens nvlist_t *config = NULL; 4237fa9e4066Sahrens char *poolname; 4238fa9e4066Sahrens spa_t *spa; 4239fa9e4066Sahrens uint64_t state; 42407b7154beSLin Ling int error; 4241fa9e4066Sahrens 4242fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4243fa9e4066Sahrens return (NULL); 4244fa9e4066Sahrens 4245fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4246fa9e4066Sahrens return (NULL); 4247fa9e4066Sahrens 4248fa9e4066Sahrens /* 42490373e76bSbonwick * Create and initialize the spa structure. 4250fa9e4066Sahrens */ 42510373e76bSbonwick mutex_enter(&spa_namespace_lock); 4252468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 42538ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4254fa9e4066Sahrens 4255fa9e4066Sahrens /* 42560373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4257ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4258ecc2d604Sbonwick * is actually the one to trust when doing an import. 4259fa9e4066Sahrens */ 42601195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4261fa9e4066Sahrens 4262fa9e4066Sahrens /* 4263fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4264fa9e4066Sahrens */ 4265fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4266fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4267fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4268fa9e4066Sahrens poolname) == 0); 4269fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4270fa9e4066Sahrens state) == 0); 427195173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 427295173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4273ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4274ad135b5dSChristopher Siden spa->spa_load_info) == 0); 427599653d4eSeschrock 4276e7cbe64fSgw /* 4277e7cbe64fSgw * If the bootfs property exists on this pool then we 4278e7cbe64fSgw * copy it out so that external consumers can tell which 4279e7cbe64fSgw * pools are bootable. 4280e7cbe64fSgw */ 42817b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4282e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4283e7cbe64fSgw 4284e7cbe64fSgw /* 4285e7cbe64fSgw * We have to play games with the name since the 4286e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4287e7cbe64fSgw */ 4288e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4289e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4290e7cbe64fSgw char *cp; 4291e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4292e7cbe64fSgw 4293e7cbe64fSgw cp = strchr(tmpname, '/'); 4294e7cbe64fSgw if (cp == NULL) { 4295e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4296e7cbe64fSgw MAXPATHLEN); 4297e7cbe64fSgw } else { 4298e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4299e7cbe64fSgw "%s/%s", poolname, ++cp); 4300e7cbe64fSgw } 4301e7cbe64fSgw VERIFY(nvlist_add_string(config, 4302e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4303e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4304e7cbe64fSgw } 4305e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4306e7cbe64fSgw } 4307e7cbe64fSgw 430899653d4eSeschrock /* 4309fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 431099653d4eSeschrock */ 43116809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 431299653d4eSeschrock spa_add_spares(spa, config); 4313fa94a07fSbrendan spa_add_l2cache(spa, config); 43146809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4315fa9e4066Sahrens } 4316fa9e4066Sahrens 4317fa9e4066Sahrens spa_unload(spa); 4318fa9e4066Sahrens spa_deactivate(spa); 4319fa9e4066Sahrens spa_remove(spa); 4320fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4321fa9e4066Sahrens 4322fa9e4066Sahrens return (config); 4323fa9e4066Sahrens } 4324fa9e4066Sahrens 4325fa9e4066Sahrens /* 4326fa9e4066Sahrens * Pool export/destroy 4327fa9e4066Sahrens * 4328fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4329fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4330fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4331394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4332394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4333fa9e4066Sahrens */ 4334fa9e4066Sahrens static int 433589a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4336394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4337fa9e4066Sahrens { 4338fa9e4066Sahrens spa_t *spa; 4339fa9e4066Sahrens 434044cd46caSbillm if (oldconfig) 434144cd46caSbillm *oldconfig = NULL; 434244cd46caSbillm 43438ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4344be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4345fa9e4066Sahrens 4346fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4347fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4348fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4349be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4350fa9e4066Sahrens } 4351fa9e4066Sahrens 4352ea8dc4b6Seschrock /* 4353ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4354ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4355ea8dc4b6Seschrock */ 4356ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4357ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4358ea8dc4b6Seschrock spa_async_suspend(spa); 4359ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4360ea8dc4b6Seschrock spa_close(spa, FTAG); 4361ea8dc4b6Seschrock 4362fa9e4066Sahrens /* 4363fa9e4066Sahrens * The pool will be in core if it's openable, 4364fa9e4066Sahrens * in which case we can modify its state. 4365fa9e4066Sahrens */ 4366fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4367fa9e4066Sahrens /* 4368fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4369fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4370fa9e4066Sahrens */ 4371fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4372bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4373fa9e4066Sahrens 4374ea8dc4b6Seschrock /* 4375ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4376ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4377ea8dc4b6Seschrock * fault injection handlers. 4378ea8dc4b6Seschrock */ 4379ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4380ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4381ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4382ea8dc4b6Seschrock spa_async_resume(spa); 4383fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4384be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4385fa9e4066Sahrens } 4386fa9e4066Sahrens 438789a89ebfSlling /* 438889a89ebfSlling * A pool cannot be exported if it has an active shared spare. 438989a89ebfSlling * This is to prevent other pools stealing the active spare 439089a89ebfSlling * from an exported pool. At user's own will, such pool can 439189a89ebfSlling * be forcedly exported. 439289a89ebfSlling */ 439389a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 439489a89ebfSlling spa_has_active_shared_spare(spa)) { 439589a89ebfSlling spa_async_resume(spa); 439689a89ebfSlling mutex_exit(&spa_namespace_lock); 4397be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 439889a89ebfSlling } 439989a89ebfSlling 4400fa9e4066Sahrens /* 4401fa9e4066Sahrens * We want this to be reflected on every label, 4402fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4403fa9e4066Sahrens * final sync that pushes these changes out. 4404fa9e4066Sahrens */ 4405394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4406e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4407ea8dc4b6Seschrock spa->spa_state = new_state; 44083f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 44093f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4410ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4411e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4412ea8dc4b6Seschrock } 4413fa9e4066Sahrens } 4414fa9e4066Sahrens 44153d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 44163d7072f8Seschrock 4417fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4418fa9e4066Sahrens spa_unload(spa); 4419fa9e4066Sahrens spa_deactivate(spa); 4420fa9e4066Sahrens } 4421fa9e4066Sahrens 442244cd46caSbillm if (oldconfig && spa->spa_config) 442344cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 442444cd46caSbillm 4425ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4426394ab0cbSGeorge Wilson if (!hardforce) 4427394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4428ea8dc4b6Seschrock spa_remove(spa); 4429ea8dc4b6Seschrock } 4430fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4431fa9e4066Sahrens 4432fa9e4066Sahrens return (0); 4433fa9e4066Sahrens } 4434fa9e4066Sahrens 4435fa9e4066Sahrens /* 4436fa9e4066Sahrens * Destroy a storage pool. 4437fa9e4066Sahrens */ 4438fa9e4066Sahrens int 4439fa9e4066Sahrens spa_destroy(char *pool) 4440fa9e4066Sahrens { 4441394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4442394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4443fa9e4066Sahrens } 4444fa9e4066Sahrens 4445fa9e4066Sahrens /* 4446fa9e4066Sahrens * Export a storage pool. 4447fa9e4066Sahrens */ 4448fa9e4066Sahrens int 4449394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4450394ab0cbSGeorge Wilson boolean_t hardforce) 4451fa9e4066Sahrens { 4452394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4453394ab0cbSGeorge Wilson force, hardforce)); 4454fa9e4066Sahrens } 4455fa9e4066Sahrens 4456ea8dc4b6Seschrock /* 4457ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4458ea8dc4b6Seschrock * from the namespace in any way. 4459ea8dc4b6Seschrock */ 4460ea8dc4b6Seschrock int 4461ea8dc4b6Seschrock spa_reset(char *pool) 4462ea8dc4b6Seschrock { 446389a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4464394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4465ea8dc4b6Seschrock } 4466ea8dc4b6Seschrock 4467fa9e4066Sahrens /* 4468fa9e4066Sahrens * ========================================================================== 4469fa9e4066Sahrens * Device manipulation 4470fa9e4066Sahrens * ========================================================================== 4471fa9e4066Sahrens */ 4472fa9e4066Sahrens 4473fa9e4066Sahrens /* 44748654d025Sperrin * Add a device to a storage pool. 4475fa9e4066Sahrens */ 4476fa9e4066Sahrens int 4477fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4478fa9e4066Sahrens { 447988ecc943SGeorge Wilson uint64_t txg, id; 44808ad4d6ddSJeff Bonwick int error; 4481fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 44820e34b6a7Sbonwick vdev_t *vd, *tvd; 4483fa94a07fSbrendan nvlist_t **spares, **l2cache; 4484fa94a07fSbrendan uint_t nspares, nl2cache; 4485fa9e4066Sahrens 4486f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4487f9af39baSGeorge Wilson 4488fa9e4066Sahrens txg = spa_vdev_enter(spa); 4489fa9e4066Sahrens 449099653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 449199653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 449299653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4493fa9e4066Sahrens 4494e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 449599653d4eSeschrock 4496fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4497fa94a07fSbrendan &nspares) != 0) 449899653d4eSeschrock nspares = 0; 449999653d4eSeschrock 4500fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4501fa94a07fSbrendan &nl2cache) != 0) 4502fa94a07fSbrendan nl2cache = 0; 4503fa94a07fSbrendan 4504e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4505fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4506fa9e4066Sahrens 4507e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4508e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4509e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 451099653d4eSeschrock 451139c23413Seschrock /* 4512fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4513fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 451439c23413Seschrock */ 4515e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 451639c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 451739c23413Seschrock 451839c23413Seschrock /* 451939c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 452039c23413Seschrock */ 45218ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 452288ecc943SGeorge Wilson 452388ecc943SGeorge Wilson /* 452488ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 452588ecc943SGeorge Wilson */ 452688ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 452788ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 452888ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 452988ecc943SGeorge Wilson break; 453088ecc943SGeorge Wilson } 453188ecc943SGeorge Wilson } 453239c23413Seschrock tvd = vd->vdev_child[c]; 453339c23413Seschrock vdev_remove_child(vd, tvd); 453488ecc943SGeorge Wilson tvd->vdev_id = id; 453539c23413Seschrock vdev_add_child(rvd, tvd); 453639c23413Seschrock vdev_config_dirty(tvd); 453739c23413Seschrock } 453839c23413Seschrock 453999653d4eSeschrock if (nspares != 0) { 4540fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4541fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 454299653d4eSeschrock spa_load_spares(spa); 4543fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4544fa94a07fSbrendan } 4545fa94a07fSbrendan 4546fa94a07fSbrendan if (nl2cache != 0) { 4547fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4548fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4549fa94a07fSbrendan spa_load_l2cache(spa); 4550fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4551fa9e4066Sahrens } 4552fa9e4066Sahrens 4553fa9e4066Sahrens /* 45540e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 45550e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 45560e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 45570e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 45580e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 45590e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 45600373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 45610e34b6a7Sbonwick * 45620e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 45630e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 45640e34b6a7Sbonwick * steps will be completed the next time we load the pool. 45650e34b6a7Sbonwick */ 45660373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 45670e34b6a7Sbonwick 45680373e76bSbonwick mutex_enter(&spa_namespace_lock); 45690373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 457014372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD); 45710373e76bSbonwick mutex_exit(&spa_namespace_lock); 4572fa9e4066Sahrens 45730373e76bSbonwick return (0); 4574fa9e4066Sahrens } 4575fa9e4066Sahrens 4576fa9e4066Sahrens /* 4577fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4578fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4579fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4580fa9e4066Sahrens * 4581fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4582fa9e4066Sahrens * existing device; in this case the two devices are made into their own 45833d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4584fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4585fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4586fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4587fa9e4066Sahrens * is automatically detached. 4588fa9e4066Sahrens */ 4589fa9e4066Sahrens int 4590ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4591fa9e4066Sahrens { 45923f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4593fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4594fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 459599653d4eSeschrock vdev_ops_t *pvops; 45969b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 45979b3f6b42SEric Kustarz int newvd_isspare; 45989b3f6b42SEric Kustarz int error; 4599fa9e4066Sahrens 4600f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4601f9af39baSGeorge Wilson 4602fa9e4066Sahrens txg = spa_vdev_enter(spa); 4603fa9e4066Sahrens 4604c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4605fa9e4066Sahrens 4606fa9e4066Sahrens if (oldvd == NULL) 4607fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4608fa9e4066Sahrens 46090e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 46100e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 46110e34b6a7Sbonwick 4612fa9e4066Sahrens pvd = oldvd->vdev_parent; 4613fa9e4066Sahrens 461499653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4615cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 46163d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 46173d7072f8Seschrock 46183d7072f8Seschrock if (newrootvd->vdev_children != 1) 4619fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4620fa9e4066Sahrens 4621fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4622fa9e4066Sahrens 4623fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4624fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4625fa9e4066Sahrens 462699653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4627fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4628fa9e4066Sahrens 46298654d025Sperrin /* 46308654d025Sperrin * Spares can't replace logs 46318654d025Sperrin */ 4632ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 46338654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 46348654d025Sperrin 463599653d4eSeschrock if (!replacing) { 463699653d4eSeschrock /* 463799653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 463899653d4eSeschrock * vdev. 463999653d4eSeschrock */ 464099653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 464199653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 464299653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 464399653d4eSeschrock 464499653d4eSeschrock pvops = &vdev_mirror_ops; 464599653d4eSeschrock } else { 464699653d4eSeschrock /* 464799653d4eSeschrock * Active hot spares can only be replaced by inactive hot 464899653d4eSeschrock * spares. 464999653d4eSeschrock */ 465099653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4651cb04b873SMark J Musante oldvd->vdev_isspare && 465299653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 465399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 465499653d4eSeschrock 465599653d4eSeschrock /* 465699653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 465799653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 465839c23413Seschrock * want to create a replacing vdev. The user is not allowed to 465939c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 466039c23413Seschrock * the same (spare replaces spare, non-spare replaces 466139c23413Seschrock * non-spare). 466299653d4eSeschrock */ 4663cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4664cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 466599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4666cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4667cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 466839c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4669cb04b873SMark J Musante } 4670cb04b873SMark J Musante 4671cb04b873SMark J Musante if (newvd->vdev_isspare) 467299653d4eSeschrock pvops = &vdev_spare_ops; 467399653d4eSeschrock else 467499653d4eSeschrock pvops = &vdev_replacing_ops; 467599653d4eSeschrock } 467699653d4eSeschrock 46772a79c5feSlling /* 4678573ca77eSGeorge Wilson * Make sure the new device is big enough. 46792a79c5feSlling */ 4680573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4681fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4682fa9e4066Sahrens 4683ecc2d604Sbonwick /* 4684ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4685ecc2d604Sbonwick * than the top-level vdev. 4686ecc2d604Sbonwick */ 4687ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4688fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4689fa9e4066Sahrens 4690fa9e4066Sahrens /* 4691fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4692fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4693fa9e4066Sahrens */ 4694fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4695fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4696fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4697fa9e4066Sahrens KM_SLEEP); 4698fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4699fa9e4066Sahrens newvd->vdev_path, "old"); 4700fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4701fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4702fa9e4066Sahrens oldvd->vdev_devid = NULL; 4703fa9e4066Sahrens } 4704fa9e4066Sahrens } 4705fa9e4066Sahrens 4706cb04b873SMark J Musante /* mark the device being resilvered */ 4707b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4708cb04b873SMark J Musante 4709fa9e4066Sahrens /* 471099653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 471199653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4712fa9e4066Sahrens */ 4713fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4714fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4715fa9e4066Sahrens 4716fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4717fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4718fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4719fa9e4066Sahrens 4720fa9e4066Sahrens /* 4721fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4722fa9e4066Sahrens */ 4723fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4724fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 472588ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4726fa9e4066Sahrens vdev_add_child(pvd, newvd); 4727fa9e4066Sahrens 4728fa9e4066Sahrens tvd = newvd->vdev_top; 4729fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4730fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4731fa9e4066Sahrens 4732fa9e4066Sahrens vdev_config_dirty(tvd); 4733fa9e4066Sahrens 4734fa9e4066Sahrens /* 47353f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 47363f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 47373f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4738fa9e4066Sahrens */ 47393f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4740fa9e4066Sahrens 47413f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 47423f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4743fa9e4066Sahrens 47446809eb4eSEric Schrock if (newvd->vdev_isspare) { 474539c23413Seschrock spa_spare_activate(newvd); 47466809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 47476809eb4eSEric Schrock } 47486809eb4eSEric Schrock 4749e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4750e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 47519b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4752ea8dc4b6Seschrock 4753fa9e4066Sahrens /* 4754fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4755fa9e4066Sahrens */ 4756ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4757fa9e4066Sahrens 47583f9d6ad7SLin Ling /* 47590713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 47600713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 47610713e232SGeorge Wilson * respective datasets. 47623f9d6ad7SLin Ling */ 47633f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 47643f9d6ad7SLin Ling 476514372834SHans Rosenfeld if (spa->spa_bootfs) 476614372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 476714372834SHans Rosenfeld 476814372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH); 476914372834SHans Rosenfeld 47703f9d6ad7SLin Ling /* 47713f9d6ad7SLin Ling * Commit the config 47723f9d6ad7SLin Ling */ 47733f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4774fa9e4066Sahrens 47754445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 47763f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4777c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4778c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4779c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 47809b3f6b42SEric Kustarz 47819b3f6b42SEric Kustarz spa_strfree(oldvdpath); 47829b3f6b42SEric Kustarz spa_strfree(newvdpath); 47839b3f6b42SEric Kustarz 4784fa9e4066Sahrens return (0); 4785fa9e4066Sahrens } 4786fa9e4066Sahrens 4787fa9e4066Sahrens /* 4788fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4789f7170741SWill Andrews * 4790fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4791fa9e4066Sahrens * is a replacing vdev. 4792fa9e4066Sahrens */ 4793fa9e4066Sahrens int 47948ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4795fa9e4066Sahrens { 4796fa9e4066Sahrens uint64_t txg; 47978ad4d6ddSJeff Bonwick int error; 4798fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4799fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 480099653d4eSeschrock boolean_t unspare = B_FALSE; 4801d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 48021195e687SMark J Musante char *vdpath; 4803fa9e4066Sahrens 4804f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4805f9af39baSGeorge Wilson 4806fa9e4066Sahrens txg = spa_vdev_enter(spa); 4807fa9e4066Sahrens 4808c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4809fa9e4066Sahrens 4810fa9e4066Sahrens if (vd == NULL) 4811fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4812fa9e4066Sahrens 48130e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 48140e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 48150e34b6a7Sbonwick 4816fa9e4066Sahrens pvd = vd->vdev_parent; 4817fa9e4066Sahrens 48188ad4d6ddSJeff Bonwick /* 48198ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 48208ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 48218ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 48228ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 48238ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 48248ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 48258ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 48268ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 48278ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 48288ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 48298ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 48308ad4d6ddSJeff Bonwick */ 48318ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 48328ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 48338ad4d6ddSJeff Bonwick 4834fa9e4066Sahrens /* 4835cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 483699653d4eSeschrock */ 4837cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4838cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4839cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 484099653d4eSeschrock 484199653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4842e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4843fa9e4066Sahrens 4844fa9e4066Sahrens /* 484599653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4846fa9e4066Sahrens */ 4847fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 484899653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 484999653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4850fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4851fa9e4066Sahrens 4852fa9e4066Sahrens /* 48538ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 48548ad4d6ddSJeff Bonwick * we cannot safely detach it. 4855fa9e4066Sahrens */ 48568ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4857fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4858fa9e4066Sahrens 48598ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4860fa9e4066Sahrens 4861bf82a41bSeschrock /* 4862bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4863bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4864bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4865bf82a41bSeschrock */ 4866cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4867cb04b873SMark J Musante vd->vdev_path != NULL) { 4868cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4869cb04b873SMark J Musante 4870cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4871cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4872cb04b873SMark J Musante 4873cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4874cb04b873SMark J Musante continue; 4875cb04b873SMark J Musante 4876cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4877cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4878cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4879cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4880cb04b873SMark J Musante break; 4881cb04b873SMark J Musante } 4882bf82a41bSeschrock } 4883bf82a41bSeschrock } 4884bf82a41bSeschrock 488599653d4eSeschrock /* 488699653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 488799653d4eSeschrock * that the spare should become a real disk, and be removed from the 488899653d4eSeschrock * active spare list for the pool. 488999653d4eSeschrock */ 489099653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4891cb04b873SMark J Musante vd->vdev_id == 0 && 4892cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 489399653d4eSeschrock unspare = B_TRUE; 489499653d4eSeschrock 4895fa9e4066Sahrens /* 4896fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4897fa9e4066Sahrens * This must be done after all other error cases are handled, 4898fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4899fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4900fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4901fa9e4066Sahrens * it's being detached! 4902fa9e4066Sahrens */ 490339c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4904fa9e4066Sahrens 4905fa9e4066Sahrens /* 4906fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4907fa9e4066Sahrens */ 4908fa9e4066Sahrens vdev_remove_child(pvd, vd); 4909fa9e4066Sahrens vdev_compact_children(pvd); 4910fa9e4066Sahrens 4911fa9e4066Sahrens /* 4912fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4913fa9e4066Sahrens */ 4914cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4915fa9e4066Sahrens 491699653d4eSeschrock /* 491799653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 49188ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 49198ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 49208ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 49218ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 49228ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 49238ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 492499653d4eSeschrock */ 492599653d4eSeschrock if (unspare) { 492699653d4eSeschrock ASSERT(cvd->vdev_isspare); 492739c23413Seschrock spa_spare_remove(cvd); 492899653d4eSeschrock unspare_guid = cvd->vdev_guid; 49298ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4930cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 493199653d4eSeschrock } 493299653d4eSeschrock 4933fa9e4066Sahrens /* 4934fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4935fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4936fa9e4066Sahrens */ 4937cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4938cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4939cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4940fa9e4066Sahrens vdev_remove_parent(cvd); 4941cb04b873SMark J Musante } 4942cb04b873SMark J Musante 4943fa9e4066Sahrens 4944fa9e4066Sahrens /* 4945fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4946fa9e4066Sahrens * may have been the previous top-level vdev. 4947fa9e4066Sahrens */ 4948fa9e4066Sahrens tvd = cvd->vdev_top; 4949fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4950fa9e4066Sahrens 4951fa9e4066Sahrens /* 495239c23413Seschrock * Reevaluate the parent vdev state. 4953fa9e4066Sahrens */ 49543d7072f8Seschrock vdev_propagate_state(cvd); 4955fa9e4066Sahrens 4956fa9e4066Sahrens /* 4957573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4958573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4959573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4960573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4961573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4962fa9e4066Sahrens */ 4963573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4964573ca77eSGeorge Wilson vdev_reopen(tvd); 4965573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4966573ca77eSGeorge Wilson } 4967fa9e4066Sahrens 4968fa9e4066Sahrens vdev_config_dirty(tvd); 4969fa9e4066Sahrens 4970fa9e4066Sahrens /* 497139c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 497239c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 497339c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 497439c23413Seschrock * prevent vd from being accessed after it's freed. 4975fa9e4066Sahrens */ 49761195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 49778ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4978fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4979ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4980ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4981fa9e4066Sahrens 49823d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 49833d7072f8Seschrock 4984cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4985cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4986cb04b873SMark J Musante 498799653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 498899653d4eSeschrock 49894445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 49901195e687SMark J Musante "vdev=%s", vdpath); 49911195e687SMark J Musante spa_strfree(vdpath); 49921195e687SMark J Musante 499399653d4eSeschrock /* 499439c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 499539c23413Seschrock * then we want to go through and remove the device from the hot spare 499639c23413Seschrock * list of every other pool. 499799653d4eSeschrock */ 499899653d4eSeschrock if (unspare) { 4999cb04b873SMark J Musante spa_t *altspa = NULL; 5000cb04b873SMark J Musante 500199653d4eSeschrock mutex_enter(&spa_namespace_lock); 5002cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 5003cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 5004cb04b873SMark J Musante altspa == spa) 500599653d4eSeschrock continue; 5006cb04b873SMark J Musante 5007cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 50089af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 5009cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 50109af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 5011cb04b873SMark J Musante spa_close(altspa, FTAG); 501299653d4eSeschrock } 501399653d4eSeschrock mutex_exit(&spa_namespace_lock); 5014cb04b873SMark J Musante 5015cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 5016cb04b873SMark J Musante spa_vdev_resilver_done(spa); 501799653d4eSeschrock } 501899653d4eSeschrock 5019cb04b873SMark J Musante /* all done with the spa; OK to release */ 5020cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5021cb04b873SMark J Musante spa_close(spa, FTAG); 5022cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5023cb04b873SMark J Musante 502499653d4eSeschrock return (error); 502599653d4eSeschrock } 502699653d4eSeschrock 50271195e687SMark J Musante /* 50281195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 50291195e687SMark J Musante */ 50301195e687SMark J Musante int 50311195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 50321195e687SMark J Musante nvlist_t *props, boolean_t exp) 50331195e687SMark J Musante { 50341195e687SMark J Musante int error = 0; 50351195e687SMark J Musante uint64_t txg, *glist; 50361195e687SMark J Musante spa_t *newspa; 50371195e687SMark J Musante uint_t c, children, lastlog; 50381195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 50391195e687SMark J Musante dmu_tx_t *tx; 50401195e687SMark J Musante char *altroot = NULL; 50411195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 50421195e687SMark J Musante boolean_t activate_slog; 50431195e687SMark J Musante 5044f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 50451195e687SMark J Musante 50461195e687SMark J Musante txg = spa_vdev_enter(spa); 50471195e687SMark J Musante 50481195e687SMark J Musante /* clear the log and flush everything up to now */ 50491195e687SMark J Musante activate_slog = spa_passivate_log(spa); 50501195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 50511195e687SMark J Musante error = spa_offline_log(spa); 50521195e687SMark J Musante txg = spa_vdev_config_enter(spa); 50531195e687SMark J Musante 50541195e687SMark J Musante if (activate_slog) 50551195e687SMark J Musante spa_activate_log(spa); 50561195e687SMark J Musante 50571195e687SMark J Musante if (error != 0) 50581195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 50591195e687SMark J Musante 50601195e687SMark J Musante /* check new spa name before going any further */ 50611195e687SMark J Musante if (spa_lookup(newname) != NULL) 50621195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 50631195e687SMark J Musante 50641195e687SMark J Musante /* 50651195e687SMark J Musante * scan through all the children to ensure they're all mirrors 50661195e687SMark J Musante */ 50671195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 50681195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 50691195e687SMark J Musante &children) != 0) 50701195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50711195e687SMark J Musante 50721195e687SMark J Musante /* first, check to ensure we've got the right child count */ 50731195e687SMark J Musante rvd = spa->spa_root_vdev; 50741195e687SMark J Musante lastlog = 0; 50751195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 50761195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 50771195e687SMark J Musante 50781195e687SMark J Musante /* don't count the holes & logs as children */ 50791195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 50801195e687SMark J Musante if (lastlog == 0) 50811195e687SMark J Musante lastlog = c; 50821195e687SMark J Musante continue; 50831195e687SMark J Musante } 50841195e687SMark J Musante 50851195e687SMark J Musante lastlog = 0; 50861195e687SMark J Musante } 50871195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 50881195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50891195e687SMark J Musante 50901195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 50911195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 50921195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 50931195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50941195e687SMark J Musante 50951195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 50961195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 50971195e687SMark J Musante 50981195e687SMark J Musante /* then, loop over each vdev and validate it */ 50991195e687SMark J Musante for (c = 0; c < children; c++) { 51001195e687SMark J Musante uint64_t is_hole = 0; 51011195e687SMark J Musante 51021195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 51031195e687SMark J Musante &is_hole); 51041195e687SMark J Musante 51051195e687SMark J Musante if (is_hole != 0) { 51061195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 51071195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 51081195e687SMark J Musante continue; 51091195e687SMark J Musante } else { 5110be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51111195e687SMark J Musante break; 51121195e687SMark J Musante } 51131195e687SMark J Musante } 51141195e687SMark J Musante 51151195e687SMark J Musante /* which disk is going to be split? */ 51161195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 51171195e687SMark J Musante &glist[c]) != 0) { 5118be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51191195e687SMark J Musante break; 51201195e687SMark J Musante } 51211195e687SMark J Musante 51221195e687SMark J Musante /* look it up in the spa */ 51231195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 51241195e687SMark J Musante if (vml[c] == NULL) { 5125be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 51261195e687SMark J Musante break; 51271195e687SMark J Musante } 51281195e687SMark J Musante 51291195e687SMark J Musante /* make sure there's nothing stopping the split */ 51301195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 51311195e687SMark J Musante vml[c]->vdev_islog || 51321195e687SMark J Musante vml[c]->vdev_ishole || 51331195e687SMark J Musante vml[c]->vdev_isspare || 51341195e687SMark J Musante vml[c]->vdev_isl2cache || 51351195e687SMark J Musante !vdev_writeable(vml[c]) || 5136d41c4376SMark J Musante vml[c]->vdev_children != 0 || 51371195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 51381195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5139be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51401195e687SMark J Musante break; 51411195e687SMark J Musante } 51421195e687SMark J Musante 51431195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5144be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 51451195e687SMark J Musante break; 51461195e687SMark J Musante } 51471195e687SMark J Musante 51481195e687SMark J Musante /* we need certain info from the top level */ 51491195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 51501195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 51511195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 51521195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 51531195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 51541195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 51551195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 51561195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5157215198a6SJoe Stein 5158215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5159215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5160215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5161215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5162215198a6SJoe Stein 5163215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5164215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5165215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5166215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 51671195e687SMark J Musante } 51681195e687SMark J Musante 51691195e687SMark J Musante if (error != 0) { 51701195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51711195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51721195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 51731195e687SMark J Musante } 51741195e687SMark J Musante 51751195e687SMark J Musante /* stop writers from using the disks */ 51761195e687SMark J Musante for (c = 0; c < children; c++) { 51771195e687SMark J Musante if (vml[c] != NULL) 51781195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 51791195e687SMark J Musante } 51801195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 51811195e687SMark J Musante 51821195e687SMark J Musante /* 51831195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 51841195e687SMark J Musante * will disappear once the config is regenerated. 51851195e687SMark J Musante */ 51861195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 51871195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 51881195e687SMark J Musante glist, children) == 0); 51891195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51901195e687SMark J Musante 519198295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 51921195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 51931195e687SMark J Musante nvl) == 0); 519498295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 51951195e687SMark J Musante spa->spa_config_splitting = nvl; 51961195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 51971195e687SMark J Musante 51981195e687SMark J Musante /* configure and create the new pool */ 51991195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 52001195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 52011195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 52021195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 52031195e687SMark J Musante spa_version(spa)) == 0); 52041195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 52051195e687SMark J Musante spa->spa_config_txg) == 0); 52061195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 52071195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5208215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 52091195e687SMark J Musante (void) nvlist_lookup_string(props, 52101195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 52111195e687SMark J Musante 5212d41c4376SMark J Musante /* add the new pool to the namespace */ 52131195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5214215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 52151195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 52161195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 52171195e687SMark J Musante 52181195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 52191195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 52201195e687SMark J Musante 52211195e687SMark J Musante if (zio_injection_enabled) 52221195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 52231195e687SMark J Musante 52241195e687SMark J Musante spa_activate(newspa, spa_mode_global); 52251195e687SMark J Musante spa_async_suspend(newspa); 52261195e687SMark J Musante 52271195e687SMark J Musante /* create the new pool from the disks of the original pool */ 52281195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 52291195e687SMark J Musante if (error) 52301195e687SMark J Musante goto out; 52311195e687SMark J Musante 52321195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 52331195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 52341195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 52351195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 52361195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 52371195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 52381195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 52391195e687SMark J Musante B_TRUE)); 52401195e687SMark J Musante } 52411195e687SMark J Musante 52421195e687SMark J Musante /* set the props */ 52431195e687SMark J Musante if (props != NULL) { 52441195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 52451195e687SMark J Musante error = spa_prop_set(newspa, props); 52461195e687SMark J Musante if (error) 52471195e687SMark J Musante goto out; 52481195e687SMark J Musante } 52491195e687SMark J Musante 52501195e687SMark J Musante /* flush everything */ 52511195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 52521195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 52531195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 52541195e687SMark J Musante 52551195e687SMark J Musante if (zio_injection_enabled) 52561195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 52571195e687SMark J Musante 52581195e687SMark J Musante spa_async_resume(newspa); 52591195e687SMark J Musante 52601195e687SMark J Musante /* finally, update the original pool's config */ 52611195e687SMark J Musante txg = spa_vdev_config_enter(spa); 52621195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 52631195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 52641195e687SMark J Musante if (error != 0) 52651195e687SMark J Musante dmu_tx_abort(tx); 52661195e687SMark J Musante for (c = 0; c < children; c++) { 52671195e687SMark J Musante if (vml[c] != NULL) { 52681195e687SMark J Musante vdev_split(vml[c]); 52691195e687SMark J Musante if (error == 0) 52704445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 52714445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5272215198a6SJoe Stein 52731195e687SMark J Musante vdev_free(vml[c]); 52741195e687SMark J Musante } 52751195e687SMark J Musante } 5276215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 52771195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 52781195e687SMark J Musante spa->spa_config_splitting = NULL; 52791195e687SMark J Musante nvlist_free(nvl); 52801195e687SMark J Musante if (error == 0) 52811195e687SMark J Musante dmu_tx_commit(tx); 52821195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 52831195e687SMark J Musante 52841195e687SMark J Musante if (zio_injection_enabled) 52851195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 52861195e687SMark J Musante 52871195e687SMark J Musante /* split is complete; log a history record */ 52884445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 52894445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 52901195e687SMark J Musante 52911195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 52921195e687SMark J Musante 52931195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 52941195e687SMark J Musante if (exp) 52951195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 52961195e687SMark J Musante B_FALSE, B_FALSE); 52971195e687SMark J Musante 52981195e687SMark J Musante return (error); 52991195e687SMark J Musante 53001195e687SMark J Musante out: 53011195e687SMark J Musante spa_unload(newspa); 53021195e687SMark J Musante spa_deactivate(newspa); 53031195e687SMark J Musante spa_remove(newspa); 53041195e687SMark J Musante 53051195e687SMark J Musante txg = spa_vdev_config_enter(spa); 530698295d61SMark J Musante 530798295d61SMark J Musante /* re-online all offlined disks */ 530898295d61SMark J Musante for (c = 0; c < children; c++) { 530998295d61SMark J Musante if (vml[c] != NULL) 531098295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 531198295d61SMark J Musante } 531298295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 531398295d61SMark J Musante 53141195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 53151195e687SMark J Musante spa->spa_config_splitting = NULL; 5316d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 53171195e687SMark J Musante 53181195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 53191195e687SMark J Musante return (error); 53201195e687SMark J Musante } 53211195e687SMark J Musante 5322e14bb325SJeff Bonwick static nvlist_t * 5323e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 532499653d4eSeschrock { 5325e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5326e14bb325SJeff Bonwick uint64_t guid; 532799653d4eSeschrock 5328e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5329e14bb325SJeff Bonwick &guid) == 0); 533099653d4eSeschrock 5331e14bb325SJeff Bonwick if (guid == target_guid) 5332e14bb325SJeff Bonwick return (nvpp[i]); 533399653d4eSeschrock } 533499653d4eSeschrock 5335e14bb325SJeff Bonwick return (NULL); 5336fa94a07fSbrendan } 5337fa94a07fSbrendan 5338e14bb325SJeff Bonwick static void 5339e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 53400f7643c7SGeorge Wilson nvlist_t *dev_to_remove) 5341fa94a07fSbrendan { 5342e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5343fa94a07fSbrendan 5344e14bb325SJeff Bonwick if (count > 1) 5345e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5346fa94a07fSbrendan 5347e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5348e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5349e14bb325SJeff Bonwick continue; 5350e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5351fa94a07fSbrendan } 5352fa94a07fSbrendan 5353e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5354e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5355fa94a07fSbrendan 5356e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5357e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5358fa94a07fSbrendan 5359e14bb325SJeff Bonwick if (count > 1) 5360e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5361fa94a07fSbrendan } 5362fa94a07fSbrendan 536388ecc943SGeorge Wilson /* 536488ecc943SGeorge Wilson * Evacuate the device. 536588ecc943SGeorge Wilson */ 53663f9d6ad7SLin Ling static int 536788ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 536888ecc943SGeorge Wilson { 536988ecc943SGeorge Wilson uint64_t txg; 53703f9d6ad7SLin Ling int error = 0; 537188ecc943SGeorge Wilson 537288ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 537388ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5374b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 537588ecc943SGeorge Wilson 537688ecc943SGeorge Wilson /* 537788ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 537888ecc943SGeorge Wilson * since we need to do I/O but we do keep the 537988ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 538088ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 538188ecc943SGeorge Wilson */ 538288ecc943SGeorge Wilson if (vd->vdev_islog) { 53833f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 53843f9d6ad7SLin Ling error = spa_offline_log(spa); 5385a1521560SJeff Bonwick } else { 5386be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 538788ecc943SGeorge Wilson } 538888ecc943SGeorge Wilson 5389a1521560SJeff Bonwick if (error) 5390a1521560SJeff Bonwick return (error); 5391a1521560SJeff Bonwick 539288ecc943SGeorge Wilson /* 5393a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5394a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 539588ecc943SGeorge Wilson */ 5396fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 539788ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 539888ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 53990713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg); 540088ecc943SGeorge Wilson vdev_config_dirty(vd); 540188ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 540288ecc943SGeorge Wilson 540388ecc943SGeorge Wilson return (0); 540488ecc943SGeorge Wilson } 540588ecc943SGeorge Wilson 540688ecc943SGeorge Wilson /* 540788ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 540888ecc943SGeorge Wilson */ 54093f9d6ad7SLin Ling static void 5410a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 541188ecc943SGeorge Wilson { 541288ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 541388ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 541488ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 541588ecc943SGeorge Wilson 541688ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 541788ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5418b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 541988ecc943SGeorge Wilson 54203f9d6ad7SLin Ling /* 54213f9d6ad7SLin Ling * Only remove any devices which are empty. 54223f9d6ad7SLin Ling */ 54233f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 54243f9d6ad7SLin Ling return; 54253f9d6ad7SLin Ling 542688ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5427b24ab676SJeff Bonwick 5428b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5429b24ab676SJeff Bonwick vdev_state_clean(vd); 5430b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5431b24ab676SJeff Bonwick vdev_config_clean(vd); 5432b24ab676SJeff Bonwick 543388ecc943SGeorge Wilson vdev_free(vd); 543488ecc943SGeorge Wilson 543588ecc943SGeorge Wilson if (last_vdev) { 543688ecc943SGeorge Wilson vdev_compact_children(rvd); 543788ecc943SGeorge Wilson } else { 543888ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 543988ecc943SGeorge Wilson vdev_add_child(rvd, vd); 544088ecc943SGeorge Wilson } 5441fcbfa62bSLin Ling vdev_config_dirty(rvd); 5442fcbfa62bSLin Ling 5443fcbfa62bSLin Ling /* 5444fcbfa62bSLin Ling * Reassess the health of our root vdev. 5445fcbfa62bSLin Ling */ 5446fcbfa62bSLin Ling vdev_reopen(rvd); 544788ecc943SGeorge Wilson } 544888ecc943SGeorge Wilson 54493f9d6ad7SLin Ling /* 54503f9d6ad7SLin Ling * Remove a device from the pool - 54513f9d6ad7SLin Ling * 54523f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 54533f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 54543f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 54553f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 54563f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 5457f7170741SWill Andrews * 5458f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC 5459f7170741SWill Andrews * devices. 5460fa94a07fSbrendan */ 5461fa94a07fSbrendan int 5462fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5463fa94a07fSbrendan { 5464fa94a07fSbrendan vdev_t *vd; 5465b72b6bb1SAlan Somers sysevent_t *ev = NULL; 5466a1521560SJeff Bonwick metaslab_group_t *mg; 5467e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 54688ad4d6ddSJeff Bonwick uint64_t txg = 0; 546988ecc943SGeorge Wilson uint_t nspares, nl2cache; 5470fa94a07fSbrendan int error = 0; 54718ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5472fa94a07fSbrendan 5473f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5474f9af39baSGeorge Wilson 54758ad4d6ddSJeff Bonwick if (!locked) 54768ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5477fa94a07fSbrendan 5478c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5479fa94a07fSbrendan 5480fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5481fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5482e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5483e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5484e14bb325SJeff Bonwick /* 5485e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5486e14bb325SJeff Bonwick * in this pool. 5487e14bb325SJeff Bonwick */ 5488e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5489b72b6bb1SAlan Somers if (vd == NULL) 5490b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5491b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); 5492e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5493e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5494e14bb325SJeff Bonwick spa_load_spares(spa); 5495e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5496e14bb325SJeff Bonwick } else { 5497be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 5498e14bb325SJeff Bonwick } 5499e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5500fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5501e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5502e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5503e14bb325SJeff Bonwick /* 5504e14bb325SJeff Bonwick * Cache devices can always be removed. 5505e14bb325SJeff Bonwick */ 5506b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5507b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); 5508e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5509e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5510fa94a07fSbrendan spa_load_l2cache(spa); 5511fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 551288ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 551388ecc943SGeorge Wilson ASSERT(!locked); 5514b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 551588ecc943SGeorge Wilson 5516a1521560SJeff Bonwick mg = vd->vdev_mg; 5517a1521560SJeff Bonwick 551888ecc943SGeorge Wilson /* 5519a1521560SJeff Bonwick * Stop allocating from this vdev. 552088ecc943SGeorge Wilson */ 5521a1521560SJeff Bonwick metaslab_group_passivate(mg); 552288ecc943SGeorge Wilson 5523b24ab676SJeff Bonwick /* 5524b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5525b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5526b24ab676SJeff Bonwick */ 5527b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5528b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5529b24ab676SJeff Bonwick 5530a1521560SJeff Bonwick /* 5531a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5532a1521560SJeff Bonwick */ 5533a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5534a1521560SJeff Bonwick 553588ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 553688ecc943SGeorge Wilson 5537a1521560SJeff Bonwick /* 5538a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5539a1521560SJeff Bonwick */ 5540a1521560SJeff Bonwick if (error) { 5541a1521560SJeff Bonwick metaslab_group_activate(mg); 5542a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5543a1521560SJeff Bonwick } 5544a1521560SJeff Bonwick 5545a1521560SJeff Bonwick /* 5546a1521560SJeff Bonwick * Clean up the vdev namespace. 5547a1521560SJeff Bonwick */ 5548b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV); 5549a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 555088ecc943SGeorge Wilson 5551e14bb325SJeff Bonwick } else if (vd != NULL) { 5552e14bb325SJeff Bonwick /* 5553e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5554e14bb325SJeff Bonwick */ 5555be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5556e14bb325SJeff Bonwick } else { 5557e14bb325SJeff Bonwick /* 5558e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5559e14bb325SJeff Bonwick */ 5560be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 5561fa94a07fSbrendan } 556299653d4eSeschrock 55638ad4d6ddSJeff Bonwick if (!locked) 556463364b0eSAlan Somers error = spa_vdev_exit(spa, NULL, txg, error); 55658ad4d6ddSJeff Bonwick 5566b72b6bb1SAlan Somers if (ev) 5567b72b6bb1SAlan Somers spa_event_post(ev); 5568b72b6bb1SAlan Somers 55698ad4d6ddSJeff Bonwick return (error); 5570fa9e4066Sahrens } 5571fa9e4066Sahrens 5572fa9e4066Sahrens /* 55733d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5574f7170741SWill Andrews * currently spared, so we can detach it. 5575fa9e4066Sahrens */ 5576ea8dc4b6Seschrock static vdev_t * 55773d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5578fa9e4066Sahrens { 5579ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5580fa9e4066Sahrens 5581573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 55823d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5583ea8dc4b6Seschrock if (oldvd != NULL) 5584ea8dc4b6Seschrock return (oldvd); 5585ea8dc4b6Seschrock } 5586fa9e4066Sahrens 55873d7072f8Seschrock /* 5588cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5589cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5590cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5591cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5592cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5593cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 55943d7072f8Seschrock */ 5595cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5596cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5597cb04b873SMark J Musante 5598cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5599ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5600ea8dc4b6Seschrock 56018ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5602e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 56038ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5604ea8dc4b6Seschrock return (oldvd); 5605fa9e4066Sahrens } 5606ea8dc4b6Seschrock 56073d7072f8Seschrock /* 56083d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 56093d7072f8Seschrock */ 5610cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5611cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5612cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5613cb04b873SMark J Musante 5614cb04b873SMark J Musante if (last->vdev_unspare) { 5615cb04b873SMark J Musante oldvd = first; 5616cb04b873SMark J Musante newvd = last; 5617cb04b873SMark J Musante } else if (first->vdev_unspare) { 5618cb04b873SMark J Musante oldvd = last; 5619cb04b873SMark J Musante newvd = first; 5620cb04b873SMark J Musante } else { 5621cb04b873SMark J Musante oldvd = NULL; 5622cb04b873SMark J Musante } 56233d7072f8Seschrock 5624cb04b873SMark J Musante if (oldvd != NULL && 56258ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5626e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5627cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 56283d7072f8Seschrock return (oldvd); 5629cb04b873SMark J Musante 5630cb04b873SMark J Musante /* 5631cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5632cb04b873SMark J Musante * and those spares are not required, then we want to 5633cb04b873SMark J Musante * attempt to free them up now so that they can be used 5634cb04b873SMark J Musante * by other pools. Once we're back down to a single 5635cb04b873SMark J Musante * disk+spare, we stop removing them. 5636cb04b873SMark J Musante */ 5637cb04b873SMark J Musante if (vd->vdev_children > 2) { 5638cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5639cb04b873SMark J Musante 5640cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5641cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5642cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5643cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5644cb04b873SMark J Musante return (newvd); 56453d7072f8Seschrock } 56463d7072f8Seschrock } 56473d7072f8Seschrock 5648ea8dc4b6Seschrock return (NULL); 5649fa9e4066Sahrens } 5650fa9e4066Sahrens 5651ea8dc4b6Seschrock static void 56523d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5653fa9e4066Sahrens { 56548ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 56558ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5656ea8dc4b6Seschrock 56578ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5658ea8dc4b6Seschrock 56593d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 56608ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 56618ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5662ea8dc4b6Seschrock guid = vd->vdev_guid; 56638ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 56648ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 56658ad4d6ddSJeff Bonwick sguid = 0; 566699653d4eSeschrock /* 566799653d4eSeschrock * If we have just finished replacing a hot spared device, then 566899653d4eSeschrock * we need to detach the parent's first child (the original hot 566999653d4eSeschrock * spare) as well. 567099653d4eSeschrock */ 5671cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5672cb04b873SMark J Musante ppvd->vdev_children == 2) { 567399653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 56748ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 567599653d4eSeschrock } 5676b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5677b4952e17SGeorge Wilson 56788ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 56798ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5680ea8dc4b6Seschrock return; 56818ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 568299653d4eSeschrock return; 56838ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5684fa9e4066Sahrens } 5685fa9e4066Sahrens 56868ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5687fa9e4066Sahrens } 5688fa9e4066Sahrens 5689c67d9675Seschrock /* 5690b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5691c67d9675Seschrock */ 5692c67d9675Seschrock int 56936809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 56946809eb4eSEric Schrock boolean_t ispath) 5695c67d9675Seschrock { 5696c5904d13Seschrock vdev_t *vd; 5697208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5698c67d9675Seschrock 5699f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5700f9af39baSGeorge Wilson 5701b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5702c67d9675Seschrock 57036809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5704b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5705c67d9675Seschrock 57060e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5707b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 57080e34b6a7Sbonwick 57096809eb4eSEric Schrock if (ispath) { 5710208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5711208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5712208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5713208044b8SGeorge Wilson sync = B_TRUE; 5714208044b8SGeorge Wilson } 57156809eb4eSEric Schrock } else { 5716208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5717208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5718208044b8SGeorge Wilson sync = B_TRUE; 5719208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 57206809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5721208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5722208044b8SGeorge Wilson sync = B_TRUE; 5723208044b8SGeorge Wilson } 57246809eb4eSEric Schrock } 5725c67d9675Seschrock 5726208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5727c67d9675Seschrock } 5728c67d9675Seschrock 57296809eb4eSEric Schrock int 57306809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 57316809eb4eSEric Schrock { 57326809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 57336809eb4eSEric Schrock } 57346809eb4eSEric Schrock 57356809eb4eSEric Schrock int 57366809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 57376809eb4eSEric Schrock { 57386809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 57396809eb4eSEric Schrock } 57406809eb4eSEric Schrock 5741fa9e4066Sahrens /* 5742fa9e4066Sahrens * ========================================================================== 57433f9d6ad7SLin Ling * SPA Scanning 5744fa9e4066Sahrens * ========================================================================== 5745fa9e4066Sahrens */ 5746fa9e4066Sahrens 5747ea8dc4b6Seschrock int 57483f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5749fa9e4066Sahrens { 5750e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57513f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5752be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 57533f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 57543f9d6ad7SLin Ling } 5755bb8b5132Sek 57563f9d6ad7SLin Ling int 57573f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 57583f9d6ad7SLin Ling { 57593f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57603f9d6ad7SLin Ling 57613f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5762be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5763fa9e4066Sahrens 5764fa9e4066Sahrens /* 5765088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5766088f3894Sahrens * writeable leaf device, we have nothing to do. 5767fa9e4066Sahrens */ 57683f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5769088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5770088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5771ea8dc4b6Seschrock return (0); 5772ea8dc4b6Seschrock } 5773fa9e4066Sahrens 57743f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5775fa9e4066Sahrens } 5776fa9e4066Sahrens 5777ea8dc4b6Seschrock /* 5778ea8dc4b6Seschrock * ========================================================================== 5779ea8dc4b6Seschrock * SPA async task processing 5780ea8dc4b6Seschrock * ========================================================================== 5781ea8dc4b6Seschrock */ 5782ea8dc4b6Seschrock 5783ea8dc4b6Seschrock static void 57843d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5785fa9e4066Sahrens { 578649cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 578798d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 578898d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 578949cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 57901d713200SEric Schrock 57911d713200SEric Schrock /* 57921d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 57931d713200SEric Schrock * vdev_clear() as that will cause us to throw away 57941d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 57951d713200SEric Schrock * device, all of which is a waste. 57961d713200SEric Schrock */ 57971d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 57981d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 57991d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 58001d713200SEric Schrock 5801e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5802ea8dc4b6Seschrock } 580349cf58c0SBrendan Gregg - Sun Microsystems 5804e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 580549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5806ea8dc4b6Seschrock } 5807fa9e4066Sahrens 5808e14bb325SJeff Bonwick static void 5809e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5810e14bb325SJeff Bonwick { 5811e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 581298d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5813e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5814e14bb325SJeff Bonwick } 5815e14bb325SJeff Bonwick 5816e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5817e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5818e14bb325SJeff Bonwick } 5819e14bb325SJeff Bonwick 5820573ca77eSGeorge Wilson static void 5821573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5822573ca77eSGeorge Wilson { 5823573ca77eSGeorge Wilson sysevent_id_t eid; 5824573ca77eSGeorge Wilson nvlist_t *attr; 5825573ca77eSGeorge Wilson char *physpath; 5826573ca77eSGeorge Wilson 5827573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5828573ca77eSGeorge Wilson return; 5829573ca77eSGeorge Wilson 5830573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5831573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5832573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5833573ca77eSGeorge Wilson } 5834573ca77eSGeorge Wilson 5835573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5836573ca77eSGeorge Wilson return; 5837573ca77eSGeorge Wilson 5838573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5839573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5840573ca77eSGeorge Wilson 5841573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5842573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5843573ca77eSGeorge Wilson 5844573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5845573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5846573ca77eSGeorge Wilson 5847573ca77eSGeorge Wilson nvlist_free(attr); 5848573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5849573ca77eSGeorge Wilson } 5850573ca77eSGeorge Wilson 5851ea8dc4b6Seschrock static void 5852ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5853ea8dc4b6Seschrock { 5854e14bb325SJeff Bonwick int tasks; 5855ea8dc4b6Seschrock 5856ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5857ea8dc4b6Seschrock 5858ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5859ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5860ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5861ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5862ea8dc4b6Seschrock 58630373e76bSbonwick /* 58640373e76bSbonwick * See if the config needs to be updated. 58650373e76bSbonwick */ 58660373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5867b24ab676SJeff Bonwick uint64_t old_space, new_space; 5868573ca77eSGeorge Wilson 58690373e76bSbonwick mutex_enter(&spa_namespace_lock); 5870b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 58710373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5872b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 58730373e76bSbonwick mutex_exit(&spa_namespace_lock); 5874573ca77eSGeorge Wilson 5875573ca77eSGeorge Wilson /* 5876573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5877573ca77eSGeorge Wilson * then log an internal history event. 5878573ca77eSGeorge Wilson */ 5879b24ab676SJeff Bonwick if (new_space != old_space) { 58804445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5881c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5882b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5883573ca77eSGeorge Wilson } 58840373e76bSbonwick } 58850373e76bSbonwick 5886ea8dc4b6Seschrock /* 58873d7072f8Seschrock * See if any devices need to be marked REMOVED. 5888ea8dc4b6Seschrock */ 5889e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 58908f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 58913d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5892e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 589349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5894e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 589549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5896e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5897e14bb325SJeff Bonwick } 5898e14bb325SJeff Bonwick 5899573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5900573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5901573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5902573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5903573ca77eSGeorge Wilson } 5904573ca77eSGeorge Wilson 5905e14bb325SJeff Bonwick /* 5906e14bb325SJeff Bonwick * See if any devices need to be probed. 5907e14bb325SJeff Bonwick */ 5908e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 59098f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5910e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5911e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 59123d7072f8Seschrock } 5913ea8dc4b6Seschrock 5914ea8dc4b6Seschrock /* 5915ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5916ea8dc4b6Seschrock */ 59173d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 59183d7072f8Seschrock spa_vdev_resilver_done(spa); 5919fa9e4066Sahrens 5920ea8dc4b6Seschrock /* 5921ea8dc4b6Seschrock * Kick off a resilver. 5922ea8dc4b6Seschrock */ 5923088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 59243f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5925ea8dc4b6Seschrock 5926ea8dc4b6Seschrock /* 5927ea8dc4b6Seschrock * Let the world know that we're done. 5928ea8dc4b6Seschrock */ 5929ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5930ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5931ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5932ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5933ea8dc4b6Seschrock thread_exit(); 5934ea8dc4b6Seschrock } 5935ea8dc4b6Seschrock 5936ea8dc4b6Seschrock void 5937ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5938ea8dc4b6Seschrock { 5939ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5940ea8dc4b6Seschrock spa->spa_async_suspended++; 5941ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5942ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5943ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5944ea8dc4b6Seschrock } 5945ea8dc4b6Seschrock 5946ea8dc4b6Seschrock void 5947ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5948ea8dc4b6Seschrock { 5949ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5950ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5951ea8dc4b6Seschrock spa->spa_async_suspended--; 5952ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5953ea8dc4b6Seschrock } 5954ea8dc4b6Seschrock 59553cb69f73SWill Andrews static boolean_t 59563cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 59573cb69f73SWill Andrews { 59583cb69f73SWill Andrews uint_t non_config_tasks; 59593cb69f73SWill Andrews uint_t config_task; 59603cb69f73SWill Andrews boolean_t config_task_suspended; 59613cb69f73SWill Andrews 59623cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 59633cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 59643cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 59653cb69f73SWill Andrews config_task_suspended = B_FALSE; 59663cb69f73SWill Andrews } else { 59673cb69f73SWill Andrews config_task_suspended = 59683cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 59693cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 59703cb69f73SWill Andrews } 59713cb69f73SWill Andrews 59723cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 59733cb69f73SWill Andrews } 59743cb69f73SWill Andrews 5975ea8dc4b6Seschrock static void 5976ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5977ea8dc4b6Seschrock { 5978ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 59793cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 59803cb69f73SWill Andrews !spa->spa_async_suspended && 59810373e76bSbonwick spa->spa_async_thread == NULL && 59823cb69f73SWill Andrews rootdir != NULL) 5983ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5984ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5985ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5986ea8dc4b6Seschrock } 5987ea8dc4b6Seschrock 5988ea8dc4b6Seschrock void 5989ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5990ea8dc4b6Seschrock { 59913f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5992ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5993ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5994ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5995fa9e4066Sahrens } 5996fa9e4066Sahrens 5997fa9e4066Sahrens /* 5998fa9e4066Sahrens * ========================================================================== 5999fa9e4066Sahrens * SPA syncing routines 6000fa9e4066Sahrens * ========================================================================== 6001fa9e4066Sahrens */ 6002fa9e4066Sahrens 6003cde58dbcSMatthew Ahrens static int 6004cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6005cde58dbcSMatthew Ahrens { 6006cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 6007cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 6008cde58dbcSMatthew Ahrens return (0); 6009b24ab676SJeff Bonwick } 6010b24ab676SJeff Bonwick 6011cde58dbcSMatthew Ahrens static int 6012cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6013b24ab676SJeff Bonwick { 6014b24ab676SJeff Bonwick zio_t *zio = arg; 6015b24ab676SJeff Bonwick 6016b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 6017b24ab676SJeff Bonwick zio->io_flags)); 6018cde58dbcSMatthew Ahrens return (0); 6019fa9e4066Sahrens } 6020fa9e4066Sahrens 602169962b56SMatthew Ahrens /* 602269962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 602369962b56SMatthew Ahrens * amount of time spent syncing frees. 602469962b56SMatthew Ahrens */ 602569962b56SMatthew Ahrens static void 602669962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 602769962b56SMatthew Ahrens { 602869962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 602969962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 603069962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 603169962b56SMatthew Ahrens } 603269962b56SMatthew Ahrens 603369962b56SMatthew Ahrens /* 603469962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 603569962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 603669962b56SMatthew Ahrens */ 603769962b56SMatthew Ahrens static void 603869962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 603969962b56SMatthew Ahrens { 604069962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 604169962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 604269962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 604369962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 604469962b56SMatthew Ahrens } 604569962b56SMatthew Ahrens 604669962b56SMatthew Ahrens 6047fa9e4066Sahrens static void 604899653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6049fa9e4066Sahrens { 6050fa9e4066Sahrens char *packed = NULL; 6051f7991ba4STim Haley size_t bufsize; 6052fa9e4066Sahrens size_t nvsize = 0; 6053fa9e4066Sahrens dmu_buf_t *db; 6054fa9e4066Sahrens 605599653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6056fa9e4066Sahrens 6057f7991ba4STim Haley /* 6058f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 605943466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6060f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6061f7991ba4STim Haley */ 6062ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6063f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6064fa9e4066Sahrens 606599653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6066ea8dc4b6Seschrock KM_SLEEP) == 0); 6067f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6068fa9e4066Sahrens 6069f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6070fa9e4066Sahrens 6071f7991ba4STim Haley kmem_free(packed, bufsize); 6072fa9e4066Sahrens 607399653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6074fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6075fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6076ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6077fa9e4066Sahrens } 6078fa9e4066Sahrens 607999653d4eSeschrock static void 6080fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6081fa94a07fSbrendan const char *config, const char *entry) 608299653d4eSeschrock { 608399653d4eSeschrock nvlist_t *nvroot; 6084fa94a07fSbrendan nvlist_t **list; 608599653d4eSeschrock int i; 608699653d4eSeschrock 6087fa94a07fSbrendan if (!sav->sav_sync) 608899653d4eSeschrock return; 608999653d4eSeschrock 609099653d4eSeschrock /* 6091fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6092fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 60933d7072f8Seschrock * valid and the vdevs are labeled appropriately. 609499653d4eSeschrock */ 6095fa94a07fSbrendan if (sav->sav_object == 0) { 6096fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6097fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6098fa94a07fSbrendan sizeof (uint64_t), tx); 609999653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6100fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6101fa94a07fSbrendan &sav->sav_object, tx) == 0); 610299653d4eSeschrock } 610399653d4eSeschrock 610499653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6105fa94a07fSbrendan if (sav->sav_count == 0) { 6106fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 610799653d4eSeschrock } else { 6108fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6109fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6110fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 61113f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6112fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6113fa94a07fSbrendan sav->sav_count) == 0); 6114fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6115fa94a07fSbrendan nvlist_free(list[i]); 6116fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 611799653d4eSeschrock } 611899653d4eSeschrock 6119fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 612006eeb2adSek nvlist_free(nvroot); 612199653d4eSeschrock 6122fa94a07fSbrendan sav->sav_sync = B_FALSE; 612399653d4eSeschrock } 612499653d4eSeschrock 6125215198a6SJoe Stein /* 6126215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6127215198a6SJoe Stein * The all-vdev ZAP must be empty. 6128215198a6SJoe Stein */ 6129215198a6SJoe Stein static void 6130215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6131215198a6SJoe Stein { 6132215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6133215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6134215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6135215198a6SJoe Stein vd->vdev_top_zap, tx)); 6136215198a6SJoe Stein } 6137215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6138215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6139215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6140215198a6SJoe Stein } 6141215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6142215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6143215198a6SJoe Stein } 6144215198a6SJoe Stein } 6145215198a6SJoe Stein 614699653d4eSeschrock static void 614799653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 614899653d4eSeschrock { 614999653d4eSeschrock nvlist_t *config; 615099653d4eSeschrock 6151215198a6SJoe Stein /* 6152215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6153215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6154215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6155215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6156215198a6SJoe Stein */ 6157215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6158215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 615999653d4eSeschrock return; 616099653d4eSeschrock 6161e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6162e14bb325SJeff Bonwick 6163215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6164555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 6165215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6166215198a6SJoe Stein 6167215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6168215198a6SJoe Stein /* Make and build the new AVZ */ 6169215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6170215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6171215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6172215198a6SJoe Stein 6173215198a6SJoe Stein /* Diff old AVZ with new one */ 6174215198a6SJoe Stein zap_cursor_t zc; 6175215198a6SJoe Stein zap_attribute_t za; 6176215198a6SJoe Stein 6177215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6178215198a6SJoe Stein spa->spa_all_vdev_zaps); 6179215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6180215198a6SJoe Stein zap_cursor_advance(&zc)) { 6181215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6182215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6183215198a6SJoe Stein vdzap) == ENOENT) { 6184215198a6SJoe Stein /* 6185215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6186215198a6SJoe Stein * destroy it 6187215198a6SJoe Stein */ 6188215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6189215198a6SJoe Stein tx)); 6190215198a6SJoe Stein } 6191215198a6SJoe Stein } 6192215198a6SJoe Stein 6193215198a6SJoe Stein zap_cursor_fini(&zc); 6194215198a6SJoe Stein 6195215198a6SJoe Stein /* Destroy the old AVZ */ 6196215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6197215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6198215198a6SJoe Stein 6199215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6200215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6201215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6202215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6203215198a6SJoe Stein 6204215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6205215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6206215198a6SJoe Stein zap_cursor_t zc; 6207215198a6SJoe Stein zap_attribute_t za; 6208215198a6SJoe Stein 6209215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6210215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6211215198a6SJoe Stein spa->spa_all_vdev_zaps); 6212215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6213215198a6SJoe Stein zap_cursor_advance(&zc)) { 6214215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6215215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6216215198a6SJoe Stein } 6217215198a6SJoe Stein 6218215198a6SJoe Stein zap_cursor_fini(&zc); 6219215198a6SJoe Stein 6220215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6221215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6222215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6223215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6224215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6225215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6226215198a6SJoe Stein } 6227215198a6SJoe Stein 6228215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6229215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6230215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6231215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6232215198a6SJoe Stein } 6233215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6234215198a6SJoe Stein 6235215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6236215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6237215198a6SJoe Stein 6238e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6239e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6240e14bb325SJeff Bonwick 624125345e46SGeorge Wilson /* 624225345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 624325345e46SGeorge Wilson * the config object gets updated with the correct version. 624425345e46SGeorge Wilson */ 624525345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 624625345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 624725345e46SGeorge Wilson spa->spa_uberblock.ub_version); 624825345e46SGeorge Wilson 6249e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 625099653d4eSeschrock 6251aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 625299653d4eSeschrock spa->spa_config_syncing = config; 625399653d4eSeschrock 625499653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 625599653d4eSeschrock } 625699653d4eSeschrock 6257ad135b5dSChristopher Siden static void 62583b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6259ad135b5dSChristopher Siden { 62603b2aab18SMatthew Ahrens uint64_t *versionp = arg; 62613b2aab18SMatthew Ahrens uint64_t version = *versionp; 62623b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6263ad135b5dSChristopher Siden 6264ad135b5dSChristopher Siden /* 6265ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6266ad135b5dSChristopher Siden */ 6267ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6268ad135b5dSChristopher Siden 626962eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6270ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6271ad135b5dSChristopher Siden 6272ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6273ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 62744445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6275ad135b5dSChristopher Siden } 6276ad135b5dSChristopher Siden 6277990b4856Slling /* 6278990b4856Slling * Set zpool properties. 6279990b4856Slling */ 6280b1b8ab34Slling static void 62813b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6282b1b8ab34Slling { 62833b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 62843b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6285b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6286ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6287b1b8ab34Slling 6288e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6289e14bb325SJeff Bonwick 6290990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6291ad135b5dSChristopher Siden uint64_t intval; 6292ad135b5dSChristopher Siden char *strval, *fname; 6293ad135b5dSChristopher Siden zpool_prop_t prop; 6294ad135b5dSChristopher Siden const char *propname; 6295ad135b5dSChristopher Siden zprop_type_t proptype; 62962acef22dSMatthew Ahrens spa_feature_t fid; 6297ad135b5dSChristopher Siden 6298990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6299ad135b5dSChristopher Siden case ZPROP_INVAL: 6300ad135b5dSChristopher Siden /* 6301ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6302ad135b5dSChristopher Siden */ 6303ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6304ad135b5dSChristopher Siden 6305ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 63062acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6307ad135b5dSChristopher Siden 63082acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 63094445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63104445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6311ad135b5dSChristopher Siden break; 6312ad135b5dSChristopher Siden 6313990b4856Slling case ZPOOL_PROP_VERSION: 63140713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6315990b4856Slling /* 6316ad135b5dSChristopher Siden * The version is synced seperatly before other 6317ad135b5dSChristopher Siden * properties and should be correct by now. 6318990b4856Slling */ 6319ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6320ecd6cf80Smarks break; 6321990b4856Slling 6322990b4856Slling case ZPOOL_PROP_ALTROOT: 6323990b4856Slling /* 6324990b4856Slling * 'altroot' is a non-persistent property. It should 6325990b4856Slling * have been set temporarily at creation or import time. 6326990b4856Slling */ 6327990b4856Slling ASSERT(spa->spa_root != NULL); 6328b1b8ab34Slling break; 63293d7072f8Seschrock 6330f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 63312f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6332990b4856Slling /* 6333f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6334f9af39baSGeorge Wilson * properties. 6335990b4856Slling */ 63363d7072f8Seschrock break; 63378704186eSDan McDonald case ZPOOL_PROP_COMMENT: 63380713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63398704186eSDan McDonald if (spa->spa_comment != NULL) 63408704186eSDan McDonald spa_strfree(spa->spa_comment); 63418704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 63428704186eSDan McDonald /* 63438704186eSDan McDonald * We need to dirty the configuration on all the vdevs 63448704186eSDan McDonald * so that their labels get updated. It's unnecessary 63458704186eSDan McDonald * to do this for pool creation since the vdev's 63468704186eSDan McDonald * configuratoin has already been dirtied. 63478704186eSDan McDonald */ 63488704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 63498704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 63504445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63514445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 63528704186eSDan McDonald break; 6353990b4856Slling default: 6354990b4856Slling /* 6355990b4856Slling * Set pool property values in the poolprops mos object. 6356990b4856Slling */ 6357990b4856Slling if (spa->spa_pool_props_object == 0) { 6358ad135b5dSChristopher Siden spa->spa_pool_props_object = 6359ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6360990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6361ad135b5dSChristopher Siden tx); 6362990b4856Slling } 6363990b4856Slling 6364990b4856Slling /* normalize the property name */ 6365990b4856Slling propname = zpool_prop_to_name(prop); 6366990b4856Slling proptype = zpool_prop_get_type(prop); 6367990b4856Slling 6368990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6369990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 63700713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63710713e232SGeorge Wilson VERIFY0(zap_update(mos, 6372990b4856Slling spa->spa_pool_props_object, propname, 63730713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 63744445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63754445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6376990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 63770713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6378990b4856Slling 6379990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6380990b4856Slling const char *unused; 63810713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 63820713e232SGeorge Wilson prop, intval, &unused)); 6383990b4856Slling } 63840713e232SGeorge Wilson VERIFY0(zap_update(mos, 6385990b4856Slling spa->spa_pool_props_object, propname, 63860713e232SGeorge Wilson 8, 1, &intval, tx)); 63874445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63884445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6389990b4856Slling } else { 6390990b4856Slling ASSERT(0); /* not allowed */ 6391990b4856Slling } 6392990b4856Slling 63930a4e9518Sgw switch (prop) { 63940a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6395990b4856Slling spa->spa_delegation = intval; 63960a4e9518Sgw break; 63970a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6398990b4856Slling spa->spa_bootfs = intval; 63990a4e9518Sgw break; 64000a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 64010a4e9518Sgw spa->spa_failmode = intval; 64020a4e9518Sgw break; 6403573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6404573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6405b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6406b98131cfSEric Taylor spa_async_request(spa, 6407b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6408573ca77eSGeorge Wilson break; 6409b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6410b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6411b24ab676SJeff Bonwick break; 64120a4e9518Sgw default: 64130a4e9518Sgw break; 64140a4e9518Sgw } 6415990b4856Slling } 6416990b4856Slling 6417b1b8ab34Slling } 6418e14bb325SJeff Bonwick 6419e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6420b1b8ab34Slling } 6421b1b8ab34Slling 6422cde58dbcSMatthew Ahrens /* 6423cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6424cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6425cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6426cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6427cde58dbcSMatthew Ahrens * tasks. 6428cde58dbcSMatthew Ahrens */ 6429cde58dbcSMatthew Ahrens static void 6430cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6431cde58dbcSMatthew Ahrens { 6432cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6433cde58dbcSMatthew Ahrens 6434cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6435cde58dbcSMatthew Ahrens 64363b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 64373b2aab18SMatthew Ahrens 6438cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6439cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6440cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6441cde58dbcSMatthew Ahrens 6442cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6443cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6444cde58dbcSMatthew Ahrens } 6445cde58dbcSMatthew Ahrens 6446cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6447cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6448cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6449cde58dbcSMatthew Ahrens } 6450cde58dbcSMatthew Ahrens 6451cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6452cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6453cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6454cde58dbcSMatthew Ahrens 6455cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6456cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6457cde58dbcSMatthew Ahrens } 6458ad135b5dSChristopher Siden 6459ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6460ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6461ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6462ad135b5dSChristopher Siden } 6463b8289d24SDaniil Lunev 6464b8289d24SDaniil Lunev /* 6465b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6466b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6467b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6468b8289d24SDaniil Lunev * this feature active 6469b8289d24SDaniil Lunev */ 6470b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6471b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6472b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6473b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6474b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6475b8289d24SDaniil Lunev 6476b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6477b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6478b8289d24SDaniil Lunev } 647945818ee1SMatthew Ahrens 648045818ee1SMatthew Ahrens /* 648145818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 648245818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 648345818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 648445818ee1SMatthew Ahrens */ 648545818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 648645818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 648745818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 648845818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 648945818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 649045818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 649145818ee1SMatthew Ahrens } 649245818ee1SMatthew Ahrens 64933b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6494cde58dbcSMatthew Ahrens } 6495cde58dbcSMatthew Ahrens 6496fa9e4066Sahrens /* 6497fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6498fa9e4066Sahrens * part of the process, so we iterate until it converges. 6499fa9e4066Sahrens */ 6500fa9e4066Sahrens void 6501fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6502fa9e4066Sahrens { 6503fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6504fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6505b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 65060373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6507fa9e4066Sahrens vdev_t *vd; 6508fa9e4066Sahrens dmu_tx_t *tx; 6509e14bb325SJeff Bonwick int error; 65100f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 65110f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 6512fa9e4066Sahrens 6513f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6514f9af39baSGeorge Wilson 6515fa9e4066Sahrens /* 6516fa9e4066Sahrens * Lock out configuration changes. 6517fa9e4066Sahrens */ 6518e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6519fa9e4066Sahrens 6520fa9e4066Sahrens spa->spa_syncing_txg = txg; 6521fa9e4066Sahrens spa->spa_sync_pass = 0; 6522fa9e4066Sahrens 65230f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 65240f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 65250f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 65260f7643c7SGeorge Wilson 6527e14bb325SJeff Bonwick /* 6528e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6529e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6530e14bb325SJeff Bonwick */ 6531e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 65328ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 65338ad4d6ddSJeff Bonwick /* 65348ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 65358ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 65368ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 65378ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 65388ad4d6ddSJeff Bonwick * into the root vdev tree. 65398ad4d6ddSJeff Bonwick */ 65408ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65418ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 65428ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 65438ad4d6ddSJeff Bonwick vdev_state_clean(vd); 65448ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 65458ad4d6ddSJeff Bonwick } 65468ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65478ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6548e14bb325SJeff Bonwick } 6549e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6550e14bb325SJeff Bonwick 655199653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 655299653d4eSeschrock 6553283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6554283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6555283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6556283b8460SGeorge.Wilson 655799653d4eSeschrock /* 6558e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 655999653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 656099653d4eSeschrock */ 6561e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6562e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 656399653d4eSeschrock int i; 656499653d4eSeschrock 656599653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 656699653d4eSeschrock vd = rvd->vdev_child[i]; 656799653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 656899653d4eSeschrock break; 656999653d4eSeschrock } 657099653d4eSeschrock if (i == rvd->vdev_children) { 657199653d4eSeschrock spa->spa_deflate = TRUE; 657299653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 657399653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 657499653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 657599653d4eSeschrock } 657699653d4eSeschrock } 657799653d4eSeschrock 65780f7643c7SGeorge Wilson /* 65790f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 65800f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 65810f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 65820f7643c7SGeorge Wilson * out this txg. 65830f7643c7SGeorge Wilson */ 65840f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 65850f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 65860f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 65870f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 65880f7643c7SGeorge Wilson 65890f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 65900f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 65910f7643c7SGeorge Wilson continue; 65920f7643c7SGeorge Wilson 65930f7643c7SGeorge Wilson /* 65940f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 65950f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 65960f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 65970f7643c7SGeorge Wilson */ 65980f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 65990f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 66000f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 66010f7643c7SGeorge Wilson } 66020f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 66030f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 66040f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 66050f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 66060f7643c7SGeorge Wilson 66070f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 66080f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 66090f7643c7SGeorge Wilson 6610fa9e4066Sahrens /* 6611fa9e4066Sahrens * Iterate to convergence. 6612fa9e4066Sahrens */ 6613fa9e4066Sahrens do { 6614b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6615fa9e4066Sahrens 6616fa9e4066Sahrens spa_sync_config_object(spa, tx); 6617fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6618fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6619fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6620fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6621ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6622fa9e4066Sahrens dsl_pool_sync(dp, txg); 6623fa9e4066Sahrens 662401f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 662569962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6626b24ab676SJeff Bonwick } else { 6627231aab85SMatthew Ahrens /* 6628231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6629231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6630231aab85SMatthew Ahrens */ 6631231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6632cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 663369962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6634fa9e4066Sahrens } 6635fa9e4066Sahrens 6636b24ab676SJeff Bonwick ddt_sync(spa, txg); 66373f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6638afee20e4SGeorge Wilson 6639b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6640b24ab676SJeff Bonwick vdev_sync(vd, txg); 6641b24ab676SJeff Bonwick 6642231aab85SMatthew Ahrens if (pass == 1) { 6643cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6644231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6645231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6646231aab85SMatthew Ahrens /* 6647231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6648231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6649231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6650231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6651231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6652231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6653231aab85SMatthew Ahrens * data (though in this case the MOS will most 6654231aab85SMatthew Ahrens * likely also be dirty due to second order 6655231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6656231aab85SMatthew Ahrens */ 6657231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6658231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6659231aab85SMatthew Ahrens /* 6660231aab85SMatthew Ahrens * Nothing changed on the first pass, 6661231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6662231aab85SMatthew Ahrens * syncing deferred frees, so that we 6663231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6664231aab85SMatthew Ahrens */ 6665231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6666231aab85SMatthew Ahrens txg)); 6667231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6668231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6669231aab85SMatthew Ahrens break; 6670231aab85SMatthew Ahrens } 6671231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6672231aab85SMatthew Ahrens } 6673fa9e4066Sahrens 6674cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6675fa9e4066Sahrens 6676215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 6677215198a6SJoe Stein /* 6678215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 6679215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 6680215198a6SJoe Stein * called if the config is dirty; otherwise there may be 6681215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 6682215198a6SJoe Stein * spa_sync_config_object. 6683215198a6SJoe Stein */ 6684215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 6685215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 6686215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 6687215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 6688215198a6SJoe Stein all_vdev_zap_entry_count); 6689215198a6SJoe Stein } 6690215198a6SJoe Stein 6691fa9e4066Sahrens /* 6692fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6693fa9e4066Sahrens * to commit the transaction group. 66940373e76bSbonwick * 669517f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 669617f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6697e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6698e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 66990373e76bSbonwick */ 6700e14bb325SJeff Bonwick for (;;) { 6701e14bb325SJeff Bonwick /* 6702e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6703e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6704e14bb325SJeff Bonwick */ 6705e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6706e14bb325SJeff Bonwick 6707e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6708e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6709e14bb325SJeff Bonwick int svdcount = 0; 6710e14bb325SJeff Bonwick int children = rvd->vdev_children; 6711e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6712e14bb325SJeff Bonwick 6713573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6714e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6715e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6716e14bb325SJeff Bonwick continue; 6717e14bb325SJeff Bonwick svd[svdcount++] = vd; 6718e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6719e14bb325SJeff Bonwick break; 6720e14bb325SJeff Bonwick } 6721eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 6722e14bb325SJeff Bonwick } else { 6723e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 6724eb5bb584SWill Andrews rvd->vdev_children, txg); 67250373e76bSbonwick } 6726e14bb325SJeff Bonwick 6727dfbb9432SGeorge Wilson if (error == 0) 6728dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6729dfbb9432SGeorge Wilson 6730e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6731e14bb325SJeff Bonwick 6732e14bb325SJeff Bonwick if (error == 0) 6733e14bb325SJeff Bonwick break; 6734e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6735e14bb325SJeff Bonwick zio_resume_wait(spa); 67360373e76bSbonwick } 673799653d4eSeschrock dmu_tx_commit(tx); 673899653d4eSeschrock 6739283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6740283b8460SGeorge.Wilson 67410373e76bSbonwick /* 67420373e76bSbonwick * Clear the dirty config list. 6743fa9e4066Sahrens */ 6744e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 67450373e76bSbonwick vdev_config_clean(vd); 67460373e76bSbonwick 67470373e76bSbonwick /* 67480373e76bSbonwick * Now that the new config has synced transactionally, 67490373e76bSbonwick * let it become visible to the config cache. 67500373e76bSbonwick */ 67510373e76bSbonwick if (spa->spa_config_syncing != NULL) { 67520373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 67530373e76bSbonwick spa->spa_config_txg = txg; 67540373e76bSbonwick spa->spa_config_syncing = NULL; 67550373e76bSbonwick } 6756fa9e4066Sahrens 6757b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6758fa9e4066Sahrens 67590f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 67600f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 67610f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 67620f7643c7SGeorge Wilson 6763fa9e4066Sahrens /* 6764fa9e4066Sahrens * Update usable space statistics. 6765fa9e4066Sahrens */ 6766fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6767fa9e4066Sahrens vdev_sync_done(vd, txg); 6768fa9e4066Sahrens 6769485bbbf5SGeorge Wilson spa_update_dspace(spa); 6770485bbbf5SGeorge Wilson 6771fa9e4066Sahrens /* 6772fa9e4066Sahrens * It had better be the case that we didn't dirty anything 677399653d4eSeschrock * since vdev_config_sync(). 6774fa9e4066Sahrens */ 6775fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6776fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6777fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6778b24ab676SJeff Bonwick 6779b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6780fa9e4066Sahrens 678143297f97SGeorge Wilson /* 678243297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 678343297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 678443297f97SGeorge Wilson * will be guaranteed that all the processing associated with 678543297f97SGeorge Wilson * that txg has been completed. 678643297f97SGeorge Wilson */ 678743297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 6788e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6789ea8dc4b6Seschrock 6790468c413aSTim Haley spa_handle_ignored_writes(spa); 6791468c413aSTim Haley 6792ea8dc4b6Seschrock /* 6793ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6794ea8dc4b6Seschrock */ 6795ea8dc4b6Seschrock spa_async_dispatch(spa); 6796fa9e4066Sahrens } 6797fa9e4066Sahrens 6798fa9e4066Sahrens /* 6799fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6800fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6801fa9e4066Sahrens * sync. 6802fa9e4066Sahrens */ 6803fa9e4066Sahrens void 6804fa9e4066Sahrens spa_sync_allpools(void) 6805fa9e4066Sahrens { 6806fa9e4066Sahrens spa_t *spa = NULL; 6807fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6808fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6809f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6810f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6811fa9e4066Sahrens continue; 6812fa9e4066Sahrens spa_open_ref(spa, FTAG); 6813fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6814fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6815fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6816fa9e4066Sahrens spa_close(spa, FTAG); 6817fa9e4066Sahrens } 6818fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6819fa9e4066Sahrens } 6820fa9e4066Sahrens 6821fa9e4066Sahrens /* 6822fa9e4066Sahrens * ========================================================================== 6823fa9e4066Sahrens * Miscellaneous routines 6824fa9e4066Sahrens * ========================================================================== 6825fa9e4066Sahrens */ 6826fa9e4066Sahrens 6827fa9e4066Sahrens /* 6828fa9e4066Sahrens * Remove all pools in the system. 6829fa9e4066Sahrens */ 6830fa9e4066Sahrens void 6831fa9e4066Sahrens spa_evict_all(void) 6832fa9e4066Sahrens { 6833fa9e4066Sahrens spa_t *spa; 6834fa9e4066Sahrens 6835fa9e4066Sahrens /* 6836fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6837fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6838fa9e4066Sahrens */ 6839fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6840fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6841fa9e4066Sahrens /* 6842ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6843ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6844ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6845fa9e4066Sahrens */ 6846fa9e4066Sahrens spa_open_ref(spa, FTAG); 6847fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6848ea8dc4b6Seschrock spa_async_suspend(spa); 6849fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6850fa9e4066Sahrens spa_close(spa, FTAG); 6851fa9e4066Sahrens 6852fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6853fa9e4066Sahrens spa_unload(spa); 6854fa9e4066Sahrens spa_deactivate(spa); 6855fa9e4066Sahrens } 6856fa9e4066Sahrens spa_remove(spa); 6857fa9e4066Sahrens } 6858fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6859fa9e4066Sahrens } 6860ea8dc4b6Seschrock 6861ea8dc4b6Seschrock vdev_t * 68626809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6863ea8dc4b6Seschrock { 6864c5904d13Seschrock vdev_t *vd; 6865c5904d13Seschrock int i; 6866c5904d13Seschrock 6867c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6868c5904d13Seschrock return (vd); 6869c5904d13Seschrock 68706809eb4eSEric Schrock if (aux) { 6871c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6872c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 68736809eb4eSEric Schrock if (vd->vdev_guid == guid) 68746809eb4eSEric Schrock return (vd); 68756809eb4eSEric Schrock } 68766809eb4eSEric Schrock 68776809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 68786809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6879c5904d13Seschrock if (vd->vdev_guid == guid) 6880c5904d13Seschrock return (vd); 6881c5904d13Seschrock } 6882c5904d13Seschrock } 6883c5904d13Seschrock 6884c5904d13Seschrock return (NULL); 6885ea8dc4b6Seschrock } 6886eaca9bbdSeschrock 6887eaca9bbdSeschrock void 6888990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6889eaca9bbdSeschrock { 6890f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6891f9af39baSGeorge Wilson 6892e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6893eaca9bbdSeschrock 6894eaca9bbdSeschrock /* 6895eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6896eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6897eaca9bbdSeschrock * possible. 6898eaca9bbdSeschrock */ 689962eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 69005d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6901eaca9bbdSeschrock 6902990b4856Slling spa->spa_uberblock.ub_version = version; 6903eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6904eaca9bbdSeschrock 6905e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 690699653d4eSeschrock 690799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 690899653d4eSeschrock } 690999653d4eSeschrock 691099653d4eSeschrock boolean_t 691199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 691299653d4eSeschrock { 691399653d4eSeschrock int i; 691439c23413Seschrock uint64_t spareguid; 6915fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 691699653d4eSeschrock 6917fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6918fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 691999653d4eSeschrock return (B_TRUE); 692099653d4eSeschrock 6921fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6922fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6923fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 692439c23413Seschrock return (B_TRUE); 692539c23413Seschrock } 692639c23413Seschrock 692799653d4eSeschrock return (B_FALSE); 6928eaca9bbdSeschrock } 6929b1b8ab34Slling 693089a89ebfSlling /* 693189a89ebfSlling * Check if a pool has an active shared spare device. 693289a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 693389a89ebfSlling */ 693489a89ebfSlling static boolean_t 693589a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 693689a89ebfSlling { 693789a89ebfSlling int i, refcnt; 693889a89ebfSlling uint64_t pool; 693989a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 694089a89ebfSlling 694189a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 694289a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 694389a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 694489a89ebfSlling refcnt > 2) 694589a89ebfSlling return (B_TRUE); 694689a89ebfSlling } 694789a89ebfSlling 694889a89ebfSlling return (B_FALSE); 694989a89ebfSlling } 695089a89ebfSlling 6951b72b6bb1SAlan Somers static sysevent_t * 6952b72b6bb1SAlan Somers spa_event_create(spa_t *spa, vdev_t *vd, const char *name) 69533d7072f8Seschrock { 6954b72b6bb1SAlan Somers sysevent_t *ev = NULL; 69553d7072f8Seschrock #ifdef _KERNEL 69563d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 69573d7072f8Seschrock sysevent_value_t value; 69583d7072f8Seschrock 69593d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 69603d7072f8Seschrock SE_SLEEP); 6961b72b6bb1SAlan Somers ASSERT(ev != NULL); 69623d7072f8Seschrock 69633d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69643d7072f8Seschrock value.value.sv_string = spa_name(spa); 69653d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 69663d7072f8Seschrock goto done; 69673d7072f8Seschrock 69683d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69693d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 69703d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 69713d7072f8Seschrock goto done; 69723d7072f8Seschrock 69733d7072f8Seschrock if (vd) { 69743d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69753d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 69763d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 69773d7072f8Seschrock SE_SLEEP) != 0) 69783d7072f8Seschrock goto done; 69793d7072f8Seschrock 69803d7072f8Seschrock if (vd->vdev_path) { 69813d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69823d7072f8Seschrock value.value.sv_string = vd->vdev_path; 69833d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 69843d7072f8Seschrock &value, SE_SLEEP) != 0) 69853d7072f8Seschrock goto done; 69863d7072f8Seschrock } 69873d7072f8Seschrock } 69883d7072f8Seschrock 6989b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6990b01c3b58Seschrock goto done; 6991b01c3b58Seschrock attr = NULL; 6992b01c3b58Seschrock 69933d7072f8Seschrock done: 69943d7072f8Seschrock if (attr) 69953d7072f8Seschrock sysevent_free_attr(attr); 6996b72b6bb1SAlan Somers 6997b72b6bb1SAlan Somers #endif 6998b72b6bb1SAlan Somers return (ev); 6999b72b6bb1SAlan Somers } 7000b72b6bb1SAlan Somers 7001b72b6bb1SAlan Somers static void 7002b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 7003b72b6bb1SAlan Somers { 7004b72b6bb1SAlan Somers #ifdef _KERNEL 7005b72b6bb1SAlan Somers sysevent_id_t eid; 7006b72b6bb1SAlan Somers 7007b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 70083d7072f8Seschrock sysevent_free(ev); 70093d7072f8Seschrock #endif 70103d7072f8Seschrock } 7011b72b6bb1SAlan Somers 7012b72b6bb1SAlan Somers /* 7013b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 7014b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 7015b72b6bb1SAlan Somers * filled in from the spa and (optionally) the vdev. This doesn't do anything 7016b72b6bb1SAlan Somers * in the userland libzpool, as we don't want consumers to misinterpret ztest 7017b72b6bb1SAlan Somers * or zdb as real changes. 7018b72b6bb1SAlan Somers */ 7019b72b6bb1SAlan Somers void 7020b72b6bb1SAlan Somers spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 7021b72b6bb1SAlan Somers { 7022b72b6bb1SAlan Somers spa_event_post(spa_event_create(spa, vd, name)); 7023b72b6bb1SAlan Somers } 7024