1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 2398d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24b7b2590dSMatthew Ahrens * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 2514372834SHans Rosenfeld * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. 26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2745818ee1SMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved. 28c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 29c8811bd3SToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com> 30ce1577b0SDave Eddy * Copyright 2017 Joyent, Inc. 31*1702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 325aeb9474SGarrett D'Amore */ 33fa9e4066Sahrens 34fa9e4066Sahrens /* 353e30c24aSWill Andrews * SPA: Storage Pool Allocator 363e30c24aSWill Andrews * 37fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 38fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 39fa9e4066Sahrens * pool. 40fa9e4066Sahrens */ 41fa9e4066Sahrens 42fa9e4066Sahrens #include <sys/zfs_context.h> 43ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 44fa9e4066Sahrens #include <sys/spa_impl.h> 45fa9e4066Sahrens #include <sys/zio.h> 46fa9e4066Sahrens #include <sys/zio_checksum.h> 47fa9e4066Sahrens #include <sys/dmu.h> 48fa9e4066Sahrens #include <sys/dmu_tx.h> 49fa9e4066Sahrens #include <sys/zap.h> 50fa9e4066Sahrens #include <sys/zil.h> 51b24ab676SJeff Bonwick #include <sys/ddt.h> 52fa9e4066Sahrens #include <sys/vdev_impl.h> 53fa9e4066Sahrens #include <sys/metaslab.h> 5488ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 55fa9e4066Sahrens #include <sys/uberblock_impl.h> 56fa9e4066Sahrens #include <sys/txg.h> 57fa9e4066Sahrens #include <sys/avl.h> 58fa9e4066Sahrens #include <sys/dmu_traverse.h> 59b1b8ab34Slling #include <sys/dmu_objset.h> 60fa9e4066Sahrens #include <sys/unique.h> 61fa9e4066Sahrens #include <sys/dsl_pool.h> 62b1b8ab34Slling #include <sys/dsl_dataset.h> 63fa9e4066Sahrens #include <sys/dsl_dir.h> 64fa9e4066Sahrens #include <sys/dsl_prop.h> 65b1b8ab34Slling #include <sys/dsl_synctask.h> 66fa9e4066Sahrens #include <sys/fs/zfs.h> 67fa94a07fSbrendan #include <sys/arc.h> 68fa9e4066Sahrens #include <sys/callb.h> 6995173954Sek #include <sys/systeminfo.h> 70e7cbe64fSgw #include <sys/spa_boot.h> 71573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 723f9d6ad7SLin Ling #include <sys/dsl_scan.h> 73ad135b5dSChristopher Siden #include <sys/zfeature.h> 743b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 75770499e1SDan Kimmel #include <sys/abd.h> 76fa9e4066Sahrens 775679c89fSjv #ifdef _KERNEL 78dedec472SJack Meng #include <sys/bootprops.h> 7935a5a358SJonathan Adams #include <sys/callb.h> 8035a5a358SJonathan Adams #include <sys/cpupart.h> 8135a5a358SJonathan Adams #include <sys/pool.h> 8235a5a358SJonathan Adams #include <sys/sysdc.h> 8335a5a358SJonathan Adams #include <sys/zone.h> 845679c89fSjv #endif /* _KERNEL */ 855679c89fSjv 86990b4856Slling #include "zfs_prop.h" 87b7b97454Sperrin #include "zfs_comutil.h" 88990b4856Slling 893cb69f73SWill Andrews /* 903cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 913cb69f73SWill Andrews * should be retried. 923cb69f73SWill Andrews */ 933cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300; 943cb69f73SWill Andrews 9535a5a358SJonathan Adams typedef enum zti_modes { 96ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 97ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 98ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 99ec94d322SAdam Leventhal ZTI_NMODES 10035a5a358SJonathan Adams } zti_modes_t; 101416e0cd8Sek 102ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 103ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 104ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1052e0c549eSJonathan Adams 106ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 107ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1082e0c549eSJonathan Adams 1092e0c549eSJonathan Adams typedef struct zio_taskq_info { 110ec94d322SAdam Leventhal zti_modes_t zti_mode; 11180eb36f2SGeorge Wilson uint_t zti_value; 112ec94d322SAdam Leventhal uint_t zti_count; 1132e0c549eSJonathan Adams } zio_taskq_info_t; 1142e0c549eSJonathan Adams 1152e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 11635a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1172e0c549eSJonathan Adams }; 1182e0c549eSJonathan Adams 11980eb36f2SGeorge Wilson /* 120ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 121ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 122ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 123ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 124ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 125ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 126ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 127ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 128ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 129ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 130ec94d322SAdam Leventhal * particular taskq is chosen at random. 131ec94d322SAdam Leventhal * 132ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 133ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 134ec94d322SAdam Leventhal * need to be handled with minimum delay. 13580eb36f2SGeorge Wilson */ 13680eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 13780eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 138ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1391b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 140ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 141ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 142ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 143ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1442e0c549eSJonathan Adams }; 1452e0c549eSJonathan Adams 146ce1577b0SDave Eddy static sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, 147ce1577b0SDave Eddy const char *name); 148b72b6bb1SAlan Somers static void spa_event_post(sysevent_t *ev); 1493b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1503b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 15189a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1521195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1531195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1541195e687SMark J Musante char **ereport); 155cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 156990b4856Slling 15769962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15835a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 15935a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 16035a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 16135a5a358SJonathan Adams 16235a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 16301f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16435a5a358SJonathan Adams 16535a5a358SJonathan Adams /* 16635a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 16735a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 16835a5a358SJonathan Adams */ 16935a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 17035a5a358SJonathan Adams 171990b4856Slling /* 172990b4856Slling * ========================================================================== 173990b4856Slling * SPA properties routines 174990b4856Slling * ========================================================================== 175990b4856Slling */ 176990b4856Slling 177990b4856Slling /* 178990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 179990b4856Slling */ 1809d82f4f6Slling static void 181990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 182990b4856Slling uint64_t intval, zprop_source_t src) 183990b4856Slling { 184990b4856Slling const char *propname = zpool_prop_to_name(prop); 185990b4856Slling nvlist_t *propval; 186990b4856Slling 1879d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1889d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 189990b4856Slling 1909d82f4f6Slling if (strval != NULL) 1919d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1929d82f4f6Slling else 1939d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 194990b4856Slling 1959d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 196990b4856Slling nvlist_free(propval); 197990b4856Slling } 198990b4856Slling 199990b4856Slling /* 200990b4856Slling * Get property values from the spa configuration. 201990b4856Slling */ 2029d82f4f6Slling static void 203990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 204990b4856Slling { 2054263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 206ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2072e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 208990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 209c5904d13Seschrock spa_config_dirent_t *dp; 2102e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 211990b4856Slling 212e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 213e14bb325SJeff Bonwick 2144263d13fSGeorge Wilson if (rvd != NULL) { 215485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 216b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 217379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 218379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 219485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 220485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 221485bbbf5SGeorge Wilson size - alloc, src); 2224263d13fSGeorge Wilson 2232e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2242e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2252e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2262e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 227f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 228f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 229379c004dSEric Schrock 230485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 231379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 232379c004dSEric Schrock 233b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 234b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 235b24ab676SJeff Bonwick 236379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2374263d13fSGeorge Wilson rvd->vdev_state, src); 238379c004dSEric Schrock 239379c004dSEric Schrock version = spa_version(spa); 240379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 241379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 242379c004dSEric Schrock else 243379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 244379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 245379c004dSEric Schrock } 246990b4856Slling 247ad135b5dSChristopher Siden if (pool != NULL) { 248ad135b5dSChristopher Siden /* 249ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 250ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 251ad135b5dSChristopher Siden */ 2527fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 253ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 254c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 255c1379625SJustin T. Gibbs src); 256ad135b5dSChristopher Siden } else { 257ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 258ad135b5dSChristopher Siden NULL, 0, src); 259ad135b5dSChristopher Siden } 2607fd05ac4SMatthew Ahrens 2617fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2627fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 263c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 264c1379625SJustin T. Gibbs src); 2657fd05ac4SMatthew Ahrens } else { 2667fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2677fd05ac4SMatthew Ahrens NULL, 0, src); 2687fd05ac4SMatthew Ahrens } 269ad135b5dSChristopher Siden } 270ad135b5dSChristopher Siden 2719d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 272990b4856Slling 2738704186eSDan McDonald if (spa->spa_comment != NULL) { 2748704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2758704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2768704186eSDan McDonald } 2778704186eSDan McDonald 2789d82f4f6Slling if (spa->spa_root != NULL) 2799d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2809d82f4f6Slling 0, ZPROP_SRC_LOCAL); 281990b4856Slling 282b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 283b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 284b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 285b5152584SMatthew Ahrens } else { 286b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 287b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 288b5152584SMatthew Ahrens } 289b5152584SMatthew Ahrens 290c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 291c5904d13Seschrock if (dp->scd_path == NULL) { 2929d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 293c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 294c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2959d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 296c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2972f8aaab3Seschrock } 2982f8aaab3Seschrock } 299990b4856Slling } 300990b4856Slling 301990b4856Slling /* 302990b4856Slling * Get zpool property values. 303990b4856Slling */ 304990b4856Slling int 305990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 306990b4856Slling { 307b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 308990b4856Slling zap_cursor_t zc; 309990b4856Slling zap_attribute_t za; 310990b4856Slling int err; 311990b4856Slling 3129d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 313990b4856Slling 314e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 315e14bb325SJeff Bonwick 316990b4856Slling /* 317990b4856Slling * Get properties from the spa config. 318990b4856Slling */ 3199d82f4f6Slling spa_prop_get_config(spa, nvp); 320990b4856Slling 321990b4856Slling /* If no pool property object, no more prop to get. */ 322afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 323990b4856Slling mutex_exit(&spa->spa_props_lock); 324990b4856Slling return (0); 325990b4856Slling } 326990b4856Slling 327990b4856Slling /* 328990b4856Slling * Get properties from the MOS pool property object. 329990b4856Slling */ 330990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 331990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 332990b4856Slling zap_cursor_advance(&zc)) { 333990b4856Slling uint64_t intval = 0; 334990b4856Slling char *strval = NULL; 335990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 336990b4856Slling zpool_prop_t prop; 337990b4856Slling 338990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 339990b4856Slling continue; 340990b4856Slling 341990b4856Slling switch (za.za_integer_length) { 342990b4856Slling case 8: 343990b4856Slling /* integer property */ 344990b4856Slling if (za.za_first_integer != 345990b4856Slling zpool_prop_default_numeric(prop)) 346990b4856Slling src = ZPROP_SRC_LOCAL; 347990b4856Slling 348990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 349990b4856Slling dsl_pool_t *dp; 350990b4856Slling dsl_dataset_t *ds = NULL; 351990b4856Slling 352990b4856Slling dp = spa_get_dsl(spa); 3533b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 354745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 355745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3563b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 357990b4856Slling break; 358990b4856Slling } 359990b4856Slling 3609adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 361990b4856Slling KM_SLEEP); 362990b4856Slling dsl_dataset_name(ds, strval); 363745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3643b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 365990b4856Slling } else { 366990b4856Slling strval = NULL; 367990b4856Slling intval = za.za_first_integer; 368990b4856Slling } 369990b4856Slling 3709d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 371990b4856Slling 372990b4856Slling if (strval != NULL) 3739adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 374990b4856Slling 375990b4856Slling break; 376990b4856Slling 377990b4856Slling case 1: 378990b4856Slling /* string property */ 379990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 380990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 381990b4856Slling za.za_name, 1, za.za_num_integers, strval); 382990b4856Slling if (err) { 383990b4856Slling kmem_free(strval, za.za_num_integers); 384990b4856Slling break; 385990b4856Slling } 3869d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 387990b4856Slling kmem_free(strval, za.za_num_integers); 388990b4856Slling break; 389990b4856Slling 390990b4856Slling default: 391990b4856Slling break; 392990b4856Slling } 393990b4856Slling } 394990b4856Slling zap_cursor_fini(&zc); 395990b4856Slling mutex_exit(&spa->spa_props_lock); 396990b4856Slling out: 397990b4856Slling if (err && err != ENOENT) { 398990b4856Slling nvlist_free(*nvp); 3999d82f4f6Slling *nvp = NULL; 400990b4856Slling return (err); 401990b4856Slling } 402990b4856Slling 403990b4856Slling return (0); 404990b4856Slling } 405990b4856Slling 406990b4856Slling /* 407990b4856Slling * Validate the given pool properties nvlist and modify the list 408990b4856Slling * for the property values to be set. 409990b4856Slling */ 410990b4856Slling static int 411990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 412990b4856Slling { 413990b4856Slling nvpair_t *elem; 414990b4856Slling int error = 0, reset_bootfs = 0; 415d5285caeSGeorge Wilson uint64_t objnum = 0; 416ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 417990b4856Slling 418990b4856Slling elem = NULL; 419990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 420990b4856Slling uint64_t intval; 421ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 422ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 423ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 424ad135b5dSChristopher Siden 425ad135b5dSChristopher Siden switch (prop) { 426ad135b5dSChristopher Siden case ZPROP_INVAL: 427ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 428be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 429ad135b5dSChristopher Siden break; 430ad135b5dSChristopher Siden } 431990b4856Slling 432ad135b5dSChristopher Siden /* 433ad135b5dSChristopher Siden * Sanitize the input. 434ad135b5dSChristopher Siden */ 435ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 436be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 437ad135b5dSChristopher Siden break; 438ad135b5dSChristopher Siden } 439990b4856Slling 440ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 441be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 442ad135b5dSChristopher Siden break; 443ad135b5dSChristopher Siden } 444ad135b5dSChristopher Siden 445ad135b5dSChristopher Siden if (intval != 0) { 446be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 447ad135b5dSChristopher Siden break; 448ad135b5dSChristopher Siden } 449ad135b5dSChristopher Siden 450ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 451ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 452be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 453ad135b5dSChristopher Siden break; 454ad135b5dSChristopher Siden } 455ad135b5dSChristopher Siden 456ad135b5dSChristopher Siden has_feature = B_TRUE; 457ad135b5dSChristopher Siden break; 458990b4856Slling 459990b4856Slling case ZPOOL_PROP_VERSION: 460990b4856Slling error = nvpair_value_uint64(elem, &intval); 461990b4856Slling if (!error && 462ad135b5dSChristopher Siden (intval < spa_version(spa) || 463ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 464ad135b5dSChristopher Siden has_feature)) 465be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 466990b4856Slling break; 467990b4856Slling 468990b4856Slling case ZPOOL_PROP_DELEGATION: 469990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 470d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 471573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 472990b4856Slling error = nvpair_value_uint64(elem, &intval); 473990b4856Slling if (!error && intval > 1) 474be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 475990b4856Slling break; 476990b4856Slling 477990b4856Slling case ZPOOL_PROP_BOOTFS: 47825f89ee2SJeff Bonwick /* 47925f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 48025f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 48125f89ee2SJeff Bonwick * the bootfs property cannot be set. 48225f89ee2SJeff Bonwick */ 483990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 484be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 485990b4856Slling break; 486990b4856Slling } 487990b4856Slling 488990b4856Slling /* 48915e6edf1Sgw * Make sure the vdev config is bootable 490990b4856Slling */ 49115e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 492be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 493990b4856Slling break; 494990b4856Slling } 495990b4856Slling 496990b4856Slling reset_bootfs = 1; 497990b4856Slling 498990b4856Slling error = nvpair_value_string(elem, &strval); 499990b4856Slling 500990b4856Slling if (!error) { 501ad135b5dSChristopher Siden objset_t *os; 502b5152584SMatthew Ahrens uint64_t propval; 50315e6edf1Sgw 504990b4856Slling if (strval == NULL || strval[0] == '\0') { 505990b4856Slling objnum = zpool_prop_default_numeric( 506990b4856Slling ZPOOL_PROP_BOOTFS); 507990b4856Slling break; 508990b4856Slling } 509990b4856Slling 510503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 511990b4856Slling break; 51215e6edf1Sgw 513b5152584SMatthew Ahrens /* 514b5152584SMatthew Ahrens * Must be ZPL, and its property settings 515b5152584SMatthew Ahrens * must be supported by GRUB (compression 516b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 517b5152584SMatthew Ahrens */ 518503ad85cSMatthew Ahrens 519503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 520be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5213b2aab18SMatthew Ahrens } else if ((error = 5223b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 52315e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 524b5152584SMatthew Ahrens &propval)) == 0 && 525b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 526b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 52715e6edf1Sgw } else { 52815e6edf1Sgw objnum = dmu_objset_id(os); 52915e6edf1Sgw } 530503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 531990b4856Slling } 532990b4856Slling break; 533e14bb325SJeff Bonwick 5340a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5350a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5360a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5370a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 538be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5390a4e9518Sgw 5400a4e9518Sgw /* 5410a4e9518Sgw * This is a special case which only occurs when 5420a4e9518Sgw * the pool has completely failed. This allows 5430a4e9518Sgw * the user to change the in-core failmode property 5440a4e9518Sgw * without syncing it out to disk (I/Os might 5450a4e9518Sgw * currently be blocked). We do this by returning 5460a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5470a4e9518Sgw * into thinking we encountered a property validation 5480a4e9518Sgw * error. 5490a4e9518Sgw */ 550e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5510a4e9518Sgw spa->spa_failmode = intval; 552be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5530a4e9518Sgw } 5540a4e9518Sgw break; 5552f8aaab3Seschrock 5562f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5572f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5582f8aaab3Seschrock break; 5592f8aaab3Seschrock 5602f8aaab3Seschrock if (strval[0] == '\0') 5612f8aaab3Seschrock break; 5622f8aaab3Seschrock 5632f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5642f8aaab3Seschrock break; 5652f8aaab3Seschrock 5662f8aaab3Seschrock if (strval[0] != '/') { 567be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5682f8aaab3Seschrock break; 5692f8aaab3Seschrock } 5702f8aaab3Seschrock 5712f8aaab3Seschrock slash = strrchr(strval, '/'); 5722f8aaab3Seschrock ASSERT(slash != NULL); 5732f8aaab3Seschrock 5742f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5752f8aaab3Seschrock strcmp(slash, "/..") == 0) 576be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5772f8aaab3Seschrock break; 578b24ab676SJeff Bonwick 5798704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5808704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5818704186eSDan McDonald break; 5828704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5838704186eSDan McDonald /* 5848704186eSDan McDonald * The kernel doesn't have an easy isprint() 5858704186eSDan McDonald * check. For this kernel check, we merely 5868704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5878704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5888704186eSDan McDonald */ 5898704186eSDan McDonald if (*check >= 0x7f) { 590be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5918704186eSDan McDonald break; 5928704186eSDan McDonald } 5938704186eSDan McDonald } 5948704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5958704186eSDan McDonald error = E2BIG; 5968704186eSDan McDonald break; 5978704186eSDan McDonald 598b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 599b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 600be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 601b24ab676SJeff Bonwick else 602b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 603b24ab676SJeff Bonwick if (error == 0 && 604b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 605be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 606b24ab676SJeff Bonwick break; 607990b4856Slling } 608990b4856Slling 609990b4856Slling if (error) 610990b4856Slling break; 611990b4856Slling } 612990b4856Slling 613990b4856Slling if (!error && reset_bootfs) { 614990b4856Slling error = nvlist_remove(props, 615990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 616990b4856Slling 617990b4856Slling if (!error) { 618990b4856Slling error = nvlist_add_uint64(props, 619990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 620990b4856Slling } 621990b4856Slling } 622990b4856Slling 623990b4856Slling return (error); 624990b4856Slling } 625990b4856Slling 626379c004dSEric Schrock void 627379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 628379c004dSEric Schrock { 629379c004dSEric Schrock char *cachefile; 630379c004dSEric Schrock spa_config_dirent_t *dp; 631379c004dSEric Schrock 632379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 633379c004dSEric Schrock &cachefile) != 0) 634379c004dSEric Schrock return; 635379c004dSEric Schrock 636379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 637379c004dSEric Schrock KM_SLEEP); 638379c004dSEric Schrock 639379c004dSEric Schrock if (cachefile[0] == '\0') 640379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 641379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 642379c004dSEric Schrock dp->scd_path = NULL; 643379c004dSEric Schrock else 644379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 645379c004dSEric Schrock 646379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 647379c004dSEric Schrock if (need_sync) 648379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 649379c004dSEric Schrock } 650379c004dSEric Schrock 651990b4856Slling int 652990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 653990b4856Slling { 654990b4856Slling int error; 655ad135b5dSChristopher Siden nvpair_t *elem = NULL; 656379c004dSEric Schrock boolean_t need_sync = B_FALSE; 657990b4856Slling 658990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 659990b4856Slling return (error); 660990b4856Slling 661379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 662ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 663379c004dSEric Schrock 664f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 665f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 666f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 667379c004dSEric Schrock continue; 668379c004dSEric Schrock 669ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 670ad135b5dSChristopher Siden uint64_t ver; 671ad135b5dSChristopher Siden 672ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 673ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 674ad135b5dSChristopher Siden } else { 675ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 676ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 677ad135b5dSChristopher Siden need_sync = B_TRUE; 678ad135b5dSChristopher Siden } 679ad135b5dSChristopher Siden 680ad135b5dSChristopher Siden /* Save time if the version is already set. */ 681ad135b5dSChristopher Siden if (ver == spa_version(spa)) 682ad135b5dSChristopher Siden continue; 683ad135b5dSChristopher Siden 684ad135b5dSChristopher Siden /* 685ad135b5dSChristopher Siden * In addition to the pool directory object, we might 686ad135b5dSChristopher Siden * create the pool properties object, the features for 687ad135b5dSChristopher Siden * read object, the features for write object, or the 688ad135b5dSChristopher Siden * feature descriptions object. 689ad135b5dSChristopher Siden */ 6903b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6917d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6927d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 693ad135b5dSChristopher Siden if (error) 694ad135b5dSChristopher Siden return (error); 695ad135b5dSChristopher Siden continue; 696ad135b5dSChristopher Siden } 697ad135b5dSChristopher Siden 698379c004dSEric Schrock need_sync = B_TRUE; 699379c004dSEric Schrock break; 700379c004dSEric Schrock } 701379c004dSEric Schrock 702ad135b5dSChristopher Siden if (need_sync) { 7033b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7047d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 705ad135b5dSChristopher Siden } 706ad135b5dSChristopher Siden 707ad135b5dSChristopher Siden return (0); 708990b4856Slling } 709990b4856Slling 710990b4856Slling /* 711990b4856Slling * If the bootfs property value is dsobj, clear it. 712990b4856Slling */ 713990b4856Slling void 714990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 715990b4856Slling { 716990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 717990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 718990b4856Slling spa->spa_pool_props_object, 719990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 720990b4856Slling spa->spa_bootfs = 0; 721990b4856Slling } 722990b4856Slling } 723990b4856Slling 724dfbb9432SGeorge Wilson /*ARGSUSED*/ 725dfbb9432SGeorge Wilson static int 7263b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 727dfbb9432SGeorge Wilson { 7283b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7293b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 730dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 731dfbb9432SGeorge Wilson uint64_t vdev_state; 732dfbb9432SGeorge Wilson 733dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 734dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 735dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 736dfbb9432SGeorge Wilson 737dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 738be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 739dfbb9432SGeorge Wilson 740dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 741dfbb9432SGeorge Wilson 742dfbb9432SGeorge Wilson return (0); 743dfbb9432SGeorge Wilson } 744dfbb9432SGeorge Wilson 745dfbb9432SGeorge Wilson static void 7463b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 747dfbb9432SGeorge Wilson { 7483b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7493b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 750dfbb9432SGeorge Wilson uint64_t oldguid; 751dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 752dfbb9432SGeorge Wilson 753dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 754dfbb9432SGeorge Wilson 755dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 756dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 757dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 758dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 759dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 760dfbb9432SGeorge Wilson 76120128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 762dfbb9432SGeorge Wilson oldguid, *newguid); 763dfbb9432SGeorge Wilson } 764dfbb9432SGeorge Wilson 765e9103aaeSGarrett D'Amore /* 766e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 767e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 768e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 769e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 770e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 771e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 772e9103aaeSGarrett D'Amore * sysevent to update any watchers. 773e9103aaeSGarrett D'Amore */ 774e9103aaeSGarrett D'Amore int 775e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 776e9103aaeSGarrett D'Amore { 777dfbb9432SGeorge Wilson int error; 778dfbb9432SGeorge Wilson uint64_t guid; 779e9103aaeSGarrett D'Amore 7802c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 781dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 782dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 783e9103aaeSGarrett D'Amore 7843b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7857d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 786e9103aaeSGarrett D'Amore 787dfbb9432SGeorge Wilson if (error == 0) { 788dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 789ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 790dfbb9432SGeorge Wilson } 791e9103aaeSGarrett D'Amore 792dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7932c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 794e9103aaeSGarrett D'Amore 795dfbb9432SGeorge Wilson return (error); 796e9103aaeSGarrett D'Amore } 797e9103aaeSGarrett D'Amore 798fa9e4066Sahrens /* 799fa9e4066Sahrens * ========================================================================== 800fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 801fa9e4066Sahrens * ========================================================================== 802fa9e4066Sahrens */ 803fa9e4066Sahrens 804ea8dc4b6Seschrock static int 805ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 806ea8dc4b6Seschrock { 807ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 808ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 809ea8dc4b6Seschrock int ret; 810ea8dc4b6Seschrock 811ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8127802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 813ea8dc4b6Seschrock 814ea8dc4b6Seschrock if (ret < 0) 815ea8dc4b6Seschrock return (-1); 816ea8dc4b6Seschrock else if (ret > 0) 817ea8dc4b6Seschrock return (1); 818ea8dc4b6Seschrock else 819ea8dc4b6Seschrock return (0); 820ea8dc4b6Seschrock } 821ea8dc4b6Seschrock 822ea8dc4b6Seschrock /* 823ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 824ea8dc4b6Seschrock * re-initializes them in the process. 825ea8dc4b6Seschrock */ 826ea8dc4b6Seschrock void 827ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 828ea8dc4b6Seschrock { 829ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 830ea8dc4b6Seschrock 831ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 832ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 833ea8dc4b6Seschrock 834ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 835ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 836ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 837ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 838ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 839ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 840ea8dc4b6Seschrock } 841ea8dc4b6Seschrock 842ec94d322SAdam Leventhal static void 843ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 844fa9e4066Sahrens { 845ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 846ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 847ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 848ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 849ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 850ec94d322SAdam Leventhal char name[32]; 8515aeb9474SGarrett D'Amore uint_t flags = 0; 85235a5a358SJonathan Adams boolean_t batch = B_FALSE; 853fa9e4066Sahrens 854ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 855ec94d322SAdam Leventhal tqs->stqs_count = 0; 856ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 857ec94d322SAdam Leventhal return; 858ec94d322SAdam Leventhal } 859fa9e4066Sahrens 860ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 861fa9e4066Sahrens 862ec94d322SAdam Leventhal tqs->stqs_count = count; 863ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86435a5a358SJonathan Adams 86569962b56SMatthew Ahrens switch (mode) { 86669962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86769962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86869962b56SMatthew Ahrens value = MAX(value, 1); 86969962b56SMatthew Ahrens break; 870ec94d322SAdam Leventhal 87169962b56SMatthew Ahrens case ZTI_MODE_BATCH: 87269962b56SMatthew Ahrens batch = B_TRUE; 87369962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87469962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87569962b56SMatthew Ahrens break; 876ec94d322SAdam Leventhal 87769962b56SMatthew Ahrens default: 87869962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 87969962b56SMatthew Ahrens "spa_activate()", 88069962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 88169962b56SMatthew Ahrens break; 88269962b56SMatthew Ahrens } 883ec94d322SAdam Leventhal 88469962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88569962b56SMatthew Ahrens taskq_t *tq; 886ec94d322SAdam Leventhal 887ec94d322SAdam Leventhal if (count > 1) { 888ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 889ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 890ec94d322SAdam Leventhal } else { 891ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 892ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 893ec94d322SAdam Leventhal } 894ec94d322SAdam Leventhal 895ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 896ec94d322SAdam Leventhal if (batch) 897ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 898ec94d322SAdam Leventhal 899ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 900ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 901ec94d322SAdam Leventhal } else { 90269962b56SMatthew Ahrens pri_t pri = maxclsyspri; 90369962b56SMatthew Ahrens /* 90469962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90569962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90669962b56SMatthew Ahrens * than the other taskqs. 90769962b56SMatthew Ahrens */ 90869962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 90969962b56SMatthew Ahrens pri--; 91069962b56SMatthew Ahrens 91169962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 912ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 913ec94d322SAdam Leventhal } 914ec94d322SAdam Leventhal 915ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 916ec94d322SAdam Leventhal } 917ec94d322SAdam Leventhal } 918ec94d322SAdam Leventhal 919ec94d322SAdam Leventhal static void 920ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 921ec94d322SAdam Leventhal { 922ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 923ec94d322SAdam Leventhal 924ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 925ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 926ec94d322SAdam Leventhal return; 927ec94d322SAdam Leventhal } 928ec94d322SAdam Leventhal 929ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 930ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 931ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 93235a5a358SJonathan Adams } 93335a5a358SJonathan Adams 934ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 935ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 936ec94d322SAdam Leventhal } 93735a5a358SJonathan Adams 938ec94d322SAdam Leventhal /* 939ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 940ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 941ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 942ec94d322SAdam Leventhal * the low bits of gethrtime(). 943ec94d322SAdam Leventhal */ 944ec94d322SAdam Leventhal void 945ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 946ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 947ec94d322SAdam Leventhal { 948ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 949ec94d322SAdam Leventhal taskq_t *tq; 950ec94d322SAdam Leventhal 951ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 952ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 953ec94d322SAdam Leventhal 954ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 955ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 956ec94d322SAdam Leventhal } else { 957ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95835a5a358SJonathan Adams } 959ec94d322SAdam Leventhal 960ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 96135a5a358SJonathan Adams } 96235a5a358SJonathan Adams 96335a5a358SJonathan Adams static void 96435a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96535a5a358SJonathan Adams { 966e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 967e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 968ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 96935a5a358SJonathan Adams } 97035a5a358SJonathan Adams } 97135a5a358SJonathan Adams } 97235a5a358SJonathan Adams 97335a5a358SJonathan Adams #ifdef _KERNEL 97435a5a358SJonathan Adams static void 97535a5a358SJonathan Adams spa_thread(void *arg) 97635a5a358SJonathan Adams { 97735a5a358SJonathan Adams callb_cpr_t cprinfo; 9782e0c549eSJonathan Adams 97935a5a358SJonathan Adams spa_t *spa = arg; 98035a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9812e0c549eSJonathan Adams 98235a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 98335a5a358SJonathan Adams spa->spa_name); 9842e0c549eSJonathan Adams 98535a5a358SJonathan Adams ASSERT(curproc != &p0); 98635a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98735a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98835a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9892e0c549eSJonathan Adams 99035a5a358SJonathan Adams /* bind this thread to the requested psrset */ 99135a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 99235a5a358SJonathan Adams pool_lock(); 99335a5a358SJonathan Adams mutex_enter(&cpu_lock); 99435a5a358SJonathan Adams mutex_enter(&pidlock); 99535a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99680eb36f2SGeorge Wilson 99735a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99835a5a358SJonathan Adams 0, NULL, NULL) == 0) { 99935a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 100035a5a358SJonathan Adams } else { 100135a5a358SJonathan Adams cmn_err(CE_WARN, 100235a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 100335a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100435a5a358SJonathan Adams } 100535a5a358SJonathan Adams 100635a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100735a5a358SJonathan Adams mutex_exit(&pidlock); 100835a5a358SJonathan Adams mutex_exit(&cpu_lock); 100935a5a358SJonathan Adams pool_unlock(); 101035a5a358SJonathan Adams } 101135a5a358SJonathan Adams 101235a5a358SJonathan Adams if (zio_taskq_sysdc) { 101335a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101435a5a358SJonathan Adams } 101535a5a358SJonathan Adams 101635a5a358SJonathan Adams spa->spa_proc = curproc; 101735a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101835a5a358SJonathan Adams 101935a5a358SJonathan Adams spa_create_zio_taskqs(spa); 102035a5a358SJonathan Adams 102135a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 102235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 102335a5a358SJonathan Adams 102435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102535a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102635a5a358SJonathan Adams 102735a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102835a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 102935a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 103035a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 103135a5a358SJonathan Adams 103235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 103335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103435a5a358SJonathan Adams spa->spa_proc = &p0; 103535a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103635a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103735a5a358SJonathan Adams 103835a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 103935a5a358SJonathan Adams lwp_exit(); 104035a5a358SJonathan Adams } 104135a5a358SJonathan Adams #endif 104235a5a358SJonathan Adams 104335a5a358SJonathan Adams /* 104435a5a358SJonathan Adams * Activate an uninitialized pool. 104535a5a358SJonathan Adams */ 104635a5a358SJonathan Adams static void 104735a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104835a5a358SJonathan Adams { 104935a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 105035a5a358SJonathan Adams 105135a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 105235a5a358SJonathan Adams spa->spa_mode = mode; 105335a5a358SJonathan Adams 105435a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105535a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105635a5a358SJonathan Adams 105735a5a358SJonathan Adams /* Try to create a covering process */ 105835a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 105935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 106035a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 106135a5a358SJonathan Adams spa->spa_did = 0; 106235a5a358SJonathan Adams 106335a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106435a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106535a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106635a5a358SJonathan Adams NULL, 0) == 0) { 106735a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106835a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 106935a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 107035a5a358SJonathan Adams &spa->spa_proc_lock); 10712e0c549eSJonathan Adams } 107235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 107335a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107435a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107535a5a358SJonathan Adams } else { 107635a5a358SJonathan Adams #ifdef _KERNEL 107735a5a358SJonathan Adams cmn_err(CE_WARN, 107835a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 107935a5a358SJonathan Adams spa->spa_name); 108035a5a358SJonathan Adams #endif 1081e14bb325SJeff Bonwick } 1082fa9e4066Sahrens } 108335a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108435a5a358SJonathan Adams 108535a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108635a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108735a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108835a5a358SJonathan Adams } 1089fa9e4066Sahrens 1090e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1091e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1092bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1093bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1094e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1095e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1096fa9e4066Sahrens 1097b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1098fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1099ea8dc4b6Seschrock 1100ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1101ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1102ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1103ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1104ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1105ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1106fa9e4066Sahrens } 1107fa9e4066Sahrens 1108fa9e4066Sahrens /* 1109fa9e4066Sahrens * Opposite of spa_activate(). 1110fa9e4066Sahrens */ 1111fa9e4066Sahrens static void 1112fa9e4066Sahrens spa_deactivate(spa_t *spa) 1113fa9e4066Sahrens { 1114fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1115fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1116fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 111725f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1118fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1119fa9e4066Sahrens 1120bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1121bc9014e6SJustin Gibbs 1122fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1123fa9e4066Sahrens 1124e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1125bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1126e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1127fa9e4066Sahrens 1128e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1129e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1130ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1131e14bb325SJeff Bonwick } 1132fa9e4066Sahrens } 1133fa9e4066Sahrens 1134fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1135fa9e4066Sahrens spa->spa_normal_class = NULL; 1136fa9e4066Sahrens 11378654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11388654d025Sperrin spa->spa_log_class = NULL; 11398654d025Sperrin 1140ea8dc4b6Seschrock /* 1141ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1142ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1143ea8dc4b6Seschrock */ 1144ea8dc4b6Seschrock spa_errlog_drain(spa); 1145ea8dc4b6Seschrock 1146ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1147ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1148ea8dc4b6Seschrock 1149fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 115035a5a358SJonathan Adams 115135a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 115235a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 115335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 115435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 115535a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 115635a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 115735a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 115835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 115935a5a358SJonathan Adams } 116035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 116135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 116235a5a358SJonathan Adams } 116335a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 116435a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 116535a5a358SJonathan Adams 116635a5a358SJonathan Adams /* 116735a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 116835a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 116935a5a358SJonathan Adams * it. 117035a5a358SJonathan Adams */ 117135a5a358SJonathan Adams if (spa->spa_did != 0) { 117235a5a358SJonathan Adams thread_join(spa->spa_did); 117335a5a358SJonathan Adams spa->spa_did = 0; 117435a5a358SJonathan Adams } 1175fa9e4066Sahrens } 1176fa9e4066Sahrens 1177fa9e4066Sahrens /* 1178fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1179fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1180fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1181fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1182fa9e4066Sahrens */ 118399653d4eSeschrock static int 118499653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 118599653d4eSeschrock uint_t id, int atype) 1186fa9e4066Sahrens { 1187fa9e4066Sahrens nvlist_t **child; 1188573ca77eSGeorge Wilson uint_t children; 118999653d4eSeschrock int error; 1190fa9e4066Sahrens 119199653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 119299653d4eSeschrock return (error); 1193fa9e4066Sahrens 119499653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 119599653d4eSeschrock return (0); 1196fa9e4066Sahrens 1197e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1198e14bb325SJeff Bonwick &child, &children); 1199e14bb325SJeff Bonwick 1200e14bb325SJeff Bonwick if (error == ENOENT) 1201e14bb325SJeff Bonwick return (0); 1202e14bb325SJeff Bonwick 1203e14bb325SJeff Bonwick if (error) { 120499653d4eSeschrock vdev_free(*vdp); 120599653d4eSeschrock *vdp = NULL; 1206be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1207fa9e4066Sahrens } 1208fa9e4066Sahrens 1209573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 121099653d4eSeschrock vdev_t *vd; 121199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 121299653d4eSeschrock atype)) != 0) { 121399653d4eSeschrock vdev_free(*vdp); 121499653d4eSeschrock *vdp = NULL; 121599653d4eSeschrock return (error); 1216fa9e4066Sahrens } 1217fa9e4066Sahrens } 1218fa9e4066Sahrens 121999653d4eSeschrock ASSERT(*vdp != NULL); 122099653d4eSeschrock 122199653d4eSeschrock return (0); 1222fa9e4066Sahrens } 1223fa9e4066Sahrens 1224fa9e4066Sahrens /* 1225fa9e4066Sahrens * Opposite of spa_load(). 1226fa9e4066Sahrens */ 1227fa9e4066Sahrens static void 1228fa9e4066Sahrens spa_unload(spa_t *spa) 1229fa9e4066Sahrens { 123099653d4eSeschrock int i; 123199653d4eSeschrock 1232e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1233e14bb325SJeff Bonwick 1234ea8dc4b6Seschrock /* 1235ea8dc4b6Seschrock * Stop async tasks. 1236ea8dc4b6Seschrock */ 1237ea8dc4b6Seschrock spa_async_suspend(spa); 1238ea8dc4b6Seschrock 1239fa9e4066Sahrens /* 1240fa9e4066Sahrens * Stop syncing. 1241fa9e4066Sahrens */ 1242fa9e4066Sahrens if (spa->spa_sync_on) { 1243fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1244fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1245fa9e4066Sahrens } 1246fa9e4066Sahrens 12478363e80aSGeorge Wilson /* 12488363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 12498363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 12508363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 12518363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 12528363e80aSGeorge Wilson */ 12538363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 12548363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12558363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 12568363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 12578363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 12588363e80aSGeorge Wilson } 12598363e80aSGeorge Wilson 1260fa9e4066Sahrens /* 1261e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1262fa9e4066Sahrens */ 126354d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12646f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12656f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12666f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 126754d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 126854d692b7SGeorge Wilson } 1269fa9e4066Sahrens 1270cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1271cde58dbcSMatthew Ahrens 12720713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12730713e232SGeorge Wilson 12740713e232SGeorge Wilson /* 12750713e232SGeorge Wilson * Close all vdevs. 12760713e232SGeorge Wilson */ 12770713e232SGeorge Wilson if (spa->spa_root_vdev) 12780713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12790713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12800713e232SGeorge Wilson 1281fa9e4066Sahrens /* 1282fa9e4066Sahrens * Close the dsl pool. 1283fa9e4066Sahrens */ 1284fa9e4066Sahrens if (spa->spa_dsl_pool) { 1285fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1286fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1287afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1288fa9e4066Sahrens } 1289fa9e4066Sahrens 1290b24ab676SJeff Bonwick ddt_unload(spa); 1291b24ab676SJeff Bonwick 12928ad4d6ddSJeff Bonwick /* 12938ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12948ad4d6ddSJeff Bonwick */ 12958ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12968ad4d6ddSJeff Bonwick 1297fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1298fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1299fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1300fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1301fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1302fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 130399653d4eSeschrock } 1304fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1305fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1306fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1307fa94a07fSbrendan } 13082ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1309fa94a07fSbrendan 1310cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1311cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1312fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1313cd0837ccSGeorge Wilson } 1314fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1315fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1316fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1317fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1318fa94a07fSbrendan } 1319fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1320fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1321fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 132299653d4eSeschrock } 13232ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 132499653d4eSeschrock 1325ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13268ad4d6ddSJeff Bonwick 13278704186eSDan McDonald if (spa->spa_comment != NULL) { 13288704186eSDan McDonald spa_strfree(spa->spa_comment); 13298704186eSDan McDonald spa->spa_comment = NULL; 13308704186eSDan McDonald } 13318704186eSDan McDonald 13328ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1333fa9e4066Sahrens } 1334fa9e4066Sahrens 133599653d4eSeschrock /* 133699653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 133799653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1338fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1339fa94a07fSbrendan * then re-generate a more complete list including status information. 134099653d4eSeschrock */ 134199653d4eSeschrock static void 134299653d4eSeschrock spa_load_spares(spa_t *spa) 134399653d4eSeschrock { 134499653d4eSeschrock nvlist_t **spares; 134599653d4eSeschrock uint_t nspares; 134699653d4eSeschrock int i; 134739c23413Seschrock vdev_t *vd, *tvd; 134899653d4eSeschrock 1349e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1350e14bb325SJeff Bonwick 135199653d4eSeschrock /* 135299653d4eSeschrock * First, close and free any existing spare vdevs. 135399653d4eSeschrock */ 1354fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1355fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 135639c23413Seschrock 135739c23413Seschrock /* Undo the call to spa_activate() below */ 1358c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1359c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 136039c23413Seschrock spa_spare_remove(tvd); 136139c23413Seschrock vdev_close(vd); 136239c23413Seschrock vdev_free(vd); 136399653d4eSeschrock } 136439c23413Seschrock 1365fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1366fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1367fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 136899653d4eSeschrock 1369fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 137099653d4eSeschrock nspares = 0; 137199653d4eSeschrock else 1372fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 137399653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 137499653d4eSeschrock 1375fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1376fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 137799653d4eSeschrock 137899653d4eSeschrock if (nspares == 0) 137999653d4eSeschrock return; 138099653d4eSeschrock 138199653d4eSeschrock /* 138299653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 138339c23413Seschrock * process. For each spare, there is potentially two different vdev_t 138439c23413Seschrock * structures associated with it: one in the list of spares (used only 138539c23413Seschrock * for basic validation purposes) and one in the active vdev 138639c23413Seschrock * configuration (if it's spared in). During this phase we open and 138739c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 138839c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 138999653d4eSeschrock */ 1390fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1391fa94a07fSbrendan KM_SLEEP); 1392fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 139399653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 139499653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 139599653d4eSeschrock ASSERT(vd != NULL); 139699653d4eSeschrock 1397fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 139899653d4eSeschrock 1399c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1400c5904d13Seschrock B_FALSE)) != NULL) { 140139c23413Seschrock if (!tvd->vdev_isspare) 140239c23413Seschrock spa_spare_add(tvd); 140339c23413Seschrock 140439c23413Seschrock /* 140539c23413Seschrock * We only mark the spare active if we were successfully 140639c23413Seschrock * able to load the vdev. Otherwise, importing a pool 140739c23413Seschrock * with a bad active spare would result in strange 140839c23413Seschrock * behavior, because multiple pool would think the spare 140939c23413Seschrock * is actively in use. 141039c23413Seschrock * 141139c23413Seschrock * There is a vulnerability here to an equally bizarre 141239c23413Seschrock * circumstance, where a dead active spare is later 141339c23413Seschrock * brought back to life (onlined or otherwise). Given 141439c23413Seschrock * the rarity of this scenario, and the extra complexity 141539c23413Seschrock * it adds, we ignore the possibility. 141639c23413Seschrock */ 141739c23413Seschrock if (!vdev_is_dead(tvd)) 141839c23413Seschrock spa_spare_activate(tvd); 141939c23413Seschrock } 142039c23413Seschrock 1421e14bb325SJeff Bonwick vd->vdev_top = vd; 14226809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1423e14bb325SJeff Bonwick 142499653d4eSeschrock if (vdev_open(vd) != 0) 142599653d4eSeschrock continue; 142699653d4eSeschrock 1427fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1428fa94a07fSbrendan spa_spare_add(vd); 142999653d4eSeschrock } 143099653d4eSeschrock 143199653d4eSeschrock /* 143299653d4eSeschrock * Recompute the stashed list of spares, with status information 143399653d4eSeschrock * this time. 143499653d4eSeschrock */ 1435fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 143699653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 143799653d4eSeschrock 1438fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1439fa94a07fSbrendan KM_SLEEP); 1440fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1441fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14423f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1443fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1444fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1445fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 144699653d4eSeschrock nvlist_free(spares[i]); 1447fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1448fa94a07fSbrendan } 1449fa94a07fSbrendan 1450fa94a07fSbrendan /* 1451fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1452fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1453fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1454fa94a07fSbrendan * then re-generate a more complete list including status information. 1455fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1456fa94a07fSbrendan * not re-opened. 1457fa94a07fSbrendan */ 1458fa94a07fSbrendan static void 1459fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1460fa94a07fSbrendan { 1461fa94a07fSbrendan nvlist_t **l2cache; 1462fa94a07fSbrendan uint_t nl2cache; 1463fa94a07fSbrendan int i, j, oldnvdevs; 1464573ca77eSGeorge Wilson uint64_t guid; 1465fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1466fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1467fa94a07fSbrendan 1468e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1469e14bb325SJeff Bonwick 1470fa94a07fSbrendan if (sav->sav_config != NULL) { 1471fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1472fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1473fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1474fa94a07fSbrendan } else { 1475fa94a07fSbrendan nl2cache = 0; 1476d5285caeSGeorge Wilson newvdevs = NULL; 1477fa94a07fSbrendan } 1478fa94a07fSbrendan 1479fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1480fa94a07fSbrendan oldnvdevs = sav->sav_count; 1481fa94a07fSbrendan sav->sav_vdevs = NULL; 1482fa94a07fSbrendan sav->sav_count = 0; 1483fa94a07fSbrendan 1484fa94a07fSbrendan /* 1485fa94a07fSbrendan * Process new nvlist of vdevs. 1486fa94a07fSbrendan */ 1487fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1488fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1489fa94a07fSbrendan &guid) == 0); 1490fa94a07fSbrendan 1491fa94a07fSbrendan newvdevs[i] = NULL; 1492fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1493fa94a07fSbrendan vd = oldvdevs[j]; 1494fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1495fa94a07fSbrendan /* 1496fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1497fa94a07fSbrendan */ 1498fa94a07fSbrendan newvdevs[i] = vd; 1499fa94a07fSbrendan oldvdevs[j] = NULL; 1500fa94a07fSbrendan break; 1501fa94a07fSbrendan } 1502fa94a07fSbrendan } 1503fa94a07fSbrendan 1504fa94a07fSbrendan if (newvdevs[i] == NULL) { 1505fa94a07fSbrendan /* 1506fa94a07fSbrendan * Create new vdev 1507fa94a07fSbrendan */ 1508fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1509fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1510fa94a07fSbrendan ASSERT(vd != NULL); 1511fa94a07fSbrendan newvdevs[i] = vd; 1512fa94a07fSbrendan 1513fa94a07fSbrendan /* 1514fa94a07fSbrendan * Commit this vdev as an l2cache device, 1515fa94a07fSbrendan * even if it fails to open. 1516fa94a07fSbrendan */ 1517fa94a07fSbrendan spa_l2cache_add(vd); 1518fa94a07fSbrendan 1519c5904d13Seschrock vd->vdev_top = vd; 1520c5904d13Seschrock vd->vdev_aux = sav; 1521c5904d13Seschrock 1522c5904d13Seschrock spa_l2cache_activate(vd); 1523c5904d13Seschrock 1524fa94a07fSbrendan if (vdev_open(vd) != 0) 1525fa94a07fSbrendan continue; 1526fa94a07fSbrendan 1527fa94a07fSbrendan (void) vdev_validate_aux(vd); 1528fa94a07fSbrendan 1529573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1530573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1531fa94a07fSbrendan } 1532fa94a07fSbrendan } 1533fa94a07fSbrendan 1534fa94a07fSbrendan /* 1535fa94a07fSbrendan * Purge vdevs that were dropped 1536fa94a07fSbrendan */ 1537fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1538fa94a07fSbrendan uint64_t pool; 1539fa94a07fSbrendan 1540fa94a07fSbrendan vd = oldvdevs[i]; 1541fa94a07fSbrendan if (vd != NULL) { 1542cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1543cd0837ccSGeorge Wilson 15448ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15458ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1546fa94a07fSbrendan l2arc_remove_vdev(vd); 1547cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1548cd0837ccSGeorge Wilson vdev_free(vd); 1549fa94a07fSbrendan } 1550fa94a07fSbrendan } 1551fa94a07fSbrendan 1552fa94a07fSbrendan if (oldvdevs) 1553fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1554fa94a07fSbrendan 1555fa94a07fSbrendan if (sav->sav_config == NULL) 1556fa94a07fSbrendan goto out; 1557fa94a07fSbrendan 1558fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1559fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1560fa94a07fSbrendan 1561fa94a07fSbrendan /* 1562fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1563fa94a07fSbrendan * information this time. 1564fa94a07fSbrendan */ 1565fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1566fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1567fa94a07fSbrendan 1568fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1569fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1570fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15713f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1572fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1573fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1574fa94a07fSbrendan out: 1575fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1576fa94a07fSbrendan nvlist_free(l2cache[i]); 1577fa94a07fSbrendan if (sav->sav_count) 1578fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 157999653d4eSeschrock } 158099653d4eSeschrock 158199653d4eSeschrock static int 158299653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 158399653d4eSeschrock { 158499653d4eSeschrock dmu_buf_t *db; 158599653d4eSeschrock char *packed = NULL; 158699653d4eSeschrock size_t nvsize = 0; 158799653d4eSeschrock int error; 158899653d4eSeschrock *value = NULL; 158999653d4eSeschrock 1590a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1591a45f1c3cSBrian Behlendorf if (error != 0) 1592a45f1c3cSBrian Behlendorf return (error); 1593a45f1c3cSBrian Behlendorf 159499653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 159599653d4eSeschrock dmu_buf_rele(db, FTAG); 159699653d4eSeschrock 159799653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15987bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15997bfdf011SNeil Perrin DMU_READ_PREFETCH); 160099653d4eSeschrock if (error == 0) 160199653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 160299653d4eSeschrock kmem_free(packed, nvsize); 160399653d4eSeschrock 160499653d4eSeschrock return (error); 160599653d4eSeschrock } 160699653d4eSeschrock 16073d7072f8Seschrock /* 16083d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 16093d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 16103d7072f8Seschrock */ 16113d7072f8Seschrock static void 16123d7072f8Seschrock spa_check_removed(vdev_t *vd) 16133d7072f8Seschrock { 1614573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 16153d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 16163d7072f8Seschrock 1617efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1618efb4a871SYuri Pankov !vd->vdev_ishole) { 16193d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 1620ce1577b0SDave Eddy spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 16213d7072f8Seschrock } 16223d7072f8Seschrock } 16233d7072f8Seschrock 1624215198a6SJoe Stein static void 1625215198a6SJoe Stein spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) 1626215198a6SJoe Stein { 1627215198a6SJoe Stein ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); 1628215198a6SJoe Stein 1629215198a6SJoe Stein vd->vdev_top_zap = mvd->vdev_top_zap; 1630215198a6SJoe Stein vd->vdev_leaf_zap = mvd->vdev_leaf_zap; 1631215198a6SJoe Stein 1632215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 1633215198a6SJoe Stein spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); 1634215198a6SJoe Stein } 1635215198a6SJoe Stein } 1636215198a6SJoe Stein 1637e6ca193dSGeorge Wilson /* 16384b964adaSGeorge Wilson * Validate the current config against the MOS config 1639e6ca193dSGeorge Wilson */ 16404b964adaSGeorge Wilson static boolean_t 16414b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1642e6ca193dSGeorge Wilson { 16434b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16444b964adaSGeorge Wilson nvlist_t *nv; 16454b964adaSGeorge Wilson 16464b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16474b964adaSGeorge Wilson 16484b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16494b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16504b964adaSGeorge Wilson 16514b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1652e6ca193dSGeorge Wilson 165388ecc943SGeorge Wilson /* 16544b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16554b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16564b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 165788ecc943SGeorge Wilson */ 16584b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16594b964adaSGeorge Wilson nvlist_t **child, *nv; 16604b964adaSGeorge Wilson uint64_t idx = 0; 16614b964adaSGeorge Wilson 16624b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16634b964adaSGeorge Wilson KM_SLEEP); 16644b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1665e6ca193dSGeorge Wilson 16664b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16674b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16684b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16694b964adaSGeorge Wilson 16704b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16714b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16724b964adaSGeorge Wilson mtvd->vdev_islog) 16734b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16744b964adaSGeorge Wilson B_FALSE, 0); 16754b964adaSGeorge Wilson } 16764b964adaSGeorge Wilson 16774b964adaSGeorge Wilson if (idx) { 16784b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16794b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16804b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16814b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16824b964adaSGeorge Wilson 16834b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16844b964adaSGeorge Wilson nvlist_free(child[i]); 16854b964adaSGeorge Wilson } 16864b964adaSGeorge Wilson nvlist_free(nv); 16874b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16884b964adaSGeorge Wilson } 16894b964adaSGeorge Wilson 16904b964adaSGeorge Wilson /* 16914b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16924b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16934b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16944b964adaSGeorge Wilson */ 169588ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16964b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16974b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16984b964adaSGeorge Wilson 16994b964adaSGeorge Wilson /* 17004b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 17014b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 17024b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 17034b964adaSGeorge Wilson */ 17044b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 17054b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 17064b964adaSGeorge Wilson 17074b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 17084b964adaSGeorge Wilson continue; 17094b964adaSGeorge Wilson 17104b964adaSGeorge Wilson /* 17114b964adaSGeorge Wilson * Device specific actions. 17124b964adaSGeorge Wilson */ 17134b964adaSGeorge Wilson if (mtvd->vdev_islog) { 17144b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 17154b964adaSGeorge Wilson } else { 17164b964adaSGeorge Wilson /* 17174b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 17184b964adaSGeorge Wilson * support we should be able to handle 17194b964adaSGeorge Wilson * missing data devices by transitioning 17204b964adaSGeorge Wilson * the pool to readonly. 17214b964adaSGeorge Wilson */ 17224b964adaSGeorge Wilson continue; 17234b964adaSGeorge Wilson } 17244b964adaSGeorge Wilson 17254b964adaSGeorge Wilson /* 17264b964adaSGeorge Wilson * Swap the missing vdev with the data we were 17274b964adaSGeorge Wilson * able to obtain from the MOS config. 17284b964adaSGeorge Wilson */ 17294b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 17304b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 17314b964adaSGeorge Wilson 17324b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 17334b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17344b964adaSGeorge Wilson 17354b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17364b964adaSGeorge Wilson vdev_load(mtvd); 17374b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17384b964adaSGeorge Wilson 17394b964adaSGeorge Wilson vdev_reopen(rvd); 1740215198a6SJoe Stein } else { 1741215198a6SJoe Stein if (mtvd->vdev_islog) { 1742215198a6SJoe Stein /* 1743215198a6SJoe Stein * Load the slog device's state from the MOS 1744215198a6SJoe Stein * config since it's possible that the label 1745215198a6SJoe Stein * does not contain the most up-to-date 1746215198a6SJoe Stein * information. 1747215198a6SJoe Stein */ 1748215198a6SJoe Stein vdev_load_log_state(tvd, mtvd); 1749215198a6SJoe Stein vdev_reopen(tvd); 1750215198a6SJoe Stein } 1751215198a6SJoe Stein 17524b964adaSGeorge Wilson /* 1753215198a6SJoe Stein * Per-vdev ZAP info is stored exclusively in the MOS. 17544b964adaSGeorge Wilson */ 1755215198a6SJoe Stein spa_config_valid_zaps(tvd, mtvd); 17564b964adaSGeorge Wilson } 1757e6ca193dSGeorge Wilson } 1758215198a6SJoe Stein 17594b964adaSGeorge Wilson vdev_free(mrvd); 176088ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17614b964adaSGeorge Wilson 17624b964adaSGeorge Wilson /* 17634b964adaSGeorge Wilson * Ensure we were able to validate the config. 17644b964adaSGeorge Wilson */ 17654b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1766e6ca193dSGeorge Wilson } 1767e6ca193dSGeorge Wilson 1768b87f3af3Sperrin /* 1769b87f3af3Sperrin * Check for missing log devices 1770b87f3af3Sperrin */ 17713b2aab18SMatthew Ahrens static boolean_t 1772b87f3af3Sperrin spa_check_logs(spa_t *spa) 1773b87f3af3Sperrin { 17743b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 177512380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17763b2aab18SMatthew Ahrens 1777b87f3af3Sperrin switch (spa->spa_log_state) { 1778b87f3af3Sperrin case SPA_LOG_MISSING: 1779b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1780b87f3af3Sperrin case SPA_LOG_UNKNOWN: 178112380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 178212380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 17833b2aab18SMatthew Ahrens if (rv) 17841195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1785b87f3af3Sperrin break; 1786b87f3af3Sperrin } 17873b2aab18SMatthew Ahrens return (rv); 1788b87f3af3Sperrin } 1789b87f3af3Sperrin 17901195e687SMark J Musante static boolean_t 17911195e687SMark J Musante spa_passivate_log(spa_t *spa) 17921195e687SMark J Musante { 17931195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17941195e687SMark J Musante boolean_t slog_found = B_FALSE; 17951195e687SMark J Musante 17961195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17971195e687SMark J Musante 17981195e687SMark J Musante if (!spa_has_slogs(spa)) 17991195e687SMark J Musante return (B_FALSE); 18001195e687SMark J Musante 18011195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18021195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18031195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18041195e687SMark J Musante 18051195e687SMark J Musante if (tvd->vdev_islog) { 18061195e687SMark J Musante metaslab_group_passivate(mg); 18071195e687SMark J Musante slog_found = B_TRUE; 18081195e687SMark J Musante } 18091195e687SMark J Musante } 18101195e687SMark J Musante 18111195e687SMark J Musante return (slog_found); 18121195e687SMark J Musante } 18131195e687SMark J Musante 18141195e687SMark J Musante static void 18151195e687SMark J Musante spa_activate_log(spa_t *spa) 18161195e687SMark J Musante { 18171195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18181195e687SMark J Musante 18191195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18201195e687SMark J Musante 18211195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18221195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18231195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18241195e687SMark J Musante 18251195e687SMark J Musante if (tvd->vdev_islog) 18261195e687SMark J Musante metaslab_group_activate(mg); 18271195e687SMark J Musante } 18281195e687SMark J Musante } 18291195e687SMark J Musante 18301195e687SMark J Musante int 18311195e687SMark J Musante spa_offline_log(spa_t *spa) 18321195e687SMark J Musante { 18333b2aab18SMatthew Ahrens int error; 18341195e687SMark J Musante 18353b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 18363b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18373b2aab18SMatthew Ahrens if (error == 0) { 18381195e687SMark J Musante /* 18391195e687SMark J Musante * We successfully offlined the log device, sync out the 18401195e687SMark J Musante * current txg so that the "stubby" block can be removed 18411195e687SMark J Musante * by zil_sync(). 18421195e687SMark J Musante */ 18431195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18441195e687SMark J Musante } 18451195e687SMark J Musante return (error); 18461195e687SMark J Musante } 18471195e687SMark J Musante 1848b693757aSEric Schrock static void 1849b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1850b693757aSEric Schrock { 1851b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1852b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1853b693757aSEric Schrock } 1854b693757aSEric Schrock 1855b24ab676SJeff Bonwick void 1856b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1857b24ab676SJeff Bonwick { 1858b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1859b24ab676SJeff Bonwick 1860b24ab676SJeff Bonwick if (zio->io_error) 1861b24ab676SJeff Bonwick return; 1862b24ab676SJeff Bonwick 1863b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1864b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1865b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1866b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1867b24ab676SJeff Bonwick } 1868b24ab676SJeff Bonwick 1869468c413aSTim Haley typedef struct spa_load_error { 1870c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1871468c413aSTim Haley uint64_t sle_data_count; 1872468c413aSTim Haley } spa_load_error_t; 1873468c413aSTim Haley 1874468c413aSTim Haley static void 1875468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1876468c413aSTim Haley { 1877468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1878468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1879468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1880468c413aSTim Haley int error = zio->io_error; 1881e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1882468c413aSTim Haley 1883770499e1SDan Kimmel abd_free(zio->io_abd); 1884468c413aSTim Haley if (error) { 1885ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1886468c413aSTim Haley type != DMU_OT_INTENT_LOG) 18871a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1888468c413aSTim Haley else 18891a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1890468c413aSTim Haley } 1891e42d2059SMatthew Ahrens 1892e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1893e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1894e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1895e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1896468c413aSTim Haley } 1897468c413aSTim Haley 1898e42d2059SMatthew Ahrens /* 1899e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1900e42d2059SMatthew Ahrens * a pool while importing it. 1901e42d2059SMatthew Ahrens */ 1902e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1903e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1904e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1905e42d2059SMatthew Ahrens 1906468c413aSTim Haley /*ARGSUSED*/ 1907468c413aSTim Haley static int 1908b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19097802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1910468c413aSTim Haley { 1911a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1912e42d2059SMatthew Ahrens return (0); 1913e42d2059SMatthew Ahrens /* 1914e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1915e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1916e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1917e42d2059SMatthew Ahrens */ 1918e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1919e42d2059SMatthew Ahrens return (0); 1920770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1921e42d2059SMatthew Ahrens return (0); 1922468c413aSTim Haley 1923e42d2059SMatthew Ahrens zio_t *rio = arg; 1924e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1925e42d2059SMatthew Ahrens 1926e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1927e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1928e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1929e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1930e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1931e42d2059SMatthew Ahrens 1932770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 1933e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1934e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1935e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1936468c413aSTim Haley return (0); 1937468c413aSTim Haley } 1938468c413aSTim Haley 1939c971037bSPaul Dagnelie /* ARGSUSED */ 1940c971037bSPaul Dagnelie int 1941c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1942c971037bSPaul Dagnelie { 19439adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1944c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1945c971037bSPaul Dagnelie 1946c971037bSPaul Dagnelie return (0); 1947c971037bSPaul Dagnelie } 1948c971037bSPaul Dagnelie 1949468c413aSTim Haley static int 1950468c413aSTim Haley spa_load_verify(spa_t *spa) 1951468c413aSTim Haley { 1952468c413aSTim Haley zio_t *rio; 1953468c413aSTim Haley spa_load_error_t sle = { 0 }; 1954468c413aSTim Haley zpool_rewind_policy_t policy; 1955468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1956e42d2059SMatthew Ahrens int error = 0; 1957468c413aSTim Haley 1958c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1959c8ee1847SVictor Latushkin 1960c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1961c8ee1847SVictor Latushkin return (0); 1962c8ee1847SVictor Latushkin 1963c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1964c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1965c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1966c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1967c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1968c971037bSPaul Dagnelie if (error != 0) 1969c971037bSPaul Dagnelie return (error); 1970c971037bSPaul Dagnelie 1971468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1972468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1973468c413aSTim Haley 1974e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1975e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1976e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1977e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 1978e42d2059SMatthew Ahrens } 1979468c413aSTim Haley 1980468c413aSTim Haley (void) zio_wait(rio); 1981468c413aSTim Haley 1982c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1983468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1984468c413aSTim Haley 1985c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1986468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 19874b964adaSGeorge Wilson int64_t loss = 0; 19884b964adaSGeorge Wilson 1989468c413aSTim Haley verify_ok = B_TRUE; 1990468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1991468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 19924b964adaSGeorge Wilson 19934b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 19944b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19954b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 19964b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 19974b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 19984b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19994b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 2000a33cae98STim Haley } else { 2001a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2002468c413aSTim Haley } 2003468c413aSTim Haley 2004468c413aSTim Haley if (error) { 2005468c413aSTim Haley if (error != ENXIO && error != EIO) 2006be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2007468c413aSTim Haley return (error); 2008468c413aSTim Haley } 2009468c413aSTim Haley 2010468c413aSTim Haley return (verify_ok ? 0 : EIO); 2011468c413aSTim Haley } 2012468c413aSTim Haley 20131195e687SMark J Musante /* 20141195e687SMark J Musante * Find a value in the pool props object. 20151195e687SMark J Musante */ 20161195e687SMark J Musante static void 20171195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20181195e687SMark J Musante { 20191195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20201195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20211195e687SMark J Musante } 20221195e687SMark J Musante 20231195e687SMark J Musante /* 20241195e687SMark J Musante * Find a value in the pool directory object. 20251195e687SMark J Musante */ 20261195e687SMark J Musante static int 20271195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 20281195e687SMark J Musante { 20291195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20301195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 20311195e687SMark J Musante } 20321195e687SMark J Musante 20331195e687SMark J Musante static int 20341195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20351195e687SMark J Musante { 20361195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 20371195e687SMark J Musante return (err); 20381195e687SMark J Musante } 20391195e687SMark J Musante 20401195e687SMark J Musante /* 20411195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20421195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20431195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20441195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20451195e687SMark J Musante * 20461195e687SMark J Musante * This function determines what to do with that list: either rejoin 20471195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20481195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20491195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20501195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20511195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20521195e687SMark J Musante * 2053d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2054d41c4376SMark J Musante * the original pool. 20551195e687SMark J Musante */ 20561195e687SMark J Musante static void 20571195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 20581195e687SMark J Musante { 20591195e687SMark J Musante uint_t extracted; 20601195e687SMark J Musante uint64_t *glist; 20611195e687SMark J Musante uint_t i, gcount; 20621195e687SMark J Musante nvlist_t *nvl; 20631195e687SMark J Musante vdev_t **vd; 20641195e687SMark J Musante boolean_t attempt_reopen; 20651195e687SMark J Musante 20661195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20671195e687SMark J Musante return; 20681195e687SMark J Musante 20691195e687SMark J Musante /* check that the config is complete */ 20701195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20711195e687SMark J Musante &glist, &gcount) != 0) 20721195e687SMark J Musante return; 20731195e687SMark J Musante 20741195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20751195e687SMark J Musante 20761195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20771195e687SMark J Musante attempt_reopen = B_TRUE; 20781195e687SMark J Musante for (i = 0; i < gcount; i++) { 20791195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 20801195e687SMark J Musante continue; 20811195e687SMark J Musante 20821195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 20831195e687SMark J Musante if (vd[i] == NULL) { 20841195e687SMark J Musante /* 20851195e687SMark J Musante * Don't bother attempting to reopen the disks; 20861195e687SMark J Musante * just do the split. 20871195e687SMark J Musante */ 20881195e687SMark J Musante attempt_reopen = B_FALSE; 20891195e687SMark J Musante } else { 20901195e687SMark J Musante /* attempt to re-online it */ 20911195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 20921195e687SMark J Musante } 20931195e687SMark J Musante } 20941195e687SMark J Musante 20951195e687SMark J Musante if (attempt_reopen) { 20961195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20971195e687SMark J Musante 20981195e687SMark J Musante /* check each device to see what state it's in */ 20991195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 21001195e687SMark J Musante if (vd[i] != NULL && 21011195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 21021195e687SMark J Musante break; 21031195e687SMark J Musante ++extracted; 21041195e687SMark J Musante } 21051195e687SMark J Musante } 21061195e687SMark J Musante 21071195e687SMark J Musante /* 21081195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 21091195e687SMark J Musante * even attempted to look at them, then we split them off for 21101195e687SMark J Musante * good. 21111195e687SMark J Musante */ 21121195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 21131195e687SMark J Musante for (i = 0; i < gcount; i++) 21141195e687SMark J Musante if (vd[i] != NULL) 21151195e687SMark J Musante vdev_split(vd[i]); 21161195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21171195e687SMark J Musante } 21181195e687SMark J Musante 21191195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21201195e687SMark J Musante } 21211195e687SMark J Musante 21221195e687SMark J Musante static int 21231195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 21241195e687SMark J Musante boolean_t mosconfig) 21251195e687SMark J Musante { 21261195e687SMark J Musante nvlist_t *config = spa->spa_config; 21271195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21288704186eSDan McDonald char *comment; 21291195e687SMark J Musante int error; 21301195e687SMark J Musante uint64_t pool_guid; 21311195e687SMark J Musante nvlist_t *nvl; 21321195e687SMark J Musante 21331195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2134be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21351195e687SMark J Musante 21368704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 21378704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 21388704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 21398704186eSDan McDonald 21401195e687SMark J Musante /* 21411195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 21421195e687SMark J Musante * it's not present treat it as the initial version. 21431195e687SMark J Musante */ 21441195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 21451195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 21461195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 21471195e687SMark J Musante 21481195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 21491195e687SMark J Musante &spa->spa_config_txg); 21501195e687SMark J Musante 21511195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 21521195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2153be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 21541195e687SMark J Musante } else { 2155e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 21561195e687SMark J Musante 21571195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 21581195e687SMark J Musante &nvl) == 0) { 21591195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 21601195e687SMark J Musante KM_SLEEP) == 0); 21611195e687SMark J Musante } 21621195e687SMark J Musante 2163ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2164ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2165ad135b5dSChristopher Siden 216611027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21671195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21681195e687SMark J Musante mosconfig, &ereport); 21691195e687SMark J Musante } 21701195e687SMark J Musante 2171bc9014e6SJustin Gibbs /* 2172bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2173bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2174bc9014e6SJustin Gibbs */ 2175bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21761195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 217711027bc7STim Haley if (error) { 217811027bc7STim Haley if (error != EEXIST) { 217911027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 218011027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 218111027bc7STim Haley } 218211027bc7STim Haley if (error != EBADF) { 218311027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 218411027bc7STim Haley } 218511027bc7STim Haley } 21861195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21871195e687SMark J Musante spa->spa_ena = 0; 21881195e687SMark J Musante 21891195e687SMark J Musante return (error); 21901195e687SMark J Musante } 21911195e687SMark J Musante 2192215198a6SJoe Stein /* 2193215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2194215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2195215198a6SJoe Stein * spa's per-vdev ZAP list. 2196215198a6SJoe Stein */ 2197215198a6SJoe Stein static uint64_t 2198215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2199215198a6SJoe Stein { 2200215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2201215198a6SJoe Stein uint64_t total = 0; 2202215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2203215198a6SJoe Stein total++; 2204215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2205215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2206215198a6SJoe Stein } 2207215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2208215198a6SJoe Stein total++; 2209215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2210215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2211215198a6SJoe Stein } 2212215198a6SJoe Stein 2213215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2214215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2215215198a6SJoe Stein } 2216215198a6SJoe Stein 2217215198a6SJoe Stein return (total); 2218215198a6SJoe Stein } 2219215198a6SJoe Stein 2220fa9e4066Sahrens /* 2221fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2222ea8dc4b6Seschrock * source of configuration information. 2223fa9e4066Sahrens */ 2224fa9e4066Sahrens static int 22251195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 22261195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 22271195e687SMark J Musante char **ereport) 2228fa9e4066Sahrens { 2229fa9e4066Sahrens int error = 0; 2230871a9500SMark J Musante nvlist_t *nvroot = NULL; 2231ad135b5dSChristopher Siden nvlist_t *label; 2232fa9e4066Sahrens vdev_t *rvd; 2233fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 22344b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 22358ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 22361195e687SMark J Musante int parse; 2237cde58dbcSMatthew Ahrens uint64_t obj; 2238ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2239fa9e4066Sahrens 22408ad4d6ddSJeff Bonwick /* 22418ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 22428ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 22438ad4d6ddSJeff Bonwick */ 22448ad4d6ddSJeff Bonwick if (!mosconfig) 22458ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 22468ad4d6ddSJeff Bonwick 2247e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2248e14bb325SJeff Bonwick 2249ea8dc4b6Seschrock spa->spa_load_state = state; 22500373e76bSbonwick 22511195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2252be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2253fa9e4066Sahrens 22541195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 22551195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2256b5989ec7Seschrock 225754d692b7SGeorge Wilson /* 225854d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 225954d692b7SGeorge Wilson */ 22606f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 22616f834bc1SMatthew Ahrens KM_SLEEP); 22626f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 22636f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 22646f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 22656f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 22666f834bc1SMatthew Ahrens } 226754d692b7SGeorge Wilson 2268fa9e4066Sahrens /* 226999653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 227099653d4eSeschrock * value that will be returned by spa_version() since parsing the 227199653d4eSeschrock * configuration requires knowing the version number. 2272fa9e4066Sahrens */ 2273e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22741195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2275e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2276fa9e4066Sahrens 227799653d4eSeschrock if (error != 0) 22781195e687SMark J Musante return (error); 2279fa9e4066Sahrens 22800e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 228181cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 228281cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 22831195e687SMark J Musante 22841195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22851195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 22861195e687SMark J Musante } 2287fa9e4066Sahrens 2288fa9e4066Sahrens /* 2289fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2290fa9e4066Sahrens */ 2291e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22920bf246f5Smc error = vdev_open(rvd); 2293e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 22940bf246f5Smc if (error != 0) 22951195e687SMark J Musante return (error); 2296fa9e4066Sahrens 2297560e6e96Seschrock /* 229877e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 229977e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 230077e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 23011195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 230277e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 230377e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 230477e3a39cSMark J Musante * the vdev config. 23051195e687SMark J Musante * 23061195e687SMark J Musante * If we're assembling a new pool that's been split off from an 23071195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 23081195e687SMark J Musante * validation for now. 2309560e6e96Seschrock */ 23101195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23111195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2312d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 23131195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2314560e6e96Seschrock 23151195e687SMark J Musante if (error != 0) 23161195e687SMark J Musante return (error); 23171195e687SMark J Musante 23181195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2319be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2320560e6e96Seschrock } 2321560e6e96Seschrock 2322fa9e4066Sahrens /* 2323fa9e4066Sahrens * Find the best uberblock. 2324fa9e4066Sahrens */ 2325ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2326fa9e4066Sahrens 2327fa9e4066Sahrens /* 2328fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2329fa9e4066Sahrens */ 2330ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2331ad135b5dSChristopher Siden nvlist_free(label); 23321195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2333ad135b5dSChristopher Siden } 2334ea8dc4b6Seschrock 2335ea8dc4b6Seschrock /* 2336ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2337ea8dc4b6Seschrock */ 2338ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2339ad135b5dSChristopher Siden nvlist_free(label); 23401195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2341ad135b5dSChristopher Siden } 2342ad135b5dSChristopher Siden 2343ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2344ad135b5dSChristopher Siden nvlist_t *features; 2345ad135b5dSChristopher Siden 2346ad135b5dSChristopher Siden /* 2347ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2348ad135b5dSChristopher Siden * MOS in the label, return failure. 2349ad135b5dSChristopher Siden */ 2350ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2351ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2352ad135b5dSChristopher Siden nvlist_free(label); 2353ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2354ad135b5dSChristopher Siden ENXIO)); 2355ad135b5dSChristopher Siden } 2356ad135b5dSChristopher Siden 2357ad135b5dSChristopher Siden /* 2358ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2359ad135b5dSChristopher Siden * from the label. 2360ad135b5dSChristopher Siden */ 2361ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2362ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2363ad135b5dSChristopher Siden } 2364ad135b5dSChristopher Siden 2365ad135b5dSChristopher Siden nvlist_free(label); 2366ad135b5dSChristopher Siden 2367ad135b5dSChristopher Siden /* 2368ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2369ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2370ad135b5dSChristopher Siden * cannot open a pool. 2371ad135b5dSChristopher Siden */ 2372ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2373ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2374ad135b5dSChristopher Siden 2375ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2376ad135b5dSChristopher Siden 0); 2377ad135b5dSChristopher Siden 2378ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2379ad135b5dSChristopher Siden NULL); nvp != NULL; 2380ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2381ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2382ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2383ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2384ad135b5dSChristopher Siden } 2385ad135b5dSChristopher Siden } 2386ad135b5dSChristopher Siden 2387ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2388ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2389ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2390ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2391ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2392ad135b5dSChristopher Siden ENOTSUP)); 2393ad135b5dSChristopher Siden } 2394ad135b5dSChristopher Siden 2395ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2396ad135b5dSChristopher Siden } 2397fa9e4066Sahrens 2398fa9e4066Sahrens /* 2399fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 24004b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 24014b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 24024b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 24034b964adaSGeorge Wilson * can handle missing vdevs. 2404fa9e4066Sahrens */ 24054b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 24064b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 24071195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 24081195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 24091195e687SMark J Musante 24101195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 24111195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24121195e687SMark J Musante spa_try_repair(spa, config); 24131195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 24141195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 24151195e687SMark J Musante spa->spa_config_splitting = NULL; 2416fa9e4066Sahrens } 2417fa9e4066Sahrens 2418fa9e4066Sahrens /* 2419fa9e4066Sahrens * Initialize internal SPA structures. 2420fa9e4066Sahrens */ 2421fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2422fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2423468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2424c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2425468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2426468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2427b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 24283f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2429b24ab676SJeff Bonwick 2430ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 24311195e687SMark J Musante if (error) 24321195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2433fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2434fa9e4066Sahrens 24351195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 24361195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2437fa9e4066Sahrens 2438ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2439ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 244057221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2441ad135b5dSChristopher Siden 2442ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2443ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2444ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2445ad135b5dSChristopher Siden } 2446ad135b5dSChristopher Siden 2447ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2448ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2449ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2450ad135b5dSChristopher Siden } 2451ad135b5dSChristopher Siden 2452ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2453ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2454ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2455ad135b5dSChristopher Siden } 2456ad135b5dSChristopher Siden 245757221772SChristopher Siden enabled_feat = fnvlist_alloc(); 245857221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2459ad135b5dSChristopher Siden 24602acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 246157221772SChristopher Siden unsup_feat, enabled_feat)) 2462ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2463ad135b5dSChristopher Siden 2464ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 24652acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 246657221772SChristopher Siden unsup_feat, enabled_feat)) { 2467ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 246857221772SChristopher Siden } 2469ad135b5dSChristopher Siden } 2470ad135b5dSChristopher Siden 247157221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 247257221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 247357221772SChristopher Siden 2474ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 247557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 247657221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2477ad135b5dSChristopher Siden } 2478ad135b5dSChristopher Siden 247957221772SChristopher Siden fnvlist_free(enabled_feat); 248057221772SChristopher Siden fnvlist_free(unsup_feat); 2481ad135b5dSChristopher Siden 2482ad135b5dSChristopher Siden if (!missing_feat_read) { 2483ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2484ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2485ad135b5dSChristopher Siden } 2486ad135b5dSChristopher Siden 2487ad135b5dSChristopher Siden /* 2488ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2489ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2490ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2491ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2492ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2493ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2494ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2495ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2496ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2497ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2498ad135b5dSChristopher Siden * available for open in read-only mode. 2499ad135b5dSChristopher Siden * 2500ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2501ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2502ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2503ad135b5dSChristopher Siden * userland in order to know whether to display the 2504ad135b5dSChristopher Siden * abovementioned note. 2505ad135b5dSChristopher Siden */ 2506ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2507ad135b5dSChristopher Siden spa_writeable(spa))) { 2508ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2509ad135b5dSChristopher Siden ENOTSUP)); 2510ad135b5dSChristopher Siden } 251143466aaeSMax Grossman 251243466aaeSMax Grossman /* 251343466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 251443466aaeSMax Grossman * cache during SPA initialization. 251543466aaeSMax Grossman */ 251643466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 251743466aaeSMax Grossman uint64_t refcount; 251843466aaeSMax Grossman 251943466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 252043466aaeSMax Grossman &spa_feature_table[i], &refcount); 252143466aaeSMax Grossman if (error == 0) { 252243466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 252343466aaeSMax Grossman } else if (error == ENOTSUP) { 252443466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 252543466aaeSMax Grossman SPA_FEATURE_DISABLED; 252643466aaeSMax Grossman } else { 252743466aaeSMax Grossman return (spa_vdev_err(rvd, 252843466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 252943466aaeSMax Grossman } 253043466aaeSMax Grossman } 253143466aaeSMax Grossman } 253243466aaeSMax Grossman 253343466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 253443466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 25355d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 253643466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2537ad135b5dSChristopher Siden } 2538ad135b5dSChristopher Siden 2539ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2540ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2541ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2542ad135b5dSChristopher Siden if (error != 0) 2543ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2544ad135b5dSChristopher Siden 2545fa9e4066Sahrens if (!mosconfig) { 254695173954Sek uint64_t hostid; 2547871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2548871a9500SMark J Musante 2549871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2550871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2551fa9e4066Sahrens 255288ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 255377650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 255495173954Sek char *hostname; 255595173954Sek unsigned long myhostid = 0; 255695173954Sek 255788ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 255895173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 255995173954Sek 25605679c89fSjv #ifdef _KERNEL 25615679c89fSjv myhostid = zone_get_hostid(NULL); 25625679c89fSjv #else /* _KERNEL */ 25635679c89fSjv /* 25645679c89fSjv * We're emulating the system's hostid in userland, so 25655679c89fSjv * we can't use zone_get_hostid(). 25665679c89fSjv */ 256795173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 25685679c89fSjv #endif /* _KERNEL */ 256917194a52Slling if (hostid != 0 && myhostid != 0 && 25705679c89fSjv hostid != myhostid) { 2571871a9500SMark J Musante nvlist_free(nvconfig); 257295173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 257395173954Sek "loaded as it was last accessed by " 257477650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2575654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2576e14bb325SJeff Bonwick spa_name(spa), hostname, 257795173954Sek (unsigned long)hostid); 2578be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 257995173954Sek } 258095173954Sek } 2581c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2582c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2583c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2584c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 258595173954Sek 258688ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2587fa9e4066Sahrens spa_unload(spa); 2588fa9e4066Sahrens spa_deactivate(spa); 25898ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2590fa9e4066Sahrens 25911195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2592fa9e4066Sahrens } 2593fa9e4066Sahrens 259445818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 259545818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 259645818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 259745818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 259845818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 259945818ee1SMatthew Ahrens if (error == ENOENT) { 260045818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 260145818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 260245818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 260345818ee1SMatthew Ahrens } else if (error != 0) { 260445818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 260545818ee1SMatthew Ahrens } 260645818ee1SMatthew Ahrens 2607cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2608cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2609cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2610cde58dbcSMatthew Ahrens if (error != 0) 26111195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2612fa9e4066Sahrens 261399653d4eSeschrock /* 261499653d4eSeschrock * Load the bit that tells us to use the new accounting function 261599653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 261699653d4eSeschrock * be present. 261799653d4eSeschrock */ 26181195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 26191195e687SMark J Musante if (error != 0 && error != ENOENT) 26201195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 262199653d4eSeschrock 26223f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 26233f9d6ad7SLin Ling &spa->spa_creation_version); 26243f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 26253f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26263f9d6ad7SLin Ling 2627fa9e4066Sahrens /* 2628ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2629ea8dc4b6Seschrock * not be present. 2630fa9e4066Sahrens */ 26311195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 26321195e687SMark J Musante if (error != 0 && error != ENOENT) 26331195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2634ea8dc4b6Seschrock 26351195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 26361195e687SMark J Musante &spa->spa_errlog_scrub); 26371195e687SMark J Musante if (error != 0 && error != ENOENT) 26381195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2639ea8dc4b6Seschrock 264006eeb2adSek /* 264106eeb2adSek * Load the history object. If we have an older pool, this 264206eeb2adSek * will not be present. 264306eeb2adSek */ 26441195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 26451195e687SMark J Musante if (error != 0 && error != ENOENT) 26461195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26471195e687SMark J Musante 2648215198a6SJoe Stein /* 2649215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2650215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2651215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2652215198a6SJoe Stein * spa_sync_config_object. 2653215198a6SJoe Stein */ 2654215198a6SJoe Stein 2655215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2656215198a6SJoe Stein nvlist_t *mos_config; 2657215198a6SJoe Stein if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2658215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2659215198a6SJoe Stein 2660215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 2661215198a6SJoe Stein &spa->spa_all_vdev_zaps); 2662215198a6SJoe Stein 2663555da511SPaul Dagnelie if (error == ENOENT) { 2664555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 2665555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 2666555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 2667555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2668555da511SPaul Dagnelie } else if (error != 0) { 2669215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2670555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2671215198a6SJoe Stein /* 2672215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2673215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2674215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2675215198a6SJoe Stein */ 2676215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 2677215198a6SJoe Stein /* 2678215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 2679215198a6SJoe Stein * before this. Better be sure of it. 2680215198a6SJoe Stein */ 2681215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2682215198a6SJoe Stein } 2683215198a6SJoe Stein nvlist_free(mos_config); 2684215198a6SJoe Stein 26851195e687SMark J Musante /* 26861195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 26871195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 26881195e687SMark J Musante * devices. 26891195e687SMark J Musante */ 269006eeb2adSek 269199653d4eSeschrock /* 269299653d4eSeschrock * Load any hot spares for this pool. 269399653d4eSeschrock */ 26941195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.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) { 2698e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2699fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 27001195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 27011195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 270299653d4eSeschrock 2703e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 270499653d4eSeschrock spa_load_spares(spa); 2705e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27061195e687SMark J Musante } else if (error == 0) { 27071195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 270899653d4eSeschrock } 270999653d4eSeschrock 2710fa94a07fSbrendan /* 2711fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2712fa94a07fSbrendan */ 27131195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2714fa94a07fSbrendan &spa->spa_l2cache.sav_object); 27151195e687SMark J Musante if (error != 0 && error != ENOENT) 27161195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27171195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2718fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2719fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 27201195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 27211195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2722fa94a07fSbrendan 2723e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2724fa94a07fSbrendan spa_load_l2cache(spa); 2725e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27261195e687SMark J Musante } else if (error == 0) { 27271195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2728fa94a07fSbrendan } 2729fa94a07fSbrendan 2730990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2731ecd6cf80Smarks 27321195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 27331195e687SMark J Musante if (error && error != ENOENT) 27341195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2735b1b8ab34Slling 2736b1b8ab34Slling if (error == 0) { 27371195e687SMark J Musante uint64_t autoreplace; 27381195e687SMark J Musante 27391195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 27401195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 27411195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 27421195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 27431195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 27447855d95bSToomas Soome spa_prop_find(spa, ZPOOL_PROP_BOOTSIZE, &spa->spa_bootsize); 27451195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 27461195e687SMark J Musante &spa->spa_dedup_ditto); 27471195e687SMark J Musante 2748b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2749b1b8ab34Slling } 2750b1b8ab34Slling 27513d7072f8Seschrock /* 27523d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 27533d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 27543d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 27553d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 27563d7072f8Seschrock * over. 27573d7072f8Seschrock */ 2758b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 27593d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2760b693757aSEric Schrock /* 2761b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2762b693757aSEric Schrock * at this point we're using the spare definitions from 2763b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2764b693757aSEric Schrock */ 2765b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2766b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2767b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2768b693757aSEric Schrock } 2769b693757aSEric Schrock } 27703d7072f8Seschrock 2771ea8dc4b6Seschrock /* 2772560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2773ea8dc4b6Seschrock */ 2774560e6e96Seschrock vdev_load(rvd); 27750373e76bSbonwick 2776fa9e4066Sahrens /* 2777fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2778fa9e4066Sahrens */ 2779e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2780fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2781e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2782fa9e4066Sahrens 2783b24ab676SJeff Bonwick /* 2784b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2785b24ab676SJeff Bonwick */ 2786b24ab676SJeff Bonwick error = ddt_load(spa); 27871195e687SMark J Musante if (error != 0) 27881195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2789b24ab676SJeff Bonwick 2790485bbbf5SGeorge Wilson spa_update_dspace(spa); 2791485bbbf5SGeorge Wilson 2792b24ab676SJeff Bonwick /* 27934b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 27944b964adaSGeorge Wilson * information which might be missing. If we fail to validate 27954b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 27964b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 27974b964adaSGeorge Wilson * over. 2798b24ab676SJeff Bonwick */ 27991195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2800871a9500SMark J Musante nvlist_t *nvconfig; 2801871a9500SMark J Musante 2802871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2803871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2804871a9500SMark J Musante 28054b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 28064b964adaSGeorge Wilson nvlist_free(nvconfig); 28074b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 28084b964adaSGeorge Wilson ENXIO)); 28094b964adaSGeorge Wilson } 28101195e687SMark J Musante nvlist_free(nvconfig); 28111195e687SMark J Musante 28124b964adaSGeorge Wilson /* 2813ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 28144b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 28154b964adaSGeorge Wilson * more toplevel vdevs are faulted. 28164b964adaSGeorge Wilson */ 28174b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2818be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 28194b964adaSGeorge Wilson 282023367a2fSMatthew Ahrens if (spa_writeable(spa) && spa_check_logs(spa)) { 28211195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 28221195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 28231195e687SMark J Musante } 2824b24ab676SJeff Bonwick } 2825b24ab676SJeff Bonwick 2826ad135b5dSChristopher Siden if (missing_feat_write) { 2827ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2828ad135b5dSChristopher Siden 2829ad135b5dSChristopher Siden /* 2830ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2831ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2832ad135b5dSChristopher Siden * information and can return to userland. 2833ad135b5dSChristopher Siden */ 2834ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2835ad135b5dSChristopher Siden } 2836ad135b5dSChristopher Siden 28374b964adaSGeorge Wilson /* 28384b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 28394b964adaSGeorge Wilson * to start pushing transactions. 28404b964adaSGeorge Wilson */ 28414b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 28424b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 28434b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 28444b964adaSGeorge Wilson error)); 28454b964adaSGeorge Wilson } 28464b964adaSGeorge Wilson 2847468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2848468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 28495dabedeeSbonwick dmu_tx_t *tx; 28500373e76bSbonwick int need_update = B_FALSE; 285112380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 28528ad4d6ddSJeff Bonwick 28538ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 28545dabedeeSbonwick 28550373e76bSbonwick /* 28560373e76bSbonwick * Claim log blocks that haven't been committed yet. 28570373e76bSbonwick * This must all happen in a single txg. 2858b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2859b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2860468c413aSTim Haley * Price of rollback is that we abandon the log. 28610373e76bSbonwick */ 2862b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2863b24ab676SJeff Bonwick 286412380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 286512380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 28660b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2867fa9e4066Sahrens dmu_tx_commit(tx); 2868fa9e4066Sahrens 2869b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2870b24ab676SJeff Bonwick 28711195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2872fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2873fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2874fa9e4066Sahrens 2875fa9e4066Sahrens /* 2876b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2877b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2878b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2879b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2880b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2881fa9e4066Sahrens */ 2882b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 28830e34b6a7Sbonwick 28840e34b6a7Sbonwick /* 28850373e76bSbonwick * If the config cache is stale, or we have uninitialized 28860373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2887bc758434SLin Ling * 28884b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2889bc758434SLin Ling * in-core spa_config and update the disk labels. 28900e34b6a7Sbonwick */ 28910373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 28924b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 28934b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 28944b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 28950373e76bSbonwick need_update = B_TRUE; 28960373e76bSbonwick 28978ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 28980373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 28990373e76bSbonwick need_update = B_TRUE; 29000e34b6a7Sbonwick 29010e34b6a7Sbonwick /* 29020373e76bSbonwick * Update the config cache asychronously in case we're the 29030373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 29040e34b6a7Sbonwick */ 29050373e76bSbonwick if (need_update) 29060373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 29078ad4d6ddSJeff Bonwick 29088ad4d6ddSJeff Bonwick /* 29098ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 29108ad4d6ddSJeff Bonwick */ 29113f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 29123f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 29138ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2914503ad85cSMatthew Ahrens 29154445fffbSMatthew Ahrens /* 29164445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 29174445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 29184445fffbSMatthew Ahrens */ 29194445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 29204445fffbSMatthew Ahrens 2921503ad85cSMatthew Ahrens /* 2922503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2923503ad85cSMatthew Ahrens */ 2924503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2925503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2926ca45db41SChris Kirby 2927ca45db41SChris Kirby /* 2928ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2929ca45db41SChris Kirby */ 2930ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2931fa9e4066Sahrens } 2932fa9e4066Sahrens 29331195e687SMark J Musante return (0); 2934fa9e4066Sahrens } 2935fa9e4066Sahrens 2936468c413aSTim Haley static int 2937468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2938468c413aSTim Haley { 2939f9af39baSGeorge Wilson int mode = spa->spa_mode; 2940f9af39baSGeorge Wilson 2941468c413aSTim Haley spa_unload(spa); 2942468c413aSTim Haley spa_deactivate(spa); 2943468c413aSTim Haley 2944e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 2945468c413aSTim Haley 2946f9af39baSGeorge Wilson spa_activate(spa, mode); 2947468c413aSTim Haley spa_async_suspend(spa); 2948468c413aSTim Haley 29491195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2950468c413aSTim Haley } 2951468c413aSTim Haley 2952ad135b5dSChristopher Siden /* 2953ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2954ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2955ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2956ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2957ad135b5dSChristopher Siden * spa_load(). 2958ad135b5dSChristopher Siden */ 2959468c413aSTim Haley static int 2960468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2961c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2962468c413aSTim Haley { 2963ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2964468c413aSTim Haley nvlist_t *config = NULL; 2965468c413aSTim Haley int load_error, rewind_error; 2966c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2967468c413aSTim Haley uint64_t min_txg; 2968468c413aSTim Haley 2969a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2970468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 29711195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2972a33cae98STim Haley } else { 2973468c413aSTim Haley spa->spa_load_max_txg = max_request; 2974e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 2975e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 2976a33cae98STim Haley } 2977468c413aSTim Haley 29781195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 29791195e687SMark J Musante mosconfig); 2980468c413aSTim Haley if (load_error == 0) 2981468c413aSTim Haley return (0); 2982468c413aSTim Haley 2983468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2984468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2985468c413aSTim Haley 2986468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2987468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2988468c413aSTim Haley 2989c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2990468c413aSTim Haley nvlist_free(config); 2991468c413aSTim Haley return (load_error); 2992468c413aSTim Haley } 2993468c413aSTim Haley 2994ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2995ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 29961195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2997ad135b5dSChristopher Siden } else { 2998ad135b5dSChristopher Siden /* 2999ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 3000ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 3001ad135b5dSChristopher Siden * to rewind. 3002ad135b5dSChristopher Siden */ 3003ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3004ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3005ad135b5dSChristopher Siden } 3006468c413aSTim Haley 3007c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3008c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3009c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3010c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3011468c413aSTim Haley 3012c8ee1847SVictor Latushkin /* 3013c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3014c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3015c8ee1847SVictor Latushkin */ 3016c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3017c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3018c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3019468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 3020468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 3021468c413aSTim Haley } 3022468c413aSTim Haley 3023468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3024468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3025468c413aSTim Haley 3026468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3027468c413aSTim Haley spa_config_set(spa, config); 30287d3000f7SPavel Zakharov else 30297d3000f7SPavel Zakharov nvlist_free(config); 3030468c413aSTim Haley 3031ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3032ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3033ad135b5dSChristopher Siden return (rewind_error); 3034ad135b5dSChristopher Siden } else { 3035ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3036ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3037ad135b5dSChristopher Siden spa->spa_load_info); 3038ad135b5dSChristopher Siden 3039ad135b5dSChristopher Siden /* Restore the initial load info */ 3040ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3041ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3042ad135b5dSChristopher Siden 3043ad135b5dSChristopher Siden return (load_error); 3044ad135b5dSChristopher Siden } 3045468c413aSTim Haley } 3046468c413aSTim Haley 3047fa9e4066Sahrens /* 3048fa9e4066Sahrens * Pool Open/Import 3049fa9e4066Sahrens * 3050fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3051fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3052fa9e4066Sahrens * case of an open, the pool configuration will exist in the 30533d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3054fa9e4066Sahrens * 3055fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3056fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3057fa9e4066Sahrens * ambiguous state. 3058fa9e4066Sahrens */ 3059fa9e4066Sahrens static int 3060468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3061468c413aSTim Haley nvlist_t **config) 3062fa9e4066Sahrens { 3063fa9e4066Sahrens spa_t *spa; 30644b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3065fa9e4066Sahrens int error; 3066fa9e4066Sahrens int locked = B_FALSE; 3067fa9e4066Sahrens 3068fa9e4066Sahrens *spapp = NULL; 3069fa9e4066Sahrens 3070fa9e4066Sahrens /* 3071fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3072fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3073fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3074fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3075fa9e4066Sahrens */ 3076fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3077fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3078fa9e4066Sahrens locked = B_TRUE; 3079fa9e4066Sahrens } 3080fa9e4066Sahrens 3081fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3082fa9e4066Sahrens if (locked) 3083fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3084be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3085fa9e4066Sahrens } 3086468c413aSTim Haley 3087fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 30884b44c88cSTim Haley zpool_rewind_policy_t policy; 30894b44c88cSTim Haley 30904b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 30914b44c88cSTim Haley &policy); 30924b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 30934b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3094fa9e4066Sahrens 30958ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3096fa9e4066Sahrens 3097468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3098468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3099468c413aSTim Haley 3100468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 3101c8ee1847SVictor Latushkin policy.zrp_request); 3102fa9e4066Sahrens 3103fa9e4066Sahrens if (error == EBADF) { 3104fa9e4066Sahrens /* 3105560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3106560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3107560e6e96Seschrock * that the pool has been exported or destroyed. If 3108560e6e96Seschrock * this is the case, the config cache is out of sync and 3109560e6e96Seschrock * we should remove the pool from the namespace. 3110fa9e4066Sahrens */ 3111fa9e4066Sahrens spa_unload(spa); 3112fa9e4066Sahrens spa_deactivate(spa); 3113c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 3114fa9e4066Sahrens spa_remove(spa); 3115fa9e4066Sahrens if (locked) 3116fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3117be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3118ea8dc4b6Seschrock } 3119ea8dc4b6Seschrock 3120ea8dc4b6Seschrock if (error) { 3121fa9e4066Sahrens /* 3122fa9e4066Sahrens * We can't open the pool, but we still have useful 3123fa9e4066Sahrens * information: the state of each vdev after the 3124fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3125fa9e4066Sahrens */ 31264b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3127468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3128468c413aSTim Haley KM_SLEEP) == 0); 31294b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 31304b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 31314b964adaSGeorge Wilson spa->spa_load_info) == 0); 31324b964adaSGeorge Wilson } 3133fa9e4066Sahrens spa_unload(spa); 3134fa9e4066Sahrens spa_deactivate(spa); 3135468c413aSTim Haley spa->spa_last_open_failed = error; 3136fa9e4066Sahrens if (locked) 3137fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3138fa9e4066Sahrens *spapp = NULL; 3139fa9e4066Sahrens return (error); 3140fa9e4066Sahrens } 3141fa9e4066Sahrens } 3142fa9e4066Sahrens 3143fa9e4066Sahrens spa_open_ref(spa, tag); 31443d7072f8Seschrock 3145468c413aSTim Haley if (config != NULL) 3146468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3147468c413aSTim Haley 31484b964adaSGeorge Wilson /* 31494b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 31504b964adaSGeorge Wilson * gathered while doing the load. 31514b964adaSGeorge Wilson */ 31524b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 31534b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 31544b964adaSGeorge Wilson spa->spa_load_info) == 0); 31554b964adaSGeorge Wilson } 31564b964adaSGeorge Wilson 3157a33cae98STim Haley if (locked) { 3158a33cae98STim Haley spa->spa_last_open_failed = 0; 3159a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3160a33cae98STim Haley spa->spa_load_txg = 0; 3161fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3162a33cae98STim Haley } 3163fa9e4066Sahrens 3164fa9e4066Sahrens *spapp = spa; 3165fa9e4066Sahrens 3166fa9e4066Sahrens return (0); 3167fa9e4066Sahrens } 3168fa9e4066Sahrens 3169468c413aSTim Haley int 3170468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3171468c413aSTim Haley nvlist_t **config) 3172468c413aSTim Haley { 3173468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3174468c413aSTim Haley } 3175468c413aSTim Haley 3176fa9e4066Sahrens int 3177fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3178fa9e4066Sahrens { 3179468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3180fa9e4066Sahrens } 3181fa9e4066Sahrens 3182ea8dc4b6Seschrock /* 3183ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3184ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3185ea8dc4b6Seschrock */ 3186ea8dc4b6Seschrock spa_t * 3187ea8dc4b6Seschrock spa_inject_addref(char *name) 3188ea8dc4b6Seschrock { 3189ea8dc4b6Seschrock spa_t *spa; 3190ea8dc4b6Seschrock 3191ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3192ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3193ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3194ea8dc4b6Seschrock return (NULL); 3195ea8dc4b6Seschrock } 3196ea8dc4b6Seschrock spa->spa_inject_ref++; 3197ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3198ea8dc4b6Seschrock 3199ea8dc4b6Seschrock return (spa); 3200ea8dc4b6Seschrock } 3201ea8dc4b6Seschrock 3202ea8dc4b6Seschrock void 3203ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3204ea8dc4b6Seschrock { 3205ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3206ea8dc4b6Seschrock spa->spa_inject_ref--; 3207ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3208ea8dc4b6Seschrock } 3209ea8dc4b6Seschrock 3210fa94a07fSbrendan /* 3211fa94a07fSbrendan * Add spares device information to the nvlist. 3212fa94a07fSbrendan */ 321399653d4eSeschrock static void 321499653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 321599653d4eSeschrock { 321699653d4eSeschrock nvlist_t **spares; 321799653d4eSeschrock uint_t i, nspares; 321899653d4eSeschrock nvlist_t *nvroot; 321999653d4eSeschrock uint64_t guid; 322099653d4eSeschrock vdev_stat_t *vs; 322199653d4eSeschrock uint_t vsc; 322239c23413Seschrock uint64_t pool; 322399653d4eSeschrock 32246809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32256809eb4eSEric Schrock 3226fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 322799653d4eSeschrock return; 322899653d4eSeschrock 322999653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 323099653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3231fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 323299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 323399653d4eSeschrock if (nspares != 0) { 323499653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 323599653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 323699653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 323799653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 323899653d4eSeschrock 323999653d4eSeschrock /* 324099653d4eSeschrock * Go through and find any spares which have since been 324199653d4eSeschrock * repurposed as an active spare. If this is the case, update 324299653d4eSeschrock * their status appropriately. 324399653d4eSeschrock */ 324499653d4eSeschrock for (i = 0; i < nspares; i++) { 324599653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 324699653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 324789a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 324889a89ebfSlling pool != 0ULL) { 324999653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 32503f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 325199653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 325299653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 325399653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 325499653d4eSeschrock } 325599653d4eSeschrock } 325699653d4eSeschrock } 325799653d4eSeschrock } 325899653d4eSeschrock 3259fa94a07fSbrendan /* 3260fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3261fa94a07fSbrendan */ 3262fa94a07fSbrendan static void 3263fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3264fa94a07fSbrendan { 3265fa94a07fSbrendan nvlist_t **l2cache; 3266fa94a07fSbrendan uint_t i, j, nl2cache; 3267fa94a07fSbrendan nvlist_t *nvroot; 3268fa94a07fSbrendan uint64_t guid; 3269fa94a07fSbrendan vdev_t *vd; 3270fa94a07fSbrendan vdev_stat_t *vs; 3271fa94a07fSbrendan uint_t vsc; 3272fa94a07fSbrendan 32736809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32746809eb4eSEric Schrock 3275fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3276fa94a07fSbrendan return; 3277fa94a07fSbrendan 3278fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3279fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3280fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3281fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3282fa94a07fSbrendan if (nl2cache != 0) { 3283fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3284fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3285fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3286fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3287fa94a07fSbrendan 3288fa94a07fSbrendan /* 3289fa94a07fSbrendan * Update level 2 cache device stats. 3290fa94a07fSbrendan */ 3291fa94a07fSbrendan 3292fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3293fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3294fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3295fa94a07fSbrendan 3296fa94a07fSbrendan vd = NULL; 3297fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3298fa94a07fSbrendan if (guid == 3299fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3300fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3301fa94a07fSbrendan break; 3302fa94a07fSbrendan } 3303fa94a07fSbrendan } 3304fa94a07fSbrendan ASSERT(vd != NULL); 3305fa94a07fSbrendan 3306fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 33073f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 33083f9d6ad7SLin Ling == 0); 3309fa94a07fSbrendan vdev_get_stats(vd, vs); 3310fa94a07fSbrendan } 3311fa94a07fSbrendan } 3312fa94a07fSbrendan } 3313fa94a07fSbrendan 3314ad135b5dSChristopher Siden static void 3315ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3316ad135b5dSChristopher Siden { 3317ad135b5dSChristopher Siden nvlist_t *features; 3318ad135b5dSChristopher Siden zap_cursor_t zc; 3319ad135b5dSChristopher Siden zap_attribute_t za; 3320ad135b5dSChristopher Siden 3321ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3322ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3323ad135b5dSChristopher Siden 3324ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3325ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3326ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3327ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3328ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3329ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3330ad135b5dSChristopher Siden za.za_num_integers == 1); 3331b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3332ad135b5dSChristopher Siden za.za_first_integer)); 3333ad135b5dSChristopher Siden } 3334ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3335ad135b5dSChristopher Siden } 3336ad135b5dSChristopher Siden 3337ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3338ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3339ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3340ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3341ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3342ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3343ad135b5dSChristopher Siden za.za_num_integers == 1); 3344b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3345ad135b5dSChristopher Siden za.za_first_integer)); 3346ad135b5dSChristopher Siden } 3347ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3348ad135b5dSChristopher Siden } 3349ad135b5dSChristopher Siden 3350ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3351ad135b5dSChristopher Siden features) == 0); 3352ad135b5dSChristopher Siden nvlist_free(features); 3353ad135b5dSChristopher Siden } 3354ad135b5dSChristopher Siden 3355fa9e4066Sahrens int 3356ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3357ad135b5dSChristopher Siden char *altroot, size_t buflen) 3358fa9e4066Sahrens { 3359fa9e4066Sahrens int error; 3360fa9e4066Sahrens spa_t *spa; 3361fa9e4066Sahrens 3362fa9e4066Sahrens *config = NULL; 3363468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3364fa9e4066Sahrens 33656809eb4eSEric Schrock if (spa != NULL) { 33666809eb4eSEric Schrock /* 33676809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 33686809eb4eSEric Schrock * or l2cache devices could change and the config would be 33696809eb4eSEric Schrock * self-inconsistent. 33706809eb4eSEric Schrock */ 33716809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3372ea8dc4b6Seschrock 33736809eb4eSEric Schrock if (*config != NULL) { 337411027bc7STim Haley uint64_t loadtimes[2]; 337511027bc7STim Haley 337611027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 337711027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 337811027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 337911027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 338011027bc7STim Haley 3381e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 33826809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 33836809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3384e14bb325SJeff Bonwick 33856809eb4eSEric Schrock if (spa_suspended(spa)) 33866809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 33876809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 33886809eb4eSEric Schrock spa->spa_failmode) == 0); 33896809eb4eSEric Schrock 33906809eb4eSEric Schrock spa_add_spares(spa, *config); 33916809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3392ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 33936809eb4eSEric Schrock } 339499653d4eSeschrock } 339599653d4eSeschrock 3396ea8dc4b6Seschrock /* 3397ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3398ea8dc4b6Seschrock * and call spa_lookup() directly. 3399ea8dc4b6Seschrock */ 3400ea8dc4b6Seschrock if (altroot) { 3401ea8dc4b6Seschrock if (spa == NULL) { 3402ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3403ea8dc4b6Seschrock spa = spa_lookup(name); 3404ea8dc4b6Seschrock if (spa) 3405ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3406ea8dc4b6Seschrock else 3407ea8dc4b6Seschrock altroot[0] = '\0'; 3408ea8dc4b6Seschrock spa = NULL; 3409ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3410ea8dc4b6Seschrock } else { 3411ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3412ea8dc4b6Seschrock } 3413ea8dc4b6Seschrock } 3414ea8dc4b6Seschrock 34156809eb4eSEric Schrock if (spa != NULL) { 34166809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3417fa9e4066Sahrens spa_close(spa, FTAG); 34186809eb4eSEric Schrock } 3419fa9e4066Sahrens 3420fa9e4066Sahrens return (error); 3421fa9e4066Sahrens } 3422fa9e4066Sahrens 342399653d4eSeschrock /* 3424fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3425fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3426fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3427fa94a07fSbrendan * specified, as long as they are well-formed. 342899653d4eSeschrock */ 342999653d4eSeschrock static int 3430fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3431fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3432fa94a07fSbrendan vdev_labeltype_t label) 343399653d4eSeschrock { 3434fa94a07fSbrendan nvlist_t **dev; 3435fa94a07fSbrendan uint_t i, ndev; 343699653d4eSeschrock vdev_t *vd; 343799653d4eSeschrock int error; 343899653d4eSeschrock 3439e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3440e14bb325SJeff Bonwick 344199653d4eSeschrock /* 3442fa94a07fSbrendan * It's acceptable to have no devs specified. 344399653d4eSeschrock */ 3444fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 344599653d4eSeschrock return (0); 344699653d4eSeschrock 3447fa94a07fSbrendan if (ndev == 0) 3448be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 344999653d4eSeschrock 345099653d4eSeschrock /* 3451fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3452fa94a07fSbrendan * device type. 345399653d4eSeschrock */ 3454fa94a07fSbrendan if (spa_version(spa) < version) 3455be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 345699653d4eSeschrock 345739c23413Seschrock /* 3458fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 345939c23413Seschrock * checking. 346039c23413Seschrock */ 3461fa94a07fSbrendan sav->sav_pending = dev; 3462fa94a07fSbrendan sav->sav_npending = ndev; 346339c23413Seschrock 3464fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3465fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 346699653d4eSeschrock mode)) != 0) 346739c23413Seschrock goto out; 346899653d4eSeschrock 346999653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 347099653d4eSeschrock vdev_free(vd); 3471be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 347239c23413Seschrock goto out; 347399653d4eSeschrock } 347499653d4eSeschrock 3475fa94a07fSbrendan /* 3476e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3477e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3478fa94a07fSbrendan */ 3479e14bb325SJeff Bonwick #ifdef _KERNEL 3480fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3481fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3482be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3483cd0837ccSGeorge Wilson vdev_free(vd); 3484fa94a07fSbrendan goto out; 3485fa94a07fSbrendan } 3486e14bb325SJeff Bonwick #endif 348799653d4eSeschrock vd->vdev_top = vd; 348899653d4eSeschrock 348939c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3490fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3491fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 349239c23413Seschrock vd->vdev_guid) == 0); 349339c23413Seschrock } 349499653d4eSeschrock 349599653d4eSeschrock vdev_free(vd); 349639c23413Seschrock 3497fa94a07fSbrendan if (error && 3498fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 349939c23413Seschrock goto out; 350039c23413Seschrock else 350139c23413Seschrock error = 0; 350299653d4eSeschrock } 350399653d4eSeschrock 350439c23413Seschrock out: 3505fa94a07fSbrendan sav->sav_pending = NULL; 3506fa94a07fSbrendan sav->sav_npending = 0; 350739c23413Seschrock return (error); 350899653d4eSeschrock } 350999653d4eSeschrock 3510fa94a07fSbrendan static int 3511fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3512fa94a07fSbrendan { 3513fa94a07fSbrendan int error; 3514fa94a07fSbrendan 3515e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3516e14bb325SJeff Bonwick 3517fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3518fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3519fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3520fa94a07fSbrendan return (error); 3521fa94a07fSbrendan } 3522fa94a07fSbrendan 3523fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3524fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3525fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3526fa94a07fSbrendan } 3527fa94a07fSbrendan 3528fa94a07fSbrendan static void 3529fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3530fa94a07fSbrendan const char *config) 3531fa94a07fSbrendan { 3532fa94a07fSbrendan int i; 3533fa94a07fSbrendan 3534fa94a07fSbrendan if (sav->sav_config != NULL) { 3535fa94a07fSbrendan nvlist_t **olddevs; 3536fa94a07fSbrendan uint_t oldndevs; 3537fa94a07fSbrendan nvlist_t **newdevs; 3538fa94a07fSbrendan 3539fa94a07fSbrendan /* 3540fa94a07fSbrendan * Generate new dev list by concatentating with the 3541fa94a07fSbrendan * current dev list. 3542fa94a07fSbrendan */ 3543fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3544fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3545fa94a07fSbrendan 3546fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3547fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3548fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3549fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3550fa94a07fSbrendan KM_SLEEP) == 0); 3551fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3552fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3553fa94a07fSbrendan KM_SLEEP) == 0); 3554fa94a07fSbrendan 3555fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3556fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3557fa94a07fSbrendan 3558fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3559fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3560fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3561fa94a07fSbrendan nvlist_free(newdevs[i]); 3562fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3563fa94a07fSbrendan } else { 3564fa94a07fSbrendan /* 3565fa94a07fSbrendan * Generate a new dev list. 3566fa94a07fSbrendan */ 3567fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3568fa94a07fSbrendan KM_SLEEP) == 0); 3569fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3570fa94a07fSbrendan devs, ndevs) == 0); 3571fa94a07fSbrendan } 3572fa94a07fSbrendan } 3573fa94a07fSbrendan 3574fa94a07fSbrendan /* 3575fa94a07fSbrendan * Stop and drop level 2 ARC devices 3576fa94a07fSbrendan */ 3577fa94a07fSbrendan void 3578fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3579fa94a07fSbrendan { 3580fa94a07fSbrendan vdev_t *vd; 3581fa94a07fSbrendan int i; 3582fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3583fa94a07fSbrendan 3584fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3585fa94a07fSbrendan uint64_t pool; 3586fa94a07fSbrendan 3587fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3588fa94a07fSbrendan ASSERT(vd != NULL); 3589fa94a07fSbrendan 35908ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 35918ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3592fa94a07fSbrendan l2arc_remove_vdev(vd); 3593fa94a07fSbrendan } 3594fa94a07fSbrendan } 3595fa94a07fSbrendan 3596fa9e4066Sahrens /* 3597fa9e4066Sahrens * Pool Creation 3598fa9e4066Sahrens */ 3599fa9e4066Sahrens int 3600990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 36014445fffbSMatthew Ahrens nvlist_t *zplprops) 3602fa9e4066Sahrens { 3603fa9e4066Sahrens spa_t *spa; 3604990b4856Slling char *altroot = NULL; 36050373e76bSbonwick vdev_t *rvd; 3606fa9e4066Sahrens dsl_pool_t *dp; 3607fa9e4066Sahrens dmu_tx_t *tx; 3608573ca77eSGeorge Wilson int error = 0; 3609fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3610fa94a07fSbrendan nvlist_t **spares, **l2cache; 3611fa94a07fSbrendan uint_t nspares, nl2cache; 3612cde58dbcSMatthew Ahrens uint64_t version, obj; 3613ad135b5dSChristopher Siden boolean_t has_features; 3614fa9e4066Sahrens 3615fa9e4066Sahrens /* 3616fa9e4066Sahrens * If this pool already exists, return failure. 3617fa9e4066Sahrens */ 3618fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3619fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3620fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3621be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3622fa9e4066Sahrens } 3623fa9e4066Sahrens 3624fa9e4066Sahrens /* 3625fa9e4066Sahrens * Allocate a new spa_t structure. 3626fa9e4066Sahrens */ 3627990b4856Slling (void) nvlist_lookup_string(props, 3628990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3629468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 36308ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3631fa9e4066Sahrens 3632990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3633990b4856Slling spa_deactivate(spa); 3634990b4856Slling spa_remove(spa); 3635c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3636990b4856Slling return (error); 3637990b4856Slling } 3638990b4856Slling 3639ad135b5dSChristopher Siden has_features = B_FALSE; 3640ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3641ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3642ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3643ad135b5dSChristopher Siden has_features = B_TRUE; 3644ad135b5dSChristopher Siden } 3645ad135b5dSChristopher Siden 3646ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3647ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3648990b4856Slling version = SPA_VERSION; 3649ad135b5dSChristopher Siden } 3650ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3651b24ab676SJeff Bonwick 3652b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3653b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3654990b4856Slling spa->spa_uberblock.ub_version = version; 3655fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 36560f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 3657fa9e4066Sahrens 365854d692b7SGeorge Wilson /* 365954d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 366054d692b7SGeorge Wilson */ 36616f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 36626f834bc1SMatthew Ahrens KM_SLEEP); 36636f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 36646f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 36656f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 36666f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 36676f834bc1SMatthew Ahrens } 366854d692b7SGeorge Wilson 36690373e76bSbonwick /* 36700373e76bSbonwick * Create the root vdev. 36710373e76bSbonwick */ 3672e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 36730373e76bSbonwick 367499653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 36750373e76bSbonwick 367699653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 367799653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 36780373e76bSbonwick 3679b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3680be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 368199653d4eSeschrock 368299653d4eSeschrock if (error == 0 && 368399653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3684fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 368599653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3686573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3687573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3688573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3689573ca77eSGeorge Wilson } 36900373e76bSbonwick } 36910373e76bSbonwick 3692e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3693fa9e4066Sahrens 369499653d4eSeschrock if (error != 0) { 3695fa9e4066Sahrens spa_unload(spa); 3696fa9e4066Sahrens spa_deactivate(spa); 3697fa9e4066Sahrens spa_remove(spa); 3698fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3699fa9e4066Sahrens return (error); 3700fa9e4066Sahrens } 3701fa9e4066Sahrens 370299653d4eSeschrock /* 370399653d4eSeschrock * Get the list of spares, if specified. 370499653d4eSeschrock */ 370599653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 370699653d4eSeschrock &spares, &nspares) == 0) { 3707fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 370899653d4eSeschrock KM_SLEEP) == 0); 3709fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 371099653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3711e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 371299653d4eSeschrock spa_load_spares(spa); 3713e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3714fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3715fa94a07fSbrendan } 3716fa94a07fSbrendan 3717fa94a07fSbrendan /* 3718fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3719fa94a07fSbrendan */ 3720fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3721fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3722fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3723fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3724fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3725fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3726e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3727fa94a07fSbrendan spa_load_l2cache(spa); 3728e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3729fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 373099653d4eSeschrock } 373199653d4eSeschrock 3732ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 37330a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3734fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3735ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3736fa9e4066Sahrens 3737485bbbf5SGeorge Wilson /* 3738485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3739485bbbf5SGeorge Wilson */ 3740485bbbf5SGeorge Wilson ddt_create(spa); 3741485bbbf5SGeorge Wilson 3742485bbbf5SGeorge Wilson spa_update_dspace(spa); 3743485bbbf5SGeorge Wilson 3744fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3745fa9e4066Sahrens 3746fa9e4066Sahrens /* 3747fa9e4066Sahrens * Create the pool config object. 3748fa9e4066Sahrens */ 3749fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3750f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3751fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3752fa9e4066Sahrens 3753ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3754fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3755ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3756ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3757ea8dc4b6Seschrock } 3758fa9e4066Sahrens 3759ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3760ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3761ad135b5dSChristopher Siden 37623f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 37633f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 37643f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 37653f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 37663f9d6ad7SLin Ling } 37673f9d6ad7SLin Ling 3768990b4856Slling /* Newly created pools with the right version are always deflated. */ 3769990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3770990b4856Slling spa->spa_deflate = TRUE; 3771990b4856Slling if (zap_add(spa->spa_meta_objset, 3772990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3773990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3774990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3775990b4856Slling } 377699653d4eSeschrock } 377799653d4eSeschrock 3778fa9e4066Sahrens /* 3779cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3780fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3781fa9e4066Sahrens * keeps changing. 3782fa9e4066Sahrens */ 3783cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3784cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3785cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3786ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3787cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3788cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3789cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3790ea8dc4b6Seschrock } 3791b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3792cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3793fa9e4066Sahrens 379406eeb2adSek /* 379506eeb2adSek * Create the pool's history object. 379606eeb2adSek */ 3797990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3798990b4856Slling spa_history_create_obj(spa, tx); 3799990b4856Slling 380045818ee1SMatthew Ahrens /* 380145818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 380245818ee1SMatthew Ahrens */ 380345818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 380445818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 380545818ee1SMatthew Ahrens 3806990b4856Slling /* 3807990b4856Slling * Set pool properties. 3808990b4856Slling */ 3809990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3810990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 38110a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3812573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3813b24ab676SJeff Bonwick 3814379c004dSEric Schrock if (props != NULL) { 3815379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38163b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3817379c004dSEric Schrock } 381806eeb2adSek 3819fa9e4066Sahrens dmu_tx_commit(tx); 3820fa9e4066Sahrens 3821fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3822fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3823fa9e4066Sahrens 3824fa9e4066Sahrens /* 3825fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3826fa9e4066Sahrens * bean counters are appropriately updated. 3827fa9e4066Sahrens */ 3828fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3829fa9e4066Sahrens 3830c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3831ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 3832fa9e4066Sahrens 38334445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3834228975ccSek 3835bc9014e6SJustin Gibbs /* 3836bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 3837bc9014e6SJustin Gibbs * and are making their way through the eviction process. 3838bc9014e6SJustin Gibbs */ 3839bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 3840088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 38410f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 3842088f3894Sahrens 3843daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3844daaa36a7SGeorge Wilson 3845fa9e4066Sahrens return (0); 3846fa9e4066Sahrens } 3847fa9e4066Sahrens 3848e7cbe64fSgw #ifdef _KERNEL 3849e7cbe64fSgw /* 385021ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 385121ecdf64SLin Ling * during the system boot up time. 3852e7cbe64fSgw */ 385321ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 385421ecdf64SLin Ling 385521ecdf64SLin Ling static nvlist_t * 385621ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3857e7cbe64fSgw { 385821ecdf64SLin Ling nvlist_t *config; 3859e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3860e7cbe64fSgw uint64_t pgid; 3861e7cbe64fSgw 386221ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 386321ecdf64SLin Ling return (NULL); 386421ecdf64SLin Ling 3865e7cbe64fSgw /* 3866e7cbe64fSgw * Add this top-level vdev to the child array. 3867e7cbe64fSgw */ 386821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 386921ecdf64SLin Ling &nvtop) == 0); 387021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 387121ecdf64SLin Ling &pgid) == 0); 387221ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3873e7cbe64fSgw 3874e7cbe64fSgw /* 3875e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3876e7cbe64fSgw */ 3877e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 387821ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 387921ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3880e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3881e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3882e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3883e7cbe64fSgw &nvtop, 1) == 0); 3884e7cbe64fSgw 3885e7cbe64fSgw /* 3886e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3887e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3888e7cbe64fSgw */ 3889e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3890e7cbe64fSgw nvlist_free(nvroot); 389121ecdf64SLin Ling return (config); 3892e7cbe64fSgw } 3893e7cbe64fSgw 3894e7cbe64fSgw /* 389521ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 389621ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 389721ecdf64SLin Ling * device has a more recent txg. 3898051aabe6Staylor */ 389921ecdf64SLin Ling static void 390021ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3901051aabe6Staylor { 3902573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 390321ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3904051aabe6Staylor 390521ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 390621ecdf64SLin Ling nvlist_t *label; 390721ecdf64SLin Ling uint64_t label_txg; 3908051aabe6Staylor 390921ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 391021ecdf64SLin Ling &label) != 0) 391121ecdf64SLin Ling return; 3912051aabe6Staylor 391321ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 391421ecdf64SLin Ling &label_txg) == 0); 3915051aabe6Staylor 391621ecdf64SLin Ling /* 391721ecdf64SLin Ling * Do we have a better boot device? 391821ecdf64SLin Ling */ 391921ecdf64SLin Ling if (label_txg > *txg) { 392021ecdf64SLin Ling *txg = label_txg; 392121ecdf64SLin Ling *avd = vd; 3922051aabe6Staylor } 392321ecdf64SLin Ling nvlist_free(label); 3924051aabe6Staylor } 3925051aabe6Staylor } 3926051aabe6Staylor 3927e7cbe64fSgw /* 3928e7cbe64fSgw * Import a root pool. 3929e7cbe64fSgw * 3930051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3931051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3932051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3933e7cbe64fSgw * 3934e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3935e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3936e7cbe64fSgw * e.g. 3937e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3938e7cbe64fSgw */ 3939e7cbe64fSgw int 3940051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3941e7cbe64fSgw { 394221ecdf64SLin Ling spa_t *spa; 394321ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 394421ecdf64SLin Ling nvlist_t *config, *nvtop; 394521ecdf64SLin Ling uint64_t guid, txg; 3946e7cbe64fSgw char *pname; 3947e7cbe64fSgw int error; 3948e7cbe64fSgw 3949e7cbe64fSgw /* 395021ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3951e7cbe64fSgw */ 3952dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3953dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3954dedec472SJack Meng if (config == NULL) { 3955dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3956dedec472SJack Meng /* iscsi boot */ 3957dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3958dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3959dedec472SJack Meng } 3960dedec472SJack Meng } 3961dedec472SJack Meng #endif 3962dedec472SJack Meng if (config == NULL) { 3963ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 396421ecdf64SLin Ling devpath); 3965be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 396621ecdf64SLin Ling } 3967e7cbe64fSgw 396821ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 396921ecdf64SLin Ling &pname) == 0); 397021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3971e7cbe64fSgw 39726809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 39736809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 39746809eb4eSEric Schrock /* 39756809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 39766809eb4eSEric Schrock * can replace it with the correct config we just read in. 39776809eb4eSEric Schrock */ 39786809eb4eSEric Schrock spa_remove(spa); 39796809eb4eSEric Schrock } 39806809eb4eSEric Schrock 3981468c413aSTim Haley spa = spa_add(pname, config, NULL); 39826809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 39834b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3984e7cbe64fSgw 398521ecdf64SLin Ling /* 398621ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 398721ecdf64SLin Ling */ 398821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 398921ecdf64SLin Ling &nvtop) == 0); 399021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 399121ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 399221ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 399321ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 399421ecdf64SLin Ling if (error) { 399521ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 399621ecdf64SLin Ling nvlist_free(config); 399721ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 399821ecdf64SLin Ling pname); 399921ecdf64SLin Ling return (error); 400021ecdf64SLin Ling } 400121ecdf64SLin Ling 400221ecdf64SLin Ling /* 400321ecdf64SLin Ling * Get the boot vdev. 400421ecdf64SLin Ling */ 400521ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 400621ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 400721ecdf64SLin Ling (u_longlong_t)guid); 4008be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 400921ecdf64SLin Ling goto out; 401021ecdf64SLin Ling } 4011e7cbe64fSgw 401221ecdf64SLin Ling /* 401321ecdf64SLin Ling * Determine if there is a better boot device. 401421ecdf64SLin Ling */ 401521ecdf64SLin Ling avd = bvd; 401621ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 401721ecdf64SLin Ling if (avd != bvd) { 401821ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 401921ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4020be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 402121ecdf64SLin Ling goto out; 402221ecdf64SLin Ling } 4023e7cbe64fSgw 402421ecdf64SLin Ling /* 402521ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 402621ecdf64SLin Ling * we're booting off the active spare. 402721ecdf64SLin Ling */ 402821ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 402921ecdf64SLin Ling !bvd->vdev_isspare) { 403021ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 403121ecdf64SLin Ling "try booting from '%s'", 4032cb04b873SMark J Musante bvd->vdev_parent-> 4033cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4034be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 403521ecdf64SLin Ling goto out; 403621ecdf64SLin Ling } 403721ecdf64SLin Ling 403821ecdf64SLin Ling error = 0; 403921ecdf64SLin Ling out: 404021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 404121ecdf64SLin Ling vdev_free(rvd); 404221ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 404321ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 404421ecdf64SLin Ling 404521ecdf64SLin Ling nvlist_free(config); 4046e7cbe64fSgw return (error); 4047e7cbe64fSgw } 404821ecdf64SLin Ling 4049e7cbe64fSgw #endif 4050e7cbe64fSgw 40516809eb4eSEric Schrock /* 40526809eb4eSEric Schrock * Import a non-root pool into the system. 40536809eb4eSEric Schrock */ 4054c5904d13Seschrock int 40554b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4056c5904d13Seschrock { 40576809eb4eSEric Schrock spa_t *spa; 40586809eb4eSEric Schrock char *altroot = NULL; 4059468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4060468c413aSTim Haley zpool_rewind_policy_t policy; 4061f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4062f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 40636809eb4eSEric Schrock int error; 40646809eb4eSEric Schrock nvlist_t *nvroot; 40656809eb4eSEric Schrock nvlist_t **spares, **l2cache; 40666809eb4eSEric Schrock uint_t nspares, nl2cache; 40676809eb4eSEric Schrock 40686809eb4eSEric Schrock /* 40696809eb4eSEric Schrock * If a pool with this name exists, return failure. 40706809eb4eSEric Schrock */ 40716809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 40721195e687SMark J Musante if (spa_lookup(pool) != NULL) { 40736809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4074be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 40756809eb4eSEric Schrock } 40766809eb4eSEric Schrock 40776809eb4eSEric Schrock /* 40786809eb4eSEric Schrock * Create and initialize the spa structure. 40796809eb4eSEric Schrock */ 40806809eb4eSEric Schrock (void) nvlist_lookup_string(props, 40816809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4082f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4083f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4084f9af39baSGeorge Wilson if (readonly) 4085f9af39baSGeorge Wilson mode = FREAD; 4086468c413aSTim Haley spa = spa_add(pool, config, altroot); 40874b964adaSGeorge Wilson spa->spa_import_flags = flags; 40884b964adaSGeorge Wilson 40894b964adaSGeorge Wilson /* 40904b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 40914b964adaSGeorge Wilson * as if it had been loaded at boot. 40924b964adaSGeorge Wilson */ 40934b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 40944b964adaSGeorge Wilson if (props != NULL) 40954b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 40964b964adaSGeorge Wilson 40974b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 4098ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 40994b964adaSGeorge Wilson 41004b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 41014b964adaSGeorge Wilson return (0); 41024b964adaSGeorge Wilson } 41034b964adaSGeorge Wilson 4104f9af39baSGeorge Wilson spa_activate(spa, mode); 41056809eb4eSEric Schrock 410625f89ee2SJeff Bonwick /* 410725f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 410825f89ee2SJeff Bonwick */ 410925f89ee2SJeff Bonwick spa_async_suspend(spa); 411025f89ee2SJeff Bonwick 41114b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 41124b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 41134b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 41144b964adaSGeorge Wilson 41156809eb4eSEric Schrock /* 41166809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 41176809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 41186809eb4eSEric Schrock * doing an import. 41196809eb4eSEric Schrock */ 4120468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4121468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 41224b964adaSGeorge Wilson 4123468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 4124c8ee1847SVictor Latushkin policy.zrp_request); 4125468c413aSTim Haley 4126468c413aSTim Haley /* 41274b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 41284b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4129468c413aSTim Haley */ 41304b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 41314b964adaSGeorge Wilson spa->spa_load_info) == 0); 41326809eb4eSEric Schrock 41336809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41346809eb4eSEric Schrock /* 41356809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 41366809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 41376809eb4eSEric Schrock */ 41386809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 41396809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 41406809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 41416809eb4eSEric Schrock spa_load_spares(spa); 41426809eb4eSEric Schrock } 41436809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 41446809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 41456809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 41466809eb4eSEric Schrock spa_load_l2cache(spa); 41476809eb4eSEric Schrock } 41486809eb4eSEric Schrock 41496809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 41506809eb4eSEric Schrock &nvroot) == 0); 41516809eb4eSEric Schrock if (error == 0) 41526809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41536809eb4eSEric Schrock VDEV_ALLOC_SPARE); 41546809eb4eSEric Schrock if (error == 0) 41556809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41566809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 41576809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41586809eb4eSEric Schrock 41596809eb4eSEric Schrock if (props != NULL) 41606809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 41616809eb4eSEric Schrock 41626809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 41636809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 41646809eb4eSEric Schrock spa_unload(spa); 41656809eb4eSEric Schrock spa_deactivate(spa); 41666809eb4eSEric Schrock spa_remove(spa); 41676809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 41686809eb4eSEric Schrock return (error); 41696809eb4eSEric Schrock } 41706809eb4eSEric Schrock 4171955ef359SLin Ling spa_async_resume(spa); 4172955ef359SLin Ling 41736809eb4eSEric Schrock /* 41746809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 41756809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 41766809eb4eSEric Schrock */ 41776809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 41786809eb4eSEric Schrock &spares, &nspares) == 0) { 41796809eb4eSEric Schrock if (spa->spa_spares.sav_config) 41806809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 41816809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 41826809eb4eSEric Schrock else 41836809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 41846809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41856809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 41866809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 41876809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41886809eb4eSEric Schrock spa_load_spares(spa); 41896809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41906809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 41916809eb4eSEric Schrock } 41926809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 41936809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 41946809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 41956809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 41966809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 41976809eb4eSEric Schrock else 41986809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 41996809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 42006809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 42016809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 42026809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42036809eb4eSEric Schrock spa_load_l2cache(spa); 42046809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42056809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 42066809eb4eSEric Schrock } 42076809eb4eSEric Schrock 4208b693757aSEric Schrock /* 4209b693757aSEric Schrock * Check for any removed devices. 4210b693757aSEric Schrock */ 4211b693757aSEric Schrock if (spa->spa_autoreplace) { 4212b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4213b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4214b693757aSEric Schrock } 4215b693757aSEric Schrock 42166809eb4eSEric Schrock if (spa_writeable(spa)) { 42176809eb4eSEric Schrock /* 42186809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 42196809eb4eSEric Schrock */ 4220bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 42216809eb4eSEric Schrock } 42226809eb4eSEric Schrock 4223573ca77eSGeorge Wilson /* 4224573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4225573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4226573ca77eSGeorge Wilson */ 4227573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4228573ca77eSGeorge Wilson 42294445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 42306809eb4eSEric Schrock 4231ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 423214372834SHans Rosenfeld 423314372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 423414372834SHans Rosenfeld 42356809eb4eSEric Schrock return (0); 4236c5904d13Seschrock } 4237c5904d13Seschrock 4238fa9e4066Sahrens nvlist_t * 4239fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4240fa9e4066Sahrens { 4241fa9e4066Sahrens nvlist_t *config = NULL; 4242fa9e4066Sahrens char *poolname; 4243fa9e4066Sahrens spa_t *spa; 4244fa9e4066Sahrens uint64_t state; 42457b7154beSLin Ling int error; 4246fa9e4066Sahrens 4247fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4248fa9e4066Sahrens return (NULL); 4249fa9e4066Sahrens 4250fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4251fa9e4066Sahrens return (NULL); 4252fa9e4066Sahrens 4253fa9e4066Sahrens /* 42540373e76bSbonwick * Create and initialize the spa structure. 4255fa9e4066Sahrens */ 42560373e76bSbonwick mutex_enter(&spa_namespace_lock); 4257468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 42588ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4259fa9e4066Sahrens 4260fa9e4066Sahrens /* 42610373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4262ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4263ecc2d604Sbonwick * is actually the one to trust when doing an import. 4264fa9e4066Sahrens */ 42651195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4266fa9e4066Sahrens 4267fa9e4066Sahrens /* 4268fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4269fa9e4066Sahrens */ 4270fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4271fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4272fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4273fa9e4066Sahrens poolname) == 0); 4274fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4275fa9e4066Sahrens state) == 0); 427695173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 427795173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4278ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4279ad135b5dSChristopher Siden spa->spa_load_info) == 0); 428099653d4eSeschrock 4281e7cbe64fSgw /* 4282e7cbe64fSgw * If the bootfs property exists on this pool then we 4283e7cbe64fSgw * copy it out so that external consumers can tell which 4284e7cbe64fSgw * pools are bootable. 4285e7cbe64fSgw */ 42867b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4287e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4288e7cbe64fSgw 4289e7cbe64fSgw /* 4290e7cbe64fSgw * We have to play games with the name since the 4291e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4292e7cbe64fSgw */ 4293e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4294e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4295e7cbe64fSgw char *cp; 4296e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4297e7cbe64fSgw 4298e7cbe64fSgw cp = strchr(tmpname, '/'); 4299e7cbe64fSgw if (cp == NULL) { 4300e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4301e7cbe64fSgw MAXPATHLEN); 4302e7cbe64fSgw } else { 4303e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4304e7cbe64fSgw "%s/%s", poolname, ++cp); 4305e7cbe64fSgw } 4306e7cbe64fSgw VERIFY(nvlist_add_string(config, 4307e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4308e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4309e7cbe64fSgw } 4310e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4311e7cbe64fSgw } 4312e7cbe64fSgw 431399653d4eSeschrock /* 4314fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 431599653d4eSeschrock */ 43166809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 431799653d4eSeschrock spa_add_spares(spa, config); 4318fa94a07fSbrendan spa_add_l2cache(spa, config); 43196809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4320fa9e4066Sahrens } 4321fa9e4066Sahrens 4322fa9e4066Sahrens spa_unload(spa); 4323fa9e4066Sahrens spa_deactivate(spa); 4324fa9e4066Sahrens spa_remove(spa); 4325fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4326fa9e4066Sahrens 4327fa9e4066Sahrens return (config); 4328fa9e4066Sahrens } 4329fa9e4066Sahrens 4330fa9e4066Sahrens /* 4331fa9e4066Sahrens * Pool export/destroy 4332fa9e4066Sahrens * 4333fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4334fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4335fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4336394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4337394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4338fa9e4066Sahrens */ 4339fa9e4066Sahrens static int 434089a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4341394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4342fa9e4066Sahrens { 4343fa9e4066Sahrens spa_t *spa; 4344fa9e4066Sahrens 434544cd46caSbillm if (oldconfig) 434644cd46caSbillm *oldconfig = NULL; 434744cd46caSbillm 43488ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4349be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4350fa9e4066Sahrens 4351fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4352fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4353fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4354be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4355fa9e4066Sahrens } 4356fa9e4066Sahrens 4357ea8dc4b6Seschrock /* 4358ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4359ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4360ea8dc4b6Seschrock */ 4361ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4362ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4363ea8dc4b6Seschrock spa_async_suspend(spa); 4364ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4365ea8dc4b6Seschrock spa_close(spa, FTAG); 4366ea8dc4b6Seschrock 4367fa9e4066Sahrens /* 4368fa9e4066Sahrens * The pool will be in core if it's openable, 4369fa9e4066Sahrens * in which case we can modify its state. 4370fa9e4066Sahrens */ 4371fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4372fa9e4066Sahrens /* 4373fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4374fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4375fa9e4066Sahrens */ 4376fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4377bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4378fa9e4066Sahrens 4379ea8dc4b6Seschrock /* 4380ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4381ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4382ea8dc4b6Seschrock * fault injection handlers. 4383ea8dc4b6Seschrock */ 4384ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4385ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4386ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4387ea8dc4b6Seschrock spa_async_resume(spa); 4388fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4389be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4390fa9e4066Sahrens } 4391fa9e4066Sahrens 439289a89ebfSlling /* 439389a89ebfSlling * A pool cannot be exported if it has an active shared spare. 439489a89ebfSlling * This is to prevent other pools stealing the active spare 439589a89ebfSlling * from an exported pool. At user's own will, such pool can 439689a89ebfSlling * be forcedly exported. 439789a89ebfSlling */ 439889a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 439989a89ebfSlling spa_has_active_shared_spare(spa)) { 440089a89ebfSlling spa_async_resume(spa); 440189a89ebfSlling mutex_exit(&spa_namespace_lock); 4402be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 440389a89ebfSlling } 440489a89ebfSlling 4405fa9e4066Sahrens /* 4406fa9e4066Sahrens * We want this to be reflected on every label, 4407fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4408fa9e4066Sahrens * final sync that pushes these changes out. 4409fa9e4066Sahrens */ 4410394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4411e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4412ea8dc4b6Seschrock spa->spa_state = new_state; 44133f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 44143f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4415ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4416e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4417ea8dc4b6Seschrock } 4418fa9e4066Sahrens } 4419fa9e4066Sahrens 4420ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 44213d7072f8Seschrock 4422fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4423fa9e4066Sahrens spa_unload(spa); 4424fa9e4066Sahrens spa_deactivate(spa); 4425fa9e4066Sahrens } 4426fa9e4066Sahrens 442744cd46caSbillm if (oldconfig && spa->spa_config) 442844cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 442944cd46caSbillm 4430ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4431394ab0cbSGeorge Wilson if (!hardforce) 4432394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4433ea8dc4b6Seschrock spa_remove(spa); 4434ea8dc4b6Seschrock } 4435fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4436fa9e4066Sahrens 4437fa9e4066Sahrens return (0); 4438fa9e4066Sahrens } 4439fa9e4066Sahrens 4440fa9e4066Sahrens /* 4441fa9e4066Sahrens * Destroy a storage pool. 4442fa9e4066Sahrens */ 4443fa9e4066Sahrens int 4444fa9e4066Sahrens spa_destroy(char *pool) 4445fa9e4066Sahrens { 4446394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4447394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4448fa9e4066Sahrens } 4449fa9e4066Sahrens 4450fa9e4066Sahrens /* 4451fa9e4066Sahrens * Export a storage pool. 4452fa9e4066Sahrens */ 4453fa9e4066Sahrens int 4454394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4455394ab0cbSGeorge Wilson boolean_t hardforce) 4456fa9e4066Sahrens { 4457394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4458394ab0cbSGeorge Wilson force, hardforce)); 4459fa9e4066Sahrens } 4460fa9e4066Sahrens 4461ea8dc4b6Seschrock /* 4462ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4463ea8dc4b6Seschrock * from the namespace in any way. 4464ea8dc4b6Seschrock */ 4465ea8dc4b6Seschrock int 4466ea8dc4b6Seschrock spa_reset(char *pool) 4467ea8dc4b6Seschrock { 446889a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4469394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4470ea8dc4b6Seschrock } 4471ea8dc4b6Seschrock 4472fa9e4066Sahrens /* 4473fa9e4066Sahrens * ========================================================================== 4474fa9e4066Sahrens * Device manipulation 4475fa9e4066Sahrens * ========================================================================== 4476fa9e4066Sahrens */ 4477fa9e4066Sahrens 4478fa9e4066Sahrens /* 44798654d025Sperrin * Add a device to a storage pool. 4480fa9e4066Sahrens */ 4481fa9e4066Sahrens int 4482fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4483fa9e4066Sahrens { 448488ecc943SGeorge Wilson uint64_t txg, id; 44858ad4d6ddSJeff Bonwick int error; 4486fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 44870e34b6a7Sbonwick vdev_t *vd, *tvd; 4488fa94a07fSbrendan nvlist_t **spares, **l2cache; 4489fa94a07fSbrendan uint_t nspares, nl2cache; 4490fa9e4066Sahrens 4491f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4492f9af39baSGeorge Wilson 4493fa9e4066Sahrens txg = spa_vdev_enter(spa); 4494fa9e4066Sahrens 449599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 449699653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 449799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4498fa9e4066Sahrens 4499e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 450099653d4eSeschrock 4501fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4502fa94a07fSbrendan &nspares) != 0) 450399653d4eSeschrock nspares = 0; 450499653d4eSeschrock 4505fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4506fa94a07fSbrendan &nl2cache) != 0) 4507fa94a07fSbrendan nl2cache = 0; 4508fa94a07fSbrendan 4509e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4510fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4511fa9e4066Sahrens 4512e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4513e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4514e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 451599653d4eSeschrock 451639c23413Seschrock /* 4517fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4518fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 451939c23413Seschrock */ 4520e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 452139c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 452239c23413Seschrock 452339c23413Seschrock /* 452439c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 452539c23413Seschrock */ 45268ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 452788ecc943SGeorge Wilson 452888ecc943SGeorge Wilson /* 452988ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 453088ecc943SGeorge Wilson */ 453188ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 453288ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 453388ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 453488ecc943SGeorge Wilson break; 453588ecc943SGeorge Wilson } 453688ecc943SGeorge Wilson } 453739c23413Seschrock tvd = vd->vdev_child[c]; 453839c23413Seschrock vdev_remove_child(vd, tvd); 453988ecc943SGeorge Wilson tvd->vdev_id = id; 454039c23413Seschrock vdev_add_child(rvd, tvd); 454139c23413Seschrock vdev_config_dirty(tvd); 454239c23413Seschrock } 454339c23413Seschrock 454499653d4eSeschrock if (nspares != 0) { 4545fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4546fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 454799653d4eSeschrock spa_load_spares(spa); 4548fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4549fa94a07fSbrendan } 4550fa94a07fSbrendan 4551fa94a07fSbrendan if (nl2cache != 0) { 4552fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4553fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4554fa94a07fSbrendan spa_load_l2cache(spa); 4555fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4556fa9e4066Sahrens } 4557fa9e4066Sahrens 4558fa9e4066Sahrens /* 45590e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 45600e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 45610e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 45620e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 45630e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 45640e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 45650373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 45660e34b6a7Sbonwick * 45670e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 45680e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 45690e34b6a7Sbonwick * steps will be completed the next time we load the pool. 45700e34b6a7Sbonwick */ 45710373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 45720e34b6a7Sbonwick 45730373e76bSbonwick mutex_enter(&spa_namespace_lock); 45740373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 4575ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 45760373e76bSbonwick mutex_exit(&spa_namespace_lock); 4577fa9e4066Sahrens 45780373e76bSbonwick return (0); 4579fa9e4066Sahrens } 4580fa9e4066Sahrens 4581fa9e4066Sahrens /* 4582fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4583fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4584fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4585fa9e4066Sahrens * 4586fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4587fa9e4066Sahrens * existing device; in this case the two devices are made into their own 45883d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4589fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4590fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4591fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4592fa9e4066Sahrens * is automatically detached. 4593fa9e4066Sahrens */ 4594fa9e4066Sahrens int 4595ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4596fa9e4066Sahrens { 45973f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4598fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4599fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 460099653d4eSeschrock vdev_ops_t *pvops; 46019b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 46029b3f6b42SEric Kustarz int newvd_isspare; 46039b3f6b42SEric Kustarz int error; 4604fa9e4066Sahrens 4605f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4606f9af39baSGeorge Wilson 4607fa9e4066Sahrens txg = spa_vdev_enter(spa); 4608fa9e4066Sahrens 4609c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4610fa9e4066Sahrens 4611fa9e4066Sahrens if (oldvd == NULL) 4612fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4613fa9e4066Sahrens 46140e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 46150e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 46160e34b6a7Sbonwick 4617fa9e4066Sahrens pvd = oldvd->vdev_parent; 4618fa9e4066Sahrens 461999653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4620cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 46213d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 46223d7072f8Seschrock 46233d7072f8Seschrock if (newrootvd->vdev_children != 1) 4624fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4625fa9e4066Sahrens 4626fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4627fa9e4066Sahrens 4628fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4629fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4630fa9e4066Sahrens 463199653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4632fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4633fa9e4066Sahrens 46348654d025Sperrin /* 46358654d025Sperrin * Spares can't replace logs 46368654d025Sperrin */ 4637ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 46388654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 46398654d025Sperrin 464099653d4eSeschrock if (!replacing) { 464199653d4eSeschrock /* 464299653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 464399653d4eSeschrock * vdev. 464499653d4eSeschrock */ 464599653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 464699653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 464799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 464899653d4eSeschrock 464999653d4eSeschrock pvops = &vdev_mirror_ops; 465099653d4eSeschrock } else { 465199653d4eSeschrock /* 465299653d4eSeschrock * Active hot spares can only be replaced by inactive hot 465399653d4eSeschrock * spares. 465499653d4eSeschrock */ 465599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4656cb04b873SMark J Musante oldvd->vdev_isspare && 465799653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 465899653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 465999653d4eSeschrock 466099653d4eSeschrock /* 466199653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 466299653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 466339c23413Seschrock * want to create a replacing vdev. The user is not allowed to 466439c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 466539c23413Seschrock * the same (spare replaces spare, non-spare replaces 466639c23413Seschrock * non-spare). 466799653d4eSeschrock */ 4668cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4669cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 467099653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4671cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4672cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 467339c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4674cb04b873SMark J Musante } 4675cb04b873SMark J Musante 4676cb04b873SMark J Musante if (newvd->vdev_isspare) 467799653d4eSeschrock pvops = &vdev_spare_ops; 467899653d4eSeschrock else 467999653d4eSeschrock pvops = &vdev_replacing_ops; 468099653d4eSeschrock } 468199653d4eSeschrock 46822a79c5feSlling /* 4683573ca77eSGeorge Wilson * Make sure the new device is big enough. 46842a79c5feSlling */ 4685573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4686fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4687fa9e4066Sahrens 4688ecc2d604Sbonwick /* 4689ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4690ecc2d604Sbonwick * than the top-level vdev. 4691ecc2d604Sbonwick */ 4692ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4693fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4694fa9e4066Sahrens 4695fa9e4066Sahrens /* 4696fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4697fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4698fa9e4066Sahrens */ 4699fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4700fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4701fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4702fa9e4066Sahrens KM_SLEEP); 4703fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4704fa9e4066Sahrens newvd->vdev_path, "old"); 4705fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4706fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4707fa9e4066Sahrens oldvd->vdev_devid = NULL; 4708fa9e4066Sahrens } 4709fa9e4066Sahrens } 4710fa9e4066Sahrens 4711cb04b873SMark J Musante /* mark the device being resilvered */ 4712b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4713cb04b873SMark J Musante 4714fa9e4066Sahrens /* 471599653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 471699653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4717fa9e4066Sahrens */ 4718fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4719fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4720fa9e4066Sahrens 4721fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4722fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4723fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4724fa9e4066Sahrens 4725fa9e4066Sahrens /* 4726fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4727fa9e4066Sahrens */ 4728fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4729fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 473088ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4731fa9e4066Sahrens vdev_add_child(pvd, newvd); 4732fa9e4066Sahrens 4733fa9e4066Sahrens tvd = newvd->vdev_top; 4734fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4735fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4736fa9e4066Sahrens 4737fa9e4066Sahrens vdev_config_dirty(tvd); 4738fa9e4066Sahrens 4739fa9e4066Sahrens /* 47403f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 47413f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 47423f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4743fa9e4066Sahrens */ 47443f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4745fa9e4066Sahrens 47463f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 47473f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4748fa9e4066Sahrens 47496809eb4eSEric Schrock if (newvd->vdev_isspare) { 475039c23413Seschrock spa_spare_activate(newvd); 4751ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 47526809eb4eSEric Schrock } 47536809eb4eSEric Schrock 4754e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4755e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 47569b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4757ea8dc4b6Seschrock 4758fa9e4066Sahrens /* 4759fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4760fa9e4066Sahrens */ 4761ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4762fa9e4066Sahrens 47633f9d6ad7SLin Ling /* 47640713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 47650713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 47660713e232SGeorge Wilson * respective datasets. 47673f9d6ad7SLin Ling */ 47683f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 47693f9d6ad7SLin Ling 477014372834SHans Rosenfeld if (spa->spa_bootfs) 4771ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 477214372834SHans Rosenfeld 4773ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 477414372834SHans Rosenfeld 47753f9d6ad7SLin Ling /* 47763f9d6ad7SLin Ling * Commit the config 47773f9d6ad7SLin Ling */ 47783f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4779fa9e4066Sahrens 47804445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 47813f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4782c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4783c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4784c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 47859b3f6b42SEric Kustarz 47869b3f6b42SEric Kustarz spa_strfree(oldvdpath); 47879b3f6b42SEric Kustarz spa_strfree(newvdpath); 47889b3f6b42SEric Kustarz 4789fa9e4066Sahrens return (0); 4790fa9e4066Sahrens } 4791fa9e4066Sahrens 4792fa9e4066Sahrens /* 4793fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4794f7170741SWill Andrews * 4795fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4796fa9e4066Sahrens * is a replacing vdev. 4797fa9e4066Sahrens */ 4798fa9e4066Sahrens int 47998ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4800fa9e4066Sahrens { 4801fa9e4066Sahrens uint64_t txg; 48028ad4d6ddSJeff Bonwick int error; 4803fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4804fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 480599653d4eSeschrock boolean_t unspare = B_FALSE; 4806d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 48071195e687SMark J Musante char *vdpath; 4808fa9e4066Sahrens 4809f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4810f9af39baSGeorge Wilson 4811fa9e4066Sahrens txg = spa_vdev_enter(spa); 4812fa9e4066Sahrens 4813c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4814fa9e4066Sahrens 4815fa9e4066Sahrens if (vd == NULL) 4816fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4817fa9e4066Sahrens 48180e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 48190e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 48200e34b6a7Sbonwick 4821fa9e4066Sahrens pvd = vd->vdev_parent; 4822fa9e4066Sahrens 48238ad4d6ddSJeff Bonwick /* 48248ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 48258ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 48268ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 48278ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 48288ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 48298ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 48308ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 48318ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 48328ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 48338ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 48348ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 48358ad4d6ddSJeff Bonwick */ 48368ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 48378ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 48388ad4d6ddSJeff Bonwick 4839fa9e4066Sahrens /* 4840cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 484199653d4eSeschrock */ 4842cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4843cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4844cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 484599653d4eSeschrock 484699653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4847e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4848fa9e4066Sahrens 4849fa9e4066Sahrens /* 485099653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4851fa9e4066Sahrens */ 4852fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 485399653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 485499653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4855fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4856fa9e4066Sahrens 4857fa9e4066Sahrens /* 48588ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 48598ad4d6ddSJeff Bonwick * we cannot safely detach it. 4860fa9e4066Sahrens */ 48618ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4862fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4863fa9e4066Sahrens 48648ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4865fa9e4066Sahrens 4866bf82a41bSeschrock /* 4867bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4868bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4869bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4870bf82a41bSeschrock */ 4871cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4872cb04b873SMark J Musante vd->vdev_path != NULL) { 4873cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4874cb04b873SMark J Musante 4875cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4876cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4877cb04b873SMark J Musante 4878cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4879cb04b873SMark J Musante continue; 4880cb04b873SMark J Musante 4881cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4882cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4883cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4884cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4885cb04b873SMark J Musante break; 4886cb04b873SMark J Musante } 4887bf82a41bSeschrock } 4888bf82a41bSeschrock } 4889bf82a41bSeschrock 489099653d4eSeschrock /* 489199653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 489299653d4eSeschrock * that the spare should become a real disk, and be removed from the 489399653d4eSeschrock * active spare list for the pool. 489499653d4eSeschrock */ 489599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4896cb04b873SMark J Musante vd->vdev_id == 0 && 4897cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 489899653d4eSeschrock unspare = B_TRUE; 489999653d4eSeschrock 4900fa9e4066Sahrens /* 4901fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4902fa9e4066Sahrens * This must be done after all other error cases are handled, 4903fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4904fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4905fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4906fa9e4066Sahrens * it's being detached! 4907fa9e4066Sahrens */ 490839c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4909fa9e4066Sahrens 4910fa9e4066Sahrens /* 4911fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4912fa9e4066Sahrens */ 4913fa9e4066Sahrens vdev_remove_child(pvd, vd); 4914fa9e4066Sahrens vdev_compact_children(pvd); 4915fa9e4066Sahrens 4916fa9e4066Sahrens /* 4917fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4918fa9e4066Sahrens */ 4919cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4920fa9e4066Sahrens 492199653d4eSeschrock /* 492299653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 49238ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 49248ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 49258ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 49268ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 49278ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 49288ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 492999653d4eSeschrock */ 493099653d4eSeschrock if (unspare) { 493199653d4eSeschrock ASSERT(cvd->vdev_isspare); 493239c23413Seschrock spa_spare_remove(cvd); 493399653d4eSeschrock unspare_guid = cvd->vdev_guid; 49348ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4935cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 493699653d4eSeschrock } 493799653d4eSeschrock 4938fa9e4066Sahrens /* 4939fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4940fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4941fa9e4066Sahrens */ 4942cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4943cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4944cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4945fa9e4066Sahrens vdev_remove_parent(cvd); 4946cb04b873SMark J Musante } 4947cb04b873SMark J Musante 4948fa9e4066Sahrens 4949fa9e4066Sahrens /* 4950fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4951fa9e4066Sahrens * may have been the previous top-level vdev. 4952fa9e4066Sahrens */ 4953fa9e4066Sahrens tvd = cvd->vdev_top; 4954fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4955fa9e4066Sahrens 4956fa9e4066Sahrens /* 495739c23413Seschrock * Reevaluate the parent vdev state. 4958fa9e4066Sahrens */ 49593d7072f8Seschrock vdev_propagate_state(cvd); 4960fa9e4066Sahrens 4961fa9e4066Sahrens /* 4962573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4963573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4964573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4965573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4966573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4967fa9e4066Sahrens */ 4968573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4969573ca77eSGeorge Wilson vdev_reopen(tvd); 4970573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4971573ca77eSGeorge Wilson } 4972fa9e4066Sahrens 4973fa9e4066Sahrens vdev_config_dirty(tvd); 4974fa9e4066Sahrens 4975fa9e4066Sahrens /* 497639c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 497739c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 497839c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 497939c23413Seschrock * prevent vd from being accessed after it's freed. 4980fa9e4066Sahrens */ 49811195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 49828ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4983fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4984ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4985ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4986fa9e4066Sahrens 4987ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 49883d7072f8Seschrock 4989cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4990cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4991cb04b873SMark J Musante 499299653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 499399653d4eSeschrock 49944445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 49951195e687SMark J Musante "vdev=%s", vdpath); 49961195e687SMark J Musante spa_strfree(vdpath); 49971195e687SMark J Musante 499899653d4eSeschrock /* 499939c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 500039c23413Seschrock * then we want to go through and remove the device from the hot spare 500139c23413Seschrock * list of every other pool. 500299653d4eSeschrock */ 500399653d4eSeschrock if (unspare) { 5004cb04b873SMark J Musante spa_t *altspa = NULL; 5005cb04b873SMark J Musante 500699653d4eSeschrock mutex_enter(&spa_namespace_lock); 5007cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 5008cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 5009cb04b873SMark J Musante altspa == spa) 501099653d4eSeschrock continue; 5011cb04b873SMark J Musante 5012cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 50139af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 5014cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 50159af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 5016cb04b873SMark J Musante spa_close(altspa, FTAG); 501799653d4eSeschrock } 501899653d4eSeschrock mutex_exit(&spa_namespace_lock); 5019cb04b873SMark J Musante 5020cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 5021cb04b873SMark J Musante spa_vdev_resilver_done(spa); 502299653d4eSeschrock } 502399653d4eSeschrock 5024cb04b873SMark J Musante /* all done with the spa; OK to release */ 5025cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5026cb04b873SMark J Musante spa_close(spa, FTAG); 5027cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5028cb04b873SMark J Musante 502999653d4eSeschrock return (error); 503099653d4eSeschrock } 503199653d4eSeschrock 50321195e687SMark J Musante /* 50331195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 50341195e687SMark J Musante */ 50351195e687SMark J Musante int 50361195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 50371195e687SMark J Musante nvlist_t *props, boolean_t exp) 50381195e687SMark J Musante { 50391195e687SMark J Musante int error = 0; 50401195e687SMark J Musante uint64_t txg, *glist; 50411195e687SMark J Musante spa_t *newspa; 50421195e687SMark J Musante uint_t c, children, lastlog; 50431195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 50441195e687SMark J Musante dmu_tx_t *tx; 50451195e687SMark J Musante char *altroot = NULL; 50461195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 50471195e687SMark J Musante boolean_t activate_slog; 50481195e687SMark J Musante 5049f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 50501195e687SMark J Musante 50511195e687SMark J Musante txg = spa_vdev_enter(spa); 50521195e687SMark J Musante 50531195e687SMark J Musante /* clear the log and flush everything up to now */ 50541195e687SMark J Musante activate_slog = spa_passivate_log(spa); 50551195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 50561195e687SMark J Musante error = spa_offline_log(spa); 50571195e687SMark J Musante txg = spa_vdev_config_enter(spa); 50581195e687SMark J Musante 50591195e687SMark J Musante if (activate_slog) 50601195e687SMark J Musante spa_activate_log(spa); 50611195e687SMark J Musante 50621195e687SMark J Musante if (error != 0) 50631195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 50641195e687SMark J Musante 50651195e687SMark J Musante /* check new spa name before going any further */ 50661195e687SMark J Musante if (spa_lookup(newname) != NULL) 50671195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 50681195e687SMark J Musante 50691195e687SMark J Musante /* 50701195e687SMark J Musante * scan through all the children to ensure they're all mirrors 50711195e687SMark J Musante */ 50721195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 50731195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 50741195e687SMark J Musante &children) != 0) 50751195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50761195e687SMark J Musante 50771195e687SMark J Musante /* first, check to ensure we've got the right child count */ 50781195e687SMark J Musante rvd = spa->spa_root_vdev; 50791195e687SMark J Musante lastlog = 0; 50801195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 50811195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 50821195e687SMark J Musante 50831195e687SMark J Musante /* don't count the holes & logs as children */ 50841195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 50851195e687SMark J Musante if (lastlog == 0) 50861195e687SMark J Musante lastlog = c; 50871195e687SMark J Musante continue; 50881195e687SMark J Musante } 50891195e687SMark J Musante 50901195e687SMark J Musante lastlog = 0; 50911195e687SMark J Musante } 50921195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 50931195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50941195e687SMark J Musante 50951195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 50961195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 50971195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 50981195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50991195e687SMark J Musante 51001195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 51011195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 51021195e687SMark J Musante 51031195e687SMark J Musante /* then, loop over each vdev and validate it */ 51041195e687SMark J Musante for (c = 0; c < children; c++) { 51051195e687SMark J Musante uint64_t is_hole = 0; 51061195e687SMark J Musante 51071195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 51081195e687SMark J Musante &is_hole); 51091195e687SMark J Musante 51101195e687SMark J Musante if (is_hole != 0) { 51111195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 51121195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 51131195e687SMark J Musante continue; 51141195e687SMark J Musante } else { 5115be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51161195e687SMark J Musante break; 51171195e687SMark J Musante } 51181195e687SMark J Musante } 51191195e687SMark J Musante 51201195e687SMark J Musante /* which disk is going to be split? */ 51211195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 51221195e687SMark J Musante &glist[c]) != 0) { 5123be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51241195e687SMark J Musante break; 51251195e687SMark J Musante } 51261195e687SMark J Musante 51271195e687SMark J Musante /* look it up in the spa */ 51281195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 51291195e687SMark J Musante if (vml[c] == NULL) { 5130be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 51311195e687SMark J Musante break; 51321195e687SMark J Musante } 51331195e687SMark J Musante 51341195e687SMark J Musante /* make sure there's nothing stopping the split */ 51351195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 51361195e687SMark J Musante vml[c]->vdev_islog || 51371195e687SMark J Musante vml[c]->vdev_ishole || 51381195e687SMark J Musante vml[c]->vdev_isspare || 51391195e687SMark J Musante vml[c]->vdev_isl2cache || 51401195e687SMark J Musante !vdev_writeable(vml[c]) || 5141d41c4376SMark J Musante vml[c]->vdev_children != 0 || 51421195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 51431195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5144be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51451195e687SMark J Musante break; 51461195e687SMark J Musante } 51471195e687SMark J Musante 51481195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5149be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 51501195e687SMark J Musante break; 51511195e687SMark J Musante } 51521195e687SMark J Musante 51531195e687SMark J Musante /* we need certain info from the top level */ 51541195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 51551195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 51561195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 51571195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 51581195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 51591195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 51601195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 51611195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5162215198a6SJoe Stein 5163215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5164215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5165215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5166215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5167215198a6SJoe Stein 5168215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5169215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5170215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5171215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 51721195e687SMark J Musante } 51731195e687SMark J Musante 51741195e687SMark J Musante if (error != 0) { 51751195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51761195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51771195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 51781195e687SMark J Musante } 51791195e687SMark J Musante 51801195e687SMark J Musante /* stop writers from using the disks */ 51811195e687SMark J Musante for (c = 0; c < children; c++) { 51821195e687SMark J Musante if (vml[c] != NULL) 51831195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 51841195e687SMark J Musante } 51851195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 51861195e687SMark J Musante 51871195e687SMark J Musante /* 51881195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 51891195e687SMark J Musante * will disappear once the config is regenerated. 51901195e687SMark J Musante */ 51911195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 51921195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 51931195e687SMark J Musante glist, children) == 0); 51941195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51951195e687SMark J Musante 519698295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 51971195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 51981195e687SMark J Musante nvl) == 0); 519998295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 52001195e687SMark J Musante spa->spa_config_splitting = nvl; 52011195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 52021195e687SMark J Musante 52031195e687SMark J Musante /* configure and create the new pool */ 52041195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 52051195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 52061195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 52071195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 52081195e687SMark J Musante spa_version(spa)) == 0); 52091195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 52101195e687SMark J Musante spa->spa_config_txg) == 0); 52111195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 52121195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5213215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 52141195e687SMark J Musante (void) nvlist_lookup_string(props, 52151195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 52161195e687SMark J Musante 5217d41c4376SMark J Musante /* add the new pool to the namespace */ 52181195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5219215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 52201195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 52211195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 52221195e687SMark J Musante 52231195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 52241195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 52251195e687SMark J Musante 52261195e687SMark J Musante if (zio_injection_enabled) 52271195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 52281195e687SMark J Musante 52291195e687SMark J Musante spa_activate(newspa, spa_mode_global); 52301195e687SMark J Musante spa_async_suspend(newspa); 52311195e687SMark J Musante 52321195e687SMark J Musante /* create the new pool from the disks of the original pool */ 52331195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 52341195e687SMark J Musante if (error) 52351195e687SMark J Musante goto out; 52361195e687SMark J Musante 52371195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 52381195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 52391195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 52401195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 52411195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 52421195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 52431195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 52441195e687SMark J Musante B_TRUE)); 52451195e687SMark J Musante } 52461195e687SMark J Musante 52471195e687SMark J Musante /* set the props */ 52481195e687SMark J Musante if (props != NULL) { 52491195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 52501195e687SMark J Musante error = spa_prop_set(newspa, props); 52511195e687SMark J Musante if (error) 52521195e687SMark J Musante goto out; 52531195e687SMark J Musante } 52541195e687SMark J Musante 52551195e687SMark J Musante /* flush everything */ 52561195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 52571195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 52581195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 52591195e687SMark J Musante 52601195e687SMark J Musante if (zio_injection_enabled) 52611195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 52621195e687SMark J Musante 52631195e687SMark J Musante spa_async_resume(newspa); 52641195e687SMark J Musante 52651195e687SMark J Musante /* finally, update the original pool's config */ 52661195e687SMark J Musante txg = spa_vdev_config_enter(spa); 52671195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 52681195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 52691195e687SMark J Musante if (error != 0) 52701195e687SMark J Musante dmu_tx_abort(tx); 52711195e687SMark J Musante for (c = 0; c < children; c++) { 52721195e687SMark J Musante if (vml[c] != NULL) { 52731195e687SMark J Musante vdev_split(vml[c]); 52741195e687SMark J Musante if (error == 0) 52754445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 52764445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5277215198a6SJoe Stein 52781195e687SMark J Musante vdev_free(vml[c]); 52791195e687SMark J Musante } 52801195e687SMark J Musante } 5281215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 52821195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 52831195e687SMark J Musante spa->spa_config_splitting = NULL; 52841195e687SMark J Musante nvlist_free(nvl); 52851195e687SMark J Musante if (error == 0) 52861195e687SMark J Musante dmu_tx_commit(tx); 52871195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 52881195e687SMark J Musante 52891195e687SMark J Musante if (zio_injection_enabled) 52901195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 52911195e687SMark J Musante 52921195e687SMark J Musante /* split is complete; log a history record */ 52934445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 52944445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 52951195e687SMark J Musante 52961195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 52971195e687SMark J Musante 52981195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 52991195e687SMark J Musante if (exp) 53001195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 53011195e687SMark J Musante B_FALSE, B_FALSE); 53021195e687SMark J Musante 53031195e687SMark J Musante return (error); 53041195e687SMark J Musante 53051195e687SMark J Musante out: 53061195e687SMark J Musante spa_unload(newspa); 53071195e687SMark J Musante spa_deactivate(newspa); 53081195e687SMark J Musante spa_remove(newspa); 53091195e687SMark J Musante 53101195e687SMark J Musante txg = spa_vdev_config_enter(spa); 531198295d61SMark J Musante 531298295d61SMark J Musante /* re-online all offlined disks */ 531398295d61SMark J Musante for (c = 0; c < children; c++) { 531498295d61SMark J Musante if (vml[c] != NULL) 531598295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 531698295d61SMark J Musante } 531798295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 531898295d61SMark J Musante 53191195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 53201195e687SMark J Musante spa->spa_config_splitting = NULL; 5321d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 53221195e687SMark J Musante 53231195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 53241195e687SMark J Musante return (error); 53251195e687SMark J Musante } 53261195e687SMark J Musante 5327e14bb325SJeff Bonwick static nvlist_t * 5328e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 532999653d4eSeschrock { 5330e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5331e14bb325SJeff Bonwick uint64_t guid; 533299653d4eSeschrock 5333e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5334e14bb325SJeff Bonwick &guid) == 0); 533599653d4eSeschrock 5336e14bb325SJeff Bonwick if (guid == target_guid) 5337e14bb325SJeff Bonwick return (nvpp[i]); 533899653d4eSeschrock } 533999653d4eSeschrock 5340e14bb325SJeff Bonwick return (NULL); 5341fa94a07fSbrendan } 5342fa94a07fSbrendan 5343e14bb325SJeff Bonwick static void 5344e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 53450f7643c7SGeorge Wilson nvlist_t *dev_to_remove) 5346fa94a07fSbrendan { 5347e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5348fa94a07fSbrendan 5349e14bb325SJeff Bonwick if (count > 1) 5350e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5351fa94a07fSbrendan 5352e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5353e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5354e14bb325SJeff Bonwick continue; 5355e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5356fa94a07fSbrendan } 5357fa94a07fSbrendan 5358e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5359e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5360fa94a07fSbrendan 5361e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5362e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5363fa94a07fSbrendan 5364e14bb325SJeff Bonwick if (count > 1) 5365e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5366fa94a07fSbrendan } 5367fa94a07fSbrendan 536888ecc943SGeorge Wilson /* 536988ecc943SGeorge Wilson * Evacuate the device. 537088ecc943SGeorge Wilson */ 53713f9d6ad7SLin Ling static int 537288ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 537388ecc943SGeorge Wilson { 537488ecc943SGeorge Wilson uint64_t txg; 53753f9d6ad7SLin Ling int error = 0; 537688ecc943SGeorge Wilson 537788ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 537888ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5379b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 538088ecc943SGeorge Wilson 538188ecc943SGeorge Wilson /* 538288ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 538388ecc943SGeorge Wilson * since we need to do I/O but we do keep the 538488ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 538588ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 538688ecc943SGeorge Wilson */ 538788ecc943SGeorge Wilson if (vd->vdev_islog) { 53883f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 53893f9d6ad7SLin Ling error = spa_offline_log(spa); 5390a1521560SJeff Bonwick } else { 5391be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 539288ecc943SGeorge Wilson } 539388ecc943SGeorge Wilson 5394a1521560SJeff Bonwick if (error) 5395a1521560SJeff Bonwick return (error); 5396a1521560SJeff Bonwick 539788ecc943SGeorge Wilson /* 5398a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5399a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 540088ecc943SGeorge Wilson */ 5401fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 540288ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 540388ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 54040713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg); 540588ecc943SGeorge Wilson vdev_config_dirty(vd); 540688ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 540788ecc943SGeorge Wilson 540888ecc943SGeorge Wilson return (0); 540988ecc943SGeorge Wilson } 541088ecc943SGeorge Wilson 541188ecc943SGeorge Wilson /* 541288ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 541388ecc943SGeorge Wilson */ 54143f9d6ad7SLin Ling static void 5415a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 541688ecc943SGeorge Wilson { 541788ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 541888ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 541988ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 542088ecc943SGeorge Wilson 542188ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 542288ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5423b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 542488ecc943SGeorge Wilson 54253f9d6ad7SLin Ling /* 54263f9d6ad7SLin Ling * Only remove any devices which are empty. 54273f9d6ad7SLin Ling */ 54283f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 54293f9d6ad7SLin Ling return; 54303f9d6ad7SLin Ling 543188ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5432b24ab676SJeff Bonwick 5433b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5434b24ab676SJeff Bonwick vdev_state_clean(vd); 5435b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5436b24ab676SJeff Bonwick vdev_config_clean(vd); 5437b24ab676SJeff Bonwick 543888ecc943SGeorge Wilson vdev_free(vd); 543988ecc943SGeorge Wilson 544088ecc943SGeorge Wilson if (last_vdev) { 544188ecc943SGeorge Wilson vdev_compact_children(rvd); 544288ecc943SGeorge Wilson } else { 544388ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 544488ecc943SGeorge Wilson vdev_add_child(rvd, vd); 544588ecc943SGeorge Wilson } 5446fcbfa62bSLin Ling vdev_config_dirty(rvd); 5447fcbfa62bSLin Ling 5448fcbfa62bSLin Ling /* 5449fcbfa62bSLin Ling * Reassess the health of our root vdev. 5450fcbfa62bSLin Ling */ 5451fcbfa62bSLin Ling vdev_reopen(rvd); 545288ecc943SGeorge Wilson } 545388ecc943SGeorge Wilson 54543f9d6ad7SLin Ling /* 54553f9d6ad7SLin Ling * Remove a device from the pool - 54563f9d6ad7SLin Ling * 54573f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 54583f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 54593f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 54603f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 54613f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 5462f7170741SWill Andrews * 5463f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC 5464f7170741SWill Andrews * devices. 5465fa94a07fSbrendan */ 5466fa94a07fSbrendan int 5467fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5468fa94a07fSbrendan { 5469fa94a07fSbrendan vdev_t *vd; 5470b72b6bb1SAlan Somers sysevent_t *ev = NULL; 5471a1521560SJeff Bonwick metaslab_group_t *mg; 5472e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 54738ad4d6ddSJeff Bonwick uint64_t txg = 0; 547488ecc943SGeorge Wilson uint_t nspares, nl2cache; 5475fa94a07fSbrendan int error = 0; 54768ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5477fa94a07fSbrendan 5478f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5479f9af39baSGeorge Wilson 54808ad4d6ddSJeff Bonwick if (!locked) 54818ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5482fa94a07fSbrendan 5483c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5484fa94a07fSbrendan 5485fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5486fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5487e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5488e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5489e14bb325SJeff Bonwick /* 5490e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5491e14bb325SJeff Bonwick * in this pool. 5492e14bb325SJeff Bonwick */ 5493e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5494b72b6bb1SAlan Somers if (vd == NULL) 5495b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5496ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, 5497ce1577b0SDave Eddy ESC_ZFS_VDEV_REMOVE_AUX); 5498e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5499e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5500e14bb325SJeff Bonwick spa_load_spares(spa); 5501e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5502e14bb325SJeff Bonwick } else { 5503be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 5504e14bb325SJeff Bonwick } 5505e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5506fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5507e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5508e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5509e14bb325SJeff Bonwick /* 5510e14bb325SJeff Bonwick * Cache devices can always be removed. 5511e14bb325SJeff Bonwick */ 5512b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5513ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX); 5514e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5515e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5516fa94a07fSbrendan spa_load_l2cache(spa); 5517fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 551888ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 551988ecc943SGeorge Wilson ASSERT(!locked); 5520b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 552188ecc943SGeorge Wilson 5522a1521560SJeff Bonwick mg = vd->vdev_mg; 5523a1521560SJeff Bonwick 552488ecc943SGeorge Wilson /* 5525a1521560SJeff Bonwick * Stop allocating from this vdev. 552688ecc943SGeorge Wilson */ 5527a1521560SJeff Bonwick metaslab_group_passivate(mg); 552888ecc943SGeorge Wilson 5529b24ab676SJeff Bonwick /* 5530b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5531b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5532b24ab676SJeff Bonwick */ 5533b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5534b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5535b24ab676SJeff Bonwick 5536a1521560SJeff Bonwick /* 5537a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5538a1521560SJeff Bonwick */ 5539a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5540a1521560SJeff Bonwick 554188ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 554288ecc943SGeorge Wilson 5543a1521560SJeff Bonwick /* 5544a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5545a1521560SJeff Bonwick */ 5546a1521560SJeff Bonwick if (error) { 5547a1521560SJeff Bonwick metaslab_group_activate(mg); 5548a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5549a1521560SJeff Bonwick } 5550a1521560SJeff Bonwick 5551a1521560SJeff Bonwick /* 5552a1521560SJeff Bonwick * Clean up the vdev namespace. 5553a1521560SJeff Bonwick */ 5554ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_DEV); 5555a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 555688ecc943SGeorge Wilson 5557e14bb325SJeff Bonwick } else if (vd != NULL) { 5558e14bb325SJeff Bonwick /* 5559e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5560e14bb325SJeff Bonwick */ 5561be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5562e14bb325SJeff Bonwick } else { 5563e14bb325SJeff Bonwick /* 5564e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5565e14bb325SJeff Bonwick */ 5566be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 5567fa94a07fSbrendan } 556899653d4eSeschrock 55698ad4d6ddSJeff Bonwick if (!locked) 557063364b0eSAlan Somers error = spa_vdev_exit(spa, NULL, txg, error); 55718ad4d6ddSJeff Bonwick 5572b72b6bb1SAlan Somers if (ev) 5573b72b6bb1SAlan Somers spa_event_post(ev); 5574b72b6bb1SAlan Somers 55758ad4d6ddSJeff Bonwick return (error); 5576fa9e4066Sahrens } 5577fa9e4066Sahrens 5578fa9e4066Sahrens /* 55793d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5580f7170741SWill Andrews * currently spared, so we can detach it. 5581fa9e4066Sahrens */ 5582ea8dc4b6Seschrock static vdev_t * 55833d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5584fa9e4066Sahrens { 5585ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5586fa9e4066Sahrens 5587573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 55883d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5589ea8dc4b6Seschrock if (oldvd != NULL) 5590ea8dc4b6Seschrock return (oldvd); 5591ea8dc4b6Seschrock } 5592fa9e4066Sahrens 55933d7072f8Seschrock /* 5594cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5595cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5596cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5597cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5598cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5599cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 56003d7072f8Seschrock */ 5601cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5602cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5603cb04b873SMark J Musante 5604cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5605ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5606ea8dc4b6Seschrock 56078ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5608e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 56098ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5610ea8dc4b6Seschrock return (oldvd); 5611fa9e4066Sahrens } 5612ea8dc4b6Seschrock 56133d7072f8Seschrock /* 56143d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 56153d7072f8Seschrock */ 5616cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5617cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5618cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5619cb04b873SMark J Musante 5620cb04b873SMark J Musante if (last->vdev_unspare) { 5621cb04b873SMark J Musante oldvd = first; 5622cb04b873SMark J Musante newvd = last; 5623cb04b873SMark J Musante } else if (first->vdev_unspare) { 5624cb04b873SMark J Musante oldvd = last; 5625cb04b873SMark J Musante newvd = first; 5626cb04b873SMark J Musante } else { 5627cb04b873SMark J Musante oldvd = NULL; 5628cb04b873SMark J Musante } 56293d7072f8Seschrock 5630cb04b873SMark J Musante if (oldvd != NULL && 56318ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5632e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5633cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 56343d7072f8Seschrock return (oldvd); 5635cb04b873SMark J Musante 5636cb04b873SMark J Musante /* 5637cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5638cb04b873SMark J Musante * and those spares are not required, then we want to 5639cb04b873SMark J Musante * attempt to free them up now so that they can be used 5640cb04b873SMark J Musante * by other pools. Once we're back down to a single 5641cb04b873SMark J Musante * disk+spare, we stop removing them. 5642cb04b873SMark J Musante */ 5643cb04b873SMark J Musante if (vd->vdev_children > 2) { 5644cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5645cb04b873SMark J Musante 5646cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5647cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5648cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5649cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5650cb04b873SMark J Musante return (newvd); 56513d7072f8Seschrock } 56523d7072f8Seschrock } 56533d7072f8Seschrock 5654ea8dc4b6Seschrock return (NULL); 5655fa9e4066Sahrens } 5656fa9e4066Sahrens 5657ea8dc4b6Seschrock static void 56583d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5659fa9e4066Sahrens { 56608ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 56618ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5662ea8dc4b6Seschrock 56638ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5664ea8dc4b6Seschrock 56653d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 56668ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 56678ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5668ea8dc4b6Seschrock guid = vd->vdev_guid; 56698ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 56708ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 56718ad4d6ddSJeff Bonwick sguid = 0; 567299653d4eSeschrock /* 567399653d4eSeschrock * If we have just finished replacing a hot spared device, then 567499653d4eSeschrock * we need to detach the parent's first child (the original hot 567599653d4eSeschrock * spare) as well. 567699653d4eSeschrock */ 5677cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5678cb04b873SMark J Musante ppvd->vdev_children == 2) { 567999653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 56808ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 568199653d4eSeschrock } 5682b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5683b4952e17SGeorge Wilson 56848ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 56858ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5686ea8dc4b6Seschrock return; 56878ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 568899653d4eSeschrock return; 56898ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5690fa9e4066Sahrens } 5691fa9e4066Sahrens 56928ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5693fa9e4066Sahrens } 5694fa9e4066Sahrens 5695c67d9675Seschrock /* 5696b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5697c67d9675Seschrock */ 5698c67d9675Seschrock int 56996809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 57006809eb4eSEric Schrock boolean_t ispath) 5701c67d9675Seschrock { 5702c5904d13Seschrock vdev_t *vd; 5703208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5704c67d9675Seschrock 5705f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5706f9af39baSGeorge Wilson 5707b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5708c67d9675Seschrock 57096809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5710b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5711c67d9675Seschrock 57120e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5713b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 57140e34b6a7Sbonwick 57156809eb4eSEric Schrock if (ispath) { 5716208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5717208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5718208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5719208044b8SGeorge Wilson sync = B_TRUE; 5720208044b8SGeorge Wilson } 57216809eb4eSEric Schrock } else { 5722208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5723208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5724208044b8SGeorge Wilson sync = B_TRUE; 5725208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 57266809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5727208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5728208044b8SGeorge Wilson sync = B_TRUE; 5729208044b8SGeorge Wilson } 57306809eb4eSEric Schrock } 5731c67d9675Seschrock 5732208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5733c67d9675Seschrock } 5734c67d9675Seschrock 57356809eb4eSEric Schrock int 57366809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 57376809eb4eSEric Schrock { 57386809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 57396809eb4eSEric Schrock } 57406809eb4eSEric Schrock 57416809eb4eSEric Schrock int 57426809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 57436809eb4eSEric Schrock { 57446809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 57456809eb4eSEric Schrock } 57466809eb4eSEric Schrock 5747fa9e4066Sahrens /* 5748fa9e4066Sahrens * ========================================================================== 57493f9d6ad7SLin Ling * SPA Scanning 5750fa9e4066Sahrens * ========================================================================== 5751fa9e4066Sahrens */ 5752*1702cce7SAlek Pinchuk int 5753*1702cce7SAlek Pinchuk spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) 5754*1702cce7SAlek Pinchuk { 5755*1702cce7SAlek Pinchuk ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5756*1702cce7SAlek Pinchuk 5757*1702cce7SAlek Pinchuk if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5758*1702cce7SAlek Pinchuk return (SET_ERROR(EBUSY)); 5759*1702cce7SAlek Pinchuk 5760*1702cce7SAlek Pinchuk return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); 5761*1702cce7SAlek Pinchuk } 5762fa9e4066Sahrens 5763ea8dc4b6Seschrock int 57643f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5765fa9e4066Sahrens { 5766e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57673f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5768be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 57693f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 57703f9d6ad7SLin Ling } 5771bb8b5132Sek 57723f9d6ad7SLin Ling int 57733f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 57743f9d6ad7SLin Ling { 57753f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57763f9d6ad7SLin Ling 57773f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5778be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5779fa9e4066Sahrens 5780fa9e4066Sahrens /* 5781088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5782088f3894Sahrens * writeable leaf device, we have nothing to do. 5783fa9e4066Sahrens */ 57843f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5785088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5786088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5787ea8dc4b6Seschrock return (0); 5788ea8dc4b6Seschrock } 5789fa9e4066Sahrens 57903f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5791fa9e4066Sahrens } 5792fa9e4066Sahrens 5793ea8dc4b6Seschrock /* 5794ea8dc4b6Seschrock * ========================================================================== 5795ea8dc4b6Seschrock * SPA async task processing 5796ea8dc4b6Seschrock * ========================================================================== 5797ea8dc4b6Seschrock */ 5798ea8dc4b6Seschrock 5799ea8dc4b6Seschrock static void 58003d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5801fa9e4066Sahrens { 580249cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 580398d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 580498d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 580549cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 58061d713200SEric Schrock 58071d713200SEric Schrock /* 58081d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 58091d713200SEric Schrock * vdev_clear() as that will cause us to throw away 58101d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 58111d713200SEric Schrock * device, all of which is a waste. 58121d713200SEric Schrock */ 58131d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 58141d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 58151d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 58161d713200SEric Schrock 5817e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5818ea8dc4b6Seschrock } 581949cf58c0SBrendan Gregg - Sun Microsystems 5820e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 582149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5822ea8dc4b6Seschrock } 5823fa9e4066Sahrens 5824e14bb325SJeff Bonwick static void 5825e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5826e14bb325SJeff Bonwick { 5827e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 582898d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5829e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5830e14bb325SJeff Bonwick } 5831e14bb325SJeff Bonwick 5832e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5833e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5834e14bb325SJeff Bonwick } 5835e14bb325SJeff Bonwick 5836573ca77eSGeorge Wilson static void 5837573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5838573ca77eSGeorge Wilson { 5839573ca77eSGeorge Wilson sysevent_id_t eid; 5840573ca77eSGeorge Wilson nvlist_t *attr; 5841573ca77eSGeorge Wilson char *physpath; 5842573ca77eSGeorge Wilson 5843573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5844573ca77eSGeorge Wilson return; 5845573ca77eSGeorge Wilson 5846573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5847573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5848573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5849573ca77eSGeorge Wilson } 5850573ca77eSGeorge Wilson 5851573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5852573ca77eSGeorge Wilson return; 5853573ca77eSGeorge Wilson 5854573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5855573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5856573ca77eSGeorge Wilson 5857573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5858573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5859573ca77eSGeorge Wilson 5860573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5861573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5862573ca77eSGeorge Wilson 5863573ca77eSGeorge Wilson nvlist_free(attr); 5864573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5865573ca77eSGeorge Wilson } 5866573ca77eSGeorge Wilson 5867ea8dc4b6Seschrock static void 5868ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5869ea8dc4b6Seschrock { 5870e14bb325SJeff Bonwick int tasks; 5871ea8dc4b6Seschrock 5872ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5873ea8dc4b6Seschrock 5874ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5875ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5876ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5877ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5878ea8dc4b6Seschrock 58790373e76bSbonwick /* 58800373e76bSbonwick * See if the config needs to be updated. 58810373e76bSbonwick */ 58820373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5883b24ab676SJeff Bonwick uint64_t old_space, new_space; 5884573ca77eSGeorge Wilson 58850373e76bSbonwick mutex_enter(&spa_namespace_lock); 5886b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 58870373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5888b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 58890373e76bSbonwick mutex_exit(&spa_namespace_lock); 5890573ca77eSGeorge Wilson 5891573ca77eSGeorge Wilson /* 5892573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5893573ca77eSGeorge Wilson * then log an internal history event. 5894573ca77eSGeorge Wilson */ 5895b24ab676SJeff Bonwick if (new_space != old_space) { 58964445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5897c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5898b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5899573ca77eSGeorge Wilson } 59000373e76bSbonwick } 59010373e76bSbonwick 5902ea8dc4b6Seschrock /* 59033d7072f8Seschrock * See if any devices need to be marked REMOVED. 5904ea8dc4b6Seschrock */ 5905e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 59068f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 59073d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5908e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 590949cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5910e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 591149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5912e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5913e14bb325SJeff Bonwick } 5914e14bb325SJeff Bonwick 5915573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5916573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5917573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5918573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5919573ca77eSGeorge Wilson } 5920573ca77eSGeorge Wilson 5921e14bb325SJeff Bonwick /* 5922e14bb325SJeff Bonwick * See if any devices need to be probed. 5923e14bb325SJeff Bonwick */ 5924e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 59258f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5926e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5927e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 59283d7072f8Seschrock } 5929ea8dc4b6Seschrock 5930ea8dc4b6Seschrock /* 5931ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5932ea8dc4b6Seschrock */ 59333d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 59343d7072f8Seschrock spa_vdev_resilver_done(spa); 5935fa9e4066Sahrens 5936ea8dc4b6Seschrock /* 5937ea8dc4b6Seschrock * Kick off a resilver. 5938ea8dc4b6Seschrock */ 5939088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 59403f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5941ea8dc4b6Seschrock 5942ea8dc4b6Seschrock /* 5943ea8dc4b6Seschrock * Let the world know that we're done. 5944ea8dc4b6Seschrock */ 5945ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5946ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5947ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5948ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5949ea8dc4b6Seschrock thread_exit(); 5950ea8dc4b6Seschrock } 5951ea8dc4b6Seschrock 5952ea8dc4b6Seschrock void 5953ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5954ea8dc4b6Seschrock { 5955ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5956ea8dc4b6Seschrock spa->spa_async_suspended++; 5957ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5958ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5959ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5960ea8dc4b6Seschrock } 5961ea8dc4b6Seschrock 5962ea8dc4b6Seschrock void 5963ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5964ea8dc4b6Seschrock { 5965ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5966ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5967ea8dc4b6Seschrock spa->spa_async_suspended--; 5968ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5969ea8dc4b6Seschrock } 5970ea8dc4b6Seschrock 59713cb69f73SWill Andrews static boolean_t 59723cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 59733cb69f73SWill Andrews { 59743cb69f73SWill Andrews uint_t non_config_tasks; 59753cb69f73SWill Andrews uint_t config_task; 59763cb69f73SWill Andrews boolean_t config_task_suspended; 59773cb69f73SWill Andrews 59783cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 59793cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 59803cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 59813cb69f73SWill Andrews config_task_suspended = B_FALSE; 59823cb69f73SWill Andrews } else { 59833cb69f73SWill Andrews config_task_suspended = 59843cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 59853cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 59863cb69f73SWill Andrews } 59873cb69f73SWill Andrews 59883cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 59893cb69f73SWill Andrews } 59903cb69f73SWill Andrews 5991ea8dc4b6Seschrock static void 5992ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5993ea8dc4b6Seschrock { 5994ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 59953cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 59963cb69f73SWill Andrews !spa->spa_async_suspended && 59970373e76bSbonwick spa->spa_async_thread == NULL && 59983cb69f73SWill Andrews rootdir != NULL) 5999ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 6000ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 6001ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6002ea8dc4b6Seschrock } 6003ea8dc4b6Seschrock 6004ea8dc4b6Seschrock void 6005ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 6006ea8dc4b6Seschrock { 60073f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 6008ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 6009ea8dc4b6Seschrock spa->spa_async_tasks |= task; 6010ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 6011fa9e4066Sahrens } 6012fa9e4066Sahrens 6013fa9e4066Sahrens /* 6014fa9e4066Sahrens * ========================================================================== 6015fa9e4066Sahrens * SPA syncing routines 6016fa9e4066Sahrens * ========================================================================== 6017fa9e4066Sahrens */ 6018fa9e4066Sahrens 6019cde58dbcSMatthew Ahrens static int 6020cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6021cde58dbcSMatthew Ahrens { 6022cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 6023cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 6024cde58dbcSMatthew Ahrens return (0); 6025b24ab676SJeff Bonwick } 6026b24ab676SJeff Bonwick 6027cde58dbcSMatthew Ahrens static int 6028cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6029b24ab676SJeff Bonwick { 6030b24ab676SJeff Bonwick zio_t *zio = arg; 6031b24ab676SJeff Bonwick 6032b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 6033b24ab676SJeff Bonwick zio->io_flags)); 6034cde58dbcSMatthew Ahrens return (0); 6035fa9e4066Sahrens } 6036fa9e4066Sahrens 603769962b56SMatthew Ahrens /* 603869962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 603969962b56SMatthew Ahrens * amount of time spent syncing frees. 604069962b56SMatthew Ahrens */ 604169962b56SMatthew Ahrens static void 604269962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 604369962b56SMatthew Ahrens { 604469962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 604569962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 604669962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 604769962b56SMatthew Ahrens } 604869962b56SMatthew Ahrens 604969962b56SMatthew Ahrens /* 605069962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 605169962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 605269962b56SMatthew Ahrens */ 605369962b56SMatthew Ahrens static void 605469962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 605569962b56SMatthew Ahrens { 605669962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 605769962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 605869962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 605969962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 606069962b56SMatthew Ahrens } 606169962b56SMatthew Ahrens 606269962b56SMatthew Ahrens 6063fa9e4066Sahrens static void 606499653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6065fa9e4066Sahrens { 6066fa9e4066Sahrens char *packed = NULL; 6067f7991ba4STim Haley size_t bufsize; 6068fa9e4066Sahrens size_t nvsize = 0; 6069fa9e4066Sahrens dmu_buf_t *db; 6070fa9e4066Sahrens 607199653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6072fa9e4066Sahrens 6073f7991ba4STim Haley /* 6074f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 607543466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6076f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6077f7991ba4STim Haley */ 6078ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6079f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6080fa9e4066Sahrens 608199653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6082ea8dc4b6Seschrock KM_SLEEP) == 0); 6083f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6084fa9e4066Sahrens 6085f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6086fa9e4066Sahrens 6087f7991ba4STim Haley kmem_free(packed, bufsize); 6088fa9e4066Sahrens 608999653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6090fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6091fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6092ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6093fa9e4066Sahrens } 6094fa9e4066Sahrens 609599653d4eSeschrock static void 6096fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6097fa94a07fSbrendan const char *config, const char *entry) 609899653d4eSeschrock { 609999653d4eSeschrock nvlist_t *nvroot; 6100fa94a07fSbrendan nvlist_t **list; 610199653d4eSeschrock int i; 610299653d4eSeschrock 6103fa94a07fSbrendan if (!sav->sav_sync) 610499653d4eSeschrock return; 610599653d4eSeschrock 610699653d4eSeschrock /* 6107fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6108fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 61093d7072f8Seschrock * valid and the vdevs are labeled appropriately. 611099653d4eSeschrock */ 6111fa94a07fSbrendan if (sav->sav_object == 0) { 6112fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6113fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6114fa94a07fSbrendan sizeof (uint64_t), tx); 611599653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6116fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6117fa94a07fSbrendan &sav->sav_object, tx) == 0); 611899653d4eSeschrock } 611999653d4eSeschrock 612099653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6121fa94a07fSbrendan if (sav->sav_count == 0) { 6122fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 612399653d4eSeschrock } else { 6124fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6125fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6126fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 61273f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6128fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6129fa94a07fSbrendan sav->sav_count) == 0); 6130fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6131fa94a07fSbrendan nvlist_free(list[i]); 6132fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 613399653d4eSeschrock } 613499653d4eSeschrock 6135fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 613606eeb2adSek nvlist_free(nvroot); 613799653d4eSeschrock 6138fa94a07fSbrendan sav->sav_sync = B_FALSE; 613999653d4eSeschrock } 614099653d4eSeschrock 6141215198a6SJoe Stein /* 6142215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6143215198a6SJoe Stein * The all-vdev ZAP must be empty. 6144215198a6SJoe Stein */ 6145215198a6SJoe Stein static void 6146215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6147215198a6SJoe Stein { 6148215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6149215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6150215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6151215198a6SJoe Stein vd->vdev_top_zap, tx)); 6152215198a6SJoe Stein } 6153215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6154215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6155215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6156215198a6SJoe Stein } 6157215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6158215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6159215198a6SJoe Stein } 6160215198a6SJoe Stein } 6161215198a6SJoe Stein 616299653d4eSeschrock static void 616399653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 616499653d4eSeschrock { 616599653d4eSeschrock nvlist_t *config; 616699653d4eSeschrock 6167215198a6SJoe Stein /* 6168215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6169215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6170215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6171215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6172215198a6SJoe Stein */ 6173215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6174215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 617599653d4eSeschrock return; 617699653d4eSeschrock 6177e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6178e14bb325SJeff Bonwick 6179215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6180555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 6181215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6182215198a6SJoe Stein 6183215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6184215198a6SJoe Stein /* Make and build the new AVZ */ 6185215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6186215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6187215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6188215198a6SJoe Stein 6189215198a6SJoe Stein /* Diff old AVZ with new one */ 6190215198a6SJoe Stein zap_cursor_t zc; 6191215198a6SJoe Stein zap_attribute_t za; 6192215198a6SJoe Stein 6193215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6194215198a6SJoe Stein spa->spa_all_vdev_zaps); 6195215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6196215198a6SJoe Stein zap_cursor_advance(&zc)) { 6197215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6198215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6199215198a6SJoe Stein vdzap) == ENOENT) { 6200215198a6SJoe Stein /* 6201215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6202215198a6SJoe Stein * destroy it 6203215198a6SJoe Stein */ 6204215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6205215198a6SJoe Stein tx)); 6206215198a6SJoe Stein } 6207215198a6SJoe Stein } 6208215198a6SJoe Stein 6209215198a6SJoe Stein zap_cursor_fini(&zc); 6210215198a6SJoe Stein 6211215198a6SJoe Stein /* Destroy the old AVZ */ 6212215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6213215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6214215198a6SJoe Stein 6215215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6216215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6217215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6218215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6219215198a6SJoe Stein 6220215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6221215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6222215198a6SJoe Stein zap_cursor_t zc; 6223215198a6SJoe Stein zap_attribute_t za; 6224215198a6SJoe Stein 6225215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6226215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6227215198a6SJoe Stein spa->spa_all_vdev_zaps); 6228215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6229215198a6SJoe Stein zap_cursor_advance(&zc)) { 6230215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6231215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6232215198a6SJoe Stein } 6233215198a6SJoe Stein 6234215198a6SJoe Stein zap_cursor_fini(&zc); 6235215198a6SJoe Stein 6236215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6237215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6238215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6239215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6240215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6241215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6242215198a6SJoe Stein } 6243215198a6SJoe Stein 6244215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6245215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6246215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6247215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6248215198a6SJoe Stein } 6249215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6250215198a6SJoe Stein 6251215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6252215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6253215198a6SJoe Stein 6254e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6255e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6256e14bb325SJeff Bonwick 625725345e46SGeorge Wilson /* 625825345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 625925345e46SGeorge Wilson * the config object gets updated with the correct version. 626025345e46SGeorge Wilson */ 626125345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 626225345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 626325345e46SGeorge Wilson spa->spa_uberblock.ub_version); 626425345e46SGeorge Wilson 6265e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 626699653d4eSeschrock 6267aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 626899653d4eSeschrock spa->spa_config_syncing = config; 626999653d4eSeschrock 627099653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 627199653d4eSeschrock } 627299653d4eSeschrock 6273ad135b5dSChristopher Siden static void 62743b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6275ad135b5dSChristopher Siden { 62763b2aab18SMatthew Ahrens uint64_t *versionp = arg; 62773b2aab18SMatthew Ahrens uint64_t version = *versionp; 62783b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6279ad135b5dSChristopher Siden 6280ad135b5dSChristopher Siden /* 6281ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6282ad135b5dSChristopher Siden */ 6283ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6284ad135b5dSChristopher Siden 628562eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6286ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6287ad135b5dSChristopher Siden 6288ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6289ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 62904445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6291ad135b5dSChristopher Siden } 6292ad135b5dSChristopher Siden 6293990b4856Slling /* 6294990b4856Slling * Set zpool properties. 6295990b4856Slling */ 6296b1b8ab34Slling static void 62973b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6298b1b8ab34Slling { 62993b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 63003b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6301b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6302ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6303b1b8ab34Slling 6304e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6305e14bb325SJeff Bonwick 6306990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6307ad135b5dSChristopher Siden uint64_t intval; 6308ad135b5dSChristopher Siden char *strval, *fname; 6309ad135b5dSChristopher Siden zpool_prop_t prop; 6310ad135b5dSChristopher Siden const char *propname; 6311ad135b5dSChristopher Siden zprop_type_t proptype; 63122acef22dSMatthew Ahrens spa_feature_t fid; 6313ad135b5dSChristopher Siden 6314990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6315ad135b5dSChristopher Siden case ZPROP_INVAL: 6316ad135b5dSChristopher Siden /* 6317ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6318ad135b5dSChristopher Siden */ 6319ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6320ad135b5dSChristopher Siden 6321ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 63222acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6323ad135b5dSChristopher Siden 63242acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 63254445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63264445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6327ad135b5dSChristopher Siden break; 6328ad135b5dSChristopher Siden 6329990b4856Slling case ZPOOL_PROP_VERSION: 63300713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6331990b4856Slling /* 6332ad135b5dSChristopher Siden * The version is synced seperatly before other 6333ad135b5dSChristopher Siden * properties and should be correct by now. 6334990b4856Slling */ 6335ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6336ecd6cf80Smarks break; 6337990b4856Slling 6338990b4856Slling case ZPOOL_PROP_ALTROOT: 6339990b4856Slling /* 6340990b4856Slling * 'altroot' is a non-persistent property. It should 6341990b4856Slling * have been set temporarily at creation or import time. 6342990b4856Slling */ 6343990b4856Slling ASSERT(spa->spa_root != NULL); 6344b1b8ab34Slling break; 63453d7072f8Seschrock 6346f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 63472f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6348990b4856Slling /* 6349f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6350f9af39baSGeorge Wilson * properties. 6351990b4856Slling */ 63523d7072f8Seschrock break; 63538704186eSDan McDonald case ZPOOL_PROP_COMMENT: 63540713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63558704186eSDan McDonald if (spa->spa_comment != NULL) 63568704186eSDan McDonald spa_strfree(spa->spa_comment); 63578704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 63588704186eSDan McDonald /* 63598704186eSDan McDonald * We need to dirty the configuration on all the vdevs 63608704186eSDan McDonald * so that their labels get updated. It's unnecessary 63618704186eSDan McDonald * to do this for pool creation since the vdev's 63628704186eSDan McDonald * configuratoin has already been dirtied. 63638704186eSDan McDonald */ 63648704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 63658704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 63664445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63674445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 63688704186eSDan McDonald break; 6369990b4856Slling default: 6370990b4856Slling /* 6371990b4856Slling * Set pool property values in the poolprops mos object. 6372990b4856Slling */ 6373990b4856Slling if (spa->spa_pool_props_object == 0) { 6374ad135b5dSChristopher Siden spa->spa_pool_props_object = 6375ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6376990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6377ad135b5dSChristopher Siden tx); 6378990b4856Slling } 6379990b4856Slling 6380990b4856Slling /* normalize the property name */ 6381990b4856Slling propname = zpool_prop_to_name(prop); 6382990b4856Slling proptype = zpool_prop_get_type(prop); 6383990b4856Slling 6384990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6385990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 63860713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63870713e232SGeorge Wilson VERIFY0(zap_update(mos, 6388990b4856Slling spa->spa_pool_props_object, propname, 63890713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 63904445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63914445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6392990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 63930713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6394990b4856Slling 6395990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6396990b4856Slling const char *unused; 63970713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 63980713e232SGeorge Wilson prop, intval, &unused)); 6399990b4856Slling } 64000713e232SGeorge Wilson VERIFY0(zap_update(mos, 6401990b4856Slling spa->spa_pool_props_object, propname, 64020713e232SGeorge Wilson 8, 1, &intval, tx)); 64034445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 64044445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6405990b4856Slling } else { 6406990b4856Slling ASSERT(0); /* not allowed */ 6407990b4856Slling } 6408990b4856Slling 64090a4e9518Sgw switch (prop) { 64100a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6411990b4856Slling spa->spa_delegation = intval; 64120a4e9518Sgw break; 64130a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6414990b4856Slling spa->spa_bootfs = intval; 64150a4e9518Sgw break; 64160a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 64170a4e9518Sgw spa->spa_failmode = intval; 64180a4e9518Sgw break; 6419573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6420573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6421b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6422b98131cfSEric Taylor spa_async_request(spa, 6423b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6424573ca77eSGeorge Wilson break; 6425b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6426b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6427b24ab676SJeff Bonwick break; 64280a4e9518Sgw default: 64290a4e9518Sgw break; 64300a4e9518Sgw } 6431990b4856Slling } 6432990b4856Slling 6433b1b8ab34Slling } 6434e14bb325SJeff Bonwick 6435e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6436b1b8ab34Slling } 6437b1b8ab34Slling 6438cde58dbcSMatthew Ahrens /* 6439cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6440cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6441cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6442cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6443cde58dbcSMatthew Ahrens * tasks. 6444cde58dbcSMatthew Ahrens */ 6445cde58dbcSMatthew Ahrens static void 6446cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6447cde58dbcSMatthew Ahrens { 6448cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6449cde58dbcSMatthew Ahrens 6450cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6451cde58dbcSMatthew Ahrens 64523b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 64533b2aab18SMatthew Ahrens 6454cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6455cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6456cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6457cde58dbcSMatthew Ahrens 6458cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6459cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6460cde58dbcSMatthew Ahrens } 6461cde58dbcSMatthew Ahrens 6462cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6463cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6464cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6465cde58dbcSMatthew Ahrens } 6466cde58dbcSMatthew Ahrens 6467cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6468cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6469cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6470cde58dbcSMatthew Ahrens 6471cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6472cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6473cde58dbcSMatthew Ahrens } 6474ad135b5dSChristopher Siden 6475ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6476ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6477ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6478ad135b5dSChristopher Siden } 6479b8289d24SDaniil Lunev 6480b8289d24SDaniil Lunev /* 6481b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6482b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6483b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6484b8289d24SDaniil Lunev * this feature active 6485b8289d24SDaniil Lunev */ 6486b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6487b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6488b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6489b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6490b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6491b8289d24SDaniil Lunev 6492b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6493b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6494b8289d24SDaniil Lunev } 649545818ee1SMatthew Ahrens 649645818ee1SMatthew Ahrens /* 649745818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 649845818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 649945818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 650045818ee1SMatthew Ahrens */ 650145818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 650245818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 650345818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 650445818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 650545818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 650645818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 650745818ee1SMatthew Ahrens } 650845818ee1SMatthew Ahrens 65093b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6510cde58dbcSMatthew Ahrens } 6511cde58dbcSMatthew Ahrens 6512fa9e4066Sahrens /* 6513fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6514fa9e4066Sahrens * part of the process, so we iterate until it converges. 6515fa9e4066Sahrens */ 6516fa9e4066Sahrens void 6517fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6518fa9e4066Sahrens { 6519fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6520fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6521b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 65220373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6523fa9e4066Sahrens vdev_t *vd; 6524fa9e4066Sahrens dmu_tx_t *tx; 6525e14bb325SJeff Bonwick int error; 65260f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 65270f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 6528fa9e4066Sahrens 6529f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6530f9af39baSGeorge Wilson 6531fa9e4066Sahrens /* 6532fa9e4066Sahrens * Lock out configuration changes. 6533fa9e4066Sahrens */ 6534e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6535fa9e4066Sahrens 6536fa9e4066Sahrens spa->spa_syncing_txg = txg; 6537fa9e4066Sahrens spa->spa_sync_pass = 0; 6538fa9e4066Sahrens 65390f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 65400f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 65410f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 65420f7643c7SGeorge Wilson 6543e14bb325SJeff Bonwick /* 6544e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6545e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6546e14bb325SJeff Bonwick */ 6547e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 65488ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 65498ad4d6ddSJeff Bonwick /* 65508ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 65518ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 65528ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 65538ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 65548ad4d6ddSJeff Bonwick * into the root vdev tree. 65558ad4d6ddSJeff Bonwick */ 65568ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65578ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 65588ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 65598ad4d6ddSJeff Bonwick vdev_state_clean(vd); 65608ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 65618ad4d6ddSJeff Bonwick } 65628ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65638ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6564e14bb325SJeff Bonwick } 6565e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6566e14bb325SJeff Bonwick 656799653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 656899653d4eSeschrock 6569283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6570283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6571283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6572283b8460SGeorge.Wilson 657399653d4eSeschrock /* 6574e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 657599653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 657699653d4eSeschrock */ 6577e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6578e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 657999653d4eSeschrock int i; 658099653d4eSeschrock 658199653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 658299653d4eSeschrock vd = rvd->vdev_child[i]; 658399653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 658499653d4eSeschrock break; 658599653d4eSeschrock } 658699653d4eSeschrock if (i == rvd->vdev_children) { 658799653d4eSeschrock spa->spa_deflate = TRUE; 658899653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 658999653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 659099653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 659199653d4eSeschrock } 659299653d4eSeschrock } 659399653d4eSeschrock 65940f7643c7SGeorge Wilson /* 65950f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 65960f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 65970f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 65980f7643c7SGeorge Wilson * out this txg. 65990f7643c7SGeorge Wilson */ 66000f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 66010f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 66020f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 66030f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 66040f7643c7SGeorge Wilson 66050f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 66060f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 66070f7643c7SGeorge Wilson continue; 66080f7643c7SGeorge Wilson 66090f7643c7SGeorge Wilson /* 66100f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 66110f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 66120f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 66130f7643c7SGeorge Wilson */ 66140f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 66150f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 66160f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 66170f7643c7SGeorge Wilson } 66180f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 66190f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 66200f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 66210f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 66220f7643c7SGeorge Wilson 66230f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 66240f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 66250f7643c7SGeorge Wilson 6626fa9e4066Sahrens /* 6627fa9e4066Sahrens * Iterate to convergence. 6628fa9e4066Sahrens */ 6629fa9e4066Sahrens do { 6630b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6631fa9e4066Sahrens 6632fa9e4066Sahrens spa_sync_config_object(spa, tx); 6633fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6634fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6635fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6636fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6637ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6638fa9e4066Sahrens dsl_pool_sync(dp, txg); 6639fa9e4066Sahrens 664001f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 664169962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6642b24ab676SJeff Bonwick } else { 6643231aab85SMatthew Ahrens /* 6644231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6645231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6646231aab85SMatthew Ahrens */ 6647231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6648cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 664969962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6650fa9e4066Sahrens } 6651fa9e4066Sahrens 6652b24ab676SJeff Bonwick ddt_sync(spa, txg); 66533f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6654afee20e4SGeorge Wilson 6655b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6656b24ab676SJeff Bonwick vdev_sync(vd, txg); 6657b24ab676SJeff Bonwick 6658231aab85SMatthew Ahrens if (pass == 1) { 6659cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6660231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6661231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6662231aab85SMatthew Ahrens /* 6663231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6664231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6665231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6666231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6667231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6668231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6669231aab85SMatthew Ahrens * data (though in this case the MOS will most 6670231aab85SMatthew Ahrens * likely also be dirty due to second order 6671231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6672231aab85SMatthew Ahrens */ 6673231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6674231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6675231aab85SMatthew Ahrens /* 6676231aab85SMatthew Ahrens * Nothing changed on the first pass, 6677231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6678231aab85SMatthew Ahrens * syncing deferred frees, so that we 6679231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6680231aab85SMatthew Ahrens */ 6681231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6682231aab85SMatthew Ahrens txg)); 6683231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6684231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6685231aab85SMatthew Ahrens break; 6686231aab85SMatthew Ahrens } 6687231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6688231aab85SMatthew Ahrens } 6689fa9e4066Sahrens 6690cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6691fa9e4066Sahrens 6692215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 6693215198a6SJoe Stein /* 6694215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 6695215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 6696215198a6SJoe Stein * called if the config is dirty; otherwise there may be 6697215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 6698215198a6SJoe Stein * spa_sync_config_object. 6699215198a6SJoe Stein */ 6700215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 6701215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 6702215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 6703215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 6704215198a6SJoe Stein all_vdev_zap_entry_count); 6705215198a6SJoe Stein } 6706215198a6SJoe Stein 6707fa9e4066Sahrens /* 6708fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6709fa9e4066Sahrens * to commit the transaction group. 67100373e76bSbonwick * 671117f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 671217f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6713e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6714e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 67150373e76bSbonwick */ 6716e14bb325SJeff Bonwick for (;;) { 6717e14bb325SJeff Bonwick /* 6718e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6719e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6720e14bb325SJeff Bonwick */ 6721e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6722e14bb325SJeff Bonwick 6723e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6724e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6725e14bb325SJeff Bonwick int svdcount = 0; 6726e14bb325SJeff Bonwick int children = rvd->vdev_children; 6727e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6728e14bb325SJeff Bonwick 6729573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6730e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6731e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6732e14bb325SJeff Bonwick continue; 6733e14bb325SJeff Bonwick svd[svdcount++] = vd; 6734e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6735e14bb325SJeff Bonwick break; 6736e14bb325SJeff Bonwick } 6737eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 6738e14bb325SJeff Bonwick } else { 6739e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 6740eb5bb584SWill Andrews rvd->vdev_children, txg); 67410373e76bSbonwick } 6742e14bb325SJeff Bonwick 6743dfbb9432SGeorge Wilson if (error == 0) 6744dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6745dfbb9432SGeorge Wilson 6746e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6747e14bb325SJeff Bonwick 6748e14bb325SJeff Bonwick if (error == 0) 6749e14bb325SJeff Bonwick break; 6750e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6751e14bb325SJeff Bonwick zio_resume_wait(spa); 67520373e76bSbonwick } 675399653d4eSeschrock dmu_tx_commit(tx); 675499653d4eSeschrock 6755283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6756283b8460SGeorge.Wilson 67570373e76bSbonwick /* 67580373e76bSbonwick * Clear the dirty config list. 6759fa9e4066Sahrens */ 6760e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 67610373e76bSbonwick vdev_config_clean(vd); 67620373e76bSbonwick 67630373e76bSbonwick /* 67640373e76bSbonwick * Now that the new config has synced transactionally, 67650373e76bSbonwick * let it become visible to the config cache. 67660373e76bSbonwick */ 67670373e76bSbonwick if (spa->spa_config_syncing != NULL) { 67680373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 67690373e76bSbonwick spa->spa_config_txg = txg; 67700373e76bSbonwick spa->spa_config_syncing = NULL; 67710373e76bSbonwick } 6772fa9e4066Sahrens 6773b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6774fa9e4066Sahrens 67750f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 67760f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 67770f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 67780f7643c7SGeorge Wilson 6779fa9e4066Sahrens /* 6780fa9e4066Sahrens * Update usable space statistics. 6781fa9e4066Sahrens */ 6782fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6783fa9e4066Sahrens vdev_sync_done(vd, txg); 6784fa9e4066Sahrens 6785485bbbf5SGeorge Wilson spa_update_dspace(spa); 6786485bbbf5SGeorge Wilson 6787fa9e4066Sahrens /* 6788fa9e4066Sahrens * It had better be the case that we didn't dirty anything 678999653d4eSeschrock * since vdev_config_sync(). 6790fa9e4066Sahrens */ 6791fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6792fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6793fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6794b24ab676SJeff Bonwick 6795b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6796fa9e4066Sahrens 679743297f97SGeorge Wilson /* 679843297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 679943297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 680043297f97SGeorge Wilson * will be guaranteed that all the processing associated with 680143297f97SGeorge Wilson * that txg has been completed. 680243297f97SGeorge Wilson */ 680343297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 6804e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6805ea8dc4b6Seschrock 6806468c413aSTim Haley spa_handle_ignored_writes(spa); 6807468c413aSTim Haley 6808ea8dc4b6Seschrock /* 6809ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6810ea8dc4b6Seschrock */ 6811ea8dc4b6Seschrock spa_async_dispatch(spa); 6812fa9e4066Sahrens } 6813fa9e4066Sahrens 6814fa9e4066Sahrens /* 6815fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6816fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6817fa9e4066Sahrens * sync. 6818fa9e4066Sahrens */ 6819fa9e4066Sahrens void 6820fa9e4066Sahrens spa_sync_allpools(void) 6821fa9e4066Sahrens { 6822fa9e4066Sahrens spa_t *spa = NULL; 6823fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6824fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6825f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6826f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6827fa9e4066Sahrens continue; 6828fa9e4066Sahrens spa_open_ref(spa, FTAG); 6829fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6830fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6831fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6832fa9e4066Sahrens spa_close(spa, FTAG); 6833fa9e4066Sahrens } 6834fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6835fa9e4066Sahrens } 6836fa9e4066Sahrens 6837fa9e4066Sahrens /* 6838fa9e4066Sahrens * ========================================================================== 6839fa9e4066Sahrens * Miscellaneous routines 6840fa9e4066Sahrens * ========================================================================== 6841fa9e4066Sahrens */ 6842fa9e4066Sahrens 6843fa9e4066Sahrens /* 6844fa9e4066Sahrens * Remove all pools in the system. 6845fa9e4066Sahrens */ 6846fa9e4066Sahrens void 6847fa9e4066Sahrens spa_evict_all(void) 6848fa9e4066Sahrens { 6849fa9e4066Sahrens spa_t *spa; 6850fa9e4066Sahrens 6851fa9e4066Sahrens /* 6852fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6853fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6854fa9e4066Sahrens */ 6855fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6856fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6857fa9e4066Sahrens /* 6858ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6859ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6860ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6861fa9e4066Sahrens */ 6862fa9e4066Sahrens spa_open_ref(spa, FTAG); 6863fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6864ea8dc4b6Seschrock spa_async_suspend(spa); 6865fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6866fa9e4066Sahrens spa_close(spa, FTAG); 6867fa9e4066Sahrens 6868fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6869fa9e4066Sahrens spa_unload(spa); 6870fa9e4066Sahrens spa_deactivate(spa); 6871fa9e4066Sahrens } 6872fa9e4066Sahrens spa_remove(spa); 6873fa9e4066Sahrens } 6874fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6875fa9e4066Sahrens } 6876ea8dc4b6Seschrock 6877ea8dc4b6Seschrock vdev_t * 68786809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6879ea8dc4b6Seschrock { 6880c5904d13Seschrock vdev_t *vd; 6881c5904d13Seschrock int i; 6882c5904d13Seschrock 6883c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6884c5904d13Seschrock return (vd); 6885c5904d13Seschrock 68866809eb4eSEric Schrock if (aux) { 6887c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6888c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 68896809eb4eSEric Schrock if (vd->vdev_guid == guid) 68906809eb4eSEric Schrock return (vd); 68916809eb4eSEric Schrock } 68926809eb4eSEric Schrock 68936809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 68946809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6895c5904d13Seschrock if (vd->vdev_guid == guid) 6896c5904d13Seschrock return (vd); 6897c5904d13Seschrock } 6898c5904d13Seschrock } 6899c5904d13Seschrock 6900c5904d13Seschrock return (NULL); 6901ea8dc4b6Seschrock } 6902eaca9bbdSeschrock 6903eaca9bbdSeschrock void 6904990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6905eaca9bbdSeschrock { 6906f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6907f9af39baSGeorge Wilson 6908e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6909eaca9bbdSeschrock 6910eaca9bbdSeschrock /* 6911eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6912eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6913eaca9bbdSeschrock * possible. 6914eaca9bbdSeschrock */ 691562eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 69165d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6917eaca9bbdSeschrock 6918990b4856Slling spa->spa_uberblock.ub_version = version; 6919eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6920eaca9bbdSeschrock 6921e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 692299653d4eSeschrock 692399653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 692499653d4eSeschrock } 692599653d4eSeschrock 692699653d4eSeschrock boolean_t 692799653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 692899653d4eSeschrock { 692999653d4eSeschrock int i; 693039c23413Seschrock uint64_t spareguid; 6931fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 693299653d4eSeschrock 6933fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6934fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 693599653d4eSeschrock return (B_TRUE); 693699653d4eSeschrock 6937fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6938fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6939fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 694039c23413Seschrock return (B_TRUE); 694139c23413Seschrock } 694239c23413Seschrock 694399653d4eSeschrock return (B_FALSE); 6944eaca9bbdSeschrock } 6945b1b8ab34Slling 694689a89ebfSlling /* 694789a89ebfSlling * Check if a pool has an active shared spare device. 694889a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 694989a89ebfSlling */ 695089a89ebfSlling static boolean_t 695189a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 695289a89ebfSlling { 695389a89ebfSlling int i, refcnt; 695489a89ebfSlling uint64_t pool; 695589a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 695689a89ebfSlling 695789a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 695889a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 695989a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 696089a89ebfSlling refcnt > 2) 696189a89ebfSlling return (B_TRUE); 696289a89ebfSlling } 696389a89ebfSlling 696489a89ebfSlling return (B_FALSE); 696589a89ebfSlling } 696689a89ebfSlling 6967b72b6bb1SAlan Somers static sysevent_t * 6968ce1577b0SDave Eddy spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 69693d7072f8Seschrock { 6970b72b6bb1SAlan Somers sysevent_t *ev = NULL; 69713d7072f8Seschrock #ifdef _KERNEL 69723d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 69733d7072f8Seschrock sysevent_value_t value; 69743d7072f8Seschrock 69753d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 69763d7072f8Seschrock SE_SLEEP); 6977b72b6bb1SAlan Somers ASSERT(ev != NULL); 69783d7072f8Seschrock 69793d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69803d7072f8Seschrock value.value.sv_string = spa_name(spa); 69813d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 69823d7072f8Seschrock goto done; 69833d7072f8Seschrock 69843d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69853d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 69863d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 69873d7072f8Seschrock goto done; 69883d7072f8Seschrock 69893d7072f8Seschrock if (vd) { 69903d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69913d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 69923d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 69933d7072f8Seschrock SE_SLEEP) != 0) 69943d7072f8Seschrock goto done; 69953d7072f8Seschrock 69963d7072f8Seschrock if (vd->vdev_path) { 69973d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69983d7072f8Seschrock value.value.sv_string = vd->vdev_path; 69993d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 70003d7072f8Seschrock &value, SE_SLEEP) != 0) 70013d7072f8Seschrock goto done; 70023d7072f8Seschrock } 70033d7072f8Seschrock } 70043d7072f8Seschrock 7005ce1577b0SDave Eddy if (hist_nvl != NULL) { 7006ce1577b0SDave Eddy fnvlist_merge((nvlist_t *)attr, hist_nvl); 7007ce1577b0SDave Eddy } 7008ce1577b0SDave Eddy 7009b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 7010b01c3b58Seschrock goto done; 7011b01c3b58Seschrock attr = NULL; 7012b01c3b58Seschrock 70133d7072f8Seschrock done: 70143d7072f8Seschrock if (attr) 70153d7072f8Seschrock sysevent_free_attr(attr); 7016b72b6bb1SAlan Somers 7017b72b6bb1SAlan Somers #endif 7018b72b6bb1SAlan Somers return (ev); 7019b72b6bb1SAlan Somers } 7020b72b6bb1SAlan Somers 7021b72b6bb1SAlan Somers static void 7022b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 7023b72b6bb1SAlan Somers { 7024b72b6bb1SAlan Somers #ifdef _KERNEL 7025b72b6bb1SAlan Somers sysevent_id_t eid; 7026b72b6bb1SAlan Somers 7027b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 70283d7072f8Seschrock sysevent_free(ev); 70293d7072f8Seschrock #endif 70303d7072f8Seschrock } 7031b72b6bb1SAlan Somers 7032b72b6bb1SAlan Somers /* 7033b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 7034b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 7035ce1577b0SDave Eddy * filled in from the spa and (optionally) the vdev and history nvl. This 7036ce1577b0SDave Eddy * doesn't do anything in the userland libzpool, as we don't want consumers to 7037ce1577b0SDave Eddy * misinterpret ztest or zdb as real changes. 7038b72b6bb1SAlan Somers */ 7039b72b6bb1SAlan Somers void 7040ce1577b0SDave Eddy spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 7041b72b6bb1SAlan Somers { 7042ce1577b0SDave Eddy spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 7043b72b6bb1SAlan Somers } 7044