1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 2398d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 241b497ab8SAdam H. Leventhal * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 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] 29*c8811bd3SToomas 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> 73fa9e4066Sahrens 745679c89fSjv #ifdef _KERNEL 75dedec472SJack Meng #include <sys/bootprops.h> 7635a5a358SJonathan Adams #include <sys/callb.h> 7735a5a358SJonathan Adams #include <sys/cpupart.h> 7835a5a358SJonathan Adams #include <sys/pool.h> 7935a5a358SJonathan Adams #include <sys/sysdc.h> 8035a5a358SJonathan Adams #include <sys/zone.h> 815679c89fSjv #endif /* _KERNEL */ 825679c89fSjv 83990b4856Slling #include "zfs_prop.h" 84b7b97454Sperrin #include "zfs_comutil.h" 85990b4856Slling 863cb69f73SWill Andrews /* 873cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 883cb69f73SWill Andrews * should be retried. 893cb69f73SWill Andrews */ 903cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300; 913cb69f73SWill Andrews 9235a5a358SJonathan Adams typedef enum zti_modes { 93ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 94ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 95ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 96ec94d322SAdam Leventhal ZTI_NMODES 9735a5a358SJonathan Adams } zti_modes_t; 98416e0cd8Sek 99ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 100ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 101ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1022e0c549eSJonathan Adams 103ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 104ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1052e0c549eSJonathan Adams 1062e0c549eSJonathan Adams typedef struct zio_taskq_info { 107ec94d322SAdam Leventhal zti_modes_t zti_mode; 10880eb36f2SGeorge Wilson uint_t zti_value; 109ec94d322SAdam Leventhal uint_t zti_count; 1102e0c549eSJonathan Adams } zio_taskq_info_t; 1112e0c549eSJonathan Adams 1122e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 11335a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1142e0c549eSJonathan Adams }; 1152e0c549eSJonathan Adams 11680eb36f2SGeorge Wilson /* 117ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 118ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 119ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 120ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 121ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 122ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 123ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 124ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 125ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 126ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 127ec94d322SAdam Leventhal * particular taskq is chosen at random. 128ec94d322SAdam Leventhal * 129ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 130ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 131ec94d322SAdam Leventhal * need to be handled with minimum delay. 13280eb36f2SGeorge Wilson */ 13380eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 13480eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 135ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1361b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 137ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 138ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 139ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 140ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1412e0c549eSJonathan Adams }; 1422e0c549eSJonathan Adams 143b72b6bb1SAlan Somers static sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, const char *name); 144b72b6bb1SAlan Somers static void spa_event_post(sysevent_t *ev); 1453b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1463b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 14789a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1481195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1491195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1501195e687SMark J Musante char **ereport); 151cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 152990b4856Slling 15369962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15435a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 15535a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 15635a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 15735a5a358SJonathan Adams 15835a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 15901f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16035a5a358SJonathan Adams 16135a5a358SJonathan Adams /* 16235a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 16335a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 16435a5a358SJonathan Adams */ 16535a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 16635a5a358SJonathan Adams 167990b4856Slling /* 168990b4856Slling * ========================================================================== 169990b4856Slling * SPA properties routines 170990b4856Slling * ========================================================================== 171990b4856Slling */ 172990b4856Slling 173990b4856Slling /* 174990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 175990b4856Slling */ 1769d82f4f6Slling static void 177990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 178990b4856Slling uint64_t intval, zprop_source_t src) 179990b4856Slling { 180990b4856Slling const char *propname = zpool_prop_to_name(prop); 181990b4856Slling nvlist_t *propval; 182990b4856Slling 1839d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1849d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 185990b4856Slling 1869d82f4f6Slling if (strval != NULL) 1879d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1889d82f4f6Slling else 1899d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 190990b4856Slling 1919d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 192990b4856Slling nvlist_free(propval); 193990b4856Slling } 194990b4856Slling 195990b4856Slling /* 196990b4856Slling * Get property values from the spa configuration. 197990b4856Slling */ 1989d82f4f6Slling static void 199990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 200990b4856Slling { 2014263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 202ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2032e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 204990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 205c5904d13Seschrock spa_config_dirent_t *dp; 2062e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 207990b4856Slling 208e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 209e14bb325SJeff Bonwick 2104263d13fSGeorge Wilson if (rvd != NULL) { 211485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 212b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 213379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 214379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 215485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 216485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 217485bbbf5SGeorge Wilson size - alloc, src); 2184263d13fSGeorge Wilson 2192e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2202e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2212e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2222e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 223f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 224f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 225379c004dSEric Schrock 226485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 227379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 228379c004dSEric Schrock 229b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 230b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 231b24ab676SJeff Bonwick 232379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2334263d13fSGeorge Wilson rvd->vdev_state, src); 234379c004dSEric Schrock 235379c004dSEric Schrock version = spa_version(spa); 236379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 237379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 238379c004dSEric Schrock else 239379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 240379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 241379c004dSEric Schrock } 242990b4856Slling 243ad135b5dSChristopher Siden if (pool != NULL) { 244ad135b5dSChristopher Siden /* 245ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 246ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 247ad135b5dSChristopher Siden */ 2487fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 249ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 250c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 251c1379625SJustin T. Gibbs src); 252ad135b5dSChristopher Siden } else { 253ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 254ad135b5dSChristopher Siden NULL, 0, src); 255ad135b5dSChristopher Siden } 2567fd05ac4SMatthew Ahrens 2577fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2587fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 259c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 260c1379625SJustin T. Gibbs src); 2617fd05ac4SMatthew Ahrens } else { 2627fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2637fd05ac4SMatthew Ahrens NULL, 0, src); 2647fd05ac4SMatthew Ahrens } 265ad135b5dSChristopher Siden } 266ad135b5dSChristopher Siden 2679d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 268990b4856Slling 2698704186eSDan McDonald if (spa->spa_comment != NULL) { 2708704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2718704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2728704186eSDan McDonald } 2738704186eSDan McDonald 2749d82f4f6Slling if (spa->spa_root != NULL) 2759d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2769d82f4f6Slling 0, ZPROP_SRC_LOCAL); 277990b4856Slling 278b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 279b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 280b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 281b5152584SMatthew Ahrens } else { 282b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 283b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 284b5152584SMatthew Ahrens } 285b5152584SMatthew Ahrens 286c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 287c5904d13Seschrock if (dp->scd_path == NULL) { 2889d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 289c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 290c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2919d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 292c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2932f8aaab3Seschrock } 2942f8aaab3Seschrock } 295990b4856Slling } 296990b4856Slling 297990b4856Slling /* 298990b4856Slling * Get zpool property values. 299990b4856Slling */ 300990b4856Slling int 301990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 302990b4856Slling { 303b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 304990b4856Slling zap_cursor_t zc; 305990b4856Slling zap_attribute_t za; 306990b4856Slling int err; 307990b4856Slling 3089d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 309990b4856Slling 310e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 311e14bb325SJeff Bonwick 312990b4856Slling /* 313990b4856Slling * Get properties from the spa config. 314990b4856Slling */ 3159d82f4f6Slling spa_prop_get_config(spa, nvp); 316990b4856Slling 317990b4856Slling /* If no pool property object, no more prop to get. */ 318afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 319990b4856Slling mutex_exit(&spa->spa_props_lock); 320990b4856Slling return (0); 321990b4856Slling } 322990b4856Slling 323990b4856Slling /* 324990b4856Slling * Get properties from the MOS pool property object. 325990b4856Slling */ 326990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 327990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 328990b4856Slling zap_cursor_advance(&zc)) { 329990b4856Slling uint64_t intval = 0; 330990b4856Slling char *strval = NULL; 331990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 332990b4856Slling zpool_prop_t prop; 333990b4856Slling 334990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 335990b4856Slling continue; 336990b4856Slling 337990b4856Slling switch (za.za_integer_length) { 338990b4856Slling case 8: 339990b4856Slling /* integer property */ 340990b4856Slling if (za.za_first_integer != 341990b4856Slling zpool_prop_default_numeric(prop)) 342990b4856Slling src = ZPROP_SRC_LOCAL; 343990b4856Slling 344990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 345990b4856Slling dsl_pool_t *dp; 346990b4856Slling dsl_dataset_t *ds = NULL; 347990b4856Slling 348990b4856Slling dp = spa_get_dsl(spa); 3493b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 350745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 351745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3523b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 353990b4856Slling break; 354990b4856Slling } 355990b4856Slling 3569adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 357990b4856Slling KM_SLEEP); 358990b4856Slling dsl_dataset_name(ds, strval); 359745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3603b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 361990b4856Slling } else { 362990b4856Slling strval = NULL; 363990b4856Slling intval = za.za_first_integer; 364990b4856Slling } 365990b4856Slling 3669d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 367990b4856Slling 368990b4856Slling if (strval != NULL) 3699adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 370990b4856Slling 371990b4856Slling break; 372990b4856Slling 373990b4856Slling case 1: 374990b4856Slling /* string property */ 375990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 376990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 377990b4856Slling za.za_name, 1, za.za_num_integers, strval); 378990b4856Slling if (err) { 379990b4856Slling kmem_free(strval, za.za_num_integers); 380990b4856Slling break; 381990b4856Slling } 3829d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 383990b4856Slling kmem_free(strval, za.za_num_integers); 384990b4856Slling break; 385990b4856Slling 386990b4856Slling default: 387990b4856Slling break; 388990b4856Slling } 389990b4856Slling } 390990b4856Slling zap_cursor_fini(&zc); 391990b4856Slling mutex_exit(&spa->spa_props_lock); 392990b4856Slling out: 393990b4856Slling if (err && err != ENOENT) { 394990b4856Slling nvlist_free(*nvp); 3959d82f4f6Slling *nvp = NULL; 396990b4856Slling return (err); 397990b4856Slling } 398990b4856Slling 399990b4856Slling return (0); 400990b4856Slling } 401990b4856Slling 402990b4856Slling /* 403990b4856Slling * Validate the given pool properties nvlist and modify the list 404990b4856Slling * for the property values to be set. 405990b4856Slling */ 406990b4856Slling static int 407990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 408990b4856Slling { 409990b4856Slling nvpair_t *elem; 410990b4856Slling int error = 0, reset_bootfs = 0; 411d5285caeSGeorge Wilson uint64_t objnum = 0; 412ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 413990b4856Slling 414990b4856Slling elem = NULL; 415990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 416990b4856Slling uint64_t intval; 417ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 418ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 419ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 420ad135b5dSChristopher Siden 421ad135b5dSChristopher Siden switch (prop) { 422ad135b5dSChristopher Siden case ZPROP_INVAL: 423ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 424be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 425ad135b5dSChristopher Siden break; 426ad135b5dSChristopher Siden } 427990b4856Slling 428ad135b5dSChristopher Siden /* 429ad135b5dSChristopher Siden * Sanitize the input. 430ad135b5dSChristopher Siden */ 431ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 432be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 433ad135b5dSChristopher Siden break; 434ad135b5dSChristopher Siden } 435990b4856Slling 436ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 437be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 438ad135b5dSChristopher Siden break; 439ad135b5dSChristopher Siden } 440ad135b5dSChristopher Siden 441ad135b5dSChristopher Siden if (intval != 0) { 442be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 443ad135b5dSChristopher Siden break; 444ad135b5dSChristopher Siden } 445ad135b5dSChristopher Siden 446ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 447ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 448be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 449ad135b5dSChristopher Siden break; 450ad135b5dSChristopher Siden } 451ad135b5dSChristopher Siden 452ad135b5dSChristopher Siden has_feature = B_TRUE; 453ad135b5dSChristopher Siden break; 454990b4856Slling 455990b4856Slling case ZPOOL_PROP_VERSION: 456990b4856Slling error = nvpair_value_uint64(elem, &intval); 457990b4856Slling if (!error && 458ad135b5dSChristopher Siden (intval < spa_version(spa) || 459ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 460ad135b5dSChristopher Siden has_feature)) 461be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 462990b4856Slling break; 463990b4856Slling 464990b4856Slling case ZPOOL_PROP_DELEGATION: 465990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 466d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 467573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 468990b4856Slling error = nvpair_value_uint64(elem, &intval); 469990b4856Slling if (!error && intval > 1) 470be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 471990b4856Slling break; 472990b4856Slling 473990b4856Slling case ZPOOL_PROP_BOOTFS: 47425f89ee2SJeff Bonwick /* 47525f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 47625f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 47725f89ee2SJeff Bonwick * the bootfs property cannot be set. 47825f89ee2SJeff Bonwick */ 479990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 480be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 481990b4856Slling break; 482990b4856Slling } 483990b4856Slling 484990b4856Slling /* 48515e6edf1Sgw * Make sure the vdev config is bootable 486990b4856Slling */ 48715e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 488be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 489990b4856Slling break; 490990b4856Slling } 491990b4856Slling 492990b4856Slling reset_bootfs = 1; 493990b4856Slling 494990b4856Slling error = nvpair_value_string(elem, &strval); 495990b4856Slling 496990b4856Slling if (!error) { 497ad135b5dSChristopher Siden objset_t *os; 498b5152584SMatthew Ahrens uint64_t propval; 49915e6edf1Sgw 500990b4856Slling if (strval == NULL || strval[0] == '\0') { 501990b4856Slling objnum = zpool_prop_default_numeric( 502990b4856Slling ZPOOL_PROP_BOOTFS); 503990b4856Slling break; 504990b4856Slling } 505990b4856Slling 506503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 507990b4856Slling break; 50815e6edf1Sgw 509b5152584SMatthew Ahrens /* 510b5152584SMatthew Ahrens * Must be ZPL, and its property settings 511b5152584SMatthew Ahrens * must be supported by GRUB (compression 512b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 513b5152584SMatthew Ahrens */ 514503ad85cSMatthew Ahrens 515503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 516be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5173b2aab18SMatthew Ahrens } else if ((error = 5183b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 51915e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 520b5152584SMatthew Ahrens &propval)) == 0 && 521b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 522b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 52315e6edf1Sgw } else { 52415e6edf1Sgw objnum = dmu_objset_id(os); 52515e6edf1Sgw } 526503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 527990b4856Slling } 528990b4856Slling break; 529e14bb325SJeff Bonwick 5300a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5310a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5320a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5330a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 534be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5350a4e9518Sgw 5360a4e9518Sgw /* 5370a4e9518Sgw * This is a special case which only occurs when 5380a4e9518Sgw * the pool has completely failed. This allows 5390a4e9518Sgw * the user to change the in-core failmode property 5400a4e9518Sgw * without syncing it out to disk (I/Os might 5410a4e9518Sgw * currently be blocked). We do this by returning 5420a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5430a4e9518Sgw * into thinking we encountered a property validation 5440a4e9518Sgw * error. 5450a4e9518Sgw */ 546e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5470a4e9518Sgw spa->spa_failmode = intval; 548be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5490a4e9518Sgw } 5500a4e9518Sgw break; 5512f8aaab3Seschrock 5522f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5532f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5542f8aaab3Seschrock break; 5552f8aaab3Seschrock 5562f8aaab3Seschrock if (strval[0] == '\0') 5572f8aaab3Seschrock break; 5582f8aaab3Seschrock 5592f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5602f8aaab3Seschrock break; 5612f8aaab3Seschrock 5622f8aaab3Seschrock if (strval[0] != '/') { 563be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5642f8aaab3Seschrock break; 5652f8aaab3Seschrock } 5662f8aaab3Seschrock 5672f8aaab3Seschrock slash = strrchr(strval, '/'); 5682f8aaab3Seschrock ASSERT(slash != NULL); 5692f8aaab3Seschrock 5702f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5712f8aaab3Seschrock strcmp(slash, "/..") == 0) 572be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5732f8aaab3Seschrock break; 574b24ab676SJeff Bonwick 5758704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5768704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5778704186eSDan McDonald break; 5788704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5798704186eSDan McDonald /* 5808704186eSDan McDonald * The kernel doesn't have an easy isprint() 5818704186eSDan McDonald * check. For this kernel check, we merely 5828704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5838704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5848704186eSDan McDonald */ 5858704186eSDan McDonald if (*check >= 0x7f) { 586be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5878704186eSDan McDonald break; 5888704186eSDan McDonald } 5898704186eSDan McDonald } 5908704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5918704186eSDan McDonald error = E2BIG; 5928704186eSDan McDonald break; 5938704186eSDan McDonald 594b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 595b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 596be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 597b24ab676SJeff Bonwick else 598b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 599b24ab676SJeff Bonwick if (error == 0 && 600b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 601be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 602b24ab676SJeff Bonwick break; 603990b4856Slling } 604990b4856Slling 605990b4856Slling if (error) 606990b4856Slling break; 607990b4856Slling } 608990b4856Slling 609990b4856Slling if (!error && reset_bootfs) { 610990b4856Slling error = nvlist_remove(props, 611990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 612990b4856Slling 613990b4856Slling if (!error) { 614990b4856Slling error = nvlist_add_uint64(props, 615990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 616990b4856Slling } 617990b4856Slling } 618990b4856Slling 619990b4856Slling return (error); 620990b4856Slling } 621990b4856Slling 622379c004dSEric Schrock void 623379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 624379c004dSEric Schrock { 625379c004dSEric Schrock char *cachefile; 626379c004dSEric Schrock spa_config_dirent_t *dp; 627379c004dSEric Schrock 628379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 629379c004dSEric Schrock &cachefile) != 0) 630379c004dSEric Schrock return; 631379c004dSEric Schrock 632379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 633379c004dSEric Schrock KM_SLEEP); 634379c004dSEric Schrock 635379c004dSEric Schrock if (cachefile[0] == '\0') 636379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 637379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 638379c004dSEric Schrock dp->scd_path = NULL; 639379c004dSEric Schrock else 640379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 641379c004dSEric Schrock 642379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 643379c004dSEric Schrock if (need_sync) 644379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 645379c004dSEric Schrock } 646379c004dSEric Schrock 647990b4856Slling int 648990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 649990b4856Slling { 650990b4856Slling int error; 651ad135b5dSChristopher Siden nvpair_t *elem = NULL; 652379c004dSEric Schrock boolean_t need_sync = B_FALSE; 653990b4856Slling 654990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 655990b4856Slling return (error); 656990b4856Slling 657379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 658ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 659379c004dSEric Schrock 660f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 661f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 662f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 663379c004dSEric Schrock continue; 664379c004dSEric Schrock 665ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 666ad135b5dSChristopher Siden uint64_t ver; 667ad135b5dSChristopher Siden 668ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 669ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 670ad135b5dSChristopher Siden } else { 671ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 672ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 673ad135b5dSChristopher Siden need_sync = B_TRUE; 674ad135b5dSChristopher Siden } 675ad135b5dSChristopher Siden 676ad135b5dSChristopher Siden /* Save time if the version is already set. */ 677ad135b5dSChristopher Siden if (ver == spa_version(spa)) 678ad135b5dSChristopher Siden continue; 679ad135b5dSChristopher Siden 680ad135b5dSChristopher Siden /* 681ad135b5dSChristopher Siden * In addition to the pool directory object, we might 682ad135b5dSChristopher Siden * create the pool properties object, the features for 683ad135b5dSChristopher Siden * read object, the features for write object, or the 684ad135b5dSChristopher Siden * feature descriptions object. 685ad135b5dSChristopher Siden */ 6863b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6877d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6887d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 689ad135b5dSChristopher Siden if (error) 690ad135b5dSChristopher Siden return (error); 691ad135b5dSChristopher Siden continue; 692ad135b5dSChristopher Siden } 693ad135b5dSChristopher Siden 694379c004dSEric Schrock need_sync = B_TRUE; 695379c004dSEric Schrock break; 696379c004dSEric Schrock } 697379c004dSEric Schrock 698ad135b5dSChristopher Siden if (need_sync) { 6993b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7007d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 701ad135b5dSChristopher Siden } 702ad135b5dSChristopher Siden 703ad135b5dSChristopher Siden return (0); 704990b4856Slling } 705990b4856Slling 706990b4856Slling /* 707990b4856Slling * If the bootfs property value is dsobj, clear it. 708990b4856Slling */ 709990b4856Slling void 710990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 711990b4856Slling { 712990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 713990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 714990b4856Slling spa->spa_pool_props_object, 715990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 716990b4856Slling spa->spa_bootfs = 0; 717990b4856Slling } 718990b4856Slling } 719990b4856Slling 720dfbb9432SGeorge Wilson /*ARGSUSED*/ 721dfbb9432SGeorge Wilson static int 7223b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 723dfbb9432SGeorge Wilson { 7243b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7253b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 726dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 727dfbb9432SGeorge Wilson uint64_t vdev_state; 728dfbb9432SGeorge Wilson 729dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 730dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 731dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 732dfbb9432SGeorge Wilson 733dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 734be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 735dfbb9432SGeorge Wilson 736dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 737dfbb9432SGeorge Wilson 738dfbb9432SGeorge Wilson return (0); 739dfbb9432SGeorge Wilson } 740dfbb9432SGeorge Wilson 741dfbb9432SGeorge Wilson static void 7423b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 743dfbb9432SGeorge Wilson { 7443b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7453b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 746dfbb9432SGeorge Wilson uint64_t oldguid; 747dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 748dfbb9432SGeorge Wilson 749dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 750dfbb9432SGeorge Wilson 751dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 752dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 753dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 754dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 755dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 756dfbb9432SGeorge Wilson 75720128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 758dfbb9432SGeorge Wilson oldguid, *newguid); 759dfbb9432SGeorge Wilson } 760dfbb9432SGeorge Wilson 761e9103aaeSGarrett D'Amore /* 762e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 763e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 764e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 765e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 766e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 767e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 768e9103aaeSGarrett D'Amore * sysevent to update any watchers. 769e9103aaeSGarrett D'Amore */ 770e9103aaeSGarrett D'Amore int 771e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 772e9103aaeSGarrett D'Amore { 773dfbb9432SGeorge Wilson int error; 774dfbb9432SGeorge Wilson uint64_t guid; 775e9103aaeSGarrett D'Amore 7762c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 777dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 778dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 779e9103aaeSGarrett D'Amore 7803b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7817d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 782e9103aaeSGarrett D'Amore 783dfbb9432SGeorge Wilson if (error == 0) { 784dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 785dfbb9432SGeorge Wilson spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); 786dfbb9432SGeorge Wilson } 787e9103aaeSGarrett D'Amore 788dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7892c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 790e9103aaeSGarrett D'Amore 791dfbb9432SGeorge Wilson return (error); 792e9103aaeSGarrett D'Amore } 793e9103aaeSGarrett D'Amore 794fa9e4066Sahrens /* 795fa9e4066Sahrens * ========================================================================== 796fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 797fa9e4066Sahrens * ========================================================================== 798fa9e4066Sahrens */ 799fa9e4066Sahrens 800ea8dc4b6Seschrock static int 801ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 802ea8dc4b6Seschrock { 803ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 804ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 805ea8dc4b6Seschrock int ret; 806ea8dc4b6Seschrock 807ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8087802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 809ea8dc4b6Seschrock 810ea8dc4b6Seschrock if (ret < 0) 811ea8dc4b6Seschrock return (-1); 812ea8dc4b6Seschrock else if (ret > 0) 813ea8dc4b6Seschrock return (1); 814ea8dc4b6Seschrock else 815ea8dc4b6Seschrock return (0); 816ea8dc4b6Seschrock } 817ea8dc4b6Seschrock 818ea8dc4b6Seschrock /* 819ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 820ea8dc4b6Seschrock * re-initializes them in the process. 821ea8dc4b6Seschrock */ 822ea8dc4b6Seschrock void 823ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 824ea8dc4b6Seschrock { 825ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 826ea8dc4b6Seschrock 827ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 828ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 829ea8dc4b6Seschrock 830ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 831ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 832ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 833ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 834ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 835ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 836ea8dc4b6Seschrock } 837ea8dc4b6Seschrock 838ec94d322SAdam Leventhal static void 839ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 840fa9e4066Sahrens { 841ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 842ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 843ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 844ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 845ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 846ec94d322SAdam Leventhal char name[32]; 8475aeb9474SGarrett D'Amore uint_t flags = 0; 84835a5a358SJonathan Adams boolean_t batch = B_FALSE; 849fa9e4066Sahrens 850ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 851ec94d322SAdam Leventhal tqs->stqs_count = 0; 852ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 853ec94d322SAdam Leventhal return; 854ec94d322SAdam Leventhal } 855fa9e4066Sahrens 856ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 857fa9e4066Sahrens 858ec94d322SAdam Leventhal tqs->stqs_count = count; 859ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86035a5a358SJonathan Adams 86169962b56SMatthew Ahrens switch (mode) { 86269962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86369962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86469962b56SMatthew Ahrens value = MAX(value, 1); 86569962b56SMatthew Ahrens break; 866ec94d322SAdam Leventhal 86769962b56SMatthew Ahrens case ZTI_MODE_BATCH: 86869962b56SMatthew Ahrens batch = B_TRUE; 86969962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87069962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87169962b56SMatthew Ahrens break; 872ec94d322SAdam Leventhal 87369962b56SMatthew Ahrens default: 87469962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 87569962b56SMatthew Ahrens "spa_activate()", 87669962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 87769962b56SMatthew Ahrens break; 87869962b56SMatthew Ahrens } 879ec94d322SAdam Leventhal 88069962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88169962b56SMatthew Ahrens taskq_t *tq; 882ec94d322SAdam Leventhal 883ec94d322SAdam Leventhal if (count > 1) { 884ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 885ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 886ec94d322SAdam Leventhal } else { 887ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 888ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 889ec94d322SAdam Leventhal } 890ec94d322SAdam Leventhal 891ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 892ec94d322SAdam Leventhal if (batch) 893ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 894ec94d322SAdam Leventhal 895ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 896ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 897ec94d322SAdam Leventhal } else { 89869962b56SMatthew Ahrens pri_t pri = maxclsyspri; 89969962b56SMatthew Ahrens /* 90069962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90169962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90269962b56SMatthew Ahrens * than the other taskqs. 90369962b56SMatthew Ahrens */ 90469962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 90569962b56SMatthew Ahrens pri--; 90669962b56SMatthew Ahrens 90769962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 908ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 909ec94d322SAdam Leventhal } 910ec94d322SAdam Leventhal 911ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 912ec94d322SAdam Leventhal } 913ec94d322SAdam Leventhal } 914ec94d322SAdam Leventhal 915ec94d322SAdam Leventhal static void 916ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 917ec94d322SAdam Leventhal { 918ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 919ec94d322SAdam Leventhal 920ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 921ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 922ec94d322SAdam Leventhal return; 923ec94d322SAdam Leventhal } 924ec94d322SAdam Leventhal 925ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 926ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 927ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 92835a5a358SJonathan Adams } 92935a5a358SJonathan Adams 930ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 931ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 932ec94d322SAdam Leventhal } 93335a5a358SJonathan Adams 934ec94d322SAdam Leventhal /* 935ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 936ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 937ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 938ec94d322SAdam Leventhal * the low bits of gethrtime(). 939ec94d322SAdam Leventhal */ 940ec94d322SAdam Leventhal void 941ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 942ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 943ec94d322SAdam Leventhal { 944ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 945ec94d322SAdam Leventhal taskq_t *tq; 946ec94d322SAdam Leventhal 947ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 948ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 949ec94d322SAdam Leventhal 950ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 951ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 952ec94d322SAdam Leventhal } else { 953ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95435a5a358SJonathan Adams } 955ec94d322SAdam Leventhal 956ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 95735a5a358SJonathan Adams } 95835a5a358SJonathan Adams 95935a5a358SJonathan Adams static void 96035a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96135a5a358SJonathan Adams { 962e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 963e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 964ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 96535a5a358SJonathan Adams } 96635a5a358SJonathan Adams } 96735a5a358SJonathan Adams } 96835a5a358SJonathan Adams 96935a5a358SJonathan Adams #ifdef _KERNEL 97035a5a358SJonathan Adams static void 97135a5a358SJonathan Adams spa_thread(void *arg) 97235a5a358SJonathan Adams { 97335a5a358SJonathan Adams callb_cpr_t cprinfo; 9742e0c549eSJonathan Adams 97535a5a358SJonathan Adams spa_t *spa = arg; 97635a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9772e0c549eSJonathan Adams 97835a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 97935a5a358SJonathan Adams spa->spa_name); 9802e0c549eSJonathan Adams 98135a5a358SJonathan Adams ASSERT(curproc != &p0); 98235a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98335a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98435a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9852e0c549eSJonathan Adams 98635a5a358SJonathan Adams /* bind this thread to the requested psrset */ 98735a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 98835a5a358SJonathan Adams pool_lock(); 98935a5a358SJonathan Adams mutex_enter(&cpu_lock); 99035a5a358SJonathan Adams mutex_enter(&pidlock); 99135a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99280eb36f2SGeorge Wilson 99335a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99435a5a358SJonathan Adams 0, NULL, NULL) == 0) { 99535a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 99635a5a358SJonathan Adams } else { 99735a5a358SJonathan Adams cmn_err(CE_WARN, 99835a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 99935a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100035a5a358SJonathan Adams } 100135a5a358SJonathan Adams 100235a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100335a5a358SJonathan Adams mutex_exit(&pidlock); 100435a5a358SJonathan Adams mutex_exit(&cpu_lock); 100535a5a358SJonathan Adams pool_unlock(); 100635a5a358SJonathan Adams } 100735a5a358SJonathan Adams 100835a5a358SJonathan Adams if (zio_taskq_sysdc) { 100935a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101035a5a358SJonathan Adams } 101135a5a358SJonathan Adams 101235a5a358SJonathan Adams spa->spa_proc = curproc; 101335a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101435a5a358SJonathan Adams 101535a5a358SJonathan Adams spa_create_zio_taskqs(spa); 101635a5a358SJonathan Adams 101735a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 101835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 101935a5a358SJonathan Adams 102035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102135a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102235a5a358SJonathan Adams 102335a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102435a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 102535a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 102635a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 102735a5a358SJonathan Adams 102835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 102935a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103035a5a358SJonathan Adams spa->spa_proc = &p0; 103135a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103235a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103335a5a358SJonathan Adams 103435a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 103535a5a358SJonathan Adams lwp_exit(); 103635a5a358SJonathan Adams } 103735a5a358SJonathan Adams #endif 103835a5a358SJonathan Adams 103935a5a358SJonathan Adams /* 104035a5a358SJonathan Adams * Activate an uninitialized pool. 104135a5a358SJonathan Adams */ 104235a5a358SJonathan Adams static void 104335a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104435a5a358SJonathan Adams { 104535a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 104635a5a358SJonathan Adams 104735a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 104835a5a358SJonathan Adams spa->spa_mode = mode; 104935a5a358SJonathan Adams 105035a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105135a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105235a5a358SJonathan Adams 105335a5a358SJonathan Adams /* Try to create a covering process */ 105435a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 105535a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 105635a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 105735a5a358SJonathan Adams spa->spa_did = 0; 105835a5a358SJonathan Adams 105935a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106035a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106135a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106235a5a358SJonathan Adams NULL, 0) == 0) { 106335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106435a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 106535a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 106635a5a358SJonathan Adams &spa->spa_proc_lock); 10672e0c549eSJonathan Adams } 106835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 106935a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107035a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107135a5a358SJonathan Adams } else { 107235a5a358SJonathan Adams #ifdef _KERNEL 107335a5a358SJonathan Adams cmn_err(CE_WARN, 107435a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 107535a5a358SJonathan Adams spa->spa_name); 107635a5a358SJonathan Adams #endif 1077e14bb325SJeff Bonwick } 1078fa9e4066Sahrens } 107935a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108035a5a358SJonathan Adams 108135a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108235a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108335a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108435a5a358SJonathan Adams } 1085fa9e4066Sahrens 1086e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1087e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1088bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1089bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1090e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1091e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1092fa9e4066Sahrens 1093fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 1094fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1095ea8dc4b6Seschrock 1096ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1097ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1098ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1099ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1100ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1101ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1102fa9e4066Sahrens } 1103fa9e4066Sahrens 1104fa9e4066Sahrens /* 1105fa9e4066Sahrens * Opposite of spa_activate(). 1106fa9e4066Sahrens */ 1107fa9e4066Sahrens static void 1108fa9e4066Sahrens spa_deactivate(spa_t *spa) 1109fa9e4066Sahrens { 1110fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1111fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1112fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 111325f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1114fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1115fa9e4066Sahrens 1116bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1117bc9014e6SJustin Gibbs 1118fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1119fa9e4066Sahrens 1120e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1121bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1122e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1123fa9e4066Sahrens 1124e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1125e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1126ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1127e14bb325SJeff Bonwick } 1128fa9e4066Sahrens } 1129fa9e4066Sahrens 1130fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1131fa9e4066Sahrens spa->spa_normal_class = NULL; 1132fa9e4066Sahrens 11338654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11348654d025Sperrin spa->spa_log_class = NULL; 11358654d025Sperrin 1136ea8dc4b6Seschrock /* 1137ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1138ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1139ea8dc4b6Seschrock */ 1140ea8dc4b6Seschrock spa_errlog_drain(spa); 1141ea8dc4b6Seschrock 1142ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1143ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1144ea8dc4b6Seschrock 1145fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 114635a5a358SJonathan Adams 114735a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 114835a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 114935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 115035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 115135a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 115235a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 115335a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 115435a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 115535a5a358SJonathan Adams } 115635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 115735a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 115835a5a358SJonathan Adams } 115935a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 116035a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 116135a5a358SJonathan Adams 116235a5a358SJonathan Adams /* 116335a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 116435a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 116535a5a358SJonathan Adams * it. 116635a5a358SJonathan Adams */ 116735a5a358SJonathan Adams if (spa->spa_did != 0) { 116835a5a358SJonathan Adams thread_join(spa->spa_did); 116935a5a358SJonathan Adams spa->spa_did = 0; 117035a5a358SJonathan Adams } 1171fa9e4066Sahrens } 1172fa9e4066Sahrens 1173fa9e4066Sahrens /* 1174fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1175fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1176fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1177fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1178fa9e4066Sahrens */ 117999653d4eSeschrock static int 118099653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 118199653d4eSeschrock uint_t id, int atype) 1182fa9e4066Sahrens { 1183fa9e4066Sahrens nvlist_t **child; 1184573ca77eSGeorge Wilson uint_t children; 118599653d4eSeschrock int error; 1186fa9e4066Sahrens 118799653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 118899653d4eSeschrock return (error); 1189fa9e4066Sahrens 119099653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 119199653d4eSeschrock return (0); 1192fa9e4066Sahrens 1193e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1194e14bb325SJeff Bonwick &child, &children); 1195e14bb325SJeff Bonwick 1196e14bb325SJeff Bonwick if (error == ENOENT) 1197e14bb325SJeff Bonwick return (0); 1198e14bb325SJeff Bonwick 1199e14bb325SJeff Bonwick if (error) { 120099653d4eSeschrock vdev_free(*vdp); 120199653d4eSeschrock *vdp = NULL; 1202be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1203fa9e4066Sahrens } 1204fa9e4066Sahrens 1205573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 120699653d4eSeschrock vdev_t *vd; 120799653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 120899653d4eSeschrock atype)) != 0) { 120999653d4eSeschrock vdev_free(*vdp); 121099653d4eSeschrock *vdp = NULL; 121199653d4eSeschrock return (error); 1212fa9e4066Sahrens } 1213fa9e4066Sahrens } 1214fa9e4066Sahrens 121599653d4eSeschrock ASSERT(*vdp != NULL); 121699653d4eSeschrock 121799653d4eSeschrock return (0); 1218fa9e4066Sahrens } 1219fa9e4066Sahrens 1220fa9e4066Sahrens /* 1221fa9e4066Sahrens * Opposite of spa_load(). 1222fa9e4066Sahrens */ 1223fa9e4066Sahrens static void 1224fa9e4066Sahrens spa_unload(spa_t *spa) 1225fa9e4066Sahrens { 122699653d4eSeschrock int i; 122799653d4eSeschrock 1228e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1229e14bb325SJeff Bonwick 1230ea8dc4b6Seschrock /* 1231ea8dc4b6Seschrock * Stop async tasks. 1232ea8dc4b6Seschrock */ 1233ea8dc4b6Seschrock spa_async_suspend(spa); 1234ea8dc4b6Seschrock 1235fa9e4066Sahrens /* 1236fa9e4066Sahrens * Stop syncing. 1237fa9e4066Sahrens */ 1238fa9e4066Sahrens if (spa->spa_sync_on) { 1239fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1240fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1241fa9e4066Sahrens } 1242fa9e4066Sahrens 1243fa9e4066Sahrens /* 1244e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1245fa9e4066Sahrens */ 124654d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12476f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12486f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12496f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 125054d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 125154d692b7SGeorge Wilson } 1252fa9e4066Sahrens 1253cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1254cde58dbcSMatthew Ahrens 12550713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12560713e232SGeorge Wilson 12570713e232SGeorge Wilson /* 12580713e232SGeorge Wilson * Close all vdevs. 12590713e232SGeorge Wilson */ 12600713e232SGeorge Wilson if (spa->spa_root_vdev) 12610713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12620713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12630713e232SGeorge Wilson 1264fa9e4066Sahrens /* 1265fa9e4066Sahrens * Close the dsl pool. 1266fa9e4066Sahrens */ 1267fa9e4066Sahrens if (spa->spa_dsl_pool) { 1268fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1269fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1270afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1271fa9e4066Sahrens } 1272fa9e4066Sahrens 1273b24ab676SJeff Bonwick ddt_unload(spa); 1274b24ab676SJeff Bonwick 12758ad4d6ddSJeff Bonwick /* 12768ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12778ad4d6ddSJeff Bonwick */ 12788ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12798ad4d6ddSJeff Bonwick 1280fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1281fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1282fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1283fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1284fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1285fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 128699653d4eSeschrock } 1287fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1288fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1289fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1290fa94a07fSbrendan } 12912ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1292fa94a07fSbrendan 1293cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1294cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1295fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1296cd0837ccSGeorge Wilson } 1297fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1298fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1299fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1300fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1301fa94a07fSbrendan } 1302fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1303fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1304fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 130599653d4eSeschrock } 13062ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 130799653d4eSeschrock 1308ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13098ad4d6ddSJeff Bonwick 13108704186eSDan McDonald if (spa->spa_comment != NULL) { 13118704186eSDan McDonald spa_strfree(spa->spa_comment); 13128704186eSDan McDonald spa->spa_comment = NULL; 13138704186eSDan McDonald } 13148704186eSDan McDonald 13158ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1316fa9e4066Sahrens } 1317fa9e4066Sahrens 131899653d4eSeschrock /* 131999653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 132099653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1321fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1322fa94a07fSbrendan * then re-generate a more complete list including status information. 132399653d4eSeschrock */ 132499653d4eSeschrock static void 132599653d4eSeschrock spa_load_spares(spa_t *spa) 132699653d4eSeschrock { 132799653d4eSeschrock nvlist_t **spares; 132899653d4eSeschrock uint_t nspares; 132999653d4eSeschrock int i; 133039c23413Seschrock vdev_t *vd, *tvd; 133199653d4eSeschrock 1332e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1333e14bb325SJeff Bonwick 133499653d4eSeschrock /* 133599653d4eSeschrock * First, close and free any existing spare vdevs. 133699653d4eSeschrock */ 1337fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1338fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 133939c23413Seschrock 134039c23413Seschrock /* Undo the call to spa_activate() below */ 1341c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1342c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 134339c23413Seschrock spa_spare_remove(tvd); 134439c23413Seschrock vdev_close(vd); 134539c23413Seschrock vdev_free(vd); 134699653d4eSeschrock } 134739c23413Seschrock 1348fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1349fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1350fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 135199653d4eSeschrock 1352fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 135399653d4eSeschrock nspares = 0; 135499653d4eSeschrock else 1355fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 135699653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 135799653d4eSeschrock 1358fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1359fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 136099653d4eSeschrock 136199653d4eSeschrock if (nspares == 0) 136299653d4eSeschrock return; 136399653d4eSeschrock 136499653d4eSeschrock /* 136599653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 136639c23413Seschrock * process. For each spare, there is potentially two different vdev_t 136739c23413Seschrock * structures associated with it: one in the list of spares (used only 136839c23413Seschrock * for basic validation purposes) and one in the active vdev 136939c23413Seschrock * configuration (if it's spared in). During this phase we open and 137039c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 137139c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 137299653d4eSeschrock */ 1373fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1374fa94a07fSbrendan KM_SLEEP); 1375fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 137699653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 137799653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 137899653d4eSeschrock ASSERT(vd != NULL); 137999653d4eSeschrock 1380fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 138199653d4eSeschrock 1382c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1383c5904d13Seschrock B_FALSE)) != NULL) { 138439c23413Seschrock if (!tvd->vdev_isspare) 138539c23413Seschrock spa_spare_add(tvd); 138639c23413Seschrock 138739c23413Seschrock /* 138839c23413Seschrock * We only mark the spare active if we were successfully 138939c23413Seschrock * able to load the vdev. Otherwise, importing a pool 139039c23413Seschrock * with a bad active spare would result in strange 139139c23413Seschrock * behavior, because multiple pool would think the spare 139239c23413Seschrock * is actively in use. 139339c23413Seschrock * 139439c23413Seschrock * There is a vulnerability here to an equally bizarre 139539c23413Seschrock * circumstance, where a dead active spare is later 139639c23413Seschrock * brought back to life (onlined or otherwise). Given 139739c23413Seschrock * the rarity of this scenario, and the extra complexity 139839c23413Seschrock * it adds, we ignore the possibility. 139939c23413Seschrock */ 140039c23413Seschrock if (!vdev_is_dead(tvd)) 140139c23413Seschrock spa_spare_activate(tvd); 140239c23413Seschrock } 140339c23413Seschrock 1404e14bb325SJeff Bonwick vd->vdev_top = vd; 14056809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1406e14bb325SJeff Bonwick 140799653d4eSeschrock if (vdev_open(vd) != 0) 140899653d4eSeschrock continue; 140999653d4eSeschrock 1410fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1411fa94a07fSbrendan spa_spare_add(vd); 141299653d4eSeschrock } 141399653d4eSeschrock 141499653d4eSeschrock /* 141599653d4eSeschrock * Recompute the stashed list of spares, with status information 141699653d4eSeschrock * this time. 141799653d4eSeschrock */ 1418fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 141999653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 142099653d4eSeschrock 1421fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1422fa94a07fSbrendan KM_SLEEP); 1423fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1424fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14253f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1426fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1427fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1428fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 142999653d4eSeschrock nvlist_free(spares[i]); 1430fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1431fa94a07fSbrendan } 1432fa94a07fSbrendan 1433fa94a07fSbrendan /* 1434fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1435fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1436fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1437fa94a07fSbrendan * then re-generate a more complete list including status information. 1438fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1439fa94a07fSbrendan * not re-opened. 1440fa94a07fSbrendan */ 1441fa94a07fSbrendan static void 1442fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1443fa94a07fSbrendan { 1444fa94a07fSbrendan nvlist_t **l2cache; 1445fa94a07fSbrendan uint_t nl2cache; 1446fa94a07fSbrendan int i, j, oldnvdevs; 1447573ca77eSGeorge Wilson uint64_t guid; 1448fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1449fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1450fa94a07fSbrendan 1451e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1452e14bb325SJeff Bonwick 1453fa94a07fSbrendan if (sav->sav_config != NULL) { 1454fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1455fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1456fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1457fa94a07fSbrendan } else { 1458fa94a07fSbrendan nl2cache = 0; 1459d5285caeSGeorge Wilson newvdevs = NULL; 1460fa94a07fSbrendan } 1461fa94a07fSbrendan 1462fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1463fa94a07fSbrendan oldnvdevs = sav->sav_count; 1464fa94a07fSbrendan sav->sav_vdevs = NULL; 1465fa94a07fSbrendan sav->sav_count = 0; 1466fa94a07fSbrendan 1467fa94a07fSbrendan /* 1468fa94a07fSbrendan * Process new nvlist of vdevs. 1469fa94a07fSbrendan */ 1470fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1471fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1472fa94a07fSbrendan &guid) == 0); 1473fa94a07fSbrendan 1474fa94a07fSbrendan newvdevs[i] = NULL; 1475fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1476fa94a07fSbrendan vd = oldvdevs[j]; 1477fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1478fa94a07fSbrendan /* 1479fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1480fa94a07fSbrendan */ 1481fa94a07fSbrendan newvdevs[i] = vd; 1482fa94a07fSbrendan oldvdevs[j] = NULL; 1483fa94a07fSbrendan break; 1484fa94a07fSbrendan } 1485fa94a07fSbrendan } 1486fa94a07fSbrendan 1487fa94a07fSbrendan if (newvdevs[i] == NULL) { 1488fa94a07fSbrendan /* 1489fa94a07fSbrendan * Create new vdev 1490fa94a07fSbrendan */ 1491fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1492fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1493fa94a07fSbrendan ASSERT(vd != NULL); 1494fa94a07fSbrendan newvdevs[i] = vd; 1495fa94a07fSbrendan 1496fa94a07fSbrendan /* 1497fa94a07fSbrendan * Commit this vdev as an l2cache device, 1498fa94a07fSbrendan * even if it fails to open. 1499fa94a07fSbrendan */ 1500fa94a07fSbrendan spa_l2cache_add(vd); 1501fa94a07fSbrendan 1502c5904d13Seschrock vd->vdev_top = vd; 1503c5904d13Seschrock vd->vdev_aux = sav; 1504c5904d13Seschrock 1505c5904d13Seschrock spa_l2cache_activate(vd); 1506c5904d13Seschrock 1507fa94a07fSbrendan if (vdev_open(vd) != 0) 1508fa94a07fSbrendan continue; 1509fa94a07fSbrendan 1510fa94a07fSbrendan (void) vdev_validate_aux(vd); 1511fa94a07fSbrendan 1512573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1513573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1514fa94a07fSbrendan } 1515fa94a07fSbrendan } 1516fa94a07fSbrendan 1517fa94a07fSbrendan /* 1518fa94a07fSbrendan * Purge vdevs that were dropped 1519fa94a07fSbrendan */ 1520fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1521fa94a07fSbrendan uint64_t pool; 1522fa94a07fSbrendan 1523fa94a07fSbrendan vd = oldvdevs[i]; 1524fa94a07fSbrendan if (vd != NULL) { 1525cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1526cd0837ccSGeorge Wilson 15278ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15288ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1529fa94a07fSbrendan l2arc_remove_vdev(vd); 1530cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1531cd0837ccSGeorge Wilson vdev_free(vd); 1532fa94a07fSbrendan } 1533fa94a07fSbrendan } 1534fa94a07fSbrendan 1535fa94a07fSbrendan if (oldvdevs) 1536fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1537fa94a07fSbrendan 1538fa94a07fSbrendan if (sav->sav_config == NULL) 1539fa94a07fSbrendan goto out; 1540fa94a07fSbrendan 1541fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1542fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1543fa94a07fSbrendan 1544fa94a07fSbrendan /* 1545fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1546fa94a07fSbrendan * information this time. 1547fa94a07fSbrendan */ 1548fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1549fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1550fa94a07fSbrendan 1551fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1552fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1553fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15543f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1555fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1556fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1557fa94a07fSbrendan out: 1558fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1559fa94a07fSbrendan nvlist_free(l2cache[i]); 1560fa94a07fSbrendan if (sav->sav_count) 1561fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 156299653d4eSeschrock } 156399653d4eSeschrock 156499653d4eSeschrock static int 156599653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 156699653d4eSeschrock { 156799653d4eSeschrock dmu_buf_t *db; 156899653d4eSeschrock char *packed = NULL; 156999653d4eSeschrock size_t nvsize = 0; 157099653d4eSeschrock int error; 157199653d4eSeschrock *value = NULL; 157299653d4eSeschrock 1573a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1574a45f1c3cSBrian Behlendorf if (error != 0) 1575a45f1c3cSBrian Behlendorf return (error); 1576a45f1c3cSBrian Behlendorf 157799653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 157899653d4eSeschrock dmu_buf_rele(db, FTAG); 157999653d4eSeschrock 158099653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15817bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15827bfdf011SNeil Perrin DMU_READ_PREFETCH); 158399653d4eSeschrock if (error == 0) 158499653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 158599653d4eSeschrock kmem_free(packed, nvsize); 158699653d4eSeschrock 158799653d4eSeschrock return (error); 158899653d4eSeschrock } 158999653d4eSeschrock 15903d7072f8Seschrock /* 15913d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 15923d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 15933d7072f8Seschrock */ 15943d7072f8Seschrock static void 15953d7072f8Seschrock spa_check_removed(vdev_t *vd) 15963d7072f8Seschrock { 1597573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 15983d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 15993d7072f8Seschrock 1600efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1601efb4a871SYuri Pankov !vd->vdev_ishole) { 16023d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 16033d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 16043d7072f8Seschrock } 16053d7072f8Seschrock } 16063d7072f8Seschrock 1607215198a6SJoe Stein static void 1608215198a6SJoe Stein spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) 1609215198a6SJoe Stein { 1610215198a6SJoe Stein ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); 1611215198a6SJoe Stein 1612215198a6SJoe Stein vd->vdev_top_zap = mvd->vdev_top_zap; 1613215198a6SJoe Stein vd->vdev_leaf_zap = mvd->vdev_leaf_zap; 1614215198a6SJoe Stein 1615215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 1616215198a6SJoe Stein spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); 1617215198a6SJoe Stein } 1618215198a6SJoe Stein } 1619215198a6SJoe Stein 1620e6ca193dSGeorge Wilson /* 16214b964adaSGeorge Wilson * Validate the current config against the MOS config 1622e6ca193dSGeorge Wilson */ 16234b964adaSGeorge Wilson static boolean_t 16244b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1625e6ca193dSGeorge Wilson { 16264b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16274b964adaSGeorge Wilson nvlist_t *nv; 16284b964adaSGeorge Wilson 16294b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16304b964adaSGeorge Wilson 16314b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16324b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16334b964adaSGeorge Wilson 16344b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1635e6ca193dSGeorge Wilson 163688ecc943SGeorge Wilson /* 16374b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16384b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16394b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 164088ecc943SGeorge Wilson */ 16414b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16424b964adaSGeorge Wilson nvlist_t **child, *nv; 16434b964adaSGeorge Wilson uint64_t idx = 0; 16444b964adaSGeorge Wilson 16454b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16464b964adaSGeorge Wilson KM_SLEEP); 16474b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1648e6ca193dSGeorge Wilson 16494b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16504b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16514b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16524b964adaSGeorge Wilson 16534b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16544b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16554b964adaSGeorge Wilson mtvd->vdev_islog) 16564b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16574b964adaSGeorge Wilson B_FALSE, 0); 16584b964adaSGeorge Wilson } 16594b964adaSGeorge Wilson 16604b964adaSGeorge Wilson if (idx) { 16614b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16624b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16634b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16644b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16654b964adaSGeorge Wilson 16664b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16674b964adaSGeorge Wilson nvlist_free(child[i]); 16684b964adaSGeorge Wilson } 16694b964adaSGeorge Wilson nvlist_free(nv); 16704b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16714b964adaSGeorge Wilson } 16724b964adaSGeorge Wilson 16734b964adaSGeorge Wilson /* 16744b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16754b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16764b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16774b964adaSGeorge Wilson */ 167888ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16794b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16804b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16814b964adaSGeorge Wilson 16824b964adaSGeorge Wilson /* 16834b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 16844b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 16854b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 16864b964adaSGeorge Wilson */ 16874b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16884b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 16894b964adaSGeorge Wilson 16904b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 16914b964adaSGeorge Wilson continue; 16924b964adaSGeorge Wilson 16934b964adaSGeorge Wilson /* 16944b964adaSGeorge Wilson * Device specific actions. 16954b964adaSGeorge Wilson */ 16964b964adaSGeorge Wilson if (mtvd->vdev_islog) { 16974b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 16984b964adaSGeorge Wilson } else { 16994b964adaSGeorge Wilson /* 17004b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 17014b964adaSGeorge Wilson * support we should be able to handle 17024b964adaSGeorge Wilson * missing data devices by transitioning 17034b964adaSGeorge Wilson * the pool to readonly. 17044b964adaSGeorge Wilson */ 17054b964adaSGeorge Wilson continue; 17064b964adaSGeorge Wilson } 17074b964adaSGeorge Wilson 17084b964adaSGeorge Wilson /* 17094b964adaSGeorge Wilson * Swap the missing vdev with the data we were 17104b964adaSGeorge Wilson * able to obtain from the MOS config. 17114b964adaSGeorge Wilson */ 17124b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 17134b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 17144b964adaSGeorge Wilson 17154b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 17164b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17174b964adaSGeorge Wilson 17184b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17194b964adaSGeorge Wilson vdev_load(mtvd); 17204b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17214b964adaSGeorge Wilson 17224b964adaSGeorge Wilson vdev_reopen(rvd); 1723215198a6SJoe Stein } else { 1724215198a6SJoe Stein if (mtvd->vdev_islog) { 1725215198a6SJoe Stein /* 1726215198a6SJoe Stein * Load the slog device's state from the MOS 1727215198a6SJoe Stein * config since it's possible that the label 1728215198a6SJoe Stein * does not contain the most up-to-date 1729215198a6SJoe Stein * information. 1730215198a6SJoe Stein */ 1731215198a6SJoe Stein vdev_load_log_state(tvd, mtvd); 1732215198a6SJoe Stein vdev_reopen(tvd); 1733215198a6SJoe Stein } 1734215198a6SJoe Stein 17354b964adaSGeorge Wilson /* 1736215198a6SJoe Stein * Per-vdev ZAP info is stored exclusively in the MOS. 17374b964adaSGeorge Wilson */ 1738215198a6SJoe Stein spa_config_valid_zaps(tvd, mtvd); 17394b964adaSGeorge Wilson } 1740e6ca193dSGeorge Wilson } 1741215198a6SJoe Stein 17424b964adaSGeorge Wilson vdev_free(mrvd); 174388ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17444b964adaSGeorge Wilson 17454b964adaSGeorge Wilson /* 17464b964adaSGeorge Wilson * Ensure we were able to validate the config. 17474b964adaSGeorge Wilson */ 17484b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1749e6ca193dSGeorge Wilson } 1750e6ca193dSGeorge Wilson 1751b87f3af3Sperrin /* 1752b87f3af3Sperrin * Check for missing log devices 1753b87f3af3Sperrin */ 17543b2aab18SMatthew Ahrens static boolean_t 1755b87f3af3Sperrin spa_check_logs(spa_t *spa) 1756b87f3af3Sperrin { 17573b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 175812380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17593b2aab18SMatthew Ahrens 1760b87f3af3Sperrin switch (spa->spa_log_state) { 1761b87f3af3Sperrin case SPA_LOG_MISSING: 1762b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1763b87f3af3Sperrin case SPA_LOG_UNKNOWN: 176412380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 176512380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 17663b2aab18SMatthew Ahrens if (rv) 17671195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1768b87f3af3Sperrin break; 1769b87f3af3Sperrin } 17703b2aab18SMatthew Ahrens return (rv); 1771b87f3af3Sperrin } 1772b87f3af3Sperrin 17731195e687SMark J Musante static boolean_t 17741195e687SMark J Musante spa_passivate_log(spa_t *spa) 17751195e687SMark J Musante { 17761195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17771195e687SMark J Musante boolean_t slog_found = B_FALSE; 17781195e687SMark J Musante 17791195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17801195e687SMark J Musante 17811195e687SMark J Musante if (!spa_has_slogs(spa)) 17821195e687SMark J Musante return (B_FALSE); 17831195e687SMark J Musante 17841195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17851195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17861195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17871195e687SMark J Musante 17881195e687SMark J Musante if (tvd->vdev_islog) { 17891195e687SMark J Musante metaslab_group_passivate(mg); 17901195e687SMark J Musante slog_found = B_TRUE; 17911195e687SMark J Musante } 17921195e687SMark J Musante } 17931195e687SMark J Musante 17941195e687SMark J Musante return (slog_found); 17951195e687SMark J Musante } 17961195e687SMark J Musante 17971195e687SMark J Musante static void 17981195e687SMark J Musante spa_activate_log(spa_t *spa) 17991195e687SMark J Musante { 18001195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18011195e687SMark J Musante 18021195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18031195e687SMark J Musante 18041195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18051195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18061195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18071195e687SMark J Musante 18081195e687SMark J Musante if (tvd->vdev_islog) 18091195e687SMark J Musante metaslab_group_activate(mg); 18101195e687SMark J Musante } 18111195e687SMark J Musante } 18121195e687SMark J Musante 18131195e687SMark J Musante int 18141195e687SMark J Musante spa_offline_log(spa_t *spa) 18151195e687SMark J Musante { 18163b2aab18SMatthew Ahrens int error; 18171195e687SMark J Musante 18183b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 18193b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18203b2aab18SMatthew Ahrens if (error == 0) { 18211195e687SMark J Musante /* 18221195e687SMark J Musante * We successfully offlined the log device, sync out the 18231195e687SMark J Musante * current txg so that the "stubby" block can be removed 18241195e687SMark J Musante * by zil_sync(). 18251195e687SMark J Musante */ 18261195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18271195e687SMark J Musante } 18281195e687SMark J Musante return (error); 18291195e687SMark J Musante } 18301195e687SMark J Musante 1831b693757aSEric Schrock static void 1832b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1833b693757aSEric Schrock { 1834b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1835b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1836b693757aSEric Schrock } 1837b693757aSEric Schrock 1838b24ab676SJeff Bonwick void 1839b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1840b24ab676SJeff Bonwick { 1841b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1842b24ab676SJeff Bonwick 1843b24ab676SJeff Bonwick if (zio->io_error) 1844b24ab676SJeff Bonwick return; 1845b24ab676SJeff Bonwick 1846b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1847b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1848b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1849b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1850b24ab676SJeff Bonwick } 1851b24ab676SJeff Bonwick 1852468c413aSTim Haley typedef struct spa_load_error { 1853c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1854468c413aSTim Haley uint64_t sle_data_count; 1855468c413aSTim Haley } spa_load_error_t; 1856468c413aSTim Haley 1857468c413aSTim Haley static void 1858468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1859468c413aSTim Haley { 1860468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1861468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1862468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1863468c413aSTim Haley int error = zio->io_error; 1864e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1865468c413aSTim Haley 1866468c413aSTim Haley if (error) { 1867ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1868468c413aSTim Haley type != DMU_OT_INTENT_LOG) 18691a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1870468c413aSTim Haley else 18711a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1872468c413aSTim Haley } 1873468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1874e42d2059SMatthew Ahrens 1875e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1876e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1877e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1878e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1879468c413aSTim Haley } 1880468c413aSTim Haley 1881e42d2059SMatthew Ahrens /* 1882e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1883e42d2059SMatthew Ahrens * a pool while importing it. 1884e42d2059SMatthew Ahrens */ 1885e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1886e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1887e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1888e42d2059SMatthew Ahrens 1889468c413aSTim Haley /*ARGSUSED*/ 1890468c413aSTim Haley static int 1891b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 18927802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1893468c413aSTim Haley { 1894a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1895e42d2059SMatthew Ahrens return (0); 1896e42d2059SMatthew Ahrens /* 1897e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1898e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1899e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1900e42d2059SMatthew Ahrens */ 1901e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1902e42d2059SMatthew Ahrens return (0); 1903e42d2059SMatthew Ahrens if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data) 1904e42d2059SMatthew Ahrens return (0); 1905468c413aSTim Haley 1906e42d2059SMatthew Ahrens zio_t *rio = arg; 1907e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1908e42d2059SMatthew Ahrens void *data = zio_data_buf_alloc(size); 1909e42d2059SMatthew Ahrens 1910e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1911e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1912e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1913e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1914e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1915e42d2059SMatthew Ahrens 1916e42d2059SMatthew Ahrens zio_nowait(zio_read(rio, spa, bp, data, size, 1917e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1918e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1919e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1920468c413aSTim Haley return (0); 1921468c413aSTim Haley } 1922468c413aSTim Haley 1923c971037bSPaul Dagnelie /* ARGSUSED */ 1924c971037bSPaul Dagnelie int 1925c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1926c971037bSPaul Dagnelie { 19279adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1928c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1929c971037bSPaul Dagnelie 1930c971037bSPaul Dagnelie return (0); 1931c971037bSPaul Dagnelie } 1932c971037bSPaul Dagnelie 1933468c413aSTim Haley static int 1934468c413aSTim Haley spa_load_verify(spa_t *spa) 1935468c413aSTim Haley { 1936468c413aSTim Haley zio_t *rio; 1937468c413aSTim Haley spa_load_error_t sle = { 0 }; 1938468c413aSTim Haley zpool_rewind_policy_t policy; 1939468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1940e42d2059SMatthew Ahrens int error = 0; 1941468c413aSTim Haley 1942c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1943c8ee1847SVictor Latushkin 1944c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1945c8ee1847SVictor Latushkin return (0); 1946c8ee1847SVictor Latushkin 1947c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1948c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1949c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1950c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1951c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1952c971037bSPaul Dagnelie if (error != 0) 1953c971037bSPaul Dagnelie return (error); 1954c971037bSPaul Dagnelie 1955468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1956468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1957468c413aSTim Haley 1958e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1959e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1960e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1961e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 1962e42d2059SMatthew Ahrens } 1963468c413aSTim Haley 1964468c413aSTim Haley (void) zio_wait(rio); 1965468c413aSTim Haley 1966c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1967468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1968468c413aSTim Haley 1969c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1970468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 19714b964adaSGeorge Wilson int64_t loss = 0; 19724b964adaSGeorge Wilson 1973468c413aSTim Haley verify_ok = B_TRUE; 1974468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1975468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 19764b964adaSGeorge Wilson 19774b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 19784b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19794b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 19804b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 19814b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 19824b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19834b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1984a33cae98STim Haley } else { 1985a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1986468c413aSTim Haley } 1987468c413aSTim Haley 1988468c413aSTim Haley if (error) { 1989468c413aSTim Haley if (error != ENXIO && error != EIO) 1990be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1991468c413aSTim Haley return (error); 1992468c413aSTim Haley } 1993468c413aSTim Haley 1994468c413aSTim Haley return (verify_ok ? 0 : EIO); 1995468c413aSTim Haley } 1996468c413aSTim Haley 19971195e687SMark J Musante /* 19981195e687SMark J Musante * Find a value in the pool props object. 19991195e687SMark J Musante */ 20001195e687SMark J Musante static void 20011195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20021195e687SMark J Musante { 20031195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20041195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20051195e687SMark J Musante } 20061195e687SMark J Musante 20071195e687SMark J Musante /* 20081195e687SMark J Musante * Find a value in the pool directory object. 20091195e687SMark J Musante */ 20101195e687SMark J Musante static int 20111195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 20121195e687SMark J Musante { 20131195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20141195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 20151195e687SMark J Musante } 20161195e687SMark J Musante 20171195e687SMark J Musante static int 20181195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20191195e687SMark J Musante { 20201195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 20211195e687SMark J Musante return (err); 20221195e687SMark J Musante } 20231195e687SMark J Musante 20241195e687SMark J Musante /* 20251195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20261195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20271195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20281195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20291195e687SMark J Musante * 20301195e687SMark J Musante * This function determines what to do with that list: either rejoin 20311195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20321195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20331195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20341195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20351195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20361195e687SMark J Musante * 2037d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2038d41c4376SMark J Musante * the original pool. 20391195e687SMark J Musante */ 20401195e687SMark J Musante static void 20411195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 20421195e687SMark J Musante { 20431195e687SMark J Musante uint_t extracted; 20441195e687SMark J Musante uint64_t *glist; 20451195e687SMark J Musante uint_t i, gcount; 20461195e687SMark J Musante nvlist_t *nvl; 20471195e687SMark J Musante vdev_t **vd; 20481195e687SMark J Musante boolean_t attempt_reopen; 20491195e687SMark J Musante 20501195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20511195e687SMark J Musante return; 20521195e687SMark J Musante 20531195e687SMark J Musante /* check that the config is complete */ 20541195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20551195e687SMark J Musante &glist, &gcount) != 0) 20561195e687SMark J Musante return; 20571195e687SMark J Musante 20581195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20591195e687SMark J Musante 20601195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20611195e687SMark J Musante attempt_reopen = B_TRUE; 20621195e687SMark J Musante for (i = 0; i < gcount; i++) { 20631195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 20641195e687SMark J Musante continue; 20651195e687SMark J Musante 20661195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 20671195e687SMark J Musante if (vd[i] == NULL) { 20681195e687SMark J Musante /* 20691195e687SMark J Musante * Don't bother attempting to reopen the disks; 20701195e687SMark J Musante * just do the split. 20711195e687SMark J Musante */ 20721195e687SMark J Musante attempt_reopen = B_FALSE; 20731195e687SMark J Musante } else { 20741195e687SMark J Musante /* attempt to re-online it */ 20751195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 20761195e687SMark J Musante } 20771195e687SMark J Musante } 20781195e687SMark J Musante 20791195e687SMark J Musante if (attempt_reopen) { 20801195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20811195e687SMark J Musante 20821195e687SMark J Musante /* check each device to see what state it's in */ 20831195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 20841195e687SMark J Musante if (vd[i] != NULL && 20851195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 20861195e687SMark J Musante break; 20871195e687SMark J Musante ++extracted; 20881195e687SMark J Musante } 20891195e687SMark J Musante } 20901195e687SMark J Musante 20911195e687SMark J Musante /* 20921195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 20931195e687SMark J Musante * even attempted to look at them, then we split them off for 20941195e687SMark J Musante * good. 20951195e687SMark J Musante */ 20961195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 20971195e687SMark J Musante for (i = 0; i < gcount; i++) 20981195e687SMark J Musante if (vd[i] != NULL) 20991195e687SMark J Musante vdev_split(vd[i]); 21001195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21011195e687SMark J Musante } 21021195e687SMark J Musante 21031195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21041195e687SMark J Musante } 21051195e687SMark J Musante 21061195e687SMark J Musante static int 21071195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 21081195e687SMark J Musante boolean_t mosconfig) 21091195e687SMark J Musante { 21101195e687SMark J Musante nvlist_t *config = spa->spa_config; 21111195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21128704186eSDan McDonald char *comment; 21131195e687SMark J Musante int error; 21141195e687SMark J Musante uint64_t pool_guid; 21151195e687SMark J Musante nvlist_t *nvl; 21161195e687SMark J Musante 21171195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2118be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21191195e687SMark J Musante 21208704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 21218704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 21228704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 21238704186eSDan McDonald 21241195e687SMark J Musante /* 21251195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 21261195e687SMark J Musante * it's not present treat it as the initial version. 21271195e687SMark J Musante */ 21281195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 21291195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 21301195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 21311195e687SMark J Musante 21321195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 21331195e687SMark J Musante &spa->spa_config_txg); 21341195e687SMark J Musante 21351195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 21361195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2137be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 21381195e687SMark J Musante } else { 2139e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 21401195e687SMark J Musante 21411195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 21421195e687SMark J Musante &nvl) == 0) { 21431195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 21441195e687SMark J Musante KM_SLEEP) == 0); 21451195e687SMark J Musante } 21461195e687SMark J Musante 2147ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2148ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2149ad135b5dSChristopher Siden 215011027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21511195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21521195e687SMark J Musante mosconfig, &ereport); 21531195e687SMark J Musante } 21541195e687SMark J Musante 2155bc9014e6SJustin Gibbs /* 2156bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2157bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2158bc9014e6SJustin Gibbs */ 2159bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21601195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 216111027bc7STim Haley if (error) { 216211027bc7STim Haley if (error != EEXIST) { 216311027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 216411027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 216511027bc7STim Haley } 216611027bc7STim Haley if (error != EBADF) { 216711027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 216811027bc7STim Haley } 216911027bc7STim Haley } 21701195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21711195e687SMark J Musante spa->spa_ena = 0; 21721195e687SMark J Musante 21731195e687SMark J Musante return (error); 21741195e687SMark J Musante } 21751195e687SMark J Musante 2176215198a6SJoe Stein /* 2177215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2178215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2179215198a6SJoe Stein * spa's per-vdev ZAP list. 2180215198a6SJoe Stein */ 2181215198a6SJoe Stein static uint64_t 2182215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2183215198a6SJoe Stein { 2184215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2185215198a6SJoe Stein uint64_t total = 0; 2186215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2187215198a6SJoe Stein total++; 2188215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2189215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2190215198a6SJoe Stein } 2191215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2192215198a6SJoe Stein total++; 2193215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2194215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2195215198a6SJoe Stein } 2196215198a6SJoe Stein 2197215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2198215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2199215198a6SJoe Stein } 2200215198a6SJoe Stein 2201215198a6SJoe Stein return (total); 2202215198a6SJoe Stein } 2203215198a6SJoe Stein 2204fa9e4066Sahrens /* 2205fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2206ea8dc4b6Seschrock * source of configuration information. 2207fa9e4066Sahrens */ 2208fa9e4066Sahrens static int 22091195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 22101195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 22111195e687SMark J Musante char **ereport) 2212fa9e4066Sahrens { 2213fa9e4066Sahrens int error = 0; 2214871a9500SMark J Musante nvlist_t *nvroot = NULL; 2215ad135b5dSChristopher Siden nvlist_t *label; 2216fa9e4066Sahrens vdev_t *rvd; 2217fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 22184b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 22198ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 22201195e687SMark J Musante int parse; 2221cde58dbcSMatthew Ahrens uint64_t obj; 2222ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2223fa9e4066Sahrens 22248ad4d6ddSJeff Bonwick /* 22258ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 22268ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 22278ad4d6ddSJeff Bonwick */ 22288ad4d6ddSJeff Bonwick if (!mosconfig) 22298ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 22308ad4d6ddSJeff Bonwick 2231e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2232e14bb325SJeff Bonwick 2233ea8dc4b6Seschrock spa->spa_load_state = state; 22340373e76bSbonwick 22351195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2236be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2237fa9e4066Sahrens 22381195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 22391195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2240b5989ec7Seschrock 224154d692b7SGeorge Wilson /* 224254d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 224354d692b7SGeorge Wilson */ 22446f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 22456f834bc1SMatthew Ahrens KM_SLEEP); 22466f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 22476f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 22486f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 22496f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 22506f834bc1SMatthew Ahrens } 225154d692b7SGeorge Wilson 2252fa9e4066Sahrens /* 225399653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 225499653d4eSeschrock * value that will be returned by spa_version() since parsing the 225599653d4eSeschrock * configuration requires knowing the version number. 2256fa9e4066Sahrens */ 2257e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22581195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2259e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2260fa9e4066Sahrens 226199653d4eSeschrock if (error != 0) 22621195e687SMark J Musante return (error); 2263fa9e4066Sahrens 22640e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 226581cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 226681cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 22671195e687SMark J Musante 22681195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22691195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 22701195e687SMark J Musante } 2271fa9e4066Sahrens 2272fa9e4066Sahrens /* 2273fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2274fa9e4066Sahrens */ 2275e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22760bf246f5Smc error = vdev_open(rvd); 2277e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 22780bf246f5Smc if (error != 0) 22791195e687SMark J Musante return (error); 2280fa9e4066Sahrens 2281560e6e96Seschrock /* 228277e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 228377e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 228477e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 22851195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 228677e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 228777e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 228877e3a39cSMark J Musante * the vdev config. 22891195e687SMark J Musante * 22901195e687SMark J Musante * If we're assembling a new pool that's been split off from an 22911195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 22921195e687SMark J Musante * validation for now. 2293560e6e96Seschrock */ 22941195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22951195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2296d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 22971195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2298560e6e96Seschrock 22991195e687SMark J Musante if (error != 0) 23001195e687SMark J Musante return (error); 23011195e687SMark J Musante 23021195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2303be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2304560e6e96Seschrock } 2305560e6e96Seschrock 2306fa9e4066Sahrens /* 2307fa9e4066Sahrens * Find the best uberblock. 2308fa9e4066Sahrens */ 2309ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2310fa9e4066Sahrens 2311fa9e4066Sahrens /* 2312fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2313fa9e4066Sahrens */ 2314ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2315ad135b5dSChristopher Siden nvlist_free(label); 23161195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2317ad135b5dSChristopher Siden } 2318ea8dc4b6Seschrock 2319ea8dc4b6Seschrock /* 2320ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2321ea8dc4b6Seschrock */ 2322ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2323ad135b5dSChristopher Siden nvlist_free(label); 23241195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2325ad135b5dSChristopher Siden } 2326ad135b5dSChristopher Siden 2327ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2328ad135b5dSChristopher Siden nvlist_t *features; 2329ad135b5dSChristopher Siden 2330ad135b5dSChristopher Siden /* 2331ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2332ad135b5dSChristopher Siden * MOS in the label, return failure. 2333ad135b5dSChristopher Siden */ 2334ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2335ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2336ad135b5dSChristopher Siden nvlist_free(label); 2337ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2338ad135b5dSChristopher Siden ENXIO)); 2339ad135b5dSChristopher Siden } 2340ad135b5dSChristopher Siden 2341ad135b5dSChristopher Siden /* 2342ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2343ad135b5dSChristopher Siden * from the label. 2344ad135b5dSChristopher Siden */ 2345ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2346ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2347ad135b5dSChristopher Siden } 2348ad135b5dSChristopher Siden 2349ad135b5dSChristopher Siden nvlist_free(label); 2350ad135b5dSChristopher Siden 2351ad135b5dSChristopher Siden /* 2352ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2353ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2354ad135b5dSChristopher Siden * cannot open a pool. 2355ad135b5dSChristopher Siden */ 2356ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2357ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2358ad135b5dSChristopher Siden 2359ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2360ad135b5dSChristopher Siden 0); 2361ad135b5dSChristopher Siden 2362ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2363ad135b5dSChristopher Siden NULL); nvp != NULL; 2364ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2365ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2366ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2367ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2368ad135b5dSChristopher Siden } 2369ad135b5dSChristopher Siden } 2370ad135b5dSChristopher Siden 2371ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2372ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2373ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2374ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2375ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2376ad135b5dSChristopher Siden ENOTSUP)); 2377ad135b5dSChristopher Siden } 2378ad135b5dSChristopher Siden 2379ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2380ad135b5dSChristopher Siden } 2381fa9e4066Sahrens 2382fa9e4066Sahrens /* 2383fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 23844b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 23854b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 23864b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 23874b964adaSGeorge Wilson * can handle missing vdevs. 2388fa9e4066Sahrens */ 23894b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 23904b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 23911195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 23921195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 23931195e687SMark J Musante 23941195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 23951195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 23961195e687SMark J Musante spa_try_repair(spa, config); 23971195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 23981195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 23991195e687SMark J Musante spa->spa_config_splitting = NULL; 2400fa9e4066Sahrens } 2401fa9e4066Sahrens 2402fa9e4066Sahrens /* 2403fa9e4066Sahrens * Initialize internal SPA structures. 2404fa9e4066Sahrens */ 2405fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2406fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2407468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2408c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2409468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2410468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2411b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 24123f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2413b24ab676SJeff Bonwick 2414ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 24151195e687SMark J Musante if (error) 24161195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2417fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2418fa9e4066Sahrens 24191195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 24201195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2421fa9e4066Sahrens 2422ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2423ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 242457221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2425ad135b5dSChristopher Siden 2426ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2427ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2428ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2429ad135b5dSChristopher Siden } 2430ad135b5dSChristopher Siden 2431ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2432ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2433ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2434ad135b5dSChristopher Siden } 2435ad135b5dSChristopher Siden 2436ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2437ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2438ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2439ad135b5dSChristopher Siden } 2440ad135b5dSChristopher Siden 244157221772SChristopher Siden enabled_feat = fnvlist_alloc(); 244257221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2443ad135b5dSChristopher Siden 24442acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 244557221772SChristopher Siden unsup_feat, enabled_feat)) 2446ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2447ad135b5dSChristopher Siden 2448ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 24492acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 245057221772SChristopher Siden unsup_feat, enabled_feat)) { 2451ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 245257221772SChristopher Siden } 2453ad135b5dSChristopher Siden } 2454ad135b5dSChristopher Siden 245557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 245657221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 245757221772SChristopher Siden 2458ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 245957221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 246057221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2461ad135b5dSChristopher Siden } 2462ad135b5dSChristopher Siden 246357221772SChristopher Siden fnvlist_free(enabled_feat); 246457221772SChristopher Siden fnvlist_free(unsup_feat); 2465ad135b5dSChristopher Siden 2466ad135b5dSChristopher Siden if (!missing_feat_read) { 2467ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2468ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2469ad135b5dSChristopher Siden } 2470ad135b5dSChristopher Siden 2471ad135b5dSChristopher Siden /* 2472ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2473ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2474ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2475ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2476ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2477ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2478ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2479ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2480ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2481ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2482ad135b5dSChristopher Siden * available for open in read-only mode. 2483ad135b5dSChristopher Siden * 2484ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2485ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2486ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2487ad135b5dSChristopher Siden * userland in order to know whether to display the 2488ad135b5dSChristopher Siden * abovementioned note. 2489ad135b5dSChristopher Siden */ 2490ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2491ad135b5dSChristopher Siden spa_writeable(spa))) { 2492ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2493ad135b5dSChristopher Siden ENOTSUP)); 2494ad135b5dSChristopher Siden } 249543466aaeSMax Grossman 249643466aaeSMax Grossman /* 249743466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 249843466aaeSMax Grossman * cache during SPA initialization. 249943466aaeSMax Grossman */ 250043466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 250143466aaeSMax Grossman uint64_t refcount; 250243466aaeSMax Grossman 250343466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 250443466aaeSMax Grossman &spa_feature_table[i], &refcount); 250543466aaeSMax Grossman if (error == 0) { 250643466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 250743466aaeSMax Grossman } else if (error == ENOTSUP) { 250843466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 250943466aaeSMax Grossman SPA_FEATURE_DISABLED; 251043466aaeSMax Grossman } else { 251143466aaeSMax Grossman return (spa_vdev_err(rvd, 251243466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 251343466aaeSMax Grossman } 251443466aaeSMax Grossman } 251543466aaeSMax Grossman } 251643466aaeSMax Grossman 251743466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 251843466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 25195d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 252043466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2521ad135b5dSChristopher Siden } 2522ad135b5dSChristopher Siden 2523ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2524ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2525ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2526ad135b5dSChristopher Siden if (error != 0) 2527ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2528ad135b5dSChristopher Siden 2529fa9e4066Sahrens if (!mosconfig) { 253095173954Sek uint64_t hostid; 2531871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2532871a9500SMark J Musante 2533871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2534871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2535fa9e4066Sahrens 253688ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 253777650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 253895173954Sek char *hostname; 253995173954Sek unsigned long myhostid = 0; 254095173954Sek 254188ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 254295173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 254395173954Sek 25445679c89fSjv #ifdef _KERNEL 25455679c89fSjv myhostid = zone_get_hostid(NULL); 25465679c89fSjv #else /* _KERNEL */ 25475679c89fSjv /* 25485679c89fSjv * We're emulating the system's hostid in userland, so 25495679c89fSjv * we can't use zone_get_hostid(). 25505679c89fSjv */ 255195173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 25525679c89fSjv #endif /* _KERNEL */ 255317194a52Slling if (hostid != 0 && myhostid != 0 && 25545679c89fSjv hostid != myhostid) { 2555871a9500SMark J Musante nvlist_free(nvconfig); 255695173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 255795173954Sek "loaded as it was last accessed by " 255877650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2559654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2560e14bb325SJeff Bonwick spa_name(spa), hostname, 256195173954Sek (unsigned long)hostid); 2562be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 256395173954Sek } 256495173954Sek } 2565c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2566c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2567c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2568c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 256995173954Sek 257088ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2571fa9e4066Sahrens spa_unload(spa); 2572fa9e4066Sahrens spa_deactivate(spa); 25738ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2574fa9e4066Sahrens 25751195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2576fa9e4066Sahrens } 2577fa9e4066Sahrens 257845818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 257945818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 258045818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 258145818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 258245818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 258345818ee1SMatthew Ahrens if (error == ENOENT) { 258445818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 258545818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 258645818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 258745818ee1SMatthew Ahrens } else if (error != 0) { 258845818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 258945818ee1SMatthew Ahrens } 259045818ee1SMatthew Ahrens 2591cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2592cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2593cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2594cde58dbcSMatthew Ahrens if (error != 0) 25951195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2596fa9e4066Sahrens 259799653d4eSeschrock /* 259899653d4eSeschrock * Load the bit that tells us to use the new accounting function 259999653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 260099653d4eSeschrock * be present. 260199653d4eSeschrock */ 26021195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 26031195e687SMark J Musante if (error != 0 && error != ENOENT) 26041195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 260599653d4eSeschrock 26063f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 26073f9d6ad7SLin Ling &spa->spa_creation_version); 26083f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 26093f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26103f9d6ad7SLin Ling 2611fa9e4066Sahrens /* 2612ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2613ea8dc4b6Seschrock * not be present. 2614fa9e4066Sahrens */ 26151195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 26161195e687SMark J Musante if (error != 0 && error != ENOENT) 26171195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2618ea8dc4b6Seschrock 26191195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 26201195e687SMark J Musante &spa->spa_errlog_scrub); 26211195e687SMark J Musante if (error != 0 && error != ENOENT) 26221195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2623ea8dc4b6Seschrock 262406eeb2adSek /* 262506eeb2adSek * Load the history object. If we have an older pool, this 262606eeb2adSek * will not be present. 262706eeb2adSek */ 26281195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 26291195e687SMark J Musante if (error != 0 && error != ENOENT) 26301195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26311195e687SMark J Musante 2632215198a6SJoe Stein /* 2633215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2634215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2635215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2636215198a6SJoe Stein * spa_sync_config_object. 2637215198a6SJoe Stein */ 2638215198a6SJoe Stein 2639215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2640215198a6SJoe Stein nvlist_t *mos_config; 2641215198a6SJoe Stein if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2642215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2643215198a6SJoe Stein 2644215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 2645215198a6SJoe Stein &spa->spa_all_vdev_zaps); 2646215198a6SJoe Stein 2647215198a6SJoe Stein if (error != ENOENT && error != 0) { 2648215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2649215198a6SJoe Stein } else if (error == 0 && !nvlist_exists(mos_config, 2650215198a6SJoe Stein ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2651215198a6SJoe Stein /* 2652215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2653215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2654215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2655215198a6SJoe Stein */ 2656215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 2657215198a6SJoe Stein /* 2658215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 2659215198a6SJoe Stein * before this. Better be sure of it. 2660215198a6SJoe Stein */ 2661215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2662215198a6SJoe Stein } 2663215198a6SJoe Stein nvlist_free(mos_config); 2664215198a6SJoe Stein 26651195e687SMark J Musante /* 26661195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 26671195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 26681195e687SMark J Musante * devices. 26691195e687SMark J Musante */ 267006eeb2adSek 267199653d4eSeschrock /* 267299653d4eSeschrock * Load any hot spares for this pool. 267399653d4eSeschrock */ 26741195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 26751195e687SMark J Musante if (error != 0 && error != ENOENT) 26761195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26771195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2678e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2679fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 26801195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 26811195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 268299653d4eSeschrock 2683e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 268499653d4eSeschrock spa_load_spares(spa); 2685e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 26861195e687SMark J Musante } else if (error == 0) { 26871195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 268899653d4eSeschrock } 268999653d4eSeschrock 2690fa94a07fSbrendan /* 2691fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2692fa94a07fSbrendan */ 26931195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2694fa94a07fSbrendan &spa->spa_l2cache.sav_object); 26951195e687SMark J Musante if (error != 0 && error != ENOENT) 26961195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26971195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2698fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2699fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 27001195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 27011195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2702fa94a07fSbrendan 2703e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2704fa94a07fSbrendan spa_load_l2cache(spa); 2705e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27061195e687SMark J Musante } else if (error == 0) { 27071195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2708fa94a07fSbrendan } 2709fa94a07fSbrendan 2710990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2711ecd6cf80Smarks 27121195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 27131195e687SMark J Musante if (error && error != ENOENT) 27141195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2715b1b8ab34Slling 2716b1b8ab34Slling if (error == 0) { 27171195e687SMark J Musante uint64_t autoreplace; 27181195e687SMark J Musante 27191195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 27201195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 27211195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 27221195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 27231195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 27241195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 27251195e687SMark J Musante &spa->spa_dedup_ditto); 27261195e687SMark J Musante 2727b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2728b1b8ab34Slling } 2729b1b8ab34Slling 27303d7072f8Seschrock /* 27313d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 27323d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 27333d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 27343d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 27353d7072f8Seschrock * over. 27363d7072f8Seschrock */ 2737b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 27383d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2739b693757aSEric Schrock /* 2740b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2741b693757aSEric Schrock * at this point we're using the spare definitions from 2742b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2743b693757aSEric Schrock */ 2744b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2745b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2746b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2747b693757aSEric Schrock } 2748b693757aSEric Schrock } 27493d7072f8Seschrock 2750ea8dc4b6Seschrock /* 2751560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2752ea8dc4b6Seschrock */ 2753560e6e96Seschrock vdev_load(rvd); 27540373e76bSbonwick 2755fa9e4066Sahrens /* 2756fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2757fa9e4066Sahrens */ 2758e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2759fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2760e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2761fa9e4066Sahrens 2762b24ab676SJeff Bonwick /* 2763b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2764b24ab676SJeff Bonwick */ 2765b24ab676SJeff Bonwick error = ddt_load(spa); 27661195e687SMark J Musante if (error != 0) 27671195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2768b24ab676SJeff Bonwick 2769485bbbf5SGeorge Wilson spa_update_dspace(spa); 2770485bbbf5SGeorge Wilson 2771b24ab676SJeff Bonwick /* 27724b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 27734b964adaSGeorge Wilson * information which might be missing. If we fail to validate 27744b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 27754b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 27764b964adaSGeorge Wilson * over. 2777b24ab676SJeff Bonwick */ 27781195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2779871a9500SMark J Musante nvlist_t *nvconfig; 2780871a9500SMark J Musante 2781871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2782871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2783871a9500SMark J Musante 27844b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 27854b964adaSGeorge Wilson nvlist_free(nvconfig); 27864b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 27874b964adaSGeorge Wilson ENXIO)); 27884b964adaSGeorge Wilson } 27891195e687SMark J Musante nvlist_free(nvconfig); 27901195e687SMark J Musante 27914b964adaSGeorge Wilson /* 2792ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 27934b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 27944b964adaSGeorge Wilson * more toplevel vdevs are faulted. 27954b964adaSGeorge Wilson */ 27964b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2797be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 27984b964adaSGeorge Wilson 279923367a2fSMatthew Ahrens if (spa_writeable(spa) && spa_check_logs(spa)) { 28001195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 28011195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 28021195e687SMark J Musante } 2803b24ab676SJeff Bonwick } 2804b24ab676SJeff Bonwick 2805ad135b5dSChristopher Siden if (missing_feat_write) { 2806ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2807ad135b5dSChristopher Siden 2808ad135b5dSChristopher Siden /* 2809ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2810ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2811ad135b5dSChristopher Siden * information and can return to userland. 2812ad135b5dSChristopher Siden */ 2813ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2814ad135b5dSChristopher Siden } 2815ad135b5dSChristopher Siden 28164b964adaSGeorge Wilson /* 28174b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 28184b964adaSGeorge Wilson * to start pushing transactions. 28194b964adaSGeorge Wilson */ 28204b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 28214b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 28224b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 28234b964adaSGeorge Wilson error)); 28244b964adaSGeorge Wilson } 28254b964adaSGeorge Wilson 2826468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2827468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 28285dabedeeSbonwick dmu_tx_t *tx; 28290373e76bSbonwick int need_update = B_FALSE; 283012380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 28318ad4d6ddSJeff Bonwick 28328ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 28335dabedeeSbonwick 28340373e76bSbonwick /* 28350373e76bSbonwick * Claim log blocks that haven't been committed yet. 28360373e76bSbonwick * This must all happen in a single txg. 2837b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2838b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2839468c413aSTim Haley * Price of rollback is that we abandon the log. 28400373e76bSbonwick */ 2841b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2842b24ab676SJeff Bonwick 284312380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 284412380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 28450b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2846fa9e4066Sahrens dmu_tx_commit(tx); 2847fa9e4066Sahrens 2848b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2849b24ab676SJeff Bonwick 28501195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2851fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2852fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2853fa9e4066Sahrens 2854fa9e4066Sahrens /* 2855b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2856b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2857b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2858b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2859b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2860fa9e4066Sahrens */ 2861b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 28620e34b6a7Sbonwick 28630e34b6a7Sbonwick /* 28640373e76bSbonwick * If the config cache is stale, or we have uninitialized 28650373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2866bc758434SLin Ling * 28674b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2868bc758434SLin Ling * in-core spa_config and update the disk labels. 28690e34b6a7Sbonwick */ 28700373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 28714b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 28724b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 28734b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 28740373e76bSbonwick need_update = B_TRUE; 28750373e76bSbonwick 28768ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 28770373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 28780373e76bSbonwick need_update = B_TRUE; 28790e34b6a7Sbonwick 28800e34b6a7Sbonwick /* 28810373e76bSbonwick * Update the config cache asychronously in case we're the 28820373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 28830e34b6a7Sbonwick */ 28840373e76bSbonwick if (need_update) 28850373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 28868ad4d6ddSJeff Bonwick 28878ad4d6ddSJeff Bonwick /* 28888ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 28898ad4d6ddSJeff Bonwick */ 28903f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 28913f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 28928ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2893503ad85cSMatthew Ahrens 28944445fffbSMatthew Ahrens /* 28954445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 28964445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 28974445fffbSMatthew Ahrens */ 28984445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 28994445fffbSMatthew Ahrens 2900503ad85cSMatthew Ahrens /* 2901503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2902503ad85cSMatthew Ahrens */ 2903503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2904503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2905ca45db41SChris Kirby 2906ca45db41SChris Kirby /* 2907ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2908ca45db41SChris Kirby */ 2909ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2910fa9e4066Sahrens } 2911fa9e4066Sahrens 29121195e687SMark J Musante return (0); 2913fa9e4066Sahrens } 2914fa9e4066Sahrens 2915468c413aSTim Haley static int 2916468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2917468c413aSTim Haley { 2918f9af39baSGeorge Wilson int mode = spa->spa_mode; 2919f9af39baSGeorge Wilson 2920468c413aSTim Haley spa_unload(spa); 2921468c413aSTim Haley spa_deactivate(spa); 2922468c413aSTim Haley 2923e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 2924468c413aSTim Haley 2925f9af39baSGeorge Wilson spa_activate(spa, mode); 2926468c413aSTim Haley spa_async_suspend(spa); 2927468c413aSTim Haley 29281195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2929468c413aSTim Haley } 2930468c413aSTim Haley 2931ad135b5dSChristopher Siden /* 2932ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2933ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2934ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2935ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2936ad135b5dSChristopher Siden * spa_load(). 2937ad135b5dSChristopher Siden */ 2938468c413aSTim Haley static int 2939468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2940c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2941468c413aSTim Haley { 2942ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2943468c413aSTim Haley nvlist_t *config = NULL; 2944468c413aSTim Haley int load_error, rewind_error; 2945c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2946468c413aSTim Haley uint64_t min_txg; 2947468c413aSTim Haley 2948a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2949468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 29501195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2951a33cae98STim Haley } else { 2952468c413aSTim Haley spa->spa_load_max_txg = max_request; 2953e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 2954e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 2955a33cae98STim Haley } 2956468c413aSTim Haley 29571195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 29581195e687SMark J Musante mosconfig); 2959468c413aSTim Haley if (load_error == 0) 2960468c413aSTim Haley return (0); 2961468c413aSTim Haley 2962468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2963468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2964468c413aSTim Haley 2965468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2966468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2967468c413aSTim Haley 2968c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2969468c413aSTim Haley nvlist_free(config); 2970468c413aSTim Haley return (load_error); 2971468c413aSTim Haley } 2972468c413aSTim Haley 2973ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2974ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 29751195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2976ad135b5dSChristopher Siden } else { 2977ad135b5dSChristopher Siden /* 2978ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2979ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2980ad135b5dSChristopher Siden * to rewind. 2981ad135b5dSChristopher Siden */ 2982ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 2983ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2984ad135b5dSChristopher Siden } 2985468c413aSTim Haley 2986c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2987c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2988c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2989c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2990468c413aSTim Haley 2991c8ee1847SVictor Latushkin /* 2992c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2993c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2994c8ee1847SVictor Latushkin */ 2995c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2996c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2997c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2998468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2999468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 3000468c413aSTim Haley } 3001468c413aSTim Haley 3002468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3003468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3004468c413aSTim Haley 3005468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3006468c413aSTim Haley spa_config_set(spa, config); 3007468c413aSTim Haley 3008ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3009ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3010ad135b5dSChristopher Siden return (rewind_error); 3011ad135b5dSChristopher Siden } else { 3012ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3013ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3014ad135b5dSChristopher Siden spa->spa_load_info); 3015ad135b5dSChristopher Siden 3016ad135b5dSChristopher Siden /* Restore the initial load info */ 3017ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3018ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3019ad135b5dSChristopher Siden 3020ad135b5dSChristopher Siden return (load_error); 3021ad135b5dSChristopher Siden } 3022468c413aSTim Haley } 3023468c413aSTim Haley 3024fa9e4066Sahrens /* 3025fa9e4066Sahrens * Pool Open/Import 3026fa9e4066Sahrens * 3027fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3028fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3029fa9e4066Sahrens * case of an open, the pool configuration will exist in the 30303d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3031fa9e4066Sahrens * 3032fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3033fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3034fa9e4066Sahrens * ambiguous state. 3035fa9e4066Sahrens */ 3036fa9e4066Sahrens static int 3037468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3038468c413aSTim Haley nvlist_t **config) 3039fa9e4066Sahrens { 3040fa9e4066Sahrens spa_t *spa; 30414b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3042fa9e4066Sahrens int error; 3043fa9e4066Sahrens int locked = B_FALSE; 3044fa9e4066Sahrens 3045fa9e4066Sahrens *spapp = NULL; 3046fa9e4066Sahrens 3047fa9e4066Sahrens /* 3048fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3049fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3050fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3051fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3052fa9e4066Sahrens */ 3053fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3054fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3055fa9e4066Sahrens locked = B_TRUE; 3056fa9e4066Sahrens } 3057fa9e4066Sahrens 3058fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3059fa9e4066Sahrens if (locked) 3060fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3061be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3062fa9e4066Sahrens } 3063468c413aSTim Haley 3064fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 30654b44c88cSTim Haley zpool_rewind_policy_t policy; 30664b44c88cSTim Haley 30674b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 30684b44c88cSTim Haley &policy); 30694b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 30704b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3071fa9e4066Sahrens 30728ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3073fa9e4066Sahrens 3074468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3075468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3076468c413aSTim Haley 3077468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 3078c8ee1847SVictor Latushkin policy.zrp_request); 3079fa9e4066Sahrens 3080fa9e4066Sahrens if (error == EBADF) { 3081fa9e4066Sahrens /* 3082560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3083560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3084560e6e96Seschrock * that the pool has been exported or destroyed. If 3085560e6e96Seschrock * this is the case, the config cache is out of sync and 3086560e6e96Seschrock * we should remove the pool from the namespace. 3087fa9e4066Sahrens */ 3088fa9e4066Sahrens spa_unload(spa); 3089fa9e4066Sahrens spa_deactivate(spa); 3090c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 3091fa9e4066Sahrens spa_remove(spa); 3092fa9e4066Sahrens if (locked) 3093fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3094be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3095ea8dc4b6Seschrock } 3096ea8dc4b6Seschrock 3097ea8dc4b6Seschrock if (error) { 3098fa9e4066Sahrens /* 3099fa9e4066Sahrens * We can't open the pool, but we still have useful 3100fa9e4066Sahrens * information: the state of each vdev after the 3101fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3102fa9e4066Sahrens */ 31034b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3104468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3105468c413aSTim Haley KM_SLEEP) == 0); 31064b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 31074b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 31084b964adaSGeorge Wilson spa->spa_load_info) == 0); 31094b964adaSGeorge Wilson } 3110fa9e4066Sahrens spa_unload(spa); 3111fa9e4066Sahrens spa_deactivate(spa); 3112468c413aSTim Haley spa->spa_last_open_failed = error; 3113fa9e4066Sahrens if (locked) 3114fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3115fa9e4066Sahrens *spapp = NULL; 3116fa9e4066Sahrens return (error); 3117fa9e4066Sahrens } 3118fa9e4066Sahrens } 3119fa9e4066Sahrens 3120fa9e4066Sahrens spa_open_ref(spa, tag); 31213d7072f8Seschrock 3122468c413aSTim Haley if (config != NULL) 3123468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3124468c413aSTim Haley 31254b964adaSGeorge Wilson /* 31264b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 31274b964adaSGeorge Wilson * gathered while doing the load. 31284b964adaSGeorge Wilson */ 31294b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 31304b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 31314b964adaSGeorge Wilson spa->spa_load_info) == 0); 31324b964adaSGeorge Wilson } 31334b964adaSGeorge Wilson 3134a33cae98STim Haley if (locked) { 3135a33cae98STim Haley spa->spa_last_open_failed = 0; 3136a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3137a33cae98STim Haley spa->spa_load_txg = 0; 3138fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3139a33cae98STim Haley } 3140fa9e4066Sahrens 3141fa9e4066Sahrens *spapp = spa; 3142fa9e4066Sahrens 3143fa9e4066Sahrens return (0); 3144fa9e4066Sahrens } 3145fa9e4066Sahrens 3146468c413aSTim Haley int 3147468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3148468c413aSTim Haley nvlist_t **config) 3149468c413aSTim Haley { 3150468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3151468c413aSTim Haley } 3152468c413aSTim Haley 3153fa9e4066Sahrens int 3154fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3155fa9e4066Sahrens { 3156468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3157fa9e4066Sahrens } 3158fa9e4066Sahrens 3159ea8dc4b6Seschrock /* 3160ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3161ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3162ea8dc4b6Seschrock */ 3163ea8dc4b6Seschrock spa_t * 3164ea8dc4b6Seschrock spa_inject_addref(char *name) 3165ea8dc4b6Seschrock { 3166ea8dc4b6Seschrock spa_t *spa; 3167ea8dc4b6Seschrock 3168ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3169ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3170ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3171ea8dc4b6Seschrock return (NULL); 3172ea8dc4b6Seschrock } 3173ea8dc4b6Seschrock spa->spa_inject_ref++; 3174ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3175ea8dc4b6Seschrock 3176ea8dc4b6Seschrock return (spa); 3177ea8dc4b6Seschrock } 3178ea8dc4b6Seschrock 3179ea8dc4b6Seschrock void 3180ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3181ea8dc4b6Seschrock { 3182ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3183ea8dc4b6Seschrock spa->spa_inject_ref--; 3184ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3185ea8dc4b6Seschrock } 3186ea8dc4b6Seschrock 3187fa94a07fSbrendan /* 3188fa94a07fSbrendan * Add spares device information to the nvlist. 3189fa94a07fSbrendan */ 319099653d4eSeschrock static void 319199653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 319299653d4eSeschrock { 319399653d4eSeschrock nvlist_t **spares; 319499653d4eSeschrock uint_t i, nspares; 319599653d4eSeschrock nvlist_t *nvroot; 319699653d4eSeschrock uint64_t guid; 319799653d4eSeschrock vdev_stat_t *vs; 319899653d4eSeschrock uint_t vsc; 319939c23413Seschrock uint64_t pool; 320099653d4eSeschrock 32016809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32026809eb4eSEric Schrock 3203fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 320499653d4eSeschrock return; 320599653d4eSeschrock 320699653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 320799653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3208fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 320999653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 321099653d4eSeschrock if (nspares != 0) { 321199653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 321299653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 321399653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 321499653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 321599653d4eSeschrock 321699653d4eSeschrock /* 321799653d4eSeschrock * Go through and find any spares which have since been 321899653d4eSeschrock * repurposed as an active spare. If this is the case, update 321999653d4eSeschrock * their status appropriately. 322099653d4eSeschrock */ 322199653d4eSeschrock for (i = 0; i < nspares; i++) { 322299653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 322399653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 322489a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 322589a89ebfSlling pool != 0ULL) { 322699653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 32273f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 322899653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 322999653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 323099653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 323199653d4eSeschrock } 323299653d4eSeschrock } 323399653d4eSeschrock } 323499653d4eSeschrock } 323599653d4eSeschrock 3236fa94a07fSbrendan /* 3237fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3238fa94a07fSbrendan */ 3239fa94a07fSbrendan static void 3240fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3241fa94a07fSbrendan { 3242fa94a07fSbrendan nvlist_t **l2cache; 3243fa94a07fSbrendan uint_t i, j, nl2cache; 3244fa94a07fSbrendan nvlist_t *nvroot; 3245fa94a07fSbrendan uint64_t guid; 3246fa94a07fSbrendan vdev_t *vd; 3247fa94a07fSbrendan vdev_stat_t *vs; 3248fa94a07fSbrendan uint_t vsc; 3249fa94a07fSbrendan 32506809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32516809eb4eSEric Schrock 3252fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3253fa94a07fSbrendan return; 3254fa94a07fSbrendan 3255fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3256fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3257fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3258fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3259fa94a07fSbrendan if (nl2cache != 0) { 3260fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3261fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3262fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3263fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3264fa94a07fSbrendan 3265fa94a07fSbrendan /* 3266fa94a07fSbrendan * Update level 2 cache device stats. 3267fa94a07fSbrendan */ 3268fa94a07fSbrendan 3269fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3270fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3271fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3272fa94a07fSbrendan 3273fa94a07fSbrendan vd = NULL; 3274fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3275fa94a07fSbrendan if (guid == 3276fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3277fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3278fa94a07fSbrendan break; 3279fa94a07fSbrendan } 3280fa94a07fSbrendan } 3281fa94a07fSbrendan ASSERT(vd != NULL); 3282fa94a07fSbrendan 3283fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 32843f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 32853f9d6ad7SLin Ling == 0); 3286fa94a07fSbrendan vdev_get_stats(vd, vs); 3287fa94a07fSbrendan } 3288fa94a07fSbrendan } 3289fa94a07fSbrendan } 3290fa94a07fSbrendan 3291ad135b5dSChristopher Siden static void 3292ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3293ad135b5dSChristopher Siden { 3294ad135b5dSChristopher Siden nvlist_t *features; 3295ad135b5dSChristopher Siden zap_cursor_t zc; 3296ad135b5dSChristopher Siden zap_attribute_t za; 3297ad135b5dSChristopher Siden 3298ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3299ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3300ad135b5dSChristopher Siden 3301ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3302ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3303ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3304ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3305ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3306ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3307ad135b5dSChristopher Siden za.za_num_integers == 1); 3308b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3309ad135b5dSChristopher Siden za.za_first_integer)); 3310ad135b5dSChristopher Siden } 3311ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3312ad135b5dSChristopher Siden } 3313ad135b5dSChristopher Siden 3314ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3315ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3316ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3317ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3318ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3319ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3320ad135b5dSChristopher Siden za.za_num_integers == 1); 3321b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3322ad135b5dSChristopher Siden za.za_first_integer)); 3323ad135b5dSChristopher Siden } 3324ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3325ad135b5dSChristopher Siden } 3326ad135b5dSChristopher Siden 3327ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3328ad135b5dSChristopher Siden features) == 0); 3329ad135b5dSChristopher Siden nvlist_free(features); 3330ad135b5dSChristopher Siden } 3331ad135b5dSChristopher Siden 3332fa9e4066Sahrens int 3333ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3334ad135b5dSChristopher Siden char *altroot, size_t buflen) 3335fa9e4066Sahrens { 3336fa9e4066Sahrens int error; 3337fa9e4066Sahrens spa_t *spa; 3338fa9e4066Sahrens 3339fa9e4066Sahrens *config = NULL; 3340468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3341fa9e4066Sahrens 33426809eb4eSEric Schrock if (spa != NULL) { 33436809eb4eSEric Schrock /* 33446809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 33456809eb4eSEric Schrock * or l2cache devices could change and the config would be 33466809eb4eSEric Schrock * self-inconsistent. 33476809eb4eSEric Schrock */ 33486809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3349ea8dc4b6Seschrock 33506809eb4eSEric Schrock if (*config != NULL) { 335111027bc7STim Haley uint64_t loadtimes[2]; 335211027bc7STim Haley 335311027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 335411027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 335511027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 335611027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 335711027bc7STim Haley 3358e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 33596809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 33606809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3361e14bb325SJeff Bonwick 33626809eb4eSEric Schrock if (spa_suspended(spa)) 33636809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 33646809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 33656809eb4eSEric Schrock spa->spa_failmode) == 0); 33666809eb4eSEric Schrock 33676809eb4eSEric Schrock spa_add_spares(spa, *config); 33686809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3369ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 33706809eb4eSEric Schrock } 337199653d4eSeschrock } 337299653d4eSeschrock 3373ea8dc4b6Seschrock /* 3374ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3375ea8dc4b6Seschrock * and call spa_lookup() directly. 3376ea8dc4b6Seschrock */ 3377ea8dc4b6Seschrock if (altroot) { 3378ea8dc4b6Seschrock if (spa == NULL) { 3379ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3380ea8dc4b6Seschrock spa = spa_lookup(name); 3381ea8dc4b6Seschrock if (spa) 3382ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3383ea8dc4b6Seschrock else 3384ea8dc4b6Seschrock altroot[0] = '\0'; 3385ea8dc4b6Seschrock spa = NULL; 3386ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3387ea8dc4b6Seschrock } else { 3388ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3389ea8dc4b6Seschrock } 3390ea8dc4b6Seschrock } 3391ea8dc4b6Seschrock 33926809eb4eSEric Schrock if (spa != NULL) { 33936809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3394fa9e4066Sahrens spa_close(spa, FTAG); 33956809eb4eSEric Schrock } 3396fa9e4066Sahrens 3397fa9e4066Sahrens return (error); 3398fa9e4066Sahrens } 3399fa9e4066Sahrens 340099653d4eSeschrock /* 3401fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3402fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3403fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3404fa94a07fSbrendan * specified, as long as they are well-formed. 340599653d4eSeschrock */ 340699653d4eSeschrock static int 3407fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3408fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3409fa94a07fSbrendan vdev_labeltype_t label) 341099653d4eSeschrock { 3411fa94a07fSbrendan nvlist_t **dev; 3412fa94a07fSbrendan uint_t i, ndev; 341399653d4eSeschrock vdev_t *vd; 341499653d4eSeschrock int error; 341599653d4eSeschrock 3416e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3417e14bb325SJeff Bonwick 341899653d4eSeschrock /* 3419fa94a07fSbrendan * It's acceptable to have no devs specified. 342099653d4eSeschrock */ 3421fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 342299653d4eSeschrock return (0); 342399653d4eSeschrock 3424fa94a07fSbrendan if (ndev == 0) 3425be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 342699653d4eSeschrock 342799653d4eSeschrock /* 3428fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3429fa94a07fSbrendan * device type. 343099653d4eSeschrock */ 3431fa94a07fSbrendan if (spa_version(spa) < version) 3432be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 343399653d4eSeschrock 343439c23413Seschrock /* 3435fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 343639c23413Seschrock * checking. 343739c23413Seschrock */ 3438fa94a07fSbrendan sav->sav_pending = dev; 3439fa94a07fSbrendan sav->sav_npending = ndev; 344039c23413Seschrock 3441fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3442fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 344399653d4eSeschrock mode)) != 0) 344439c23413Seschrock goto out; 344599653d4eSeschrock 344699653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 344799653d4eSeschrock vdev_free(vd); 3448be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 344939c23413Seschrock goto out; 345099653d4eSeschrock } 345199653d4eSeschrock 3452fa94a07fSbrendan /* 3453e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3454e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3455fa94a07fSbrendan */ 3456e14bb325SJeff Bonwick #ifdef _KERNEL 3457fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3458fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3459be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3460cd0837ccSGeorge Wilson vdev_free(vd); 3461fa94a07fSbrendan goto out; 3462fa94a07fSbrendan } 3463e14bb325SJeff Bonwick #endif 346499653d4eSeschrock vd->vdev_top = vd; 346599653d4eSeschrock 346639c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3467fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3468fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 346939c23413Seschrock vd->vdev_guid) == 0); 347039c23413Seschrock } 347199653d4eSeschrock 347299653d4eSeschrock vdev_free(vd); 347339c23413Seschrock 3474fa94a07fSbrendan if (error && 3475fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 347639c23413Seschrock goto out; 347739c23413Seschrock else 347839c23413Seschrock error = 0; 347999653d4eSeschrock } 348099653d4eSeschrock 348139c23413Seschrock out: 3482fa94a07fSbrendan sav->sav_pending = NULL; 3483fa94a07fSbrendan sav->sav_npending = 0; 348439c23413Seschrock return (error); 348599653d4eSeschrock } 348699653d4eSeschrock 3487fa94a07fSbrendan static int 3488fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3489fa94a07fSbrendan { 3490fa94a07fSbrendan int error; 3491fa94a07fSbrendan 3492e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3493e14bb325SJeff Bonwick 3494fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3495fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3496fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3497fa94a07fSbrendan return (error); 3498fa94a07fSbrendan } 3499fa94a07fSbrendan 3500fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3501fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3502fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3503fa94a07fSbrendan } 3504fa94a07fSbrendan 3505fa94a07fSbrendan static void 3506fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3507fa94a07fSbrendan const char *config) 3508fa94a07fSbrendan { 3509fa94a07fSbrendan int i; 3510fa94a07fSbrendan 3511fa94a07fSbrendan if (sav->sav_config != NULL) { 3512fa94a07fSbrendan nvlist_t **olddevs; 3513fa94a07fSbrendan uint_t oldndevs; 3514fa94a07fSbrendan nvlist_t **newdevs; 3515fa94a07fSbrendan 3516fa94a07fSbrendan /* 3517fa94a07fSbrendan * Generate new dev list by concatentating with the 3518fa94a07fSbrendan * current dev list. 3519fa94a07fSbrendan */ 3520fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3521fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3522fa94a07fSbrendan 3523fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3524fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3525fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3526fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3527fa94a07fSbrendan KM_SLEEP) == 0); 3528fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3529fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3530fa94a07fSbrendan KM_SLEEP) == 0); 3531fa94a07fSbrendan 3532fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3533fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3534fa94a07fSbrendan 3535fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3536fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3537fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3538fa94a07fSbrendan nvlist_free(newdevs[i]); 3539fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3540fa94a07fSbrendan } else { 3541fa94a07fSbrendan /* 3542fa94a07fSbrendan * Generate a new dev list. 3543fa94a07fSbrendan */ 3544fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3545fa94a07fSbrendan KM_SLEEP) == 0); 3546fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3547fa94a07fSbrendan devs, ndevs) == 0); 3548fa94a07fSbrendan } 3549fa94a07fSbrendan } 3550fa94a07fSbrendan 3551fa94a07fSbrendan /* 3552fa94a07fSbrendan * Stop and drop level 2 ARC devices 3553fa94a07fSbrendan */ 3554fa94a07fSbrendan void 3555fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3556fa94a07fSbrendan { 3557fa94a07fSbrendan vdev_t *vd; 3558fa94a07fSbrendan int i; 3559fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3560fa94a07fSbrendan 3561fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3562fa94a07fSbrendan uint64_t pool; 3563fa94a07fSbrendan 3564fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3565fa94a07fSbrendan ASSERT(vd != NULL); 3566fa94a07fSbrendan 35678ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 35688ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3569fa94a07fSbrendan l2arc_remove_vdev(vd); 3570fa94a07fSbrendan } 3571fa94a07fSbrendan } 3572fa94a07fSbrendan 3573fa9e4066Sahrens /* 3574fa9e4066Sahrens * Pool Creation 3575fa9e4066Sahrens */ 3576fa9e4066Sahrens int 3577990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 35784445fffbSMatthew Ahrens nvlist_t *zplprops) 3579fa9e4066Sahrens { 3580fa9e4066Sahrens spa_t *spa; 3581990b4856Slling char *altroot = NULL; 35820373e76bSbonwick vdev_t *rvd; 3583fa9e4066Sahrens dsl_pool_t *dp; 3584fa9e4066Sahrens dmu_tx_t *tx; 3585573ca77eSGeorge Wilson int error = 0; 3586fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3587fa94a07fSbrendan nvlist_t **spares, **l2cache; 3588fa94a07fSbrendan uint_t nspares, nl2cache; 3589cde58dbcSMatthew Ahrens uint64_t version, obj; 3590ad135b5dSChristopher Siden boolean_t has_features; 3591fa9e4066Sahrens 3592fa9e4066Sahrens /* 3593fa9e4066Sahrens * If this pool already exists, return failure. 3594fa9e4066Sahrens */ 3595fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3596fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3597fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3598be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3599fa9e4066Sahrens } 3600fa9e4066Sahrens 3601fa9e4066Sahrens /* 3602fa9e4066Sahrens * Allocate a new spa_t structure. 3603fa9e4066Sahrens */ 3604990b4856Slling (void) nvlist_lookup_string(props, 3605990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3606468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 36078ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3608fa9e4066Sahrens 3609990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3610990b4856Slling spa_deactivate(spa); 3611990b4856Slling spa_remove(spa); 3612c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3613990b4856Slling return (error); 3614990b4856Slling } 3615990b4856Slling 3616ad135b5dSChristopher Siden has_features = B_FALSE; 3617ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3618ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3619ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3620ad135b5dSChristopher Siden has_features = B_TRUE; 3621ad135b5dSChristopher Siden } 3622ad135b5dSChristopher Siden 3623ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3624ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3625990b4856Slling version = SPA_VERSION; 3626ad135b5dSChristopher Siden } 3627ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3628b24ab676SJeff Bonwick 3629b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3630b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3631990b4856Slling spa->spa_uberblock.ub_version = version; 3632fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 36330f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 3634fa9e4066Sahrens 363554d692b7SGeorge Wilson /* 363654d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 363754d692b7SGeorge Wilson */ 36386f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 36396f834bc1SMatthew Ahrens KM_SLEEP); 36406f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 36416f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 36426f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 36436f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 36446f834bc1SMatthew Ahrens } 364554d692b7SGeorge Wilson 36460373e76bSbonwick /* 36470373e76bSbonwick * Create the root vdev. 36480373e76bSbonwick */ 3649e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 36500373e76bSbonwick 365199653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 36520373e76bSbonwick 365399653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 365499653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 36550373e76bSbonwick 3656b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3657be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 365899653d4eSeschrock 365999653d4eSeschrock if (error == 0 && 366099653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3661fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 366299653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3663573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3664573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3665573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3666573ca77eSGeorge Wilson } 36670373e76bSbonwick } 36680373e76bSbonwick 3669e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3670fa9e4066Sahrens 367199653d4eSeschrock if (error != 0) { 3672fa9e4066Sahrens spa_unload(spa); 3673fa9e4066Sahrens spa_deactivate(spa); 3674fa9e4066Sahrens spa_remove(spa); 3675fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3676fa9e4066Sahrens return (error); 3677fa9e4066Sahrens } 3678fa9e4066Sahrens 367999653d4eSeschrock /* 368099653d4eSeschrock * Get the list of spares, if specified. 368199653d4eSeschrock */ 368299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 368399653d4eSeschrock &spares, &nspares) == 0) { 3684fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 368599653d4eSeschrock KM_SLEEP) == 0); 3686fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 368799653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3688e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 368999653d4eSeschrock spa_load_spares(spa); 3690e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3691fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3692fa94a07fSbrendan } 3693fa94a07fSbrendan 3694fa94a07fSbrendan /* 3695fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3696fa94a07fSbrendan */ 3697fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3698fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3699fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3700fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3701fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3702fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3703e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3704fa94a07fSbrendan spa_load_l2cache(spa); 3705e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3706fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 370799653d4eSeschrock } 370899653d4eSeschrock 3709ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 37100a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3711fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3712ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3713fa9e4066Sahrens 3714485bbbf5SGeorge Wilson /* 3715485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3716485bbbf5SGeorge Wilson */ 3717485bbbf5SGeorge Wilson ddt_create(spa); 3718485bbbf5SGeorge Wilson 3719485bbbf5SGeorge Wilson spa_update_dspace(spa); 3720485bbbf5SGeorge Wilson 3721fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3722fa9e4066Sahrens 3723fa9e4066Sahrens /* 3724fa9e4066Sahrens * Create the pool config object. 3725fa9e4066Sahrens */ 3726fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3727f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3728fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3729fa9e4066Sahrens 3730ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3731fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3732ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3733ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3734ea8dc4b6Seschrock } 3735fa9e4066Sahrens 3736ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3737ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3738ad135b5dSChristopher Siden 37393f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 37403f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 37413f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 37423f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 37433f9d6ad7SLin Ling } 37443f9d6ad7SLin Ling 3745990b4856Slling /* Newly created pools with the right version are always deflated. */ 3746990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3747990b4856Slling spa->spa_deflate = TRUE; 3748990b4856Slling if (zap_add(spa->spa_meta_objset, 3749990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3750990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3751990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3752990b4856Slling } 375399653d4eSeschrock } 375499653d4eSeschrock 3755fa9e4066Sahrens /* 3756cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3757fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3758fa9e4066Sahrens * keeps changing. 3759fa9e4066Sahrens */ 3760cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3761cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3762cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3763ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3764cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3765cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3766cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3767ea8dc4b6Seschrock } 3768b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3769cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3770fa9e4066Sahrens 377106eeb2adSek /* 377206eeb2adSek * Create the pool's history object. 377306eeb2adSek */ 3774990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3775990b4856Slling spa_history_create_obj(spa, tx); 3776990b4856Slling 377745818ee1SMatthew Ahrens /* 377845818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 377945818ee1SMatthew Ahrens */ 378045818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 378145818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 378245818ee1SMatthew Ahrens 3783990b4856Slling /* 3784990b4856Slling * Set pool properties. 3785990b4856Slling */ 3786990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3787990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 37880a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3789573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3790b24ab676SJeff Bonwick 3791379c004dSEric Schrock if (props != NULL) { 3792379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 37933b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3794379c004dSEric Schrock } 379506eeb2adSek 3796fa9e4066Sahrens dmu_tx_commit(tx); 3797fa9e4066Sahrens 3798fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3799fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3800fa9e4066Sahrens 3801fa9e4066Sahrens /* 3802fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3803fa9e4066Sahrens * bean counters are appropriately updated. 3804fa9e4066Sahrens */ 3805fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3806fa9e4066Sahrens 3807c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 380814372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE); 3809fa9e4066Sahrens 38104445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3811228975ccSek 3812bc9014e6SJustin Gibbs /* 3813bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 3814bc9014e6SJustin Gibbs * and are making their way through the eviction process. 3815bc9014e6SJustin Gibbs */ 3816bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 3817088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 38180f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 3819088f3894Sahrens 3820daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3821daaa36a7SGeorge Wilson 3822fa9e4066Sahrens return (0); 3823fa9e4066Sahrens } 3824fa9e4066Sahrens 3825e7cbe64fSgw #ifdef _KERNEL 3826e7cbe64fSgw /* 382721ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 382821ecdf64SLin Ling * during the system boot up time. 3829e7cbe64fSgw */ 383021ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 383121ecdf64SLin Ling 383221ecdf64SLin Ling static nvlist_t * 383321ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3834e7cbe64fSgw { 383521ecdf64SLin Ling nvlist_t *config; 3836e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3837e7cbe64fSgw uint64_t pgid; 3838e7cbe64fSgw 383921ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 384021ecdf64SLin Ling return (NULL); 384121ecdf64SLin Ling 3842e7cbe64fSgw /* 3843e7cbe64fSgw * Add this top-level vdev to the child array. 3844e7cbe64fSgw */ 384521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 384621ecdf64SLin Ling &nvtop) == 0); 384721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 384821ecdf64SLin Ling &pgid) == 0); 384921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3850e7cbe64fSgw 3851e7cbe64fSgw /* 3852e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3853e7cbe64fSgw */ 3854e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 385521ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 385621ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3857e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3858e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3859e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3860e7cbe64fSgw &nvtop, 1) == 0); 3861e7cbe64fSgw 3862e7cbe64fSgw /* 3863e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3864e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3865e7cbe64fSgw */ 3866e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3867e7cbe64fSgw nvlist_free(nvroot); 386821ecdf64SLin Ling return (config); 3869e7cbe64fSgw } 3870e7cbe64fSgw 3871e7cbe64fSgw /* 387221ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 387321ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 387421ecdf64SLin Ling * device has a more recent txg. 3875051aabe6Staylor */ 387621ecdf64SLin Ling static void 387721ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3878051aabe6Staylor { 3879573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 388021ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3881051aabe6Staylor 388221ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 388321ecdf64SLin Ling nvlist_t *label; 388421ecdf64SLin Ling uint64_t label_txg; 3885051aabe6Staylor 388621ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 388721ecdf64SLin Ling &label) != 0) 388821ecdf64SLin Ling return; 3889051aabe6Staylor 389021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 389121ecdf64SLin Ling &label_txg) == 0); 3892051aabe6Staylor 389321ecdf64SLin Ling /* 389421ecdf64SLin Ling * Do we have a better boot device? 389521ecdf64SLin Ling */ 389621ecdf64SLin Ling if (label_txg > *txg) { 389721ecdf64SLin Ling *txg = label_txg; 389821ecdf64SLin Ling *avd = vd; 3899051aabe6Staylor } 390021ecdf64SLin Ling nvlist_free(label); 3901051aabe6Staylor } 3902051aabe6Staylor } 3903051aabe6Staylor 3904e7cbe64fSgw /* 3905e7cbe64fSgw * Import a root pool. 3906e7cbe64fSgw * 3907051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3908051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3909051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3910e7cbe64fSgw * 3911e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3912e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3913e7cbe64fSgw * e.g. 3914e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3915e7cbe64fSgw */ 3916e7cbe64fSgw int 3917051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3918e7cbe64fSgw { 391921ecdf64SLin Ling spa_t *spa; 392021ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 392121ecdf64SLin Ling nvlist_t *config, *nvtop; 392221ecdf64SLin Ling uint64_t guid, txg; 3923e7cbe64fSgw char *pname; 3924e7cbe64fSgw int error; 3925e7cbe64fSgw 3926e7cbe64fSgw /* 392721ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3928e7cbe64fSgw */ 3929dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3930dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3931dedec472SJack Meng if (config == NULL) { 3932dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3933dedec472SJack Meng /* iscsi boot */ 3934dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3935dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3936dedec472SJack Meng } 3937dedec472SJack Meng } 3938dedec472SJack Meng #endif 3939dedec472SJack Meng if (config == NULL) { 3940ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 394121ecdf64SLin Ling devpath); 3942be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 394321ecdf64SLin Ling } 3944e7cbe64fSgw 394521ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 394621ecdf64SLin Ling &pname) == 0); 394721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3948e7cbe64fSgw 39496809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 39506809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 39516809eb4eSEric Schrock /* 39526809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 39536809eb4eSEric Schrock * can replace it with the correct config we just read in. 39546809eb4eSEric Schrock */ 39556809eb4eSEric Schrock spa_remove(spa); 39566809eb4eSEric Schrock } 39576809eb4eSEric Schrock 3958468c413aSTim Haley spa = spa_add(pname, config, NULL); 39596809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 39604b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3961e7cbe64fSgw 396221ecdf64SLin Ling /* 396321ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 396421ecdf64SLin Ling */ 396521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 396621ecdf64SLin Ling &nvtop) == 0); 396721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 396821ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 396921ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 397021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 397121ecdf64SLin Ling if (error) { 397221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 397321ecdf64SLin Ling nvlist_free(config); 397421ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 397521ecdf64SLin Ling pname); 397621ecdf64SLin Ling return (error); 397721ecdf64SLin Ling } 397821ecdf64SLin Ling 397921ecdf64SLin Ling /* 398021ecdf64SLin Ling * Get the boot vdev. 398121ecdf64SLin Ling */ 398221ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 398321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 398421ecdf64SLin Ling (u_longlong_t)guid); 3985be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 398621ecdf64SLin Ling goto out; 398721ecdf64SLin Ling } 3988e7cbe64fSgw 398921ecdf64SLin Ling /* 399021ecdf64SLin Ling * Determine if there is a better boot device. 399121ecdf64SLin Ling */ 399221ecdf64SLin Ling avd = bvd; 399321ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 399421ecdf64SLin Ling if (avd != bvd) { 399521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 399621ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 3997be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 399821ecdf64SLin Ling goto out; 399921ecdf64SLin Ling } 4000e7cbe64fSgw 400121ecdf64SLin Ling /* 400221ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 400321ecdf64SLin Ling * we're booting off the active spare. 400421ecdf64SLin Ling */ 400521ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 400621ecdf64SLin Ling !bvd->vdev_isspare) { 400721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 400821ecdf64SLin Ling "try booting from '%s'", 4009cb04b873SMark J Musante bvd->vdev_parent-> 4010cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4011be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 401221ecdf64SLin Ling goto out; 401321ecdf64SLin Ling } 401421ecdf64SLin Ling 401521ecdf64SLin Ling error = 0; 401621ecdf64SLin Ling out: 401721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 401821ecdf64SLin Ling vdev_free(rvd); 401921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 402021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 402121ecdf64SLin Ling 402221ecdf64SLin Ling nvlist_free(config); 4023e7cbe64fSgw return (error); 4024e7cbe64fSgw } 402521ecdf64SLin Ling 4026e7cbe64fSgw #endif 4027e7cbe64fSgw 40286809eb4eSEric Schrock /* 40296809eb4eSEric Schrock * Import a non-root pool into the system. 40306809eb4eSEric Schrock */ 4031c5904d13Seschrock int 40324b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4033c5904d13Seschrock { 40346809eb4eSEric Schrock spa_t *spa; 40356809eb4eSEric Schrock char *altroot = NULL; 4036468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4037468c413aSTim Haley zpool_rewind_policy_t policy; 4038f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4039f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 40406809eb4eSEric Schrock int error; 40416809eb4eSEric Schrock nvlist_t *nvroot; 40426809eb4eSEric Schrock nvlist_t **spares, **l2cache; 40436809eb4eSEric Schrock uint_t nspares, nl2cache; 40446809eb4eSEric Schrock 40456809eb4eSEric Schrock /* 40466809eb4eSEric Schrock * If a pool with this name exists, return failure. 40476809eb4eSEric Schrock */ 40486809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 40491195e687SMark J Musante if (spa_lookup(pool) != NULL) { 40506809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4051be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 40526809eb4eSEric Schrock } 40536809eb4eSEric Schrock 40546809eb4eSEric Schrock /* 40556809eb4eSEric Schrock * Create and initialize the spa structure. 40566809eb4eSEric Schrock */ 40576809eb4eSEric Schrock (void) nvlist_lookup_string(props, 40586809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4059f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4060f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4061f9af39baSGeorge Wilson if (readonly) 4062f9af39baSGeorge Wilson mode = FREAD; 4063468c413aSTim Haley spa = spa_add(pool, config, altroot); 40644b964adaSGeorge Wilson spa->spa_import_flags = flags; 40654b964adaSGeorge Wilson 40664b964adaSGeorge Wilson /* 40674b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 40684b964adaSGeorge Wilson * as if it had been loaded at boot. 40694b964adaSGeorge Wilson */ 40704b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 40714b964adaSGeorge Wilson if (props != NULL) 40724b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 40734b964adaSGeorge Wilson 40744b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 407514372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); 40764b964adaSGeorge Wilson 40774b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 40784b964adaSGeorge Wilson return (0); 40794b964adaSGeorge Wilson } 40804b964adaSGeorge Wilson 4081f9af39baSGeorge Wilson spa_activate(spa, mode); 40826809eb4eSEric Schrock 408325f89ee2SJeff Bonwick /* 408425f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 408525f89ee2SJeff Bonwick */ 408625f89ee2SJeff Bonwick spa_async_suspend(spa); 408725f89ee2SJeff Bonwick 40884b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 40894b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 40904b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 40914b964adaSGeorge Wilson 40926809eb4eSEric Schrock /* 40936809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 40946809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 40956809eb4eSEric Schrock * doing an import. 40966809eb4eSEric Schrock */ 4097468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4098468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 40994b964adaSGeorge Wilson 4100468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 4101c8ee1847SVictor Latushkin policy.zrp_request); 4102468c413aSTim Haley 4103468c413aSTim Haley /* 41044b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 41054b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4106468c413aSTim Haley */ 41074b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 41084b964adaSGeorge Wilson spa->spa_load_info) == 0); 41096809eb4eSEric Schrock 41106809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41116809eb4eSEric Schrock /* 41126809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 41136809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 41146809eb4eSEric Schrock */ 41156809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 41166809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 41176809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 41186809eb4eSEric Schrock spa_load_spares(spa); 41196809eb4eSEric Schrock } 41206809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 41216809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 41226809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 41236809eb4eSEric Schrock spa_load_l2cache(spa); 41246809eb4eSEric Schrock } 41256809eb4eSEric Schrock 41266809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 41276809eb4eSEric Schrock &nvroot) == 0); 41286809eb4eSEric Schrock if (error == 0) 41296809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41306809eb4eSEric Schrock VDEV_ALLOC_SPARE); 41316809eb4eSEric Schrock if (error == 0) 41326809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41336809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 41346809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41356809eb4eSEric Schrock 41366809eb4eSEric Schrock if (props != NULL) 41376809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 41386809eb4eSEric Schrock 41396809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 41406809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 41416809eb4eSEric Schrock spa_unload(spa); 41426809eb4eSEric Schrock spa_deactivate(spa); 41436809eb4eSEric Schrock spa_remove(spa); 41446809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 41456809eb4eSEric Schrock return (error); 41466809eb4eSEric Schrock } 41476809eb4eSEric Schrock 4148955ef359SLin Ling spa_async_resume(spa); 4149955ef359SLin Ling 41506809eb4eSEric Schrock /* 41516809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 41526809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 41536809eb4eSEric Schrock */ 41546809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 41556809eb4eSEric Schrock &spares, &nspares) == 0) { 41566809eb4eSEric Schrock if (spa->spa_spares.sav_config) 41576809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 41586809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 41596809eb4eSEric Schrock else 41606809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 41616809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41626809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 41636809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 41646809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41656809eb4eSEric Schrock spa_load_spares(spa); 41666809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41676809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 41686809eb4eSEric Schrock } 41696809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 41706809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 41716809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 41726809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 41736809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 41746809eb4eSEric Schrock else 41756809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 41766809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41776809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 41786809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 41796809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41806809eb4eSEric Schrock spa_load_l2cache(spa); 41816809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41826809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 41836809eb4eSEric Schrock } 41846809eb4eSEric Schrock 4185b693757aSEric Schrock /* 4186b693757aSEric Schrock * Check for any removed devices. 4187b693757aSEric Schrock */ 4188b693757aSEric Schrock if (spa->spa_autoreplace) { 4189b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4190b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4191b693757aSEric Schrock } 4192b693757aSEric Schrock 41936809eb4eSEric Schrock if (spa_writeable(spa)) { 41946809eb4eSEric Schrock /* 41956809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 41966809eb4eSEric Schrock */ 4197bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 41986809eb4eSEric Schrock } 41996809eb4eSEric Schrock 4200573ca77eSGeorge Wilson /* 4201573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4202573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4203573ca77eSGeorge Wilson */ 4204573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4205573ca77eSGeorge Wilson 42064445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 42076809eb4eSEric Schrock 420814372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT); 420914372834SHans Rosenfeld 421014372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 421114372834SHans Rosenfeld 42126809eb4eSEric Schrock return (0); 4213c5904d13Seschrock } 4214c5904d13Seschrock 4215fa9e4066Sahrens nvlist_t * 4216fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4217fa9e4066Sahrens { 4218fa9e4066Sahrens nvlist_t *config = NULL; 4219fa9e4066Sahrens char *poolname; 4220fa9e4066Sahrens spa_t *spa; 4221fa9e4066Sahrens uint64_t state; 42227b7154beSLin Ling int error; 4223fa9e4066Sahrens 4224fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4225fa9e4066Sahrens return (NULL); 4226fa9e4066Sahrens 4227fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4228fa9e4066Sahrens return (NULL); 4229fa9e4066Sahrens 4230fa9e4066Sahrens /* 42310373e76bSbonwick * Create and initialize the spa structure. 4232fa9e4066Sahrens */ 42330373e76bSbonwick mutex_enter(&spa_namespace_lock); 4234468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 42358ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4236fa9e4066Sahrens 4237fa9e4066Sahrens /* 42380373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4239ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4240ecc2d604Sbonwick * is actually the one to trust when doing an import. 4241fa9e4066Sahrens */ 42421195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4243fa9e4066Sahrens 4244fa9e4066Sahrens /* 4245fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4246fa9e4066Sahrens */ 4247fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4248fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4249fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4250fa9e4066Sahrens poolname) == 0); 4251fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4252fa9e4066Sahrens state) == 0); 425395173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 425495173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4255ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4256ad135b5dSChristopher Siden spa->spa_load_info) == 0); 425799653d4eSeschrock 4258e7cbe64fSgw /* 4259e7cbe64fSgw * If the bootfs property exists on this pool then we 4260e7cbe64fSgw * copy it out so that external consumers can tell which 4261e7cbe64fSgw * pools are bootable. 4262e7cbe64fSgw */ 42637b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4264e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4265e7cbe64fSgw 4266e7cbe64fSgw /* 4267e7cbe64fSgw * We have to play games with the name since the 4268e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4269e7cbe64fSgw */ 4270e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4271e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4272e7cbe64fSgw char *cp; 4273e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4274e7cbe64fSgw 4275e7cbe64fSgw cp = strchr(tmpname, '/'); 4276e7cbe64fSgw if (cp == NULL) { 4277e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4278e7cbe64fSgw MAXPATHLEN); 4279e7cbe64fSgw } else { 4280e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4281e7cbe64fSgw "%s/%s", poolname, ++cp); 4282e7cbe64fSgw } 4283e7cbe64fSgw VERIFY(nvlist_add_string(config, 4284e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4285e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4286e7cbe64fSgw } 4287e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4288e7cbe64fSgw } 4289e7cbe64fSgw 429099653d4eSeschrock /* 4291fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 429299653d4eSeschrock */ 42936809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 429499653d4eSeschrock spa_add_spares(spa, config); 4295fa94a07fSbrendan spa_add_l2cache(spa, config); 42966809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4297fa9e4066Sahrens } 4298fa9e4066Sahrens 4299fa9e4066Sahrens spa_unload(spa); 4300fa9e4066Sahrens spa_deactivate(spa); 4301fa9e4066Sahrens spa_remove(spa); 4302fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4303fa9e4066Sahrens 4304fa9e4066Sahrens return (config); 4305fa9e4066Sahrens } 4306fa9e4066Sahrens 4307fa9e4066Sahrens /* 4308fa9e4066Sahrens * Pool export/destroy 4309fa9e4066Sahrens * 4310fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4311fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4312fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4313394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4314394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4315fa9e4066Sahrens */ 4316fa9e4066Sahrens static int 431789a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4318394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4319fa9e4066Sahrens { 4320fa9e4066Sahrens spa_t *spa; 4321fa9e4066Sahrens 432244cd46caSbillm if (oldconfig) 432344cd46caSbillm *oldconfig = NULL; 432444cd46caSbillm 43258ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4326be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4327fa9e4066Sahrens 4328fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4329fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4330fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4331be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4332fa9e4066Sahrens } 4333fa9e4066Sahrens 4334ea8dc4b6Seschrock /* 4335ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4336ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4337ea8dc4b6Seschrock */ 4338ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4339ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4340ea8dc4b6Seschrock spa_async_suspend(spa); 4341ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4342ea8dc4b6Seschrock spa_close(spa, FTAG); 4343ea8dc4b6Seschrock 4344fa9e4066Sahrens /* 4345fa9e4066Sahrens * The pool will be in core if it's openable, 4346fa9e4066Sahrens * in which case we can modify its state. 4347fa9e4066Sahrens */ 4348fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4349fa9e4066Sahrens /* 4350fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4351fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4352fa9e4066Sahrens */ 4353fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4354bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4355fa9e4066Sahrens 4356ea8dc4b6Seschrock /* 4357ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4358ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4359ea8dc4b6Seschrock * fault injection handlers. 4360ea8dc4b6Seschrock */ 4361ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4362ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4363ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4364ea8dc4b6Seschrock spa_async_resume(spa); 4365fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4366be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4367fa9e4066Sahrens } 4368fa9e4066Sahrens 436989a89ebfSlling /* 437089a89ebfSlling * A pool cannot be exported if it has an active shared spare. 437189a89ebfSlling * This is to prevent other pools stealing the active spare 437289a89ebfSlling * from an exported pool. At user's own will, such pool can 437389a89ebfSlling * be forcedly exported. 437489a89ebfSlling */ 437589a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 437689a89ebfSlling spa_has_active_shared_spare(spa)) { 437789a89ebfSlling spa_async_resume(spa); 437889a89ebfSlling mutex_exit(&spa_namespace_lock); 4379be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 438089a89ebfSlling } 438189a89ebfSlling 4382fa9e4066Sahrens /* 4383fa9e4066Sahrens * We want this to be reflected on every label, 4384fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4385fa9e4066Sahrens * final sync that pushes these changes out. 4386fa9e4066Sahrens */ 4387394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4388e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4389ea8dc4b6Seschrock spa->spa_state = new_state; 43903f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 43913f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4392ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4393e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4394ea8dc4b6Seschrock } 4395fa9e4066Sahrens } 4396fa9e4066Sahrens 43973d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 43983d7072f8Seschrock 4399fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4400fa9e4066Sahrens spa_unload(spa); 4401fa9e4066Sahrens spa_deactivate(spa); 4402fa9e4066Sahrens } 4403fa9e4066Sahrens 440444cd46caSbillm if (oldconfig && spa->spa_config) 440544cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 440644cd46caSbillm 4407ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4408394ab0cbSGeorge Wilson if (!hardforce) 4409394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4410ea8dc4b6Seschrock spa_remove(spa); 4411ea8dc4b6Seschrock } 4412fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4413fa9e4066Sahrens 4414fa9e4066Sahrens return (0); 4415fa9e4066Sahrens } 4416fa9e4066Sahrens 4417fa9e4066Sahrens /* 4418fa9e4066Sahrens * Destroy a storage pool. 4419fa9e4066Sahrens */ 4420fa9e4066Sahrens int 4421fa9e4066Sahrens spa_destroy(char *pool) 4422fa9e4066Sahrens { 4423394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4424394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4425fa9e4066Sahrens } 4426fa9e4066Sahrens 4427fa9e4066Sahrens /* 4428fa9e4066Sahrens * Export a storage pool. 4429fa9e4066Sahrens */ 4430fa9e4066Sahrens int 4431394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4432394ab0cbSGeorge Wilson boolean_t hardforce) 4433fa9e4066Sahrens { 4434394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4435394ab0cbSGeorge Wilson force, hardforce)); 4436fa9e4066Sahrens } 4437fa9e4066Sahrens 4438ea8dc4b6Seschrock /* 4439ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4440ea8dc4b6Seschrock * from the namespace in any way. 4441ea8dc4b6Seschrock */ 4442ea8dc4b6Seschrock int 4443ea8dc4b6Seschrock spa_reset(char *pool) 4444ea8dc4b6Seschrock { 444589a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4446394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4447ea8dc4b6Seschrock } 4448ea8dc4b6Seschrock 4449fa9e4066Sahrens /* 4450fa9e4066Sahrens * ========================================================================== 4451fa9e4066Sahrens * Device manipulation 4452fa9e4066Sahrens * ========================================================================== 4453fa9e4066Sahrens */ 4454fa9e4066Sahrens 4455fa9e4066Sahrens /* 44568654d025Sperrin * Add a device to a storage pool. 4457fa9e4066Sahrens */ 4458fa9e4066Sahrens int 4459fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4460fa9e4066Sahrens { 446188ecc943SGeorge Wilson uint64_t txg, id; 44628ad4d6ddSJeff Bonwick int error; 4463fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 44640e34b6a7Sbonwick vdev_t *vd, *tvd; 4465fa94a07fSbrendan nvlist_t **spares, **l2cache; 4466fa94a07fSbrendan uint_t nspares, nl2cache; 4467fa9e4066Sahrens 4468f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4469f9af39baSGeorge Wilson 4470fa9e4066Sahrens txg = spa_vdev_enter(spa); 4471fa9e4066Sahrens 447299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 447399653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 447499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4475fa9e4066Sahrens 4476e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 447799653d4eSeschrock 4478fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4479fa94a07fSbrendan &nspares) != 0) 448099653d4eSeschrock nspares = 0; 448199653d4eSeschrock 4482fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4483fa94a07fSbrendan &nl2cache) != 0) 4484fa94a07fSbrendan nl2cache = 0; 4485fa94a07fSbrendan 4486e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4487fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4488fa9e4066Sahrens 4489e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4490e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4491e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 449299653d4eSeschrock 449339c23413Seschrock /* 4494fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4495fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 449639c23413Seschrock */ 4497e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 449839c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 449939c23413Seschrock 450039c23413Seschrock /* 450139c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 450239c23413Seschrock */ 45038ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 450488ecc943SGeorge Wilson 450588ecc943SGeorge Wilson /* 450688ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 450788ecc943SGeorge Wilson */ 450888ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 450988ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 451088ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 451188ecc943SGeorge Wilson break; 451288ecc943SGeorge Wilson } 451388ecc943SGeorge Wilson } 451439c23413Seschrock tvd = vd->vdev_child[c]; 451539c23413Seschrock vdev_remove_child(vd, tvd); 451688ecc943SGeorge Wilson tvd->vdev_id = id; 451739c23413Seschrock vdev_add_child(rvd, tvd); 451839c23413Seschrock vdev_config_dirty(tvd); 451939c23413Seschrock } 452039c23413Seschrock 452199653d4eSeschrock if (nspares != 0) { 4522fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4523fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 452499653d4eSeschrock spa_load_spares(spa); 4525fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4526fa94a07fSbrendan } 4527fa94a07fSbrendan 4528fa94a07fSbrendan if (nl2cache != 0) { 4529fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4530fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4531fa94a07fSbrendan spa_load_l2cache(spa); 4532fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4533fa9e4066Sahrens } 4534fa9e4066Sahrens 4535fa9e4066Sahrens /* 45360e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 45370e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 45380e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 45390e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 45400e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 45410e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 45420373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 45430e34b6a7Sbonwick * 45440e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 45450e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 45460e34b6a7Sbonwick * steps will be completed the next time we load the pool. 45470e34b6a7Sbonwick */ 45480373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 45490e34b6a7Sbonwick 45500373e76bSbonwick mutex_enter(&spa_namespace_lock); 45510373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 455214372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD); 45530373e76bSbonwick mutex_exit(&spa_namespace_lock); 4554fa9e4066Sahrens 45550373e76bSbonwick return (0); 4556fa9e4066Sahrens } 4557fa9e4066Sahrens 4558fa9e4066Sahrens /* 4559fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4560fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4561fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4562fa9e4066Sahrens * 4563fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4564fa9e4066Sahrens * existing device; in this case the two devices are made into their own 45653d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4566fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4567fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4568fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4569fa9e4066Sahrens * is automatically detached. 4570fa9e4066Sahrens */ 4571fa9e4066Sahrens int 4572ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4573fa9e4066Sahrens { 45743f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4575fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4576fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 457799653d4eSeschrock vdev_ops_t *pvops; 45789b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 45799b3f6b42SEric Kustarz int newvd_isspare; 45809b3f6b42SEric Kustarz int error; 4581fa9e4066Sahrens 4582f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4583f9af39baSGeorge Wilson 4584fa9e4066Sahrens txg = spa_vdev_enter(spa); 4585fa9e4066Sahrens 4586c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4587fa9e4066Sahrens 4588fa9e4066Sahrens if (oldvd == NULL) 4589fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4590fa9e4066Sahrens 45910e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 45920e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 45930e34b6a7Sbonwick 4594fa9e4066Sahrens pvd = oldvd->vdev_parent; 4595fa9e4066Sahrens 459699653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4597cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 45983d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 45993d7072f8Seschrock 46003d7072f8Seschrock if (newrootvd->vdev_children != 1) 4601fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4602fa9e4066Sahrens 4603fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4604fa9e4066Sahrens 4605fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4606fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4607fa9e4066Sahrens 460899653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4609fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4610fa9e4066Sahrens 46118654d025Sperrin /* 46128654d025Sperrin * Spares can't replace logs 46138654d025Sperrin */ 4614ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 46158654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 46168654d025Sperrin 461799653d4eSeschrock if (!replacing) { 461899653d4eSeschrock /* 461999653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 462099653d4eSeschrock * vdev. 462199653d4eSeschrock */ 462299653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 462399653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 462499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 462599653d4eSeschrock 462699653d4eSeschrock pvops = &vdev_mirror_ops; 462799653d4eSeschrock } else { 462899653d4eSeschrock /* 462999653d4eSeschrock * Active hot spares can only be replaced by inactive hot 463099653d4eSeschrock * spares. 463199653d4eSeschrock */ 463299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4633cb04b873SMark J Musante oldvd->vdev_isspare && 463499653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 463599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 463699653d4eSeschrock 463799653d4eSeschrock /* 463899653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 463999653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 464039c23413Seschrock * want to create a replacing vdev. The user is not allowed to 464139c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 464239c23413Seschrock * the same (spare replaces spare, non-spare replaces 464339c23413Seschrock * non-spare). 464499653d4eSeschrock */ 4645cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4646cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 464799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4648cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4649cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 465039c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4651cb04b873SMark J Musante } 4652cb04b873SMark J Musante 4653cb04b873SMark J Musante if (newvd->vdev_isspare) 465499653d4eSeschrock pvops = &vdev_spare_ops; 465599653d4eSeschrock else 465699653d4eSeschrock pvops = &vdev_replacing_ops; 465799653d4eSeschrock } 465899653d4eSeschrock 46592a79c5feSlling /* 4660573ca77eSGeorge Wilson * Make sure the new device is big enough. 46612a79c5feSlling */ 4662573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4663fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4664fa9e4066Sahrens 4665ecc2d604Sbonwick /* 4666ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4667ecc2d604Sbonwick * than the top-level vdev. 4668ecc2d604Sbonwick */ 4669ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4670fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4671fa9e4066Sahrens 4672fa9e4066Sahrens /* 4673fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4674fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4675fa9e4066Sahrens */ 4676fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4677fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4678fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4679fa9e4066Sahrens KM_SLEEP); 4680fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4681fa9e4066Sahrens newvd->vdev_path, "old"); 4682fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4683fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4684fa9e4066Sahrens oldvd->vdev_devid = NULL; 4685fa9e4066Sahrens } 4686fa9e4066Sahrens } 4687fa9e4066Sahrens 4688cb04b873SMark J Musante /* mark the device being resilvered */ 4689b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4690cb04b873SMark J Musante 4691fa9e4066Sahrens /* 469299653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 469399653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4694fa9e4066Sahrens */ 4695fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4696fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4697fa9e4066Sahrens 4698fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4699fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4700fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4701fa9e4066Sahrens 4702fa9e4066Sahrens /* 4703fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4704fa9e4066Sahrens */ 4705fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4706fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 470788ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4708fa9e4066Sahrens vdev_add_child(pvd, newvd); 4709fa9e4066Sahrens 4710fa9e4066Sahrens tvd = newvd->vdev_top; 4711fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4712fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4713fa9e4066Sahrens 4714fa9e4066Sahrens vdev_config_dirty(tvd); 4715fa9e4066Sahrens 4716fa9e4066Sahrens /* 47173f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 47183f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 47193f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4720fa9e4066Sahrens */ 47213f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4722fa9e4066Sahrens 47233f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 47243f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4725fa9e4066Sahrens 47266809eb4eSEric Schrock if (newvd->vdev_isspare) { 472739c23413Seschrock spa_spare_activate(newvd); 47286809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 47296809eb4eSEric Schrock } 47306809eb4eSEric Schrock 4731e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4732e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 47339b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4734ea8dc4b6Seschrock 4735fa9e4066Sahrens /* 4736fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4737fa9e4066Sahrens */ 4738ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4739fa9e4066Sahrens 47403f9d6ad7SLin Ling /* 47410713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 47420713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 47430713e232SGeorge Wilson * respective datasets. 47443f9d6ad7SLin Ling */ 47453f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 47463f9d6ad7SLin Ling 474714372834SHans Rosenfeld if (spa->spa_bootfs) 474814372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 474914372834SHans Rosenfeld 475014372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH); 475114372834SHans Rosenfeld 47523f9d6ad7SLin Ling /* 47533f9d6ad7SLin Ling * Commit the config 47543f9d6ad7SLin Ling */ 47553f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4756fa9e4066Sahrens 47574445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 47583f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4759c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4760c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4761c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 47629b3f6b42SEric Kustarz 47639b3f6b42SEric Kustarz spa_strfree(oldvdpath); 47649b3f6b42SEric Kustarz spa_strfree(newvdpath); 47659b3f6b42SEric Kustarz 4766fa9e4066Sahrens return (0); 4767fa9e4066Sahrens } 4768fa9e4066Sahrens 4769fa9e4066Sahrens /* 4770fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4771f7170741SWill Andrews * 4772fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4773fa9e4066Sahrens * is a replacing vdev. 4774fa9e4066Sahrens */ 4775fa9e4066Sahrens int 47768ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4777fa9e4066Sahrens { 4778fa9e4066Sahrens uint64_t txg; 47798ad4d6ddSJeff Bonwick int error; 4780fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4781fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 478299653d4eSeschrock boolean_t unspare = B_FALSE; 4783d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 47841195e687SMark J Musante char *vdpath; 4785fa9e4066Sahrens 4786f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4787f9af39baSGeorge Wilson 4788fa9e4066Sahrens txg = spa_vdev_enter(spa); 4789fa9e4066Sahrens 4790c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4791fa9e4066Sahrens 4792fa9e4066Sahrens if (vd == NULL) 4793fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4794fa9e4066Sahrens 47950e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 47960e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 47970e34b6a7Sbonwick 4798fa9e4066Sahrens pvd = vd->vdev_parent; 4799fa9e4066Sahrens 48008ad4d6ddSJeff Bonwick /* 48018ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 48028ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 48038ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 48048ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 48058ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 48068ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 48078ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 48088ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 48098ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 48108ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 48118ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 48128ad4d6ddSJeff Bonwick */ 48138ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 48148ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 48158ad4d6ddSJeff Bonwick 4816fa9e4066Sahrens /* 4817cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 481899653d4eSeschrock */ 4819cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4820cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4821cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 482299653d4eSeschrock 482399653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4824e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4825fa9e4066Sahrens 4826fa9e4066Sahrens /* 482799653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4828fa9e4066Sahrens */ 4829fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 483099653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 483199653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4832fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4833fa9e4066Sahrens 4834fa9e4066Sahrens /* 48358ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 48368ad4d6ddSJeff Bonwick * we cannot safely detach it. 4837fa9e4066Sahrens */ 48388ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4839fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4840fa9e4066Sahrens 48418ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4842fa9e4066Sahrens 4843bf82a41bSeschrock /* 4844bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4845bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4846bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4847bf82a41bSeschrock */ 4848cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4849cb04b873SMark J Musante vd->vdev_path != NULL) { 4850cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4851cb04b873SMark J Musante 4852cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4853cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4854cb04b873SMark J Musante 4855cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4856cb04b873SMark J Musante continue; 4857cb04b873SMark J Musante 4858cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4859cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4860cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4861cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4862cb04b873SMark J Musante break; 4863cb04b873SMark J Musante } 4864bf82a41bSeschrock } 4865bf82a41bSeschrock } 4866bf82a41bSeschrock 486799653d4eSeschrock /* 486899653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 486999653d4eSeschrock * that the spare should become a real disk, and be removed from the 487099653d4eSeschrock * active spare list for the pool. 487199653d4eSeschrock */ 487299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4873cb04b873SMark J Musante vd->vdev_id == 0 && 4874cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 487599653d4eSeschrock unspare = B_TRUE; 487699653d4eSeschrock 4877fa9e4066Sahrens /* 4878fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4879fa9e4066Sahrens * This must be done after all other error cases are handled, 4880fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4881fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4882fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4883fa9e4066Sahrens * it's being detached! 4884fa9e4066Sahrens */ 488539c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4886fa9e4066Sahrens 4887fa9e4066Sahrens /* 4888fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4889fa9e4066Sahrens */ 4890fa9e4066Sahrens vdev_remove_child(pvd, vd); 4891fa9e4066Sahrens vdev_compact_children(pvd); 4892fa9e4066Sahrens 4893fa9e4066Sahrens /* 4894fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4895fa9e4066Sahrens */ 4896cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4897fa9e4066Sahrens 489899653d4eSeschrock /* 489999653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 49008ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 49018ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 49028ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 49038ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 49048ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 49058ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 490699653d4eSeschrock */ 490799653d4eSeschrock if (unspare) { 490899653d4eSeschrock ASSERT(cvd->vdev_isspare); 490939c23413Seschrock spa_spare_remove(cvd); 491099653d4eSeschrock unspare_guid = cvd->vdev_guid; 49118ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4912cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 491399653d4eSeschrock } 491499653d4eSeschrock 4915fa9e4066Sahrens /* 4916fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4917fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4918fa9e4066Sahrens */ 4919cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4920cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4921cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4922fa9e4066Sahrens vdev_remove_parent(cvd); 4923cb04b873SMark J Musante } 4924cb04b873SMark J Musante 4925fa9e4066Sahrens 4926fa9e4066Sahrens /* 4927fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4928fa9e4066Sahrens * may have been the previous top-level vdev. 4929fa9e4066Sahrens */ 4930fa9e4066Sahrens tvd = cvd->vdev_top; 4931fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4932fa9e4066Sahrens 4933fa9e4066Sahrens /* 493439c23413Seschrock * Reevaluate the parent vdev state. 4935fa9e4066Sahrens */ 49363d7072f8Seschrock vdev_propagate_state(cvd); 4937fa9e4066Sahrens 4938fa9e4066Sahrens /* 4939573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4940573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4941573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4942573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4943573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4944fa9e4066Sahrens */ 4945573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4946573ca77eSGeorge Wilson vdev_reopen(tvd); 4947573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4948573ca77eSGeorge Wilson } 4949fa9e4066Sahrens 4950fa9e4066Sahrens vdev_config_dirty(tvd); 4951fa9e4066Sahrens 4952fa9e4066Sahrens /* 495339c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 495439c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 495539c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 495639c23413Seschrock * prevent vd from being accessed after it's freed. 4957fa9e4066Sahrens */ 49581195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 49598ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4960fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4961ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4962ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4963fa9e4066Sahrens 49643d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 49653d7072f8Seschrock 4966cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4967cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4968cb04b873SMark J Musante 496999653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 497099653d4eSeschrock 49714445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 49721195e687SMark J Musante "vdev=%s", vdpath); 49731195e687SMark J Musante spa_strfree(vdpath); 49741195e687SMark J Musante 497599653d4eSeschrock /* 497639c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 497739c23413Seschrock * then we want to go through and remove the device from the hot spare 497839c23413Seschrock * list of every other pool. 497999653d4eSeschrock */ 498099653d4eSeschrock if (unspare) { 4981cb04b873SMark J Musante spa_t *altspa = NULL; 4982cb04b873SMark J Musante 498399653d4eSeschrock mutex_enter(&spa_namespace_lock); 4984cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4985cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4986cb04b873SMark J Musante altspa == spa) 498799653d4eSeschrock continue; 4988cb04b873SMark J Musante 4989cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 49909af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4991cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 49929af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4993cb04b873SMark J Musante spa_close(altspa, FTAG); 499499653d4eSeschrock } 499599653d4eSeschrock mutex_exit(&spa_namespace_lock); 4996cb04b873SMark J Musante 4997cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4998cb04b873SMark J Musante spa_vdev_resilver_done(spa); 499999653d4eSeschrock } 500099653d4eSeschrock 5001cb04b873SMark J Musante /* all done with the spa; OK to release */ 5002cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5003cb04b873SMark J Musante spa_close(spa, FTAG); 5004cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5005cb04b873SMark J Musante 500699653d4eSeschrock return (error); 500799653d4eSeschrock } 500899653d4eSeschrock 50091195e687SMark J Musante /* 50101195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 50111195e687SMark J Musante */ 50121195e687SMark J Musante int 50131195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 50141195e687SMark J Musante nvlist_t *props, boolean_t exp) 50151195e687SMark J Musante { 50161195e687SMark J Musante int error = 0; 50171195e687SMark J Musante uint64_t txg, *glist; 50181195e687SMark J Musante spa_t *newspa; 50191195e687SMark J Musante uint_t c, children, lastlog; 50201195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 50211195e687SMark J Musante dmu_tx_t *tx; 50221195e687SMark J Musante char *altroot = NULL; 50231195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 50241195e687SMark J Musante boolean_t activate_slog; 50251195e687SMark J Musante 5026f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 50271195e687SMark J Musante 50281195e687SMark J Musante txg = spa_vdev_enter(spa); 50291195e687SMark J Musante 50301195e687SMark J Musante /* clear the log and flush everything up to now */ 50311195e687SMark J Musante activate_slog = spa_passivate_log(spa); 50321195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 50331195e687SMark J Musante error = spa_offline_log(spa); 50341195e687SMark J Musante txg = spa_vdev_config_enter(spa); 50351195e687SMark J Musante 50361195e687SMark J Musante if (activate_slog) 50371195e687SMark J Musante spa_activate_log(spa); 50381195e687SMark J Musante 50391195e687SMark J Musante if (error != 0) 50401195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 50411195e687SMark J Musante 50421195e687SMark J Musante /* check new spa name before going any further */ 50431195e687SMark J Musante if (spa_lookup(newname) != NULL) 50441195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 50451195e687SMark J Musante 50461195e687SMark J Musante /* 50471195e687SMark J Musante * scan through all the children to ensure they're all mirrors 50481195e687SMark J Musante */ 50491195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 50501195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 50511195e687SMark J Musante &children) != 0) 50521195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50531195e687SMark J Musante 50541195e687SMark J Musante /* first, check to ensure we've got the right child count */ 50551195e687SMark J Musante rvd = spa->spa_root_vdev; 50561195e687SMark J Musante lastlog = 0; 50571195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 50581195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 50591195e687SMark J Musante 50601195e687SMark J Musante /* don't count the holes & logs as children */ 50611195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 50621195e687SMark J Musante if (lastlog == 0) 50631195e687SMark J Musante lastlog = c; 50641195e687SMark J Musante continue; 50651195e687SMark J Musante } 50661195e687SMark J Musante 50671195e687SMark J Musante lastlog = 0; 50681195e687SMark J Musante } 50691195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 50701195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50711195e687SMark J Musante 50721195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 50731195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 50741195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 50751195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50761195e687SMark J Musante 50771195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 50781195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 50791195e687SMark J Musante 50801195e687SMark J Musante /* then, loop over each vdev and validate it */ 50811195e687SMark J Musante for (c = 0; c < children; c++) { 50821195e687SMark J Musante uint64_t is_hole = 0; 50831195e687SMark J Musante 50841195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 50851195e687SMark J Musante &is_hole); 50861195e687SMark J Musante 50871195e687SMark J Musante if (is_hole != 0) { 50881195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 50891195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 50901195e687SMark J Musante continue; 50911195e687SMark J Musante } else { 5092be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 50931195e687SMark J Musante break; 50941195e687SMark J Musante } 50951195e687SMark J Musante } 50961195e687SMark J Musante 50971195e687SMark J Musante /* which disk is going to be split? */ 50981195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 50991195e687SMark J Musante &glist[c]) != 0) { 5100be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51011195e687SMark J Musante break; 51021195e687SMark J Musante } 51031195e687SMark J Musante 51041195e687SMark J Musante /* look it up in the spa */ 51051195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 51061195e687SMark J Musante if (vml[c] == NULL) { 5107be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 51081195e687SMark J Musante break; 51091195e687SMark J Musante } 51101195e687SMark J Musante 51111195e687SMark J Musante /* make sure there's nothing stopping the split */ 51121195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 51131195e687SMark J Musante vml[c]->vdev_islog || 51141195e687SMark J Musante vml[c]->vdev_ishole || 51151195e687SMark J Musante vml[c]->vdev_isspare || 51161195e687SMark J Musante vml[c]->vdev_isl2cache || 51171195e687SMark J Musante !vdev_writeable(vml[c]) || 5118d41c4376SMark J Musante vml[c]->vdev_children != 0 || 51191195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 51201195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5121be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51221195e687SMark J Musante break; 51231195e687SMark J Musante } 51241195e687SMark J Musante 51251195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5126be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 51271195e687SMark J Musante break; 51281195e687SMark J Musante } 51291195e687SMark J Musante 51301195e687SMark J Musante /* we need certain info from the top level */ 51311195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 51321195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 51331195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 51341195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 51351195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 51361195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 51371195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 51381195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5139215198a6SJoe Stein 5140215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5141215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5142215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5143215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5144215198a6SJoe Stein 5145215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5146215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5147215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5148215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 51491195e687SMark J Musante } 51501195e687SMark J Musante 51511195e687SMark J Musante if (error != 0) { 51521195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51531195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51541195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 51551195e687SMark J Musante } 51561195e687SMark J Musante 51571195e687SMark J Musante /* stop writers from using the disks */ 51581195e687SMark J Musante for (c = 0; c < children; c++) { 51591195e687SMark J Musante if (vml[c] != NULL) 51601195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 51611195e687SMark J Musante } 51621195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 51631195e687SMark J Musante 51641195e687SMark J Musante /* 51651195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 51661195e687SMark J Musante * will disappear once the config is regenerated. 51671195e687SMark J Musante */ 51681195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 51691195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 51701195e687SMark J Musante glist, children) == 0); 51711195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51721195e687SMark J Musante 517398295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 51741195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 51751195e687SMark J Musante nvl) == 0); 517698295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 51771195e687SMark J Musante spa->spa_config_splitting = nvl; 51781195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 51791195e687SMark J Musante 51801195e687SMark J Musante /* configure and create the new pool */ 51811195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 51821195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 51831195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 51841195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 51851195e687SMark J Musante spa_version(spa)) == 0); 51861195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 51871195e687SMark J Musante spa->spa_config_txg) == 0); 51881195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 51891195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5190215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 51911195e687SMark J Musante (void) nvlist_lookup_string(props, 51921195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 51931195e687SMark J Musante 5194d41c4376SMark J Musante /* add the new pool to the namespace */ 51951195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5196215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 51971195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 51981195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 51991195e687SMark J Musante 52001195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 52011195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 52021195e687SMark J Musante 52031195e687SMark J Musante if (zio_injection_enabled) 52041195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 52051195e687SMark J Musante 52061195e687SMark J Musante spa_activate(newspa, spa_mode_global); 52071195e687SMark J Musante spa_async_suspend(newspa); 52081195e687SMark J Musante 52091195e687SMark J Musante /* create the new pool from the disks of the original pool */ 52101195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 52111195e687SMark J Musante if (error) 52121195e687SMark J Musante goto out; 52131195e687SMark J Musante 52141195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 52151195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 52161195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 52171195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 52181195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 52191195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 52201195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 52211195e687SMark J Musante B_TRUE)); 52221195e687SMark J Musante } 52231195e687SMark J Musante 52241195e687SMark J Musante /* set the props */ 52251195e687SMark J Musante if (props != NULL) { 52261195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 52271195e687SMark J Musante error = spa_prop_set(newspa, props); 52281195e687SMark J Musante if (error) 52291195e687SMark J Musante goto out; 52301195e687SMark J Musante } 52311195e687SMark J Musante 52321195e687SMark J Musante /* flush everything */ 52331195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 52341195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 52351195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 52361195e687SMark J Musante 52371195e687SMark J Musante if (zio_injection_enabled) 52381195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 52391195e687SMark J Musante 52401195e687SMark J Musante spa_async_resume(newspa); 52411195e687SMark J Musante 52421195e687SMark J Musante /* finally, update the original pool's config */ 52431195e687SMark J Musante txg = spa_vdev_config_enter(spa); 52441195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 52451195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 52461195e687SMark J Musante if (error != 0) 52471195e687SMark J Musante dmu_tx_abort(tx); 52481195e687SMark J Musante for (c = 0; c < children; c++) { 52491195e687SMark J Musante if (vml[c] != NULL) { 52501195e687SMark J Musante vdev_split(vml[c]); 52511195e687SMark J Musante if (error == 0) 52524445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 52534445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5254215198a6SJoe Stein 52551195e687SMark J Musante vdev_free(vml[c]); 52561195e687SMark J Musante } 52571195e687SMark J Musante } 5258215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 52591195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 52601195e687SMark J Musante spa->spa_config_splitting = NULL; 52611195e687SMark J Musante nvlist_free(nvl); 52621195e687SMark J Musante if (error == 0) 52631195e687SMark J Musante dmu_tx_commit(tx); 52641195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 52651195e687SMark J Musante 52661195e687SMark J Musante if (zio_injection_enabled) 52671195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 52681195e687SMark J Musante 52691195e687SMark J Musante /* split is complete; log a history record */ 52704445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 52714445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 52721195e687SMark J Musante 52731195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 52741195e687SMark J Musante 52751195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 52761195e687SMark J Musante if (exp) 52771195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 52781195e687SMark J Musante B_FALSE, B_FALSE); 52791195e687SMark J Musante 52801195e687SMark J Musante return (error); 52811195e687SMark J Musante 52821195e687SMark J Musante out: 52831195e687SMark J Musante spa_unload(newspa); 52841195e687SMark J Musante spa_deactivate(newspa); 52851195e687SMark J Musante spa_remove(newspa); 52861195e687SMark J Musante 52871195e687SMark J Musante txg = spa_vdev_config_enter(spa); 528898295d61SMark J Musante 528998295d61SMark J Musante /* re-online all offlined disks */ 529098295d61SMark J Musante for (c = 0; c < children; c++) { 529198295d61SMark J Musante if (vml[c] != NULL) 529298295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 529398295d61SMark J Musante } 529498295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 529598295d61SMark J Musante 52961195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 52971195e687SMark J Musante spa->spa_config_splitting = NULL; 5298d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 52991195e687SMark J Musante 53001195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 53011195e687SMark J Musante return (error); 53021195e687SMark J Musante } 53031195e687SMark J Musante 5304e14bb325SJeff Bonwick static nvlist_t * 5305e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 530699653d4eSeschrock { 5307e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5308e14bb325SJeff Bonwick uint64_t guid; 530999653d4eSeschrock 5310e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5311e14bb325SJeff Bonwick &guid) == 0); 531299653d4eSeschrock 5313e14bb325SJeff Bonwick if (guid == target_guid) 5314e14bb325SJeff Bonwick return (nvpp[i]); 531599653d4eSeschrock } 531699653d4eSeschrock 5317e14bb325SJeff Bonwick return (NULL); 5318fa94a07fSbrendan } 5319fa94a07fSbrendan 5320e14bb325SJeff Bonwick static void 5321e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 53220f7643c7SGeorge Wilson nvlist_t *dev_to_remove) 5323fa94a07fSbrendan { 5324e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5325fa94a07fSbrendan 5326e14bb325SJeff Bonwick if (count > 1) 5327e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5328fa94a07fSbrendan 5329e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5330e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5331e14bb325SJeff Bonwick continue; 5332e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5333fa94a07fSbrendan } 5334fa94a07fSbrendan 5335e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5336e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5337fa94a07fSbrendan 5338e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5339e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5340fa94a07fSbrendan 5341e14bb325SJeff Bonwick if (count > 1) 5342e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5343fa94a07fSbrendan } 5344fa94a07fSbrendan 534588ecc943SGeorge Wilson /* 534688ecc943SGeorge Wilson * Evacuate the device. 534788ecc943SGeorge Wilson */ 53483f9d6ad7SLin Ling static int 534988ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 535088ecc943SGeorge Wilson { 535188ecc943SGeorge Wilson uint64_t txg; 53523f9d6ad7SLin Ling int error = 0; 535388ecc943SGeorge Wilson 535488ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 535588ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5356b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 535788ecc943SGeorge Wilson 535888ecc943SGeorge Wilson /* 535988ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 536088ecc943SGeorge Wilson * since we need to do I/O but we do keep the 536188ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 536288ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 536388ecc943SGeorge Wilson */ 536488ecc943SGeorge Wilson if (vd->vdev_islog) { 53653f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 53663f9d6ad7SLin Ling error = spa_offline_log(spa); 5367a1521560SJeff Bonwick } else { 5368be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 536988ecc943SGeorge Wilson } 537088ecc943SGeorge Wilson 5371a1521560SJeff Bonwick if (error) 5372a1521560SJeff Bonwick return (error); 5373a1521560SJeff Bonwick 537488ecc943SGeorge Wilson /* 5375a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5376a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 537788ecc943SGeorge Wilson */ 5378fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 537988ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 538088ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 53810713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg); 538288ecc943SGeorge Wilson vdev_config_dirty(vd); 538388ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 538488ecc943SGeorge Wilson 538588ecc943SGeorge Wilson return (0); 538688ecc943SGeorge Wilson } 538788ecc943SGeorge Wilson 538888ecc943SGeorge Wilson /* 538988ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 539088ecc943SGeorge Wilson */ 53913f9d6ad7SLin Ling static void 5392a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 539388ecc943SGeorge Wilson { 539488ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 539588ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 539688ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 539788ecc943SGeorge Wilson 539888ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 539988ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5400b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 540188ecc943SGeorge Wilson 54023f9d6ad7SLin Ling /* 54033f9d6ad7SLin Ling * Only remove any devices which are empty. 54043f9d6ad7SLin Ling */ 54053f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 54063f9d6ad7SLin Ling return; 54073f9d6ad7SLin Ling 540888ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5409b24ab676SJeff Bonwick 5410b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5411b24ab676SJeff Bonwick vdev_state_clean(vd); 5412b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5413b24ab676SJeff Bonwick vdev_config_clean(vd); 5414b24ab676SJeff Bonwick 541588ecc943SGeorge Wilson vdev_free(vd); 541688ecc943SGeorge Wilson 541788ecc943SGeorge Wilson if (last_vdev) { 541888ecc943SGeorge Wilson vdev_compact_children(rvd); 541988ecc943SGeorge Wilson } else { 542088ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 542188ecc943SGeorge Wilson vdev_add_child(rvd, vd); 542288ecc943SGeorge Wilson } 5423fcbfa62bSLin Ling vdev_config_dirty(rvd); 5424fcbfa62bSLin Ling 5425fcbfa62bSLin Ling /* 5426fcbfa62bSLin Ling * Reassess the health of our root vdev. 5427fcbfa62bSLin Ling */ 5428fcbfa62bSLin Ling vdev_reopen(rvd); 542988ecc943SGeorge Wilson } 543088ecc943SGeorge Wilson 54313f9d6ad7SLin Ling /* 54323f9d6ad7SLin Ling * Remove a device from the pool - 54333f9d6ad7SLin Ling * 54343f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 54353f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 54363f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 54373f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 54383f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 5439f7170741SWill Andrews * 5440f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC 5441f7170741SWill Andrews * devices. 5442fa94a07fSbrendan */ 5443fa94a07fSbrendan int 5444fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5445fa94a07fSbrendan { 5446fa94a07fSbrendan vdev_t *vd; 5447b72b6bb1SAlan Somers sysevent_t *ev = NULL; 5448a1521560SJeff Bonwick metaslab_group_t *mg; 5449e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 54508ad4d6ddSJeff Bonwick uint64_t txg = 0; 545188ecc943SGeorge Wilson uint_t nspares, nl2cache; 5452fa94a07fSbrendan int error = 0; 54538ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5454fa94a07fSbrendan 5455f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5456f9af39baSGeorge Wilson 54578ad4d6ddSJeff Bonwick if (!locked) 54588ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5459fa94a07fSbrendan 5460c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5461fa94a07fSbrendan 5462fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5463fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5464e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5465e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5466e14bb325SJeff Bonwick /* 5467e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5468e14bb325SJeff Bonwick * in this pool. 5469e14bb325SJeff Bonwick */ 5470e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5471b72b6bb1SAlan Somers if (vd == NULL) 5472b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5473b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); 5474e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5475e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5476e14bb325SJeff Bonwick spa_load_spares(spa); 5477e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5478e14bb325SJeff Bonwick } else { 5479be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 5480e14bb325SJeff Bonwick } 5481e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5482fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5483e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5484e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5485e14bb325SJeff Bonwick /* 5486e14bb325SJeff Bonwick * Cache devices can always be removed. 5487e14bb325SJeff Bonwick */ 5488b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5489b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX); 5490e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5491e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5492fa94a07fSbrendan spa_load_l2cache(spa); 5493fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 549488ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 549588ecc943SGeorge Wilson ASSERT(!locked); 5496b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 549788ecc943SGeorge Wilson 5498a1521560SJeff Bonwick mg = vd->vdev_mg; 5499a1521560SJeff Bonwick 550088ecc943SGeorge Wilson /* 5501a1521560SJeff Bonwick * Stop allocating from this vdev. 550288ecc943SGeorge Wilson */ 5503a1521560SJeff Bonwick metaslab_group_passivate(mg); 550488ecc943SGeorge Wilson 5505b24ab676SJeff Bonwick /* 5506b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5507b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5508b24ab676SJeff Bonwick */ 5509b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5510b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5511b24ab676SJeff Bonwick 5512a1521560SJeff Bonwick /* 5513a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5514a1521560SJeff Bonwick */ 5515a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5516a1521560SJeff Bonwick 551788ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 551888ecc943SGeorge Wilson 5519a1521560SJeff Bonwick /* 5520a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5521a1521560SJeff Bonwick */ 5522a1521560SJeff Bonwick if (error) { 5523a1521560SJeff Bonwick metaslab_group_activate(mg); 5524a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5525a1521560SJeff Bonwick } 5526a1521560SJeff Bonwick 5527a1521560SJeff Bonwick /* 5528a1521560SJeff Bonwick * Clean up the vdev namespace. 5529a1521560SJeff Bonwick */ 5530b72b6bb1SAlan Somers ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV); 5531a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 553288ecc943SGeorge Wilson 5533e14bb325SJeff Bonwick } else if (vd != NULL) { 5534e14bb325SJeff Bonwick /* 5535e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5536e14bb325SJeff Bonwick */ 5537be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5538e14bb325SJeff Bonwick } else { 5539e14bb325SJeff Bonwick /* 5540e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5541e14bb325SJeff Bonwick */ 5542be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 5543fa94a07fSbrendan } 554499653d4eSeschrock 55458ad4d6ddSJeff Bonwick if (!locked) 554663364b0eSAlan Somers error = spa_vdev_exit(spa, NULL, txg, error); 55478ad4d6ddSJeff Bonwick 5548b72b6bb1SAlan Somers if (ev) 5549b72b6bb1SAlan Somers spa_event_post(ev); 5550b72b6bb1SAlan Somers 55518ad4d6ddSJeff Bonwick return (error); 5552fa9e4066Sahrens } 5553fa9e4066Sahrens 5554fa9e4066Sahrens /* 55553d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5556f7170741SWill Andrews * currently spared, so we can detach it. 5557fa9e4066Sahrens */ 5558ea8dc4b6Seschrock static vdev_t * 55593d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5560fa9e4066Sahrens { 5561ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5562fa9e4066Sahrens 5563573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 55643d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5565ea8dc4b6Seschrock if (oldvd != NULL) 5566ea8dc4b6Seschrock return (oldvd); 5567ea8dc4b6Seschrock } 5568fa9e4066Sahrens 55693d7072f8Seschrock /* 5570cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5571cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5572cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5573cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5574cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5575cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 55763d7072f8Seschrock */ 5577cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5578cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5579cb04b873SMark J Musante 5580cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5581ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5582ea8dc4b6Seschrock 55838ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5584e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 55858ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5586ea8dc4b6Seschrock return (oldvd); 5587fa9e4066Sahrens } 5588ea8dc4b6Seschrock 55893d7072f8Seschrock /* 55903d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 55913d7072f8Seschrock */ 5592cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5593cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5594cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5595cb04b873SMark J Musante 5596cb04b873SMark J Musante if (last->vdev_unspare) { 5597cb04b873SMark J Musante oldvd = first; 5598cb04b873SMark J Musante newvd = last; 5599cb04b873SMark J Musante } else if (first->vdev_unspare) { 5600cb04b873SMark J Musante oldvd = last; 5601cb04b873SMark J Musante newvd = first; 5602cb04b873SMark J Musante } else { 5603cb04b873SMark J Musante oldvd = NULL; 5604cb04b873SMark J Musante } 56053d7072f8Seschrock 5606cb04b873SMark J Musante if (oldvd != NULL && 56078ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5608e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5609cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 56103d7072f8Seschrock return (oldvd); 5611cb04b873SMark J Musante 5612cb04b873SMark J Musante /* 5613cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5614cb04b873SMark J Musante * and those spares are not required, then we want to 5615cb04b873SMark J Musante * attempt to free them up now so that they can be used 5616cb04b873SMark J Musante * by other pools. Once we're back down to a single 5617cb04b873SMark J Musante * disk+spare, we stop removing them. 5618cb04b873SMark J Musante */ 5619cb04b873SMark J Musante if (vd->vdev_children > 2) { 5620cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5621cb04b873SMark J Musante 5622cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5623cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5624cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5625cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5626cb04b873SMark J Musante return (newvd); 56273d7072f8Seschrock } 56283d7072f8Seschrock } 56293d7072f8Seschrock 5630ea8dc4b6Seschrock return (NULL); 5631fa9e4066Sahrens } 5632fa9e4066Sahrens 5633ea8dc4b6Seschrock static void 56343d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5635fa9e4066Sahrens { 56368ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 56378ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5638ea8dc4b6Seschrock 56398ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5640ea8dc4b6Seschrock 56413d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 56428ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 56438ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5644ea8dc4b6Seschrock guid = vd->vdev_guid; 56458ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 56468ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 56478ad4d6ddSJeff Bonwick sguid = 0; 564899653d4eSeschrock /* 564999653d4eSeschrock * If we have just finished replacing a hot spared device, then 565099653d4eSeschrock * we need to detach the parent's first child (the original hot 565199653d4eSeschrock * spare) as well. 565299653d4eSeschrock */ 5653cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5654cb04b873SMark J Musante ppvd->vdev_children == 2) { 565599653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 56568ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 565799653d4eSeschrock } 5658b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5659b4952e17SGeorge Wilson 56608ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 56618ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5662ea8dc4b6Seschrock return; 56638ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 566499653d4eSeschrock return; 56658ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5666fa9e4066Sahrens } 5667fa9e4066Sahrens 56688ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5669fa9e4066Sahrens } 5670fa9e4066Sahrens 5671c67d9675Seschrock /* 5672b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5673c67d9675Seschrock */ 5674c67d9675Seschrock int 56756809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 56766809eb4eSEric Schrock boolean_t ispath) 5677c67d9675Seschrock { 5678c5904d13Seschrock vdev_t *vd; 5679208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5680c67d9675Seschrock 5681f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5682f9af39baSGeorge Wilson 5683b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5684c67d9675Seschrock 56856809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5686b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5687c67d9675Seschrock 56880e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5689b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 56900e34b6a7Sbonwick 56916809eb4eSEric Schrock if (ispath) { 5692208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5693208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5694208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5695208044b8SGeorge Wilson sync = B_TRUE; 5696208044b8SGeorge Wilson } 56976809eb4eSEric Schrock } else { 5698208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5699208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5700208044b8SGeorge Wilson sync = B_TRUE; 5701208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 57026809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5703208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5704208044b8SGeorge Wilson sync = B_TRUE; 5705208044b8SGeorge Wilson } 57066809eb4eSEric Schrock } 5707c67d9675Seschrock 5708208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5709c67d9675Seschrock } 5710c67d9675Seschrock 57116809eb4eSEric Schrock int 57126809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 57136809eb4eSEric Schrock { 57146809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 57156809eb4eSEric Schrock } 57166809eb4eSEric Schrock 57176809eb4eSEric Schrock int 57186809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 57196809eb4eSEric Schrock { 57206809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 57216809eb4eSEric Schrock } 57226809eb4eSEric Schrock 5723fa9e4066Sahrens /* 5724fa9e4066Sahrens * ========================================================================== 57253f9d6ad7SLin Ling * SPA Scanning 5726fa9e4066Sahrens * ========================================================================== 5727fa9e4066Sahrens */ 5728fa9e4066Sahrens 5729ea8dc4b6Seschrock int 57303f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5731fa9e4066Sahrens { 5732e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57333f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5734be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 57353f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 57363f9d6ad7SLin Ling } 5737bb8b5132Sek 57383f9d6ad7SLin Ling int 57393f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 57403f9d6ad7SLin Ling { 57413f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57423f9d6ad7SLin Ling 57433f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5744be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5745fa9e4066Sahrens 5746fa9e4066Sahrens /* 5747088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5748088f3894Sahrens * writeable leaf device, we have nothing to do. 5749fa9e4066Sahrens */ 57503f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5751088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5752088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5753ea8dc4b6Seschrock return (0); 5754ea8dc4b6Seschrock } 5755fa9e4066Sahrens 57563f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5757fa9e4066Sahrens } 5758fa9e4066Sahrens 5759ea8dc4b6Seschrock /* 5760ea8dc4b6Seschrock * ========================================================================== 5761ea8dc4b6Seschrock * SPA async task processing 5762ea8dc4b6Seschrock * ========================================================================== 5763ea8dc4b6Seschrock */ 5764ea8dc4b6Seschrock 5765ea8dc4b6Seschrock static void 57663d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5767fa9e4066Sahrens { 576849cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 576998d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 577098d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 577149cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 57721d713200SEric Schrock 57731d713200SEric Schrock /* 57741d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 57751d713200SEric Schrock * vdev_clear() as that will cause us to throw away 57761d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 57771d713200SEric Schrock * device, all of which is a waste. 57781d713200SEric Schrock */ 57791d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 57801d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 57811d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 57821d713200SEric Schrock 5783e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5784ea8dc4b6Seschrock } 578549cf58c0SBrendan Gregg - Sun Microsystems 5786e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 578749cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5788ea8dc4b6Seschrock } 5789fa9e4066Sahrens 5790e14bb325SJeff Bonwick static void 5791e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5792e14bb325SJeff Bonwick { 5793e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 579498d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5795e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5796e14bb325SJeff Bonwick } 5797e14bb325SJeff Bonwick 5798e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5799e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5800e14bb325SJeff Bonwick } 5801e14bb325SJeff Bonwick 5802573ca77eSGeorge Wilson static void 5803573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5804573ca77eSGeorge Wilson { 5805573ca77eSGeorge Wilson sysevent_id_t eid; 5806573ca77eSGeorge Wilson nvlist_t *attr; 5807573ca77eSGeorge Wilson char *physpath; 5808573ca77eSGeorge Wilson 5809573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5810573ca77eSGeorge Wilson return; 5811573ca77eSGeorge Wilson 5812573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5813573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5814573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5815573ca77eSGeorge Wilson } 5816573ca77eSGeorge Wilson 5817573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5818573ca77eSGeorge Wilson return; 5819573ca77eSGeorge Wilson 5820573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5821573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5822573ca77eSGeorge Wilson 5823573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5824573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5825573ca77eSGeorge Wilson 5826573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5827573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5828573ca77eSGeorge Wilson 5829573ca77eSGeorge Wilson nvlist_free(attr); 5830573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5831573ca77eSGeorge Wilson } 5832573ca77eSGeorge Wilson 5833ea8dc4b6Seschrock static void 5834ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5835ea8dc4b6Seschrock { 5836e14bb325SJeff Bonwick int tasks; 5837ea8dc4b6Seschrock 5838ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5839ea8dc4b6Seschrock 5840ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5841ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5842ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5843ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5844ea8dc4b6Seschrock 58450373e76bSbonwick /* 58460373e76bSbonwick * See if the config needs to be updated. 58470373e76bSbonwick */ 58480373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5849b24ab676SJeff Bonwick uint64_t old_space, new_space; 5850573ca77eSGeorge Wilson 58510373e76bSbonwick mutex_enter(&spa_namespace_lock); 5852b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 58530373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5854b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 58550373e76bSbonwick mutex_exit(&spa_namespace_lock); 5856573ca77eSGeorge Wilson 5857573ca77eSGeorge Wilson /* 5858573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5859573ca77eSGeorge Wilson * then log an internal history event. 5860573ca77eSGeorge Wilson */ 5861b24ab676SJeff Bonwick if (new_space != old_space) { 58624445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5863c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5864b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5865573ca77eSGeorge Wilson } 58660373e76bSbonwick } 58670373e76bSbonwick 5868ea8dc4b6Seschrock /* 58693d7072f8Seschrock * See if any devices need to be marked REMOVED. 5870ea8dc4b6Seschrock */ 5871e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 58728f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 58733d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5874e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 587549cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5876e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 587749cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5878e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5879e14bb325SJeff Bonwick } 5880e14bb325SJeff Bonwick 5881573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5882573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5883573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5884573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5885573ca77eSGeorge Wilson } 5886573ca77eSGeorge Wilson 5887e14bb325SJeff Bonwick /* 5888e14bb325SJeff Bonwick * See if any devices need to be probed. 5889e14bb325SJeff Bonwick */ 5890e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 58918f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5892e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5893e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 58943d7072f8Seschrock } 5895ea8dc4b6Seschrock 5896ea8dc4b6Seschrock /* 5897ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5898ea8dc4b6Seschrock */ 58993d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 59003d7072f8Seschrock spa_vdev_resilver_done(spa); 5901fa9e4066Sahrens 5902ea8dc4b6Seschrock /* 5903ea8dc4b6Seschrock * Kick off a resilver. 5904ea8dc4b6Seschrock */ 5905088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 59063f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5907ea8dc4b6Seschrock 5908ea8dc4b6Seschrock /* 5909ea8dc4b6Seschrock * Let the world know that we're done. 5910ea8dc4b6Seschrock */ 5911ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5912ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5913ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5914ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5915ea8dc4b6Seschrock thread_exit(); 5916ea8dc4b6Seschrock } 5917ea8dc4b6Seschrock 5918ea8dc4b6Seschrock void 5919ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5920ea8dc4b6Seschrock { 5921ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5922ea8dc4b6Seschrock spa->spa_async_suspended++; 5923ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5924ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5925ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5926ea8dc4b6Seschrock } 5927ea8dc4b6Seschrock 5928ea8dc4b6Seschrock void 5929ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5930ea8dc4b6Seschrock { 5931ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5932ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5933ea8dc4b6Seschrock spa->spa_async_suspended--; 5934ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5935ea8dc4b6Seschrock } 5936ea8dc4b6Seschrock 59373cb69f73SWill Andrews static boolean_t 59383cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 59393cb69f73SWill Andrews { 59403cb69f73SWill Andrews uint_t non_config_tasks; 59413cb69f73SWill Andrews uint_t config_task; 59423cb69f73SWill Andrews boolean_t config_task_suspended; 59433cb69f73SWill Andrews 59443cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 59453cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 59463cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 59473cb69f73SWill Andrews config_task_suspended = B_FALSE; 59483cb69f73SWill Andrews } else { 59493cb69f73SWill Andrews config_task_suspended = 59503cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 59513cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 59523cb69f73SWill Andrews } 59533cb69f73SWill Andrews 59543cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 59553cb69f73SWill Andrews } 59563cb69f73SWill Andrews 5957ea8dc4b6Seschrock static void 5958ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5959ea8dc4b6Seschrock { 5960ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 59613cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 59623cb69f73SWill Andrews !spa->spa_async_suspended && 59630373e76bSbonwick spa->spa_async_thread == NULL && 59643cb69f73SWill Andrews rootdir != NULL) 5965ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5966ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5967ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5968ea8dc4b6Seschrock } 5969ea8dc4b6Seschrock 5970ea8dc4b6Seschrock void 5971ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5972ea8dc4b6Seschrock { 59733f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5974ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5975ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5976ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5977fa9e4066Sahrens } 5978fa9e4066Sahrens 5979fa9e4066Sahrens /* 5980fa9e4066Sahrens * ========================================================================== 5981fa9e4066Sahrens * SPA syncing routines 5982fa9e4066Sahrens * ========================================================================== 5983fa9e4066Sahrens */ 5984fa9e4066Sahrens 5985cde58dbcSMatthew Ahrens static int 5986cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5987cde58dbcSMatthew Ahrens { 5988cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5989cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5990cde58dbcSMatthew Ahrens return (0); 5991b24ab676SJeff Bonwick } 5992b24ab676SJeff Bonwick 5993cde58dbcSMatthew Ahrens static int 5994cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5995b24ab676SJeff Bonwick { 5996b24ab676SJeff Bonwick zio_t *zio = arg; 5997b24ab676SJeff Bonwick 5998b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5999b24ab676SJeff Bonwick zio->io_flags)); 6000cde58dbcSMatthew Ahrens return (0); 6001fa9e4066Sahrens } 6002fa9e4066Sahrens 600369962b56SMatthew Ahrens /* 600469962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 600569962b56SMatthew Ahrens * amount of time spent syncing frees. 600669962b56SMatthew Ahrens */ 600769962b56SMatthew Ahrens static void 600869962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 600969962b56SMatthew Ahrens { 601069962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 601169962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 601269962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 601369962b56SMatthew Ahrens } 601469962b56SMatthew Ahrens 601569962b56SMatthew Ahrens /* 601669962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 601769962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 601869962b56SMatthew Ahrens */ 601969962b56SMatthew Ahrens static void 602069962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 602169962b56SMatthew Ahrens { 602269962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 602369962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 602469962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 602569962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 602669962b56SMatthew Ahrens } 602769962b56SMatthew Ahrens 602869962b56SMatthew Ahrens 6029fa9e4066Sahrens static void 603099653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6031fa9e4066Sahrens { 6032fa9e4066Sahrens char *packed = NULL; 6033f7991ba4STim Haley size_t bufsize; 6034fa9e4066Sahrens size_t nvsize = 0; 6035fa9e4066Sahrens dmu_buf_t *db; 6036fa9e4066Sahrens 603799653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6038fa9e4066Sahrens 6039f7991ba4STim Haley /* 6040f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 604143466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6042f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6043f7991ba4STim Haley */ 6044ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6045f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6046fa9e4066Sahrens 604799653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6048ea8dc4b6Seschrock KM_SLEEP) == 0); 6049f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6050fa9e4066Sahrens 6051f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6052fa9e4066Sahrens 6053f7991ba4STim Haley kmem_free(packed, bufsize); 6054fa9e4066Sahrens 605599653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6056fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6057fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6058ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6059fa9e4066Sahrens } 6060fa9e4066Sahrens 606199653d4eSeschrock static void 6062fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6063fa94a07fSbrendan const char *config, const char *entry) 606499653d4eSeschrock { 606599653d4eSeschrock nvlist_t *nvroot; 6066fa94a07fSbrendan nvlist_t **list; 606799653d4eSeschrock int i; 606899653d4eSeschrock 6069fa94a07fSbrendan if (!sav->sav_sync) 607099653d4eSeschrock return; 607199653d4eSeschrock 607299653d4eSeschrock /* 6073fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6074fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 60753d7072f8Seschrock * valid and the vdevs are labeled appropriately. 607699653d4eSeschrock */ 6077fa94a07fSbrendan if (sav->sav_object == 0) { 6078fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6079fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6080fa94a07fSbrendan sizeof (uint64_t), tx); 608199653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6082fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6083fa94a07fSbrendan &sav->sav_object, tx) == 0); 608499653d4eSeschrock } 608599653d4eSeschrock 608699653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6087fa94a07fSbrendan if (sav->sav_count == 0) { 6088fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 608999653d4eSeschrock } else { 6090fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6091fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6092fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 60933f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6094fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6095fa94a07fSbrendan sav->sav_count) == 0); 6096fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6097fa94a07fSbrendan nvlist_free(list[i]); 6098fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 609999653d4eSeschrock } 610099653d4eSeschrock 6101fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 610206eeb2adSek nvlist_free(nvroot); 610399653d4eSeschrock 6104fa94a07fSbrendan sav->sav_sync = B_FALSE; 610599653d4eSeschrock } 610699653d4eSeschrock 6107215198a6SJoe Stein /* 6108215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6109215198a6SJoe Stein * The all-vdev ZAP must be empty. 6110215198a6SJoe Stein */ 6111215198a6SJoe Stein static void 6112215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6113215198a6SJoe Stein { 6114215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6115215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6116215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6117215198a6SJoe Stein vd->vdev_top_zap, tx)); 6118215198a6SJoe Stein } 6119215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6120215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6121215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6122215198a6SJoe Stein } 6123215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6124215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6125215198a6SJoe Stein } 6126215198a6SJoe Stein } 6127215198a6SJoe Stein 612899653d4eSeschrock static void 612999653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 613099653d4eSeschrock { 613199653d4eSeschrock nvlist_t *config; 613299653d4eSeschrock 6133215198a6SJoe Stein /* 6134215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6135215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6136215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6137215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6138215198a6SJoe Stein */ 6139215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6140215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 614199653d4eSeschrock return; 614299653d4eSeschrock 6143e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6144e14bb325SJeff Bonwick 6145215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6146215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6147215198a6SJoe Stein 6148215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6149215198a6SJoe Stein /* Make and build the new AVZ */ 6150215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6151215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6152215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6153215198a6SJoe Stein 6154215198a6SJoe Stein /* Diff old AVZ with new one */ 6155215198a6SJoe Stein zap_cursor_t zc; 6156215198a6SJoe Stein zap_attribute_t za; 6157215198a6SJoe Stein 6158215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6159215198a6SJoe Stein spa->spa_all_vdev_zaps); 6160215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6161215198a6SJoe Stein zap_cursor_advance(&zc)) { 6162215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6163215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6164215198a6SJoe Stein vdzap) == ENOENT) { 6165215198a6SJoe Stein /* 6166215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6167215198a6SJoe Stein * destroy it 6168215198a6SJoe Stein */ 6169215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6170215198a6SJoe Stein tx)); 6171215198a6SJoe Stein } 6172215198a6SJoe Stein } 6173215198a6SJoe Stein 6174215198a6SJoe Stein zap_cursor_fini(&zc); 6175215198a6SJoe Stein 6176215198a6SJoe Stein /* Destroy the old AVZ */ 6177215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6178215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6179215198a6SJoe Stein 6180215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6181215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6182215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6183215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6184215198a6SJoe Stein 6185215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6186215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6187215198a6SJoe Stein zap_cursor_t zc; 6188215198a6SJoe Stein zap_attribute_t za; 6189215198a6SJoe Stein 6190215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6191215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6192215198a6SJoe Stein spa->spa_all_vdev_zaps); 6193215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6194215198a6SJoe Stein zap_cursor_advance(&zc)) { 6195215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6196215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6197215198a6SJoe Stein } 6198215198a6SJoe Stein 6199215198a6SJoe Stein zap_cursor_fini(&zc); 6200215198a6SJoe Stein 6201215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6202215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6203215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6204215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6205215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6206215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6207215198a6SJoe Stein } 6208215198a6SJoe Stein 6209215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6210215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6211215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6212215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6213215198a6SJoe Stein } 6214215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6215215198a6SJoe Stein 6216215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6217215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6218215198a6SJoe Stein 6219e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6220e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6221e14bb325SJeff Bonwick 622225345e46SGeorge Wilson /* 622325345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 622425345e46SGeorge Wilson * the config object gets updated with the correct version. 622525345e46SGeorge Wilson */ 622625345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 622725345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 622825345e46SGeorge Wilson spa->spa_uberblock.ub_version); 622925345e46SGeorge Wilson 6230e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 623199653d4eSeschrock 6232aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 623399653d4eSeschrock spa->spa_config_syncing = config; 623499653d4eSeschrock 623599653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 623699653d4eSeschrock } 623799653d4eSeschrock 6238ad135b5dSChristopher Siden static void 62393b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6240ad135b5dSChristopher Siden { 62413b2aab18SMatthew Ahrens uint64_t *versionp = arg; 62423b2aab18SMatthew Ahrens uint64_t version = *versionp; 62433b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6244ad135b5dSChristopher Siden 6245ad135b5dSChristopher Siden /* 6246ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6247ad135b5dSChristopher Siden */ 6248ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6249ad135b5dSChristopher Siden 625062eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6251ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6252ad135b5dSChristopher Siden 6253ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6254ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 62554445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6256ad135b5dSChristopher Siden } 6257ad135b5dSChristopher Siden 6258990b4856Slling /* 6259990b4856Slling * Set zpool properties. 6260990b4856Slling */ 6261b1b8ab34Slling static void 62623b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6263b1b8ab34Slling { 62643b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 62653b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6266b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6267ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6268b1b8ab34Slling 6269e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6270e14bb325SJeff Bonwick 6271990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6272ad135b5dSChristopher Siden uint64_t intval; 6273ad135b5dSChristopher Siden char *strval, *fname; 6274ad135b5dSChristopher Siden zpool_prop_t prop; 6275ad135b5dSChristopher Siden const char *propname; 6276ad135b5dSChristopher Siden zprop_type_t proptype; 62772acef22dSMatthew Ahrens spa_feature_t fid; 6278ad135b5dSChristopher Siden 6279990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6280ad135b5dSChristopher Siden case ZPROP_INVAL: 6281ad135b5dSChristopher Siden /* 6282ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6283ad135b5dSChristopher Siden */ 6284ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6285ad135b5dSChristopher Siden 6286ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 62872acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6288ad135b5dSChristopher Siden 62892acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 62904445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 62914445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6292ad135b5dSChristopher Siden break; 6293ad135b5dSChristopher Siden 6294990b4856Slling case ZPOOL_PROP_VERSION: 62950713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6296990b4856Slling /* 6297ad135b5dSChristopher Siden * The version is synced seperatly before other 6298ad135b5dSChristopher Siden * properties and should be correct by now. 6299990b4856Slling */ 6300ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6301ecd6cf80Smarks break; 6302990b4856Slling 6303990b4856Slling case ZPOOL_PROP_ALTROOT: 6304990b4856Slling /* 6305990b4856Slling * 'altroot' is a non-persistent property. It should 6306990b4856Slling * have been set temporarily at creation or import time. 6307990b4856Slling */ 6308990b4856Slling ASSERT(spa->spa_root != NULL); 6309b1b8ab34Slling break; 63103d7072f8Seschrock 6311f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 63122f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6313990b4856Slling /* 6314f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6315f9af39baSGeorge Wilson * properties. 6316990b4856Slling */ 63173d7072f8Seschrock break; 63188704186eSDan McDonald case ZPOOL_PROP_COMMENT: 63190713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63208704186eSDan McDonald if (spa->spa_comment != NULL) 63218704186eSDan McDonald spa_strfree(spa->spa_comment); 63228704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 63238704186eSDan McDonald /* 63248704186eSDan McDonald * We need to dirty the configuration on all the vdevs 63258704186eSDan McDonald * so that their labels get updated. It's unnecessary 63268704186eSDan McDonald * to do this for pool creation since the vdev's 63278704186eSDan McDonald * configuratoin has already been dirtied. 63288704186eSDan McDonald */ 63298704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 63308704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 63314445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63324445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 63338704186eSDan McDonald break; 6334990b4856Slling default: 6335990b4856Slling /* 6336990b4856Slling * Set pool property values in the poolprops mos object. 6337990b4856Slling */ 6338990b4856Slling if (spa->spa_pool_props_object == 0) { 6339ad135b5dSChristopher Siden spa->spa_pool_props_object = 6340ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6341990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6342ad135b5dSChristopher Siden tx); 6343990b4856Slling } 6344990b4856Slling 6345990b4856Slling /* normalize the property name */ 6346990b4856Slling propname = zpool_prop_to_name(prop); 6347990b4856Slling proptype = zpool_prop_get_type(prop); 6348990b4856Slling 6349990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6350990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 63510713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63520713e232SGeorge Wilson VERIFY0(zap_update(mos, 6353990b4856Slling spa->spa_pool_props_object, propname, 63540713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 63554445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63564445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6357990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 63580713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6359990b4856Slling 6360990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6361990b4856Slling const char *unused; 63620713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 63630713e232SGeorge Wilson prop, intval, &unused)); 6364990b4856Slling } 63650713e232SGeorge Wilson VERIFY0(zap_update(mos, 6366990b4856Slling spa->spa_pool_props_object, propname, 63670713e232SGeorge Wilson 8, 1, &intval, tx)); 63684445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63694445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6370990b4856Slling } else { 6371990b4856Slling ASSERT(0); /* not allowed */ 6372990b4856Slling } 6373990b4856Slling 63740a4e9518Sgw switch (prop) { 63750a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6376990b4856Slling spa->spa_delegation = intval; 63770a4e9518Sgw break; 63780a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6379990b4856Slling spa->spa_bootfs = intval; 63800a4e9518Sgw break; 63810a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 63820a4e9518Sgw spa->spa_failmode = intval; 63830a4e9518Sgw break; 6384573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6385573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6386b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6387b98131cfSEric Taylor spa_async_request(spa, 6388b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6389573ca77eSGeorge Wilson break; 6390b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6391b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6392b24ab676SJeff Bonwick break; 63930a4e9518Sgw default: 63940a4e9518Sgw break; 63950a4e9518Sgw } 6396990b4856Slling } 6397990b4856Slling 6398b1b8ab34Slling } 6399e14bb325SJeff Bonwick 6400e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6401b1b8ab34Slling } 6402b1b8ab34Slling 6403cde58dbcSMatthew Ahrens /* 6404cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6405cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6406cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6407cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6408cde58dbcSMatthew Ahrens * tasks. 6409cde58dbcSMatthew Ahrens */ 6410cde58dbcSMatthew Ahrens static void 6411cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6412cde58dbcSMatthew Ahrens { 6413cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6414cde58dbcSMatthew Ahrens 6415cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6416cde58dbcSMatthew Ahrens 64173b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 64183b2aab18SMatthew Ahrens 6419cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6420cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6421cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6422cde58dbcSMatthew Ahrens 6423cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6424cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6425cde58dbcSMatthew Ahrens } 6426cde58dbcSMatthew Ahrens 6427cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6428cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6429cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6430cde58dbcSMatthew Ahrens } 6431cde58dbcSMatthew Ahrens 6432cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6433cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6434cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6435cde58dbcSMatthew Ahrens 6436cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6437cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6438cde58dbcSMatthew Ahrens } 6439ad135b5dSChristopher Siden 6440ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6441ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6442ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6443ad135b5dSChristopher Siden } 6444b8289d24SDaniil Lunev 6445b8289d24SDaniil Lunev /* 6446b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6447b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6448b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6449b8289d24SDaniil Lunev * this feature active 6450b8289d24SDaniil Lunev */ 6451b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6452b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6453b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6454b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6455b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6456b8289d24SDaniil Lunev 6457b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6458b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6459b8289d24SDaniil Lunev } 646045818ee1SMatthew Ahrens 646145818ee1SMatthew Ahrens /* 646245818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 646345818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 646445818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 646545818ee1SMatthew Ahrens */ 646645818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 646745818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 646845818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 646945818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 647045818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 647145818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 647245818ee1SMatthew Ahrens } 647345818ee1SMatthew Ahrens 64743b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6475cde58dbcSMatthew Ahrens } 6476cde58dbcSMatthew Ahrens 6477fa9e4066Sahrens /* 6478fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6479fa9e4066Sahrens * part of the process, so we iterate until it converges. 6480fa9e4066Sahrens */ 6481fa9e4066Sahrens void 6482fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6483fa9e4066Sahrens { 6484fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6485fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6486b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 64870373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6488fa9e4066Sahrens vdev_t *vd; 6489fa9e4066Sahrens dmu_tx_t *tx; 6490e14bb325SJeff Bonwick int error; 64910f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 64920f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 6493fa9e4066Sahrens 6494f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6495f9af39baSGeorge Wilson 6496fa9e4066Sahrens /* 6497fa9e4066Sahrens * Lock out configuration changes. 6498fa9e4066Sahrens */ 6499e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6500fa9e4066Sahrens 6501fa9e4066Sahrens spa->spa_syncing_txg = txg; 6502fa9e4066Sahrens spa->spa_sync_pass = 0; 6503fa9e4066Sahrens 65040f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 65050f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 65060f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 65070f7643c7SGeorge Wilson 6508e14bb325SJeff Bonwick /* 6509e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6510e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6511e14bb325SJeff Bonwick */ 6512e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 65138ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 65148ad4d6ddSJeff Bonwick /* 65158ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 65168ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 65178ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 65188ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 65198ad4d6ddSJeff Bonwick * into the root vdev tree. 65208ad4d6ddSJeff Bonwick */ 65218ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65228ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 65238ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 65248ad4d6ddSJeff Bonwick vdev_state_clean(vd); 65258ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 65268ad4d6ddSJeff Bonwick } 65278ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65288ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6529e14bb325SJeff Bonwick } 6530e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6531e14bb325SJeff Bonwick 653299653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 653399653d4eSeschrock 6534283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6535283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6536283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6537283b8460SGeorge.Wilson 653899653d4eSeschrock /* 6539e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 654099653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 654199653d4eSeschrock */ 6542e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6543e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 654499653d4eSeschrock int i; 654599653d4eSeschrock 654699653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 654799653d4eSeschrock vd = rvd->vdev_child[i]; 654899653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 654999653d4eSeschrock break; 655099653d4eSeschrock } 655199653d4eSeschrock if (i == rvd->vdev_children) { 655299653d4eSeschrock spa->spa_deflate = TRUE; 655399653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 655499653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 655599653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 655699653d4eSeschrock } 655799653d4eSeschrock } 655899653d4eSeschrock 65590f7643c7SGeorge Wilson /* 65600f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 65610f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 65620f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 65630f7643c7SGeorge Wilson * out this txg. 65640f7643c7SGeorge Wilson */ 65650f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 65660f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 65670f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 65680f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 65690f7643c7SGeorge Wilson 65700f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 65710f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 65720f7643c7SGeorge Wilson continue; 65730f7643c7SGeorge Wilson 65740f7643c7SGeorge Wilson /* 65750f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 65760f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 65770f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 65780f7643c7SGeorge Wilson */ 65790f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 65800f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 65810f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 65820f7643c7SGeorge Wilson } 65830f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 65840f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 65850f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 65860f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 65870f7643c7SGeorge Wilson 65880f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 65890f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 65900f7643c7SGeorge Wilson 6591fa9e4066Sahrens /* 6592fa9e4066Sahrens * Iterate to convergence. 6593fa9e4066Sahrens */ 6594fa9e4066Sahrens do { 6595b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6596fa9e4066Sahrens 6597fa9e4066Sahrens spa_sync_config_object(spa, tx); 6598fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6599fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6600fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6601fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6602ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6603fa9e4066Sahrens dsl_pool_sync(dp, txg); 6604fa9e4066Sahrens 660501f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 660669962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6607b24ab676SJeff Bonwick } else { 6608231aab85SMatthew Ahrens /* 6609231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6610231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6611231aab85SMatthew Ahrens */ 6612231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6613cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 661469962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6615fa9e4066Sahrens } 6616fa9e4066Sahrens 6617b24ab676SJeff Bonwick ddt_sync(spa, txg); 66183f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6619afee20e4SGeorge Wilson 6620b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6621b24ab676SJeff Bonwick vdev_sync(vd, txg); 6622b24ab676SJeff Bonwick 6623231aab85SMatthew Ahrens if (pass == 1) { 6624cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6625231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6626231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6627231aab85SMatthew Ahrens /* 6628231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6629231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6630231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6631231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6632231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6633231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6634231aab85SMatthew Ahrens * data (though in this case the MOS will most 6635231aab85SMatthew Ahrens * likely also be dirty due to second order 6636231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6637231aab85SMatthew Ahrens */ 6638231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6639231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6640231aab85SMatthew Ahrens /* 6641231aab85SMatthew Ahrens * Nothing changed on the first pass, 6642231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6643231aab85SMatthew Ahrens * syncing deferred frees, so that we 6644231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6645231aab85SMatthew Ahrens */ 6646231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6647231aab85SMatthew Ahrens txg)); 6648231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6649231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6650231aab85SMatthew Ahrens break; 6651231aab85SMatthew Ahrens } 6652231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6653231aab85SMatthew Ahrens } 6654fa9e4066Sahrens 6655cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6656fa9e4066Sahrens 6657215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 6658215198a6SJoe Stein /* 6659215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 6660215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 6661215198a6SJoe Stein * called if the config is dirty; otherwise there may be 6662215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 6663215198a6SJoe Stein * spa_sync_config_object. 6664215198a6SJoe Stein */ 6665215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 6666215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 6667215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 6668215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 6669215198a6SJoe Stein all_vdev_zap_entry_count); 6670215198a6SJoe Stein } 6671215198a6SJoe Stein 6672fa9e4066Sahrens /* 6673fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6674fa9e4066Sahrens * to commit the transaction group. 66750373e76bSbonwick * 667617f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 667717f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6678e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6679e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 66800373e76bSbonwick */ 6681e14bb325SJeff Bonwick for (;;) { 6682e14bb325SJeff Bonwick /* 6683e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6684e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6685e14bb325SJeff Bonwick */ 6686e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6687e14bb325SJeff Bonwick 6688e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6689e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6690e14bb325SJeff Bonwick int svdcount = 0; 6691e14bb325SJeff Bonwick int children = rvd->vdev_children; 6692e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6693e14bb325SJeff Bonwick 6694573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6695e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6696e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6697e14bb325SJeff Bonwick continue; 6698e14bb325SJeff Bonwick svd[svdcount++] = vd; 6699e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6700e14bb325SJeff Bonwick break; 6701e14bb325SJeff Bonwick } 6702eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 6703e14bb325SJeff Bonwick } else { 6704e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 6705eb5bb584SWill Andrews rvd->vdev_children, txg); 67060373e76bSbonwick } 6707e14bb325SJeff Bonwick 6708dfbb9432SGeorge Wilson if (error == 0) 6709dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6710dfbb9432SGeorge Wilson 6711e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6712e14bb325SJeff Bonwick 6713e14bb325SJeff Bonwick if (error == 0) 6714e14bb325SJeff Bonwick break; 6715e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6716e14bb325SJeff Bonwick zio_resume_wait(spa); 67170373e76bSbonwick } 671899653d4eSeschrock dmu_tx_commit(tx); 671999653d4eSeschrock 6720283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6721283b8460SGeorge.Wilson 67220373e76bSbonwick /* 67230373e76bSbonwick * Clear the dirty config list. 6724fa9e4066Sahrens */ 6725e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 67260373e76bSbonwick vdev_config_clean(vd); 67270373e76bSbonwick 67280373e76bSbonwick /* 67290373e76bSbonwick * Now that the new config has synced transactionally, 67300373e76bSbonwick * let it become visible to the config cache. 67310373e76bSbonwick */ 67320373e76bSbonwick if (spa->spa_config_syncing != NULL) { 67330373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 67340373e76bSbonwick spa->spa_config_txg = txg; 67350373e76bSbonwick spa->spa_config_syncing = NULL; 67360373e76bSbonwick } 6737fa9e4066Sahrens 6738fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 6739fa9e4066Sahrens 6740b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6741fa9e4066Sahrens 67420f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 67430f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 67440f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 67450f7643c7SGeorge Wilson 6746fa9e4066Sahrens /* 6747fa9e4066Sahrens * Update usable space statistics. 6748fa9e4066Sahrens */ 6749fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6750fa9e4066Sahrens vdev_sync_done(vd, txg); 6751fa9e4066Sahrens 6752485bbbf5SGeorge Wilson spa_update_dspace(spa); 6753485bbbf5SGeorge Wilson 6754fa9e4066Sahrens /* 6755fa9e4066Sahrens * It had better be the case that we didn't dirty anything 675699653d4eSeschrock * since vdev_config_sync(). 6757fa9e4066Sahrens */ 6758fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6759fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6760fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6761b24ab676SJeff Bonwick 6762b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6763fa9e4066Sahrens 6764e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6765ea8dc4b6Seschrock 6766468c413aSTim Haley spa_handle_ignored_writes(spa); 6767468c413aSTim Haley 6768ea8dc4b6Seschrock /* 6769ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6770ea8dc4b6Seschrock */ 6771ea8dc4b6Seschrock spa_async_dispatch(spa); 6772fa9e4066Sahrens } 6773fa9e4066Sahrens 6774fa9e4066Sahrens /* 6775fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6776fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6777fa9e4066Sahrens * sync. 6778fa9e4066Sahrens */ 6779fa9e4066Sahrens void 6780fa9e4066Sahrens spa_sync_allpools(void) 6781fa9e4066Sahrens { 6782fa9e4066Sahrens spa_t *spa = NULL; 6783fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6784fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6785f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6786f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6787fa9e4066Sahrens continue; 6788fa9e4066Sahrens spa_open_ref(spa, FTAG); 6789fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6790fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6791fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6792fa9e4066Sahrens spa_close(spa, FTAG); 6793fa9e4066Sahrens } 6794fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6795fa9e4066Sahrens } 6796fa9e4066Sahrens 6797fa9e4066Sahrens /* 6798fa9e4066Sahrens * ========================================================================== 6799fa9e4066Sahrens * Miscellaneous routines 6800fa9e4066Sahrens * ========================================================================== 6801fa9e4066Sahrens */ 6802fa9e4066Sahrens 6803fa9e4066Sahrens /* 6804fa9e4066Sahrens * Remove all pools in the system. 6805fa9e4066Sahrens */ 6806fa9e4066Sahrens void 6807fa9e4066Sahrens spa_evict_all(void) 6808fa9e4066Sahrens { 6809fa9e4066Sahrens spa_t *spa; 6810fa9e4066Sahrens 6811fa9e4066Sahrens /* 6812fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6813fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6814fa9e4066Sahrens */ 6815fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6816fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6817fa9e4066Sahrens /* 6818ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6819ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6820ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6821fa9e4066Sahrens */ 6822fa9e4066Sahrens spa_open_ref(spa, FTAG); 6823fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6824ea8dc4b6Seschrock spa_async_suspend(spa); 6825fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6826fa9e4066Sahrens spa_close(spa, FTAG); 6827fa9e4066Sahrens 6828fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6829fa9e4066Sahrens spa_unload(spa); 6830fa9e4066Sahrens spa_deactivate(spa); 6831fa9e4066Sahrens } 6832fa9e4066Sahrens spa_remove(spa); 6833fa9e4066Sahrens } 6834fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6835fa9e4066Sahrens } 6836ea8dc4b6Seschrock 6837ea8dc4b6Seschrock vdev_t * 68386809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6839ea8dc4b6Seschrock { 6840c5904d13Seschrock vdev_t *vd; 6841c5904d13Seschrock int i; 6842c5904d13Seschrock 6843c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6844c5904d13Seschrock return (vd); 6845c5904d13Seschrock 68466809eb4eSEric Schrock if (aux) { 6847c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6848c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 68496809eb4eSEric Schrock if (vd->vdev_guid == guid) 68506809eb4eSEric Schrock return (vd); 68516809eb4eSEric Schrock } 68526809eb4eSEric Schrock 68536809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 68546809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6855c5904d13Seschrock if (vd->vdev_guid == guid) 6856c5904d13Seschrock return (vd); 6857c5904d13Seschrock } 6858c5904d13Seschrock } 6859c5904d13Seschrock 6860c5904d13Seschrock return (NULL); 6861ea8dc4b6Seschrock } 6862eaca9bbdSeschrock 6863eaca9bbdSeschrock void 6864990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6865eaca9bbdSeschrock { 6866f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6867f9af39baSGeorge Wilson 6868e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6869eaca9bbdSeschrock 6870eaca9bbdSeschrock /* 6871eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6872eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6873eaca9bbdSeschrock * possible. 6874eaca9bbdSeschrock */ 687562eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 68765d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6877eaca9bbdSeschrock 6878990b4856Slling spa->spa_uberblock.ub_version = version; 6879eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6880eaca9bbdSeschrock 6881e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 688299653d4eSeschrock 688399653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 688499653d4eSeschrock } 688599653d4eSeschrock 688699653d4eSeschrock boolean_t 688799653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 688899653d4eSeschrock { 688999653d4eSeschrock int i; 689039c23413Seschrock uint64_t spareguid; 6891fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 689299653d4eSeschrock 6893fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6894fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 689599653d4eSeschrock return (B_TRUE); 689699653d4eSeschrock 6897fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6898fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6899fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 690039c23413Seschrock return (B_TRUE); 690139c23413Seschrock } 690239c23413Seschrock 690399653d4eSeschrock return (B_FALSE); 6904eaca9bbdSeschrock } 6905b1b8ab34Slling 690689a89ebfSlling /* 690789a89ebfSlling * Check if a pool has an active shared spare device. 690889a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 690989a89ebfSlling */ 691089a89ebfSlling static boolean_t 691189a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 691289a89ebfSlling { 691389a89ebfSlling int i, refcnt; 691489a89ebfSlling uint64_t pool; 691589a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 691689a89ebfSlling 691789a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 691889a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 691989a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 692089a89ebfSlling refcnt > 2) 692189a89ebfSlling return (B_TRUE); 692289a89ebfSlling } 692389a89ebfSlling 692489a89ebfSlling return (B_FALSE); 692589a89ebfSlling } 692689a89ebfSlling 6927b72b6bb1SAlan Somers static sysevent_t * 6928b72b6bb1SAlan Somers spa_event_create(spa_t *spa, vdev_t *vd, const char *name) 69293d7072f8Seschrock { 6930b72b6bb1SAlan Somers sysevent_t *ev = NULL; 69313d7072f8Seschrock #ifdef _KERNEL 69323d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 69333d7072f8Seschrock sysevent_value_t value; 69343d7072f8Seschrock 69353d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 69363d7072f8Seschrock SE_SLEEP); 6937b72b6bb1SAlan Somers ASSERT(ev != NULL); 69383d7072f8Seschrock 69393d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69403d7072f8Seschrock value.value.sv_string = spa_name(spa); 69413d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 69423d7072f8Seschrock goto done; 69433d7072f8Seschrock 69443d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69453d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 69463d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 69473d7072f8Seschrock goto done; 69483d7072f8Seschrock 69493d7072f8Seschrock if (vd) { 69503d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69513d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 69523d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 69533d7072f8Seschrock SE_SLEEP) != 0) 69543d7072f8Seschrock goto done; 69553d7072f8Seschrock 69563d7072f8Seschrock if (vd->vdev_path) { 69573d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69583d7072f8Seschrock value.value.sv_string = vd->vdev_path; 69593d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 69603d7072f8Seschrock &value, SE_SLEEP) != 0) 69613d7072f8Seschrock goto done; 69623d7072f8Seschrock } 69633d7072f8Seschrock } 69643d7072f8Seschrock 6965b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6966b01c3b58Seschrock goto done; 6967b01c3b58Seschrock attr = NULL; 6968b01c3b58Seschrock 69693d7072f8Seschrock done: 69703d7072f8Seschrock if (attr) 69713d7072f8Seschrock sysevent_free_attr(attr); 6972b72b6bb1SAlan Somers 6973b72b6bb1SAlan Somers #endif 6974b72b6bb1SAlan Somers return (ev); 6975b72b6bb1SAlan Somers } 6976b72b6bb1SAlan Somers 6977b72b6bb1SAlan Somers static void 6978b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 6979b72b6bb1SAlan Somers { 6980b72b6bb1SAlan Somers #ifdef _KERNEL 6981b72b6bb1SAlan Somers sysevent_id_t eid; 6982b72b6bb1SAlan Somers 6983b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 69843d7072f8Seschrock sysevent_free(ev); 69853d7072f8Seschrock #endif 69863d7072f8Seschrock } 6987b72b6bb1SAlan Somers 6988b72b6bb1SAlan Somers /* 6989b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 6990b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 6991b72b6bb1SAlan Somers * filled in from the spa and (optionally) the vdev. This doesn't do anything 6992b72b6bb1SAlan Somers * in the userland libzpool, as we don't want consumers to misinterpret ztest 6993b72b6bb1SAlan Somers * or zdb as real changes. 6994b72b6bb1SAlan Somers */ 6995b72b6bb1SAlan Somers void 6996b72b6bb1SAlan Somers spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 6997b72b6bb1SAlan Somers { 6998b72b6bb1SAlan Somers spa_event_post(spa_event_create(spa, vd, name)); 6999b72b6bb1SAlan Somers } 7000