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> 30*ce1577b0SDave Eddy * Copyright 2017 Joyent, Inc. 315aeb9474SGarrett D'Amore */ 32fa9e4066Sahrens 33fa9e4066Sahrens /* 343e30c24aSWill Andrews * SPA: Storage Pool Allocator 353e30c24aSWill Andrews * 36fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 37fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 38fa9e4066Sahrens * pool. 39fa9e4066Sahrens */ 40fa9e4066Sahrens 41fa9e4066Sahrens #include <sys/zfs_context.h> 42ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 43fa9e4066Sahrens #include <sys/spa_impl.h> 44fa9e4066Sahrens #include <sys/zio.h> 45fa9e4066Sahrens #include <sys/zio_checksum.h> 46fa9e4066Sahrens #include <sys/dmu.h> 47fa9e4066Sahrens #include <sys/dmu_tx.h> 48fa9e4066Sahrens #include <sys/zap.h> 49fa9e4066Sahrens #include <sys/zil.h> 50b24ab676SJeff Bonwick #include <sys/ddt.h> 51fa9e4066Sahrens #include <sys/vdev_impl.h> 52fa9e4066Sahrens #include <sys/metaslab.h> 5388ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 54fa9e4066Sahrens #include <sys/uberblock_impl.h> 55fa9e4066Sahrens #include <sys/txg.h> 56fa9e4066Sahrens #include <sys/avl.h> 57fa9e4066Sahrens #include <sys/dmu_traverse.h> 58b1b8ab34Slling #include <sys/dmu_objset.h> 59fa9e4066Sahrens #include <sys/unique.h> 60fa9e4066Sahrens #include <sys/dsl_pool.h> 61b1b8ab34Slling #include <sys/dsl_dataset.h> 62fa9e4066Sahrens #include <sys/dsl_dir.h> 63fa9e4066Sahrens #include <sys/dsl_prop.h> 64b1b8ab34Slling #include <sys/dsl_synctask.h> 65fa9e4066Sahrens #include <sys/fs/zfs.h> 66fa94a07fSbrendan #include <sys/arc.h> 67fa9e4066Sahrens #include <sys/callb.h> 6895173954Sek #include <sys/systeminfo.h> 69e7cbe64fSgw #include <sys/spa_boot.h> 70573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 713f9d6ad7SLin Ling #include <sys/dsl_scan.h> 72ad135b5dSChristopher Siden #include <sys/zfeature.h> 733b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 74770499e1SDan Kimmel #include <sys/abd.h> 75fa9e4066Sahrens 765679c89fSjv #ifdef _KERNEL 77dedec472SJack Meng #include <sys/bootprops.h> 7835a5a358SJonathan Adams #include <sys/callb.h> 7935a5a358SJonathan Adams #include <sys/cpupart.h> 8035a5a358SJonathan Adams #include <sys/pool.h> 8135a5a358SJonathan Adams #include <sys/sysdc.h> 8235a5a358SJonathan Adams #include <sys/zone.h> 835679c89fSjv #endif /* _KERNEL */ 845679c89fSjv 85990b4856Slling #include "zfs_prop.h" 86b7b97454Sperrin #include "zfs_comutil.h" 87990b4856Slling 883cb69f73SWill Andrews /* 893cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes 903cb69f73SWill Andrews * should be retried. 913cb69f73SWill Andrews */ 923cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300; 933cb69f73SWill Andrews 9435a5a358SJonathan Adams typedef enum zti_modes { 95ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 96ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 97ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 98ec94d322SAdam Leventhal ZTI_NMODES 9935a5a358SJonathan Adams } zti_modes_t; 100416e0cd8Sek 101ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 102ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 103ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 1042e0c549eSJonathan Adams 105ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 106ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 1072e0c549eSJonathan Adams 1082e0c549eSJonathan Adams typedef struct zio_taskq_info { 109ec94d322SAdam Leventhal zti_modes_t zti_mode; 11080eb36f2SGeorge Wilson uint_t zti_value; 111ec94d322SAdam Leventhal uint_t zti_count; 1122e0c549eSJonathan Adams } zio_taskq_info_t; 1132e0c549eSJonathan Adams 1142e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 11535a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1162e0c549eSJonathan Adams }; 1172e0c549eSJonathan Adams 11880eb36f2SGeorge Wilson /* 119ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 120ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 121ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 122ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 123ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 124ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 125ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 126ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 127ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 128ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 129ec94d322SAdam Leventhal * particular taskq is chosen at random. 130ec94d322SAdam Leventhal * 131ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 132ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 133ec94d322SAdam Leventhal * need to be handled with minimum delay. 13480eb36f2SGeorge Wilson */ 13580eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 13680eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 137ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 1381b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 139ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 140ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 141ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 142ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1432e0c549eSJonathan Adams }; 1442e0c549eSJonathan Adams 145*ce1577b0SDave Eddy static sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, 146*ce1577b0SDave Eddy const char *name); 147b72b6bb1SAlan Somers static void spa_event_post(sysevent_t *ev); 1483b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 1493b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 15089a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1511195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1521195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1531195e687SMark J Musante char **ereport); 154cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 155990b4856Slling 15669962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 15735a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 15835a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 15935a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 16035a5a358SJonathan Adams 16135a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 16201f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 16335a5a358SJonathan Adams 16435a5a358SJonathan Adams /* 16535a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 16635a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 16735a5a358SJonathan Adams */ 16835a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 16935a5a358SJonathan Adams 170990b4856Slling /* 171990b4856Slling * ========================================================================== 172990b4856Slling * SPA properties routines 173990b4856Slling * ========================================================================== 174990b4856Slling */ 175990b4856Slling 176990b4856Slling /* 177990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 178990b4856Slling */ 1799d82f4f6Slling static void 180990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 181990b4856Slling uint64_t intval, zprop_source_t src) 182990b4856Slling { 183990b4856Slling const char *propname = zpool_prop_to_name(prop); 184990b4856Slling nvlist_t *propval; 185990b4856Slling 1869d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1879d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 188990b4856Slling 1899d82f4f6Slling if (strval != NULL) 1909d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1919d82f4f6Slling else 1929d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 193990b4856Slling 1949d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 195990b4856Slling nvlist_free(propval); 196990b4856Slling } 197990b4856Slling 198990b4856Slling /* 199990b4856Slling * Get property values from the spa configuration. 200990b4856Slling */ 2019d82f4f6Slling static void 202990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 203990b4856Slling { 2044263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 205ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 2062e4c9986SGeorge Wilson uint64_t size, alloc, cap, version; 207990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 208c5904d13Seschrock spa_config_dirent_t *dp; 2092e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 210990b4856Slling 211e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 212e14bb325SJeff Bonwick 2134263d13fSGeorge Wilson if (rvd != NULL) { 214485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 215b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 216379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 217379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 218485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 219485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 220485bbbf5SGeorge Wilson size - alloc, src); 2214263d13fSGeorge Wilson 2222e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 2232e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src); 2242e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 2252e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src); 226f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 227f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 228379c004dSEric Schrock 229485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 230379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 231379c004dSEric Schrock 232b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 233b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 234b24ab676SJeff Bonwick 235379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2364263d13fSGeorge Wilson rvd->vdev_state, src); 237379c004dSEric Schrock 238379c004dSEric Schrock version = spa_version(spa); 239379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 240379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 241379c004dSEric Schrock else 242379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 243379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 244379c004dSEric Schrock } 245990b4856Slling 246ad135b5dSChristopher Siden if (pool != NULL) { 247ad135b5dSChristopher Siden /* 248ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 249ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 250ad135b5dSChristopher Siden */ 2517fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) { 252ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 253c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 254c1379625SJustin T. Gibbs src); 255ad135b5dSChristopher Siden } else { 256ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 257ad135b5dSChristopher Siden NULL, 0, src); 258ad135b5dSChristopher Siden } 2597fd05ac4SMatthew Ahrens 2607fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) { 2617fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 262c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 263c1379625SJustin T. Gibbs src); 2647fd05ac4SMatthew Ahrens } else { 2657fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 2667fd05ac4SMatthew Ahrens NULL, 0, src); 2677fd05ac4SMatthew Ahrens } 268ad135b5dSChristopher Siden } 269ad135b5dSChristopher Siden 2709d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 271990b4856Slling 2728704186eSDan McDonald if (spa->spa_comment != NULL) { 2738704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2748704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2758704186eSDan McDonald } 2768704186eSDan McDonald 2779d82f4f6Slling if (spa->spa_root != NULL) 2789d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2799d82f4f6Slling 0, ZPROP_SRC_LOCAL); 280990b4856Slling 281b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 282b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 283b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 284b5152584SMatthew Ahrens } else { 285b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 286b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 287b5152584SMatthew Ahrens } 288b5152584SMatthew Ahrens 289c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 290c5904d13Seschrock if (dp->scd_path == NULL) { 2919d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 292c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 293c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2949d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 295c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2962f8aaab3Seschrock } 2972f8aaab3Seschrock } 298990b4856Slling } 299990b4856Slling 300990b4856Slling /* 301990b4856Slling * Get zpool property values. 302990b4856Slling */ 303990b4856Slling int 304990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 305990b4856Slling { 306b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 307990b4856Slling zap_cursor_t zc; 308990b4856Slling zap_attribute_t za; 309990b4856Slling int err; 310990b4856Slling 3119d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 312990b4856Slling 313e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 314e14bb325SJeff Bonwick 315990b4856Slling /* 316990b4856Slling * Get properties from the spa config. 317990b4856Slling */ 3189d82f4f6Slling spa_prop_get_config(spa, nvp); 319990b4856Slling 320990b4856Slling /* If no pool property object, no more prop to get. */ 321afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 322990b4856Slling mutex_exit(&spa->spa_props_lock); 323990b4856Slling return (0); 324990b4856Slling } 325990b4856Slling 326990b4856Slling /* 327990b4856Slling * Get properties from the MOS pool property object. 328990b4856Slling */ 329990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 330990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 331990b4856Slling zap_cursor_advance(&zc)) { 332990b4856Slling uint64_t intval = 0; 333990b4856Slling char *strval = NULL; 334990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 335990b4856Slling zpool_prop_t prop; 336990b4856Slling 337990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 338990b4856Slling continue; 339990b4856Slling 340990b4856Slling switch (za.za_integer_length) { 341990b4856Slling case 8: 342990b4856Slling /* integer property */ 343990b4856Slling if (za.za_first_integer != 344990b4856Slling zpool_prop_default_numeric(prop)) 345990b4856Slling src = ZPROP_SRC_LOCAL; 346990b4856Slling 347990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 348990b4856Slling dsl_pool_t *dp; 349990b4856Slling dsl_dataset_t *ds = NULL; 350990b4856Slling 351990b4856Slling dp = spa_get_dsl(spa); 3523b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 353745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 354745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 3553b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 356990b4856Slling break; 357990b4856Slling } 358990b4856Slling 3599adfa60dSMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 360990b4856Slling KM_SLEEP); 361990b4856Slling dsl_dataset_name(ds, strval); 362745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 3633b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 364990b4856Slling } else { 365990b4856Slling strval = NULL; 366990b4856Slling intval = za.za_first_integer; 367990b4856Slling } 368990b4856Slling 3699d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 370990b4856Slling 371990b4856Slling if (strval != NULL) 3729adfa60dSMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 373990b4856Slling 374990b4856Slling break; 375990b4856Slling 376990b4856Slling case 1: 377990b4856Slling /* string property */ 378990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 379990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 380990b4856Slling za.za_name, 1, za.za_num_integers, strval); 381990b4856Slling if (err) { 382990b4856Slling kmem_free(strval, za.za_num_integers); 383990b4856Slling break; 384990b4856Slling } 3859d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 386990b4856Slling kmem_free(strval, za.za_num_integers); 387990b4856Slling break; 388990b4856Slling 389990b4856Slling default: 390990b4856Slling break; 391990b4856Slling } 392990b4856Slling } 393990b4856Slling zap_cursor_fini(&zc); 394990b4856Slling mutex_exit(&spa->spa_props_lock); 395990b4856Slling out: 396990b4856Slling if (err && err != ENOENT) { 397990b4856Slling nvlist_free(*nvp); 3989d82f4f6Slling *nvp = NULL; 399990b4856Slling return (err); 400990b4856Slling } 401990b4856Slling 402990b4856Slling return (0); 403990b4856Slling } 404990b4856Slling 405990b4856Slling /* 406990b4856Slling * Validate the given pool properties nvlist and modify the list 407990b4856Slling * for the property values to be set. 408990b4856Slling */ 409990b4856Slling static int 410990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 411990b4856Slling { 412990b4856Slling nvpair_t *elem; 413990b4856Slling int error = 0, reset_bootfs = 0; 414d5285caeSGeorge Wilson uint64_t objnum = 0; 415ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 416990b4856Slling 417990b4856Slling elem = NULL; 418990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 419990b4856Slling uint64_t intval; 420ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 421ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 422ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 423ad135b5dSChristopher Siden 424ad135b5dSChristopher Siden switch (prop) { 425ad135b5dSChristopher Siden case ZPROP_INVAL: 426ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 427be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 428ad135b5dSChristopher Siden break; 429ad135b5dSChristopher Siden } 430990b4856Slling 431ad135b5dSChristopher Siden /* 432ad135b5dSChristopher Siden * Sanitize the input. 433ad135b5dSChristopher Siden */ 434ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 435be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 436ad135b5dSChristopher Siden break; 437ad135b5dSChristopher Siden } 438990b4856Slling 439ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 440be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 441ad135b5dSChristopher Siden break; 442ad135b5dSChristopher Siden } 443ad135b5dSChristopher Siden 444ad135b5dSChristopher Siden if (intval != 0) { 445be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 446ad135b5dSChristopher Siden break; 447ad135b5dSChristopher Siden } 448ad135b5dSChristopher Siden 449ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 450ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 451be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 452ad135b5dSChristopher Siden break; 453ad135b5dSChristopher Siden } 454ad135b5dSChristopher Siden 455ad135b5dSChristopher Siden has_feature = B_TRUE; 456ad135b5dSChristopher Siden break; 457990b4856Slling 458990b4856Slling case ZPOOL_PROP_VERSION: 459990b4856Slling error = nvpair_value_uint64(elem, &intval); 460990b4856Slling if (!error && 461ad135b5dSChristopher Siden (intval < spa_version(spa) || 462ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 463ad135b5dSChristopher Siden has_feature)) 464be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 465990b4856Slling break; 466990b4856Slling 467990b4856Slling case ZPOOL_PROP_DELEGATION: 468990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 469d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 470573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 471990b4856Slling error = nvpair_value_uint64(elem, &intval); 472990b4856Slling if (!error && intval > 1) 473be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 474990b4856Slling break; 475990b4856Slling 476990b4856Slling case ZPOOL_PROP_BOOTFS: 47725f89ee2SJeff Bonwick /* 47825f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 47925f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 48025f89ee2SJeff Bonwick * the bootfs property cannot be set. 48125f89ee2SJeff Bonwick */ 482990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 483be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 484990b4856Slling break; 485990b4856Slling } 486990b4856Slling 487990b4856Slling /* 48815e6edf1Sgw * Make sure the vdev config is bootable 489990b4856Slling */ 49015e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 491be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 492990b4856Slling break; 493990b4856Slling } 494990b4856Slling 495990b4856Slling reset_bootfs = 1; 496990b4856Slling 497990b4856Slling error = nvpair_value_string(elem, &strval); 498990b4856Slling 499990b4856Slling if (!error) { 500ad135b5dSChristopher Siden objset_t *os; 501b5152584SMatthew Ahrens uint64_t propval; 50215e6edf1Sgw 503990b4856Slling if (strval == NULL || strval[0] == '\0') { 504990b4856Slling objnum = zpool_prop_default_numeric( 505990b4856Slling ZPOOL_PROP_BOOTFS); 506990b4856Slling break; 507990b4856Slling } 508990b4856Slling 509503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 510990b4856Slling break; 51115e6edf1Sgw 512b5152584SMatthew Ahrens /* 513b5152584SMatthew Ahrens * Must be ZPL, and its property settings 514b5152584SMatthew Ahrens * must be supported by GRUB (compression 515b5152584SMatthew Ahrens * is not gzip, and large blocks are not used). 516b5152584SMatthew Ahrens */ 517503ad85cSMatthew Ahrens 518503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 519be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5203b2aab18SMatthew Ahrens } else if ((error = 5213b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 52215e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 523b5152584SMatthew Ahrens &propval)) == 0 && 524b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) { 525b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP); 52615e6edf1Sgw } else { 52715e6edf1Sgw objnum = dmu_objset_id(os); 52815e6edf1Sgw } 529503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 530990b4856Slling } 531990b4856Slling break; 532e14bb325SJeff Bonwick 5330a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5340a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5350a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5360a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 537be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5380a4e9518Sgw 5390a4e9518Sgw /* 5400a4e9518Sgw * This is a special case which only occurs when 5410a4e9518Sgw * the pool has completely failed. This allows 5420a4e9518Sgw * the user to change the in-core failmode property 5430a4e9518Sgw * without syncing it out to disk (I/Os might 5440a4e9518Sgw * currently be blocked). We do this by returning 5450a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5460a4e9518Sgw * into thinking we encountered a property validation 5470a4e9518Sgw * error. 5480a4e9518Sgw */ 549e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5500a4e9518Sgw spa->spa_failmode = intval; 551be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 5520a4e9518Sgw } 5530a4e9518Sgw break; 5542f8aaab3Seschrock 5552f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5562f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5572f8aaab3Seschrock break; 5582f8aaab3Seschrock 5592f8aaab3Seschrock if (strval[0] == '\0') 5602f8aaab3Seschrock break; 5612f8aaab3Seschrock 5622f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5632f8aaab3Seschrock break; 5642f8aaab3Seschrock 5652f8aaab3Seschrock if (strval[0] != '/') { 566be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5672f8aaab3Seschrock break; 5682f8aaab3Seschrock } 5692f8aaab3Seschrock 5702f8aaab3Seschrock slash = strrchr(strval, '/'); 5712f8aaab3Seschrock ASSERT(slash != NULL); 5722f8aaab3Seschrock 5732f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5742f8aaab3Seschrock strcmp(slash, "/..") == 0) 575be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5762f8aaab3Seschrock break; 577b24ab676SJeff Bonwick 5788704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5798704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5808704186eSDan McDonald break; 5818704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5828704186eSDan McDonald /* 5838704186eSDan McDonald * The kernel doesn't have an easy isprint() 5848704186eSDan McDonald * check. For this kernel check, we merely 5858704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5868704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5878704186eSDan McDonald */ 5888704186eSDan McDonald if (*check >= 0x7f) { 589be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 5908704186eSDan McDonald break; 5918704186eSDan McDonald } 5928704186eSDan McDonald } 5938704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5948704186eSDan McDonald error = E2BIG; 5958704186eSDan McDonald break; 5968704186eSDan McDonald 597b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 598b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 599be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 600b24ab676SJeff Bonwick else 601b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 602b24ab676SJeff Bonwick if (error == 0 && 603b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 604be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 605b24ab676SJeff Bonwick break; 606990b4856Slling } 607990b4856Slling 608990b4856Slling if (error) 609990b4856Slling break; 610990b4856Slling } 611990b4856Slling 612990b4856Slling if (!error && reset_bootfs) { 613990b4856Slling error = nvlist_remove(props, 614990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 615990b4856Slling 616990b4856Slling if (!error) { 617990b4856Slling error = nvlist_add_uint64(props, 618990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 619990b4856Slling } 620990b4856Slling } 621990b4856Slling 622990b4856Slling return (error); 623990b4856Slling } 624990b4856Slling 625379c004dSEric Schrock void 626379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 627379c004dSEric Schrock { 628379c004dSEric Schrock char *cachefile; 629379c004dSEric Schrock spa_config_dirent_t *dp; 630379c004dSEric Schrock 631379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 632379c004dSEric Schrock &cachefile) != 0) 633379c004dSEric Schrock return; 634379c004dSEric Schrock 635379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 636379c004dSEric Schrock KM_SLEEP); 637379c004dSEric Schrock 638379c004dSEric Schrock if (cachefile[0] == '\0') 639379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 640379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 641379c004dSEric Schrock dp->scd_path = NULL; 642379c004dSEric Schrock else 643379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 644379c004dSEric Schrock 645379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 646379c004dSEric Schrock if (need_sync) 647379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 648379c004dSEric Schrock } 649379c004dSEric Schrock 650990b4856Slling int 651990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 652990b4856Slling { 653990b4856Slling int error; 654ad135b5dSChristopher Siden nvpair_t *elem = NULL; 655379c004dSEric Schrock boolean_t need_sync = B_FALSE; 656990b4856Slling 657990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 658990b4856Slling return (error); 659990b4856Slling 660379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 661ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 662379c004dSEric Schrock 663f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 664f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 665f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 666379c004dSEric Schrock continue; 667379c004dSEric Schrock 668ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 669ad135b5dSChristopher Siden uint64_t ver; 670ad135b5dSChristopher Siden 671ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 672ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 673ad135b5dSChristopher Siden } else { 674ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 675ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 676ad135b5dSChristopher Siden need_sync = B_TRUE; 677ad135b5dSChristopher Siden } 678ad135b5dSChristopher Siden 679ad135b5dSChristopher Siden /* Save time if the version is already set. */ 680ad135b5dSChristopher Siden if (ver == spa_version(spa)) 681ad135b5dSChristopher Siden continue; 682ad135b5dSChristopher Siden 683ad135b5dSChristopher Siden /* 684ad135b5dSChristopher Siden * In addition to the pool directory object, we might 685ad135b5dSChristopher Siden * create the pool properties object, the features for 686ad135b5dSChristopher Siden * read object, the features for write object, or the 687ad135b5dSChristopher Siden * feature descriptions object. 688ad135b5dSChristopher Siden */ 6893b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 6907d46dc6cSMatthew Ahrens spa_sync_version, &ver, 6917d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED); 692ad135b5dSChristopher Siden if (error) 693ad135b5dSChristopher Siden return (error); 694ad135b5dSChristopher Siden continue; 695ad135b5dSChristopher Siden } 696ad135b5dSChristopher Siden 697379c004dSEric Schrock need_sync = B_TRUE; 698379c004dSEric Schrock break; 699379c004dSEric Schrock } 700379c004dSEric Schrock 701ad135b5dSChristopher Siden if (need_sync) { 7023b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 7037d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 704ad135b5dSChristopher Siden } 705ad135b5dSChristopher Siden 706ad135b5dSChristopher Siden return (0); 707990b4856Slling } 708990b4856Slling 709990b4856Slling /* 710990b4856Slling * If the bootfs property value is dsobj, clear it. 711990b4856Slling */ 712990b4856Slling void 713990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 714990b4856Slling { 715990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 716990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 717990b4856Slling spa->spa_pool_props_object, 718990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 719990b4856Slling spa->spa_bootfs = 0; 720990b4856Slling } 721990b4856Slling } 722990b4856Slling 723dfbb9432SGeorge Wilson /*ARGSUSED*/ 724dfbb9432SGeorge Wilson static int 7253b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 726dfbb9432SGeorge Wilson { 7273b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7283b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 729dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 730dfbb9432SGeorge Wilson uint64_t vdev_state; 731dfbb9432SGeorge Wilson 732dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 733dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 734dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 735dfbb9432SGeorge Wilson 736dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 737be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 738dfbb9432SGeorge Wilson 739dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 740dfbb9432SGeorge Wilson 741dfbb9432SGeorge Wilson return (0); 742dfbb9432SGeorge Wilson } 743dfbb9432SGeorge Wilson 744dfbb9432SGeorge Wilson static void 7453b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 746dfbb9432SGeorge Wilson { 7473b2aab18SMatthew Ahrens uint64_t *newguid = arg; 7483b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 749dfbb9432SGeorge Wilson uint64_t oldguid; 750dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 751dfbb9432SGeorge Wilson 752dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 753dfbb9432SGeorge Wilson 754dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 755dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 756dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 757dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 758dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 759dfbb9432SGeorge Wilson 76020128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 761dfbb9432SGeorge Wilson oldguid, *newguid); 762dfbb9432SGeorge Wilson } 763dfbb9432SGeorge Wilson 764e9103aaeSGarrett D'Amore /* 765e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 766e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 767e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 768e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 769e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 770e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 771e9103aaeSGarrett D'Amore * sysevent to update any watchers. 772e9103aaeSGarrett D'Amore */ 773e9103aaeSGarrett D'Amore int 774e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 775e9103aaeSGarrett D'Amore { 776dfbb9432SGeorge Wilson int error; 777dfbb9432SGeorge Wilson uint64_t guid; 778e9103aaeSGarrett D'Amore 7792c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock); 780dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 781dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 782e9103aaeSGarrett D'Amore 7833b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 7847d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 785e9103aaeSGarrett D'Amore 786dfbb9432SGeorge Wilson if (error == 0) { 787dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 788*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 789dfbb9432SGeorge Wilson } 790e9103aaeSGarrett D'Amore 791dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 7922c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock); 793e9103aaeSGarrett D'Amore 794dfbb9432SGeorge Wilson return (error); 795e9103aaeSGarrett D'Amore } 796e9103aaeSGarrett D'Amore 797fa9e4066Sahrens /* 798fa9e4066Sahrens * ========================================================================== 799fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 800fa9e4066Sahrens * ========================================================================== 801fa9e4066Sahrens */ 802fa9e4066Sahrens 803ea8dc4b6Seschrock static int 804ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 805ea8dc4b6Seschrock { 806ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 807ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 808ea8dc4b6Seschrock int ret; 809ea8dc4b6Seschrock 810ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 8117802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 812ea8dc4b6Seschrock 813ea8dc4b6Seschrock if (ret < 0) 814ea8dc4b6Seschrock return (-1); 815ea8dc4b6Seschrock else if (ret > 0) 816ea8dc4b6Seschrock return (1); 817ea8dc4b6Seschrock else 818ea8dc4b6Seschrock return (0); 819ea8dc4b6Seschrock } 820ea8dc4b6Seschrock 821ea8dc4b6Seschrock /* 822ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 823ea8dc4b6Seschrock * re-initializes them in the process. 824ea8dc4b6Seschrock */ 825ea8dc4b6Seschrock void 826ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 827ea8dc4b6Seschrock { 828ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 829ea8dc4b6Seschrock 830ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 831ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 832ea8dc4b6Seschrock 833ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 834ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 835ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 836ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 837ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 838ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 839ea8dc4b6Seschrock } 840ea8dc4b6Seschrock 841ec94d322SAdam Leventhal static void 842ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 843fa9e4066Sahrens { 844ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 845ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 846ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 847ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 848ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 849ec94d322SAdam Leventhal char name[32]; 8505aeb9474SGarrett D'Amore uint_t flags = 0; 85135a5a358SJonathan Adams boolean_t batch = B_FALSE; 852fa9e4066Sahrens 853ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 854ec94d322SAdam Leventhal tqs->stqs_count = 0; 855ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 856ec94d322SAdam Leventhal return; 857ec94d322SAdam Leventhal } 858fa9e4066Sahrens 859ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 860fa9e4066Sahrens 861ec94d322SAdam Leventhal tqs->stqs_count = count; 862ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 86335a5a358SJonathan Adams 86469962b56SMatthew Ahrens switch (mode) { 86569962b56SMatthew Ahrens case ZTI_MODE_FIXED: 86669962b56SMatthew Ahrens ASSERT3U(value, >=, 1); 86769962b56SMatthew Ahrens value = MAX(value, 1); 86869962b56SMatthew Ahrens break; 869ec94d322SAdam Leventhal 87069962b56SMatthew Ahrens case ZTI_MODE_BATCH: 87169962b56SMatthew Ahrens batch = B_TRUE; 87269962b56SMatthew Ahrens flags |= TASKQ_THREADS_CPU_PCT; 87369962b56SMatthew Ahrens value = zio_taskq_batch_pct; 87469962b56SMatthew Ahrens break; 875ec94d322SAdam Leventhal 87669962b56SMatthew Ahrens default: 87769962b56SMatthew Ahrens panic("unrecognized mode for %s_%s taskq (%u:%u) in " 87869962b56SMatthew Ahrens "spa_activate()", 87969962b56SMatthew Ahrens zio_type_name[t], zio_taskq_types[q], mode, value); 88069962b56SMatthew Ahrens break; 88169962b56SMatthew Ahrens } 882ec94d322SAdam Leventhal 88369962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) { 88469962b56SMatthew Ahrens taskq_t *tq; 885ec94d322SAdam Leventhal 886ec94d322SAdam Leventhal if (count > 1) { 887ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 888ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 889ec94d322SAdam Leventhal } else { 890ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 891ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 892ec94d322SAdam Leventhal } 893ec94d322SAdam Leventhal 894ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 895ec94d322SAdam Leventhal if (batch) 896ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 897ec94d322SAdam Leventhal 898ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 899ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 900ec94d322SAdam Leventhal } else { 90169962b56SMatthew Ahrens pri_t pri = maxclsyspri; 90269962b56SMatthew Ahrens /* 90369962b56SMatthew Ahrens * The write issue taskq can be extremely CPU 90469962b56SMatthew Ahrens * intensive. Run it at slightly lower priority 90569962b56SMatthew Ahrens * than the other taskqs. 90669962b56SMatthew Ahrens */ 90769962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 90869962b56SMatthew Ahrens pri--; 90969962b56SMatthew Ahrens 91069962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50, 911ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 912ec94d322SAdam Leventhal } 913ec94d322SAdam Leventhal 914ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 915ec94d322SAdam Leventhal } 916ec94d322SAdam Leventhal } 917ec94d322SAdam Leventhal 918ec94d322SAdam Leventhal static void 919ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 920ec94d322SAdam Leventhal { 921ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 922ec94d322SAdam Leventhal 923ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 924ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 925ec94d322SAdam Leventhal return; 926ec94d322SAdam Leventhal } 927ec94d322SAdam Leventhal 928ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 929ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 930ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 93135a5a358SJonathan Adams } 93235a5a358SJonathan Adams 933ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 934ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 935ec94d322SAdam Leventhal } 93635a5a358SJonathan Adams 937ec94d322SAdam Leventhal /* 938ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 939ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 940ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 941ec94d322SAdam Leventhal * the low bits of gethrtime(). 942ec94d322SAdam Leventhal */ 943ec94d322SAdam Leventhal void 944ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 945ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 946ec94d322SAdam Leventhal { 947ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 948ec94d322SAdam Leventhal taskq_t *tq; 949ec94d322SAdam Leventhal 950ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 951ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 952ec94d322SAdam Leventhal 953ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 954ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 955ec94d322SAdam Leventhal } else { 956ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 95735a5a358SJonathan Adams } 958ec94d322SAdam Leventhal 959ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 96035a5a358SJonathan Adams } 96135a5a358SJonathan Adams 96235a5a358SJonathan Adams static void 96335a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 96435a5a358SJonathan Adams { 965e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 966e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 967ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 96835a5a358SJonathan Adams } 96935a5a358SJonathan Adams } 97035a5a358SJonathan Adams } 97135a5a358SJonathan Adams 97235a5a358SJonathan Adams #ifdef _KERNEL 97335a5a358SJonathan Adams static void 97435a5a358SJonathan Adams spa_thread(void *arg) 97535a5a358SJonathan Adams { 97635a5a358SJonathan Adams callb_cpr_t cprinfo; 9772e0c549eSJonathan Adams 97835a5a358SJonathan Adams spa_t *spa = arg; 97935a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9802e0c549eSJonathan Adams 98135a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 98235a5a358SJonathan Adams spa->spa_name); 9832e0c549eSJonathan Adams 98435a5a358SJonathan Adams ASSERT(curproc != &p0); 98535a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 98635a5a358SJonathan Adams "zpool-%s", spa->spa_name); 98735a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9882e0c549eSJonathan Adams 98935a5a358SJonathan Adams /* bind this thread to the requested psrset */ 99035a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 99135a5a358SJonathan Adams pool_lock(); 99235a5a358SJonathan Adams mutex_enter(&cpu_lock); 99335a5a358SJonathan Adams mutex_enter(&pidlock); 99435a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 99580eb36f2SGeorge Wilson 99635a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 99735a5a358SJonathan Adams 0, NULL, NULL) == 0) { 99835a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 99935a5a358SJonathan Adams } else { 100035a5a358SJonathan Adams cmn_err(CE_WARN, 100135a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 100235a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 100335a5a358SJonathan Adams } 100435a5a358SJonathan Adams 100535a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 100635a5a358SJonathan Adams mutex_exit(&pidlock); 100735a5a358SJonathan Adams mutex_exit(&cpu_lock); 100835a5a358SJonathan Adams pool_unlock(); 100935a5a358SJonathan Adams } 101035a5a358SJonathan Adams 101135a5a358SJonathan Adams if (zio_taskq_sysdc) { 101235a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 101335a5a358SJonathan Adams } 101435a5a358SJonathan Adams 101535a5a358SJonathan Adams spa->spa_proc = curproc; 101635a5a358SJonathan Adams spa->spa_did = curthread->t_did; 101735a5a358SJonathan Adams 101835a5a358SJonathan Adams spa_create_zio_taskqs(spa); 101935a5a358SJonathan Adams 102035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 102135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 102235a5a358SJonathan Adams 102335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 102435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 102535a5a358SJonathan Adams 102635a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 102735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 102835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 102935a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 103035a5a358SJonathan Adams 103135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 103235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 103335a5a358SJonathan Adams spa->spa_proc = &p0; 103435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 103535a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 103635a5a358SJonathan Adams 103735a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 103835a5a358SJonathan Adams lwp_exit(); 103935a5a358SJonathan Adams } 104035a5a358SJonathan Adams #endif 104135a5a358SJonathan Adams 104235a5a358SJonathan Adams /* 104335a5a358SJonathan Adams * Activate an uninitialized pool. 104435a5a358SJonathan Adams */ 104535a5a358SJonathan Adams static void 104635a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 104735a5a358SJonathan Adams { 104835a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 104935a5a358SJonathan Adams 105035a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 105135a5a358SJonathan Adams spa->spa_mode = mode; 105235a5a358SJonathan Adams 105335a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 105435a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 105535a5a358SJonathan Adams 105635a5a358SJonathan Adams /* Try to create a covering process */ 105735a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 105835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 105935a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 106035a5a358SJonathan Adams spa->spa_did = 0; 106135a5a358SJonathan Adams 106235a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 106335a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 106435a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 106535a5a358SJonathan Adams NULL, 0) == 0) { 106635a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 106735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 106835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 106935a5a358SJonathan Adams &spa->spa_proc_lock); 10702e0c549eSJonathan Adams } 107135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 107235a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 107335a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 107435a5a358SJonathan Adams } else { 107535a5a358SJonathan Adams #ifdef _KERNEL 107635a5a358SJonathan Adams cmn_err(CE_WARN, 107735a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 107835a5a358SJonathan Adams spa->spa_name); 107935a5a358SJonathan Adams #endif 1080e14bb325SJeff Bonwick } 1081fa9e4066Sahrens } 108235a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 108335a5a358SJonathan Adams 108435a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 108535a5a358SJonathan Adams if (spa->spa_proc == &p0) { 108635a5a358SJonathan Adams spa_create_zio_taskqs(spa); 108735a5a358SJonathan Adams } 1088fa9e4066Sahrens 1089e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1090e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1091bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1092bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node)); 1093e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1094e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1095fa9e4066Sahrens 1096b7b2590dSMatthew Ahrens txg_list_create(&spa->spa_vdev_txg_list, spa, 1097fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1098ea8dc4b6Seschrock 1099ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1100ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1101ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1102ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1103ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1104ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1105fa9e4066Sahrens } 1106fa9e4066Sahrens 1107fa9e4066Sahrens /* 1108fa9e4066Sahrens * Opposite of spa_activate(). 1109fa9e4066Sahrens */ 1110fa9e4066Sahrens static void 1111fa9e4066Sahrens spa_deactivate(spa_t *spa) 1112fa9e4066Sahrens { 1113fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1114fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1115fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 111625f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1117fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1118fa9e4066Sahrens 1119bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 1120bc9014e6SJustin Gibbs 1121fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1122fa9e4066Sahrens 1123e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1124bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list); 1125e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1126fa9e4066Sahrens 1127e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1128e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1129ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1130e14bb325SJeff Bonwick } 1131fa9e4066Sahrens } 1132fa9e4066Sahrens 1133fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1134fa9e4066Sahrens spa->spa_normal_class = NULL; 1135fa9e4066Sahrens 11368654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 11378654d025Sperrin spa->spa_log_class = NULL; 11388654d025Sperrin 1139ea8dc4b6Seschrock /* 1140ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1141ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1142ea8dc4b6Seschrock */ 1143ea8dc4b6Seschrock spa_errlog_drain(spa); 1144ea8dc4b6Seschrock 1145ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1146ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1147ea8dc4b6Seschrock 1148fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 114935a5a358SJonathan Adams 115035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 115135a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 115235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 115335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 115435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 115535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 115635a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 115735a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 115835a5a358SJonathan Adams } 115935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 116035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 116135a5a358SJonathan Adams } 116235a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 116335a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 116435a5a358SJonathan Adams 116535a5a358SJonathan Adams /* 116635a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 116735a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 116835a5a358SJonathan Adams * it. 116935a5a358SJonathan Adams */ 117035a5a358SJonathan Adams if (spa->spa_did != 0) { 117135a5a358SJonathan Adams thread_join(spa->spa_did); 117235a5a358SJonathan Adams spa->spa_did = 0; 117335a5a358SJonathan Adams } 1174fa9e4066Sahrens } 1175fa9e4066Sahrens 1176fa9e4066Sahrens /* 1177fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1178fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1179fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1180fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1181fa9e4066Sahrens */ 118299653d4eSeschrock static int 118399653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 118499653d4eSeschrock uint_t id, int atype) 1185fa9e4066Sahrens { 1186fa9e4066Sahrens nvlist_t **child; 1187573ca77eSGeorge Wilson uint_t children; 118899653d4eSeschrock int error; 1189fa9e4066Sahrens 119099653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 119199653d4eSeschrock return (error); 1192fa9e4066Sahrens 119399653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 119499653d4eSeschrock return (0); 1195fa9e4066Sahrens 1196e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1197e14bb325SJeff Bonwick &child, &children); 1198e14bb325SJeff Bonwick 1199e14bb325SJeff Bonwick if (error == ENOENT) 1200e14bb325SJeff Bonwick return (0); 1201e14bb325SJeff Bonwick 1202e14bb325SJeff Bonwick if (error) { 120399653d4eSeschrock vdev_free(*vdp); 120499653d4eSeschrock *vdp = NULL; 1205be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1206fa9e4066Sahrens } 1207fa9e4066Sahrens 1208573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 120999653d4eSeschrock vdev_t *vd; 121099653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 121199653d4eSeschrock atype)) != 0) { 121299653d4eSeschrock vdev_free(*vdp); 121399653d4eSeschrock *vdp = NULL; 121499653d4eSeschrock return (error); 1215fa9e4066Sahrens } 1216fa9e4066Sahrens } 1217fa9e4066Sahrens 121899653d4eSeschrock ASSERT(*vdp != NULL); 121999653d4eSeschrock 122099653d4eSeschrock return (0); 1221fa9e4066Sahrens } 1222fa9e4066Sahrens 1223fa9e4066Sahrens /* 1224fa9e4066Sahrens * Opposite of spa_load(). 1225fa9e4066Sahrens */ 1226fa9e4066Sahrens static void 1227fa9e4066Sahrens spa_unload(spa_t *spa) 1228fa9e4066Sahrens { 122999653d4eSeschrock int i; 123099653d4eSeschrock 1231e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1232e14bb325SJeff Bonwick 1233ea8dc4b6Seschrock /* 1234ea8dc4b6Seschrock * Stop async tasks. 1235ea8dc4b6Seschrock */ 1236ea8dc4b6Seschrock spa_async_suspend(spa); 1237ea8dc4b6Seschrock 1238fa9e4066Sahrens /* 1239fa9e4066Sahrens * Stop syncing. 1240fa9e4066Sahrens */ 1241fa9e4066Sahrens if (spa->spa_sync_on) { 1242fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1243fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1244fa9e4066Sahrens } 1245fa9e4066Sahrens 12468363e80aSGeorge Wilson /* 12478363e80aSGeorge Wilson * Even though vdev_free() also calls vdev_metaslab_fini, we need 12488363e80aSGeorge Wilson * to call it earlier, before we wait for async i/o to complete. 12498363e80aSGeorge Wilson * This ensures that there is no async metaslab prefetching, by 12508363e80aSGeorge Wilson * calling taskq_wait(mg_taskq). 12518363e80aSGeorge Wilson */ 12528363e80aSGeorge Wilson if (spa->spa_root_vdev != NULL) { 12538363e80aSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12548363e80aSGeorge Wilson for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) 12558363e80aSGeorge Wilson vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); 12568363e80aSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 12578363e80aSGeorge Wilson } 12588363e80aSGeorge Wilson 1259fa9e4066Sahrens /* 1260e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1261fa9e4066Sahrens */ 126254d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 12636f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) 12646f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]); 12656f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 126654d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 126754d692b7SGeorge Wilson } 1268fa9e4066Sahrens 1269cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1270cde58dbcSMatthew Ahrens 12710713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12720713e232SGeorge Wilson 12730713e232SGeorge Wilson /* 12740713e232SGeorge Wilson * Close all vdevs. 12750713e232SGeorge Wilson */ 12760713e232SGeorge Wilson if (spa->spa_root_vdev) 12770713e232SGeorge Wilson vdev_free(spa->spa_root_vdev); 12780713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL); 12790713e232SGeorge Wilson 1280fa9e4066Sahrens /* 1281fa9e4066Sahrens * Close the dsl pool. 1282fa9e4066Sahrens */ 1283fa9e4066Sahrens if (spa->spa_dsl_pool) { 1284fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1285fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1286afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1287fa9e4066Sahrens } 1288fa9e4066Sahrens 1289b24ab676SJeff Bonwick ddt_unload(spa); 1290b24ab676SJeff Bonwick 12918ad4d6ddSJeff Bonwick /* 12928ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12938ad4d6ddSJeff Bonwick */ 12948ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12958ad4d6ddSJeff Bonwick 1296fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1297fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1298fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1299fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1300fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1301fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 130299653d4eSeschrock } 1303fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1304fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1305fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1306fa94a07fSbrendan } 13072ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1308fa94a07fSbrendan 1309cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1310cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1311fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1312cd0837ccSGeorge Wilson } 1313fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1314fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1315fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1316fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1317fa94a07fSbrendan } 1318fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1319fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1320fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 132199653d4eSeschrock } 13222ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 132399653d4eSeschrock 1324ea8dc4b6Seschrock spa->spa_async_suspended = 0; 13258ad4d6ddSJeff Bonwick 13268704186eSDan McDonald if (spa->spa_comment != NULL) { 13278704186eSDan McDonald spa_strfree(spa->spa_comment); 13288704186eSDan McDonald spa->spa_comment = NULL; 13298704186eSDan McDonald } 13308704186eSDan McDonald 13318ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1332fa9e4066Sahrens } 1333fa9e4066Sahrens 133499653d4eSeschrock /* 133599653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 133699653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1337fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1338fa94a07fSbrendan * then re-generate a more complete list including status information. 133999653d4eSeschrock */ 134099653d4eSeschrock static void 134199653d4eSeschrock spa_load_spares(spa_t *spa) 134299653d4eSeschrock { 134399653d4eSeschrock nvlist_t **spares; 134499653d4eSeschrock uint_t nspares; 134599653d4eSeschrock int i; 134639c23413Seschrock vdev_t *vd, *tvd; 134799653d4eSeschrock 1348e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1349e14bb325SJeff Bonwick 135099653d4eSeschrock /* 135199653d4eSeschrock * First, close and free any existing spare vdevs. 135299653d4eSeschrock */ 1353fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1354fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 135539c23413Seschrock 135639c23413Seschrock /* Undo the call to spa_activate() below */ 1357c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1358c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 135939c23413Seschrock spa_spare_remove(tvd); 136039c23413Seschrock vdev_close(vd); 136139c23413Seschrock vdev_free(vd); 136299653d4eSeschrock } 136339c23413Seschrock 1364fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1365fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1366fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 136799653d4eSeschrock 1368fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 136999653d4eSeschrock nspares = 0; 137099653d4eSeschrock else 1371fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 137299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 137399653d4eSeschrock 1374fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1375fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 137699653d4eSeschrock 137799653d4eSeschrock if (nspares == 0) 137899653d4eSeschrock return; 137999653d4eSeschrock 138099653d4eSeschrock /* 138199653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 138239c23413Seschrock * process. For each spare, there is potentially two different vdev_t 138339c23413Seschrock * structures associated with it: one in the list of spares (used only 138439c23413Seschrock * for basic validation purposes) and one in the active vdev 138539c23413Seschrock * configuration (if it's spared in). During this phase we open and 138639c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 138739c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 138899653d4eSeschrock */ 1389fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1390fa94a07fSbrendan KM_SLEEP); 1391fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 139299653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 139399653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 139499653d4eSeschrock ASSERT(vd != NULL); 139599653d4eSeschrock 1396fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 139799653d4eSeschrock 1398c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1399c5904d13Seschrock B_FALSE)) != NULL) { 140039c23413Seschrock if (!tvd->vdev_isspare) 140139c23413Seschrock spa_spare_add(tvd); 140239c23413Seschrock 140339c23413Seschrock /* 140439c23413Seschrock * We only mark the spare active if we were successfully 140539c23413Seschrock * able to load the vdev. Otherwise, importing a pool 140639c23413Seschrock * with a bad active spare would result in strange 140739c23413Seschrock * behavior, because multiple pool would think the spare 140839c23413Seschrock * is actively in use. 140939c23413Seschrock * 141039c23413Seschrock * There is a vulnerability here to an equally bizarre 141139c23413Seschrock * circumstance, where a dead active spare is later 141239c23413Seschrock * brought back to life (onlined or otherwise). Given 141339c23413Seschrock * the rarity of this scenario, and the extra complexity 141439c23413Seschrock * it adds, we ignore the possibility. 141539c23413Seschrock */ 141639c23413Seschrock if (!vdev_is_dead(tvd)) 141739c23413Seschrock spa_spare_activate(tvd); 141839c23413Seschrock } 141939c23413Seschrock 1420e14bb325SJeff Bonwick vd->vdev_top = vd; 14216809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1422e14bb325SJeff Bonwick 142399653d4eSeschrock if (vdev_open(vd) != 0) 142499653d4eSeschrock continue; 142599653d4eSeschrock 1426fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1427fa94a07fSbrendan spa_spare_add(vd); 142899653d4eSeschrock } 142999653d4eSeschrock 143099653d4eSeschrock /* 143199653d4eSeschrock * Recompute the stashed list of spares, with status information 143299653d4eSeschrock * this time. 143399653d4eSeschrock */ 1434fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 143599653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 143699653d4eSeschrock 1437fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1438fa94a07fSbrendan KM_SLEEP); 1439fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1440fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 14413f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1442fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1443fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1444fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 144599653d4eSeschrock nvlist_free(spares[i]); 1446fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1447fa94a07fSbrendan } 1448fa94a07fSbrendan 1449fa94a07fSbrendan /* 1450fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1451fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1452fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1453fa94a07fSbrendan * then re-generate a more complete list including status information. 1454fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1455fa94a07fSbrendan * not re-opened. 1456fa94a07fSbrendan */ 1457fa94a07fSbrendan static void 1458fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1459fa94a07fSbrendan { 1460fa94a07fSbrendan nvlist_t **l2cache; 1461fa94a07fSbrendan uint_t nl2cache; 1462fa94a07fSbrendan int i, j, oldnvdevs; 1463573ca77eSGeorge Wilson uint64_t guid; 1464fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1465fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1466fa94a07fSbrendan 1467e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1468e14bb325SJeff Bonwick 1469fa94a07fSbrendan if (sav->sav_config != NULL) { 1470fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1471fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1472fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1473fa94a07fSbrendan } else { 1474fa94a07fSbrendan nl2cache = 0; 1475d5285caeSGeorge Wilson newvdevs = NULL; 1476fa94a07fSbrendan } 1477fa94a07fSbrendan 1478fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1479fa94a07fSbrendan oldnvdevs = sav->sav_count; 1480fa94a07fSbrendan sav->sav_vdevs = NULL; 1481fa94a07fSbrendan sav->sav_count = 0; 1482fa94a07fSbrendan 1483fa94a07fSbrendan /* 1484fa94a07fSbrendan * Process new nvlist of vdevs. 1485fa94a07fSbrendan */ 1486fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1487fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1488fa94a07fSbrendan &guid) == 0); 1489fa94a07fSbrendan 1490fa94a07fSbrendan newvdevs[i] = NULL; 1491fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1492fa94a07fSbrendan vd = oldvdevs[j]; 1493fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1494fa94a07fSbrendan /* 1495fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1496fa94a07fSbrendan */ 1497fa94a07fSbrendan newvdevs[i] = vd; 1498fa94a07fSbrendan oldvdevs[j] = NULL; 1499fa94a07fSbrendan break; 1500fa94a07fSbrendan } 1501fa94a07fSbrendan } 1502fa94a07fSbrendan 1503fa94a07fSbrendan if (newvdevs[i] == NULL) { 1504fa94a07fSbrendan /* 1505fa94a07fSbrendan * Create new vdev 1506fa94a07fSbrendan */ 1507fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1508fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1509fa94a07fSbrendan ASSERT(vd != NULL); 1510fa94a07fSbrendan newvdevs[i] = vd; 1511fa94a07fSbrendan 1512fa94a07fSbrendan /* 1513fa94a07fSbrendan * Commit this vdev as an l2cache device, 1514fa94a07fSbrendan * even if it fails to open. 1515fa94a07fSbrendan */ 1516fa94a07fSbrendan spa_l2cache_add(vd); 1517fa94a07fSbrendan 1518c5904d13Seschrock vd->vdev_top = vd; 1519c5904d13Seschrock vd->vdev_aux = sav; 1520c5904d13Seschrock 1521c5904d13Seschrock spa_l2cache_activate(vd); 1522c5904d13Seschrock 1523fa94a07fSbrendan if (vdev_open(vd) != 0) 1524fa94a07fSbrendan continue; 1525fa94a07fSbrendan 1526fa94a07fSbrendan (void) vdev_validate_aux(vd); 1527fa94a07fSbrendan 1528573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1529573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1530fa94a07fSbrendan } 1531fa94a07fSbrendan } 1532fa94a07fSbrendan 1533fa94a07fSbrendan /* 1534fa94a07fSbrendan * Purge vdevs that were dropped 1535fa94a07fSbrendan */ 1536fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1537fa94a07fSbrendan uint64_t pool; 1538fa94a07fSbrendan 1539fa94a07fSbrendan vd = oldvdevs[i]; 1540fa94a07fSbrendan if (vd != NULL) { 1541cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1542cd0837ccSGeorge Wilson 15438ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 15448ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1545fa94a07fSbrendan l2arc_remove_vdev(vd); 1546cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1547cd0837ccSGeorge Wilson vdev_free(vd); 1548fa94a07fSbrendan } 1549fa94a07fSbrendan } 1550fa94a07fSbrendan 1551fa94a07fSbrendan if (oldvdevs) 1552fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1553fa94a07fSbrendan 1554fa94a07fSbrendan if (sav->sav_config == NULL) 1555fa94a07fSbrendan goto out; 1556fa94a07fSbrendan 1557fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1558fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1559fa94a07fSbrendan 1560fa94a07fSbrendan /* 1561fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1562fa94a07fSbrendan * information this time. 1563fa94a07fSbrendan */ 1564fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1565fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1566fa94a07fSbrendan 1567fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1568fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1569fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15703f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1571fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1572fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1573fa94a07fSbrendan out: 1574fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1575fa94a07fSbrendan nvlist_free(l2cache[i]); 1576fa94a07fSbrendan if (sav->sav_count) 1577fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 157899653d4eSeschrock } 157999653d4eSeschrock 158099653d4eSeschrock static int 158199653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 158299653d4eSeschrock { 158399653d4eSeschrock dmu_buf_t *db; 158499653d4eSeschrock char *packed = NULL; 158599653d4eSeschrock size_t nvsize = 0; 158699653d4eSeschrock int error; 158799653d4eSeschrock *value = NULL; 158899653d4eSeschrock 1589a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1590a45f1c3cSBrian Behlendorf if (error != 0) 1591a45f1c3cSBrian Behlendorf return (error); 1592a45f1c3cSBrian Behlendorf 159399653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 159499653d4eSeschrock dmu_buf_rele(db, FTAG); 159599653d4eSeschrock 159699653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15977bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15987bfdf011SNeil Perrin DMU_READ_PREFETCH); 159999653d4eSeschrock if (error == 0) 160099653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 160199653d4eSeschrock kmem_free(packed, nvsize); 160299653d4eSeschrock 160399653d4eSeschrock return (error); 160499653d4eSeschrock } 160599653d4eSeschrock 16063d7072f8Seschrock /* 16073d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 16083d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 16093d7072f8Seschrock */ 16103d7072f8Seschrock static void 16113d7072f8Seschrock spa_check_removed(vdev_t *vd) 16123d7072f8Seschrock { 1613573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 16143d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 16153d7072f8Seschrock 1616efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1617efb4a871SYuri Pankov !vd->vdev_ishole) { 16183d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 1619*ce1577b0SDave Eddy spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 16203d7072f8Seschrock } 16213d7072f8Seschrock } 16223d7072f8Seschrock 1623215198a6SJoe Stein static void 1624215198a6SJoe Stein spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd) 1625215198a6SJoe Stein { 1626215198a6SJoe Stein ASSERT3U(vd->vdev_children, ==, mvd->vdev_children); 1627215198a6SJoe Stein 1628215198a6SJoe Stein vd->vdev_top_zap = mvd->vdev_top_zap; 1629215198a6SJoe Stein vd->vdev_leaf_zap = mvd->vdev_leaf_zap; 1630215198a6SJoe Stein 1631215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 1632215198a6SJoe Stein spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]); 1633215198a6SJoe Stein } 1634215198a6SJoe Stein } 1635215198a6SJoe Stein 1636e6ca193dSGeorge Wilson /* 16374b964adaSGeorge Wilson * Validate the current config against the MOS config 1638e6ca193dSGeorge Wilson */ 16394b964adaSGeorge Wilson static boolean_t 16404b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1641e6ca193dSGeorge Wilson { 16424b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 16434b964adaSGeorge Wilson nvlist_t *nv; 16444b964adaSGeorge Wilson 16454b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 16464b964adaSGeorge Wilson 16474b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16484b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 16494b964adaSGeorge Wilson 16504b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1651e6ca193dSGeorge Wilson 165288ecc943SGeorge Wilson /* 16534b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 16544b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 16554b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 165688ecc943SGeorge Wilson */ 16574b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 16584b964adaSGeorge Wilson nvlist_t **child, *nv; 16594b964adaSGeorge Wilson uint64_t idx = 0; 16604b964adaSGeorge Wilson 16614b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 16624b964adaSGeorge Wilson KM_SLEEP); 16634b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1664e6ca193dSGeorge Wilson 16654b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16664b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16674b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16684b964adaSGeorge Wilson 16694b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16704b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16714b964adaSGeorge Wilson mtvd->vdev_islog) 16724b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16734b964adaSGeorge Wilson B_FALSE, 0); 16744b964adaSGeorge Wilson } 16754b964adaSGeorge Wilson 16764b964adaSGeorge Wilson if (idx) { 16774b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16784b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16794b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16804b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16814b964adaSGeorge Wilson 16824b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16834b964adaSGeorge Wilson nvlist_free(child[i]); 16844b964adaSGeorge Wilson } 16854b964adaSGeorge Wilson nvlist_free(nv); 16864b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16874b964adaSGeorge Wilson } 16884b964adaSGeorge Wilson 16894b964adaSGeorge Wilson /* 16904b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16914b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16924b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16934b964adaSGeorge Wilson */ 169488ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16954b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16964b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16974b964adaSGeorge Wilson 16984b964adaSGeorge Wilson /* 16994b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 17004b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 17014b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 17024b964adaSGeorge Wilson */ 17034b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 17044b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 17054b964adaSGeorge Wilson 17064b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 17074b964adaSGeorge Wilson continue; 17084b964adaSGeorge Wilson 17094b964adaSGeorge Wilson /* 17104b964adaSGeorge Wilson * Device specific actions. 17114b964adaSGeorge Wilson */ 17124b964adaSGeorge Wilson if (mtvd->vdev_islog) { 17134b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 17144b964adaSGeorge Wilson } else { 17154b964adaSGeorge Wilson /* 17164b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 17174b964adaSGeorge Wilson * support we should be able to handle 17184b964adaSGeorge Wilson * missing data devices by transitioning 17194b964adaSGeorge Wilson * the pool to readonly. 17204b964adaSGeorge Wilson */ 17214b964adaSGeorge Wilson continue; 17224b964adaSGeorge Wilson } 17234b964adaSGeorge Wilson 17244b964adaSGeorge Wilson /* 17254b964adaSGeorge Wilson * Swap the missing vdev with the data we were 17264b964adaSGeorge Wilson * able to obtain from the MOS config. 17274b964adaSGeorge Wilson */ 17284b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 17294b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 17304b964adaSGeorge Wilson 17314b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 17324b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 17334b964adaSGeorge Wilson 17344b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17354b964adaSGeorge Wilson vdev_load(mtvd); 17364b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 17374b964adaSGeorge Wilson 17384b964adaSGeorge Wilson vdev_reopen(rvd); 1739215198a6SJoe Stein } else { 1740215198a6SJoe Stein if (mtvd->vdev_islog) { 1741215198a6SJoe Stein /* 1742215198a6SJoe Stein * Load the slog device's state from the MOS 1743215198a6SJoe Stein * config since it's possible that the label 1744215198a6SJoe Stein * does not contain the most up-to-date 1745215198a6SJoe Stein * information. 1746215198a6SJoe Stein */ 1747215198a6SJoe Stein vdev_load_log_state(tvd, mtvd); 1748215198a6SJoe Stein vdev_reopen(tvd); 1749215198a6SJoe Stein } 1750215198a6SJoe Stein 17514b964adaSGeorge Wilson /* 1752215198a6SJoe Stein * Per-vdev ZAP info is stored exclusively in the MOS. 17534b964adaSGeorge Wilson */ 1754215198a6SJoe Stein spa_config_valid_zaps(tvd, mtvd); 17554b964adaSGeorge Wilson } 1756e6ca193dSGeorge Wilson } 1757215198a6SJoe Stein 17584b964adaSGeorge Wilson vdev_free(mrvd); 175988ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 17604b964adaSGeorge Wilson 17614b964adaSGeorge Wilson /* 17624b964adaSGeorge Wilson * Ensure we were able to validate the config. 17634b964adaSGeorge Wilson */ 17644b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1765e6ca193dSGeorge Wilson } 1766e6ca193dSGeorge Wilson 1767b87f3af3Sperrin /* 1768b87f3af3Sperrin * Check for missing log devices 1769b87f3af3Sperrin */ 17703b2aab18SMatthew Ahrens static boolean_t 1771b87f3af3Sperrin spa_check_logs(spa_t *spa) 1772b87f3af3Sperrin { 17733b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 177412380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 17753b2aab18SMatthew Ahrens 1776b87f3af3Sperrin switch (spa->spa_log_state) { 1777b87f3af3Sperrin case SPA_LOG_MISSING: 1778b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1779b87f3af3Sperrin case SPA_LOG_UNKNOWN: 178012380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 178112380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 17823b2aab18SMatthew Ahrens if (rv) 17831195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1784b87f3af3Sperrin break; 1785b87f3af3Sperrin } 17863b2aab18SMatthew Ahrens return (rv); 1787b87f3af3Sperrin } 1788b87f3af3Sperrin 17891195e687SMark J Musante static boolean_t 17901195e687SMark J Musante spa_passivate_log(spa_t *spa) 17911195e687SMark J Musante { 17921195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17931195e687SMark J Musante boolean_t slog_found = B_FALSE; 17941195e687SMark J Musante 17951195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17961195e687SMark J Musante 17971195e687SMark J Musante if (!spa_has_slogs(spa)) 17981195e687SMark J Musante return (B_FALSE); 17991195e687SMark J Musante 18001195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18011195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18021195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18031195e687SMark J Musante 18041195e687SMark J Musante if (tvd->vdev_islog) { 18051195e687SMark J Musante metaslab_group_passivate(mg); 18061195e687SMark J Musante slog_found = B_TRUE; 18071195e687SMark J Musante } 18081195e687SMark J Musante } 18091195e687SMark J Musante 18101195e687SMark J Musante return (slog_found); 18111195e687SMark J Musante } 18121195e687SMark J Musante 18131195e687SMark J Musante static void 18141195e687SMark J Musante spa_activate_log(spa_t *spa) 18151195e687SMark J Musante { 18161195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 18171195e687SMark J Musante 18181195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 18191195e687SMark J Musante 18201195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 18211195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 18221195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 18231195e687SMark J Musante 18241195e687SMark J Musante if (tvd->vdev_islog) 18251195e687SMark J Musante metaslab_group_activate(mg); 18261195e687SMark J Musante } 18271195e687SMark J Musante } 18281195e687SMark J Musante 18291195e687SMark J Musante int 18301195e687SMark J Musante spa_offline_log(spa_t *spa) 18311195e687SMark J Musante { 18323b2aab18SMatthew Ahrens int error; 18331195e687SMark J Musante 18343b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 18353b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 18363b2aab18SMatthew Ahrens if (error == 0) { 18371195e687SMark J Musante /* 18381195e687SMark J Musante * We successfully offlined the log device, sync out the 18391195e687SMark J Musante * current txg so that the "stubby" block can be removed 18401195e687SMark J Musante * by zil_sync(). 18411195e687SMark J Musante */ 18421195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 18431195e687SMark J Musante } 18441195e687SMark J Musante return (error); 18451195e687SMark J Musante } 18461195e687SMark J Musante 1847b693757aSEric Schrock static void 1848b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1849b693757aSEric Schrock { 1850b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1851b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1852b693757aSEric Schrock } 1853b693757aSEric Schrock 1854b24ab676SJeff Bonwick void 1855b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1856b24ab676SJeff Bonwick { 1857b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1858b24ab676SJeff Bonwick 1859b24ab676SJeff Bonwick if (zio->io_error) 1860b24ab676SJeff Bonwick return; 1861b24ab676SJeff Bonwick 1862b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1863b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1864b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1865b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1866b24ab676SJeff Bonwick } 1867b24ab676SJeff Bonwick 1868468c413aSTim Haley typedef struct spa_load_error { 1869c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1870468c413aSTim Haley uint64_t sle_data_count; 1871468c413aSTim Haley } spa_load_error_t; 1872468c413aSTim Haley 1873468c413aSTim Haley static void 1874468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1875468c413aSTim Haley { 1876468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1877468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1878468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1879468c413aSTim Haley int error = zio->io_error; 1880e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa; 1881468c413aSTim Haley 1882770499e1SDan Kimmel abd_free(zio->io_abd); 1883468c413aSTim Haley if (error) { 1884ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1885468c413aSTim Haley type != DMU_OT_INTENT_LOG) 18861a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count); 1887468c413aSTim Haley else 18881a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count); 1889468c413aSTim Haley } 1890e42d2059SMatthew Ahrens 1891e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1892e42d2059SMatthew Ahrens spa->spa_scrub_inflight--; 1893e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv); 1894e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1895468c413aSTim Haley } 1896468c413aSTim Haley 1897e42d2059SMatthew Ahrens /* 1898e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying 1899e42d2059SMatthew Ahrens * a pool while importing it. 1900e42d2059SMatthew Ahrens */ 1901e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000; 1902e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE; 1903e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE; 1904e42d2059SMatthew Ahrens 1905468c413aSTim Haley /*ARGSUSED*/ 1906468c413aSTim Haley static int 1907b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 19087802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 1909468c413aSTim Haley { 1910a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 1911e42d2059SMatthew Ahrens return (0); 1912e42d2059SMatthew Ahrens /* 1913e42d2059SMatthew Ahrens * Note: normally this routine will not be called if 1914e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful 1915e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun. 1916e42d2059SMatthew Ahrens */ 1917e42d2059SMatthew Ahrens if (!spa_load_verify_metadata) 1918e42d2059SMatthew Ahrens return (0); 1919770499e1SDan Kimmel if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 1920e42d2059SMatthew Ahrens return (0); 1921468c413aSTim Haley 1922e42d2059SMatthew Ahrens zio_t *rio = arg; 1923e42d2059SMatthew Ahrens size_t size = BP_GET_PSIZE(bp); 1924e42d2059SMatthew Ahrens 1925e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock); 1926e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight) 1927e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 1928e42d2059SMatthew Ahrens spa->spa_scrub_inflight++; 1929e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock); 1930e42d2059SMatthew Ahrens 1931770499e1SDan Kimmel zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 1932e42d2059SMatthew Ahrens spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1933e42d2059SMatthew Ahrens ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1934e42d2059SMatthew Ahrens ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1935468c413aSTim Haley return (0); 1936468c413aSTim Haley } 1937468c413aSTim Haley 1938c971037bSPaul Dagnelie /* ARGSUSED */ 1939c971037bSPaul Dagnelie int 1940c971037bSPaul Dagnelie verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1941c971037bSPaul Dagnelie { 19429adfa60dSMatthew Ahrens if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 1943c971037bSPaul Dagnelie return (SET_ERROR(ENAMETOOLONG)); 1944c971037bSPaul Dagnelie 1945c971037bSPaul Dagnelie return (0); 1946c971037bSPaul Dagnelie } 1947c971037bSPaul Dagnelie 1948468c413aSTim Haley static int 1949468c413aSTim Haley spa_load_verify(spa_t *spa) 1950468c413aSTim Haley { 1951468c413aSTim Haley zio_t *rio; 1952468c413aSTim Haley spa_load_error_t sle = { 0 }; 1953468c413aSTim Haley zpool_rewind_policy_t policy; 1954468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1955e42d2059SMatthew Ahrens int error = 0; 1956468c413aSTim Haley 1957c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1958c8ee1847SVictor Latushkin 1959c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1960c8ee1847SVictor Latushkin return (0); 1961c8ee1847SVictor Latushkin 1962c971037bSPaul Dagnelie dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 1963c971037bSPaul Dagnelie error = dmu_objset_find_dp(spa->spa_dsl_pool, 1964c971037bSPaul Dagnelie spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 1965c971037bSPaul Dagnelie DS_FIND_CHILDREN); 1966c971037bSPaul Dagnelie dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 1967c971037bSPaul Dagnelie if (error != 0) 1968c971037bSPaul Dagnelie return (error); 1969c971037bSPaul Dagnelie 1970468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1971468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1972468c413aSTim Haley 1973e42d2059SMatthew Ahrens if (spa_load_verify_metadata) { 1974e42d2059SMatthew Ahrens error = traverse_pool(spa, spa->spa_verify_min_txg, 1975e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 1976e42d2059SMatthew Ahrens spa_load_verify_cb, rio); 1977e42d2059SMatthew Ahrens } 1978468c413aSTim Haley 1979468c413aSTim Haley (void) zio_wait(rio); 1980468c413aSTim Haley 1981c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1982468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1983468c413aSTim Haley 1984c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1985468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 19864b964adaSGeorge Wilson int64_t loss = 0; 19874b964adaSGeorge Wilson 1988468c413aSTim Haley verify_ok = B_TRUE; 1989468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1990468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 19914b964adaSGeorge Wilson 19924b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 19934b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19944b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 19954b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 19964b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 19974b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 19984b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1999a33cae98STim Haley } else { 2000a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2001468c413aSTim Haley } 2002468c413aSTim Haley 2003468c413aSTim Haley if (error) { 2004468c413aSTim Haley if (error != ENXIO && error != EIO) 2005be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 2006468c413aSTim Haley return (error); 2007468c413aSTim Haley } 2008468c413aSTim Haley 2009468c413aSTim Haley return (verify_ok ? 0 : EIO); 2010468c413aSTim Haley } 2011468c413aSTim Haley 20121195e687SMark J Musante /* 20131195e687SMark J Musante * Find a value in the pool props object. 20141195e687SMark J Musante */ 20151195e687SMark J Musante static void 20161195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 20171195e687SMark J Musante { 20181195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 20191195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 20201195e687SMark J Musante } 20211195e687SMark J Musante 20221195e687SMark J Musante /* 20231195e687SMark J Musante * Find a value in the pool directory object. 20241195e687SMark J Musante */ 20251195e687SMark J Musante static int 20261195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 20271195e687SMark J Musante { 20281195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 20291195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 20301195e687SMark J Musante } 20311195e687SMark J Musante 20321195e687SMark J Musante static int 20331195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 20341195e687SMark J Musante { 20351195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 20361195e687SMark J Musante return (err); 20371195e687SMark J Musante } 20381195e687SMark J Musante 20391195e687SMark J Musante /* 20401195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 20411195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 20421195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 20431195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 20441195e687SMark J Musante * 20451195e687SMark J Musante * This function determines what to do with that list: either rejoin 20461195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 20471195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 20481195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 20491195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 20501195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 20511195e687SMark J Musante * 2052d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 2053d41c4376SMark J Musante * the original pool. 20541195e687SMark J Musante */ 20551195e687SMark J Musante static void 20561195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 20571195e687SMark J Musante { 20581195e687SMark J Musante uint_t extracted; 20591195e687SMark J Musante uint64_t *glist; 20601195e687SMark J Musante uint_t i, gcount; 20611195e687SMark J Musante nvlist_t *nvl; 20621195e687SMark J Musante vdev_t **vd; 20631195e687SMark J Musante boolean_t attempt_reopen; 20641195e687SMark J Musante 20651195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 20661195e687SMark J Musante return; 20671195e687SMark J Musante 20681195e687SMark J Musante /* check that the config is complete */ 20691195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 20701195e687SMark J Musante &glist, &gcount) != 0) 20711195e687SMark J Musante return; 20721195e687SMark J Musante 20731195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 20741195e687SMark J Musante 20751195e687SMark J Musante /* attempt to online all the vdevs & validate */ 20761195e687SMark J Musante attempt_reopen = B_TRUE; 20771195e687SMark J Musante for (i = 0; i < gcount; i++) { 20781195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 20791195e687SMark J Musante continue; 20801195e687SMark J Musante 20811195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 20821195e687SMark J Musante if (vd[i] == NULL) { 20831195e687SMark J Musante /* 20841195e687SMark J Musante * Don't bother attempting to reopen the disks; 20851195e687SMark J Musante * just do the split. 20861195e687SMark J Musante */ 20871195e687SMark J Musante attempt_reopen = B_FALSE; 20881195e687SMark J Musante } else { 20891195e687SMark J Musante /* attempt to re-online it */ 20901195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 20911195e687SMark J Musante } 20921195e687SMark J Musante } 20931195e687SMark J Musante 20941195e687SMark J Musante if (attempt_reopen) { 20951195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 20961195e687SMark J Musante 20971195e687SMark J Musante /* check each device to see what state it's in */ 20981195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 20991195e687SMark J Musante if (vd[i] != NULL && 21001195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 21011195e687SMark J Musante break; 21021195e687SMark J Musante ++extracted; 21031195e687SMark J Musante } 21041195e687SMark J Musante } 21051195e687SMark J Musante 21061195e687SMark J Musante /* 21071195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 21081195e687SMark J Musante * even attempted to look at them, then we split them off for 21091195e687SMark J Musante * good. 21101195e687SMark J Musante */ 21111195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 21121195e687SMark J Musante for (i = 0; i < gcount; i++) 21131195e687SMark J Musante if (vd[i] != NULL) 21141195e687SMark J Musante vdev_split(vd[i]); 21151195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 21161195e687SMark J Musante } 21171195e687SMark J Musante 21181195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 21191195e687SMark J Musante } 21201195e687SMark J Musante 21211195e687SMark J Musante static int 21221195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 21231195e687SMark J Musante boolean_t mosconfig) 21241195e687SMark J Musante { 21251195e687SMark J Musante nvlist_t *config = spa->spa_config; 21261195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 21278704186eSDan McDonald char *comment; 21281195e687SMark J Musante int error; 21291195e687SMark J Musante uint64_t pool_guid; 21301195e687SMark J Musante nvlist_t *nvl; 21311195e687SMark J Musante 21321195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 2133be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21341195e687SMark J Musante 21358704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 21368704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 21378704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 21388704186eSDan McDonald 21391195e687SMark J Musante /* 21401195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 21411195e687SMark J Musante * it's not present treat it as the initial version. 21421195e687SMark J Musante */ 21431195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 21441195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 21451195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 21461195e687SMark J Musante 21471195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 21481195e687SMark J Musante &spa->spa_config_txg); 21491195e687SMark J Musante 21501195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 21511195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 2152be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 21531195e687SMark J Musante } else { 2154e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 21551195e687SMark J Musante 21561195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 21571195e687SMark J Musante &nvl) == 0) { 21581195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 21591195e687SMark J Musante KM_SLEEP) == 0); 21601195e687SMark J Musante } 21611195e687SMark J Musante 2162ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2163ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2164ad135b5dSChristopher Siden 216511027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 21661195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 21671195e687SMark J Musante mosconfig, &ereport); 21681195e687SMark J Musante } 21691195e687SMark J Musante 2170bc9014e6SJustin Gibbs /* 2171bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 2172bc9014e6SJustin Gibbs * and are making their way through the eviction process. 2173bc9014e6SJustin Gibbs */ 2174bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 21751195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 217611027bc7STim Haley if (error) { 217711027bc7STim Haley if (error != EEXIST) { 217811027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 217911027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 218011027bc7STim Haley } 218111027bc7STim Haley if (error != EBADF) { 218211027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 218311027bc7STim Haley } 218411027bc7STim Haley } 21851195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 21861195e687SMark J Musante spa->spa_ena = 0; 21871195e687SMark J Musante 21881195e687SMark J Musante return (error); 21891195e687SMark J Musante } 21901195e687SMark J Musante 2191215198a6SJoe Stein /* 2192215198a6SJoe Stein * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2193215198a6SJoe Stein * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2194215198a6SJoe Stein * spa's per-vdev ZAP list. 2195215198a6SJoe Stein */ 2196215198a6SJoe Stein static uint64_t 2197215198a6SJoe Stein vdev_count_verify_zaps(vdev_t *vd) 2198215198a6SJoe Stein { 2199215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 2200215198a6SJoe Stein uint64_t total = 0; 2201215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 2202215198a6SJoe Stein total++; 2203215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2204215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2205215198a6SJoe Stein } 2206215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 2207215198a6SJoe Stein total++; 2208215198a6SJoe Stein ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2209215198a6SJoe Stein spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2210215198a6SJoe Stein } 2211215198a6SJoe Stein 2212215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 2213215198a6SJoe Stein total += vdev_count_verify_zaps(vd->vdev_child[i]); 2214215198a6SJoe Stein } 2215215198a6SJoe Stein 2216215198a6SJoe Stein return (total); 2217215198a6SJoe Stein } 2218215198a6SJoe Stein 2219fa9e4066Sahrens /* 2220fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2221ea8dc4b6Seschrock * source of configuration information. 2222fa9e4066Sahrens */ 2223fa9e4066Sahrens static int 22241195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 22251195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 22261195e687SMark J Musante char **ereport) 2227fa9e4066Sahrens { 2228fa9e4066Sahrens int error = 0; 2229871a9500SMark J Musante nvlist_t *nvroot = NULL; 2230ad135b5dSChristopher Siden nvlist_t *label; 2231fa9e4066Sahrens vdev_t *rvd; 2232fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 22334b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 22348ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 22351195e687SMark J Musante int parse; 2236cde58dbcSMatthew Ahrens uint64_t obj; 2237ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2238fa9e4066Sahrens 22398ad4d6ddSJeff Bonwick /* 22408ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 22418ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 22428ad4d6ddSJeff Bonwick */ 22438ad4d6ddSJeff Bonwick if (!mosconfig) 22448ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 22458ad4d6ddSJeff Bonwick 2246e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2247e14bb325SJeff Bonwick 2248ea8dc4b6Seschrock spa->spa_load_state = state; 22490373e76bSbonwick 22501195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 2251be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2252fa9e4066Sahrens 22531195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 22541195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2255b5989ec7Seschrock 225654d692b7SGeorge Wilson /* 225754d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 225854d692b7SGeorge Wilson */ 22596f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 22606f834bc1SMatthew Ahrens KM_SLEEP); 22616f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 22626f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 22636f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 22646f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 22656f834bc1SMatthew Ahrens } 226654d692b7SGeorge Wilson 2267fa9e4066Sahrens /* 226899653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 226999653d4eSeschrock * value that will be returned by spa_version() since parsing the 227099653d4eSeschrock * configuration requires knowing the version number. 2271fa9e4066Sahrens */ 2272e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22731195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2274e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2275fa9e4066Sahrens 227699653d4eSeschrock if (error != 0) 22771195e687SMark J Musante return (error); 2278fa9e4066Sahrens 22790e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 228081cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 228181cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 22821195e687SMark J Musante 22831195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 22841195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 22851195e687SMark J Musante } 2286fa9e4066Sahrens 2287fa9e4066Sahrens /* 2288fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2289fa9e4066Sahrens */ 2290e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22910bf246f5Smc error = vdev_open(rvd); 2292e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 22930bf246f5Smc if (error != 0) 22941195e687SMark J Musante return (error); 2295fa9e4066Sahrens 2296560e6e96Seschrock /* 229777e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 229877e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 229977e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 23001195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 230177e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 230277e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 230377e3a39cSMark J Musante * the vdev config. 23041195e687SMark J Musante * 23051195e687SMark J Musante * If we're assembling a new pool that's been split off from an 23061195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 23071195e687SMark J Musante * validation for now. 2308560e6e96Seschrock */ 23091195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 23101195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2311d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 23121195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2313560e6e96Seschrock 23141195e687SMark J Musante if (error != 0) 23151195e687SMark J Musante return (error); 23161195e687SMark J Musante 23171195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2318be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 2319560e6e96Seschrock } 2320560e6e96Seschrock 2321fa9e4066Sahrens /* 2322fa9e4066Sahrens * Find the best uberblock. 2323fa9e4066Sahrens */ 2324ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2325fa9e4066Sahrens 2326fa9e4066Sahrens /* 2327fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2328fa9e4066Sahrens */ 2329ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2330ad135b5dSChristopher Siden nvlist_free(label); 23311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2332ad135b5dSChristopher Siden } 2333ea8dc4b6Seschrock 2334ea8dc4b6Seschrock /* 2335ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2336ea8dc4b6Seschrock */ 2337ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2338ad135b5dSChristopher Siden nvlist_free(label); 23391195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2340ad135b5dSChristopher Siden } 2341ad135b5dSChristopher Siden 2342ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2343ad135b5dSChristopher Siden nvlist_t *features; 2344ad135b5dSChristopher Siden 2345ad135b5dSChristopher Siden /* 2346ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2347ad135b5dSChristopher Siden * MOS in the label, return failure. 2348ad135b5dSChristopher Siden */ 2349ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2350ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2351ad135b5dSChristopher Siden nvlist_free(label); 2352ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2353ad135b5dSChristopher Siden ENXIO)); 2354ad135b5dSChristopher Siden } 2355ad135b5dSChristopher Siden 2356ad135b5dSChristopher Siden /* 2357ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2358ad135b5dSChristopher Siden * from the label. 2359ad135b5dSChristopher Siden */ 2360ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2361ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2362ad135b5dSChristopher Siden } 2363ad135b5dSChristopher Siden 2364ad135b5dSChristopher Siden nvlist_free(label); 2365ad135b5dSChristopher Siden 2366ad135b5dSChristopher Siden /* 2367ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2368ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2369ad135b5dSChristopher Siden * cannot open a pool. 2370ad135b5dSChristopher Siden */ 2371ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2372ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2373ad135b5dSChristopher Siden 2374ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2375ad135b5dSChristopher Siden 0); 2376ad135b5dSChristopher Siden 2377ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2378ad135b5dSChristopher Siden NULL); nvp != NULL; 2379ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2380ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2381ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2382ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2383ad135b5dSChristopher Siden } 2384ad135b5dSChristopher Siden } 2385ad135b5dSChristopher Siden 2386ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2387ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2388ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2389ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2390ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2391ad135b5dSChristopher Siden ENOTSUP)); 2392ad135b5dSChristopher Siden } 2393ad135b5dSChristopher Siden 2394ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2395ad135b5dSChristopher Siden } 2396fa9e4066Sahrens 2397fa9e4066Sahrens /* 2398fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 23994b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 24004b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 24014b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 24024b964adaSGeorge Wilson * can handle missing vdevs. 2403fa9e4066Sahrens */ 24044b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 24054b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 24061195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 24071195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 24081195e687SMark J Musante 24091195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 24101195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 24111195e687SMark J Musante spa_try_repair(spa, config); 24121195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 24131195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 24141195e687SMark J Musante spa->spa_config_splitting = NULL; 2415fa9e4066Sahrens } 2416fa9e4066Sahrens 2417fa9e4066Sahrens /* 2418fa9e4066Sahrens * Initialize internal SPA structures. 2419fa9e4066Sahrens */ 2420fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2421fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2422468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2423c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2424468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2425468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2426b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 24273f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2428b24ab676SJeff Bonwick 2429ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 24301195e687SMark J Musante if (error) 24311195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2432fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2433fa9e4066Sahrens 24341195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 24351195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2436fa9e4066Sahrens 2437ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2438ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 243957221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2440ad135b5dSChristopher Siden 2441ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2442ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2443ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2444ad135b5dSChristopher Siden } 2445ad135b5dSChristopher Siden 2446ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2447ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2448ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2449ad135b5dSChristopher Siden } 2450ad135b5dSChristopher Siden 2451ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2452ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2453ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2454ad135b5dSChristopher Siden } 2455ad135b5dSChristopher Siden 245657221772SChristopher Siden enabled_feat = fnvlist_alloc(); 245757221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2458ad135b5dSChristopher Siden 24592acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE, 246057221772SChristopher Siden unsup_feat, enabled_feat)) 2461ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2462ad135b5dSChristopher Siden 2463ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 24642acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE, 246557221772SChristopher Siden unsup_feat, enabled_feat)) { 2466ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 246757221772SChristopher Siden } 2468ad135b5dSChristopher Siden } 2469ad135b5dSChristopher Siden 247057221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 247157221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 247257221772SChristopher Siden 2473ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 247457221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 247557221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2476ad135b5dSChristopher Siden } 2477ad135b5dSChristopher Siden 247857221772SChristopher Siden fnvlist_free(enabled_feat); 247957221772SChristopher Siden fnvlist_free(unsup_feat); 2480ad135b5dSChristopher Siden 2481ad135b5dSChristopher Siden if (!missing_feat_read) { 2482ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2483ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2484ad135b5dSChristopher Siden } 2485ad135b5dSChristopher Siden 2486ad135b5dSChristopher Siden /* 2487ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2488ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2489ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2490ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2491ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2492ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2493ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2494ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2495ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2496ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2497ad135b5dSChristopher Siden * available for open in read-only mode. 2498ad135b5dSChristopher Siden * 2499ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2500ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2501ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2502ad135b5dSChristopher Siden * userland in order to know whether to display the 2503ad135b5dSChristopher Siden * abovementioned note. 2504ad135b5dSChristopher Siden */ 2505ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2506ad135b5dSChristopher Siden spa_writeable(spa))) { 2507ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2508ad135b5dSChristopher Siden ENOTSUP)); 2509ad135b5dSChristopher Siden } 251043466aaeSMax Grossman 251143466aaeSMax Grossman /* 251243466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory 251343466aaeSMax Grossman * cache during SPA initialization. 251443466aaeSMax Grossman */ 251543466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 251643466aaeSMax Grossman uint64_t refcount; 251743466aaeSMax Grossman 251843466aaeSMax Grossman error = feature_get_refcount_from_disk(spa, 251943466aaeSMax Grossman &spa_feature_table[i], &refcount); 252043466aaeSMax Grossman if (error == 0) { 252143466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount; 252243466aaeSMax Grossman } else if (error == ENOTSUP) { 252343466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = 252443466aaeSMax Grossman SPA_FEATURE_DISABLED; 252543466aaeSMax Grossman } else { 252643466aaeSMax Grossman return (spa_vdev_err(rvd, 252743466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO)); 252843466aaeSMax Grossman } 252943466aaeSMax Grossman } 253043466aaeSMax Grossman } 253143466aaeSMax Grossman 253243466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 253343466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 25345d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0) 253543466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2536ad135b5dSChristopher Siden } 2537ad135b5dSChristopher Siden 2538ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2539ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2540ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2541ad135b5dSChristopher Siden if (error != 0) 2542ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2543ad135b5dSChristopher Siden 2544fa9e4066Sahrens if (!mosconfig) { 254595173954Sek uint64_t hostid; 2546871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2547871a9500SMark J Musante 2548871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2549871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2550fa9e4066Sahrens 255188ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 255277650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 255395173954Sek char *hostname; 255495173954Sek unsigned long myhostid = 0; 255595173954Sek 255688ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 255795173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 255895173954Sek 25595679c89fSjv #ifdef _KERNEL 25605679c89fSjv myhostid = zone_get_hostid(NULL); 25615679c89fSjv #else /* _KERNEL */ 25625679c89fSjv /* 25635679c89fSjv * We're emulating the system's hostid in userland, so 25645679c89fSjv * we can't use zone_get_hostid(). 25655679c89fSjv */ 256695173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 25675679c89fSjv #endif /* _KERNEL */ 256817194a52Slling if (hostid != 0 && myhostid != 0 && 25695679c89fSjv hostid != myhostid) { 2570871a9500SMark J Musante nvlist_free(nvconfig); 257195173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 257295173954Sek "loaded as it was last accessed by " 257377650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2574654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2575e14bb325SJeff Bonwick spa_name(spa), hostname, 257695173954Sek (unsigned long)hostid); 2577be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF)); 257895173954Sek } 257995173954Sek } 2580c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2581c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2582c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2583c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 258495173954Sek 258588ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2586fa9e4066Sahrens spa_unload(spa); 2587fa9e4066Sahrens spa_deactivate(spa); 25888ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2589fa9e4066Sahrens 25901195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2591fa9e4066Sahrens } 2592fa9e4066Sahrens 259345818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */ 259445818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 259545818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1, 259645818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 259745818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes); 259845818ee1SMatthew Ahrens if (error == ENOENT) { 259945818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */ 260045818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 260145818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 260245818ee1SMatthew Ahrens } else if (error != 0) { 260345818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 260445818ee1SMatthew Ahrens } 260545818ee1SMatthew Ahrens 2606cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2607cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2608cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2609cde58dbcSMatthew Ahrens if (error != 0) 26101195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2611fa9e4066Sahrens 261299653d4eSeschrock /* 261399653d4eSeschrock * Load the bit that tells us to use the new accounting function 261499653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 261599653d4eSeschrock * be present. 261699653d4eSeschrock */ 26171195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 26181195e687SMark J Musante if (error != 0 && error != ENOENT) 26191195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 262099653d4eSeschrock 26213f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 26223f9d6ad7SLin Ling &spa->spa_creation_version); 26233f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 26243f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26253f9d6ad7SLin Ling 2626fa9e4066Sahrens /* 2627ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2628ea8dc4b6Seschrock * not be present. 2629fa9e4066Sahrens */ 26301195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 26311195e687SMark J Musante if (error != 0 && error != ENOENT) 26321195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2633ea8dc4b6Seschrock 26341195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 26351195e687SMark J Musante &spa->spa_errlog_scrub); 26361195e687SMark J Musante if (error != 0 && error != ENOENT) 26371195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2638ea8dc4b6Seschrock 263906eeb2adSek /* 264006eeb2adSek * Load the history object. If we have an older pool, this 264106eeb2adSek * will not be present. 264206eeb2adSek */ 26431195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 26441195e687SMark J Musante if (error != 0 && error != ENOENT) 26451195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26461195e687SMark J Musante 2647215198a6SJoe Stein /* 2648215198a6SJoe Stein * Load the per-vdev ZAP map. If we have an older pool, this will not 2649215198a6SJoe Stein * be present; in this case, defer its creation to a later time to 2650215198a6SJoe Stein * avoid dirtying the MOS this early / out of sync context. See 2651215198a6SJoe Stein * spa_sync_config_object. 2652215198a6SJoe Stein */ 2653215198a6SJoe Stein 2654215198a6SJoe Stein /* The sentinel is only available in the MOS config. */ 2655215198a6SJoe Stein nvlist_t *mos_config; 2656215198a6SJoe Stein if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) 2657215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2658215198a6SJoe Stein 2659215198a6SJoe Stein error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 2660215198a6SJoe Stein &spa->spa_all_vdev_zaps); 2661215198a6SJoe Stein 2662555da511SPaul Dagnelie if (error == ENOENT) { 2663555da511SPaul Dagnelie VERIFY(!nvlist_exists(mos_config, 2664555da511SPaul Dagnelie ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 2665555da511SPaul Dagnelie spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 2666555da511SPaul Dagnelie ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2667555da511SPaul Dagnelie } else if (error != 0) { 2668215198a6SJoe Stein return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2669555da511SPaul Dagnelie } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 2670215198a6SJoe Stein /* 2671215198a6SJoe Stein * An older version of ZFS overwrote the sentinel value, so 2672215198a6SJoe Stein * we have orphaned per-vdev ZAPs in the MOS. Defer their 2673215198a6SJoe Stein * destruction to later; see spa_sync_config_object. 2674215198a6SJoe Stein */ 2675215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_DESTROY; 2676215198a6SJoe Stein /* 2677215198a6SJoe Stein * We're assuming that no vdevs have had their ZAPs created 2678215198a6SJoe Stein * before this. Better be sure of it. 2679215198a6SJoe Stein */ 2680215198a6SJoe Stein ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 2681215198a6SJoe Stein } 2682215198a6SJoe Stein nvlist_free(mos_config); 2683215198a6SJoe Stein 26841195e687SMark J Musante /* 26851195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 26861195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 26871195e687SMark J Musante * devices. 26881195e687SMark J Musante */ 268906eeb2adSek 269099653d4eSeschrock /* 269199653d4eSeschrock * Load any hot spares for this pool. 269299653d4eSeschrock */ 26931195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 26941195e687SMark J Musante if (error != 0 && error != ENOENT) 26951195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 26961195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2697e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2698fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 26991195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 27001195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 270199653d4eSeschrock 2702e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 270399653d4eSeschrock spa_load_spares(spa); 2704e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27051195e687SMark J Musante } else if (error == 0) { 27061195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 270799653d4eSeschrock } 270899653d4eSeschrock 2709fa94a07fSbrendan /* 2710fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2711fa94a07fSbrendan */ 27121195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2713fa94a07fSbrendan &spa->spa_l2cache.sav_object); 27141195e687SMark J Musante if (error != 0 && error != ENOENT) 27151195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 27161195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2717fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2718fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 27191195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 27201195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2721fa94a07fSbrendan 2722e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2723fa94a07fSbrendan spa_load_l2cache(spa); 2724e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 27251195e687SMark J Musante } else if (error == 0) { 27261195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2727fa94a07fSbrendan } 2728fa94a07fSbrendan 2729990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2730ecd6cf80Smarks 27311195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 27321195e687SMark J Musante if (error && error != ENOENT) 27331195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2734b1b8ab34Slling 2735b1b8ab34Slling if (error == 0) { 27361195e687SMark J Musante uint64_t autoreplace; 27371195e687SMark J Musante 27381195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 27391195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 27401195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 27411195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 27421195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 27437855d95bSToomas Soome spa_prop_find(spa, ZPOOL_PROP_BOOTSIZE, &spa->spa_bootsize); 27441195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 27451195e687SMark J Musante &spa->spa_dedup_ditto); 27461195e687SMark J Musante 2747b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2748b1b8ab34Slling } 2749b1b8ab34Slling 27503d7072f8Seschrock /* 27513d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 27523d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 27533d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 27543d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 27553d7072f8Seschrock * over. 27563d7072f8Seschrock */ 2757b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 27583d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2759b693757aSEric Schrock /* 2760b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2761b693757aSEric Schrock * at this point we're using the spare definitions from 2762b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2763b693757aSEric Schrock */ 2764b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2765b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2766b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2767b693757aSEric Schrock } 2768b693757aSEric Schrock } 27693d7072f8Seschrock 2770ea8dc4b6Seschrock /* 2771560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2772ea8dc4b6Seschrock */ 2773560e6e96Seschrock vdev_load(rvd); 27740373e76bSbonwick 2775fa9e4066Sahrens /* 2776fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2777fa9e4066Sahrens */ 2778e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2779fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2780e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2781fa9e4066Sahrens 2782b24ab676SJeff Bonwick /* 2783b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2784b24ab676SJeff Bonwick */ 2785b24ab676SJeff Bonwick error = ddt_load(spa); 27861195e687SMark J Musante if (error != 0) 27871195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2788b24ab676SJeff Bonwick 2789485bbbf5SGeorge Wilson spa_update_dspace(spa); 2790485bbbf5SGeorge Wilson 2791b24ab676SJeff Bonwick /* 27924b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 27934b964adaSGeorge Wilson * information which might be missing. If we fail to validate 27944b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 27954b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 27964b964adaSGeorge Wilson * over. 2797b24ab676SJeff Bonwick */ 27981195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2799871a9500SMark J Musante nvlist_t *nvconfig; 2800871a9500SMark J Musante 2801871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2802871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2803871a9500SMark J Musante 28044b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 28054b964adaSGeorge Wilson nvlist_free(nvconfig); 28064b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 28074b964adaSGeorge Wilson ENXIO)); 28084b964adaSGeorge Wilson } 28091195e687SMark J Musante nvlist_free(nvconfig); 28101195e687SMark J Musante 28114b964adaSGeorge Wilson /* 2812ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 28134b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 28144b964adaSGeorge Wilson * more toplevel vdevs are faulted. 28154b964adaSGeorge Wilson */ 28164b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 2817be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 28184b964adaSGeorge Wilson 281923367a2fSMatthew Ahrens if (spa_writeable(spa) && spa_check_logs(spa)) { 28201195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 28211195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 28221195e687SMark J Musante } 2823b24ab676SJeff Bonwick } 2824b24ab676SJeff Bonwick 2825ad135b5dSChristopher Siden if (missing_feat_write) { 2826ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2827ad135b5dSChristopher Siden 2828ad135b5dSChristopher Siden /* 2829ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2830ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2831ad135b5dSChristopher Siden * information and can return to userland. 2832ad135b5dSChristopher Siden */ 2833ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2834ad135b5dSChristopher Siden } 2835ad135b5dSChristopher Siden 28364b964adaSGeorge Wilson /* 28374b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 28384b964adaSGeorge Wilson * to start pushing transactions. 28394b964adaSGeorge Wilson */ 28404b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 28414b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 28424b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 28434b964adaSGeorge Wilson error)); 28444b964adaSGeorge Wilson } 28454b964adaSGeorge Wilson 2846468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2847468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 28485dabedeeSbonwick dmu_tx_t *tx; 28490373e76bSbonwick int need_update = B_FALSE; 285012380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa); 28518ad4d6ddSJeff Bonwick 28528ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 28535dabedeeSbonwick 28540373e76bSbonwick /* 28550373e76bSbonwick * Claim log blocks that haven't been committed yet. 28560373e76bSbonwick * This must all happen in a single txg. 2857b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2858b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2859468c413aSTim Haley * Price of rollback is that we abandon the log. 28600373e76bSbonwick */ 2861b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2862b24ab676SJeff Bonwick 286312380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 286412380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 28650b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2866fa9e4066Sahrens dmu_tx_commit(tx); 2867fa9e4066Sahrens 2868b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2869b24ab676SJeff Bonwick 28701195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2871fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2872fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2873fa9e4066Sahrens 2874fa9e4066Sahrens /* 2875b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2876b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2877b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2878b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2879b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2880fa9e4066Sahrens */ 2881b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 28820e34b6a7Sbonwick 28830e34b6a7Sbonwick /* 28840373e76bSbonwick * If the config cache is stale, or we have uninitialized 28850373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2886bc758434SLin Ling * 28874b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2888bc758434SLin Ling * in-core spa_config and update the disk labels. 28890e34b6a7Sbonwick */ 28900373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 28914b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 28924b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 28934b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 28940373e76bSbonwick need_update = B_TRUE; 28950373e76bSbonwick 28968ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 28970373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 28980373e76bSbonwick need_update = B_TRUE; 28990e34b6a7Sbonwick 29000e34b6a7Sbonwick /* 29010373e76bSbonwick * Update the config cache asychronously in case we're the 29020373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 29030e34b6a7Sbonwick */ 29040373e76bSbonwick if (need_update) 29050373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 29068ad4d6ddSJeff Bonwick 29078ad4d6ddSJeff Bonwick /* 29088ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 29098ad4d6ddSJeff Bonwick */ 29103f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 29113f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 29128ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2913503ad85cSMatthew Ahrens 29144445fffbSMatthew Ahrens /* 29154445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 29164445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 29174445fffbSMatthew Ahrens */ 29184445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 29194445fffbSMatthew Ahrens 2920503ad85cSMatthew Ahrens /* 2921503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2922503ad85cSMatthew Ahrens */ 2923503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2924503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2925ca45db41SChris Kirby 2926ca45db41SChris Kirby /* 2927ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2928ca45db41SChris Kirby */ 2929ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2930fa9e4066Sahrens } 2931fa9e4066Sahrens 29321195e687SMark J Musante return (0); 2933fa9e4066Sahrens } 2934fa9e4066Sahrens 2935468c413aSTim Haley static int 2936468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2937468c413aSTim Haley { 2938f9af39baSGeorge Wilson int mode = spa->spa_mode; 2939f9af39baSGeorge Wilson 2940468c413aSTim Haley spa_unload(spa); 2941468c413aSTim Haley spa_deactivate(spa); 2942468c413aSTim Haley 2943e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 2944468c413aSTim Haley 2945f9af39baSGeorge Wilson spa_activate(spa, mode); 2946468c413aSTim Haley spa_async_suspend(spa); 2947468c413aSTim Haley 29481195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2949468c413aSTim Haley } 2950468c413aSTim Haley 2951ad135b5dSChristopher Siden /* 2952ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2953ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2954ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2955ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2956ad135b5dSChristopher Siden * spa_load(). 2957ad135b5dSChristopher Siden */ 2958468c413aSTim Haley static int 2959468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2960c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2961468c413aSTim Haley { 2962ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2963468c413aSTim Haley nvlist_t *config = NULL; 2964468c413aSTim Haley int load_error, rewind_error; 2965c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2966468c413aSTim Haley uint64_t min_txg; 2967468c413aSTim Haley 2968a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2969468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 29701195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2971a33cae98STim Haley } else { 2972468c413aSTim Haley spa->spa_load_max_txg = max_request; 2973e42d2059SMatthew Ahrens if (max_request != UINT64_MAX) 2974e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE; 2975a33cae98STim Haley } 2976468c413aSTim Haley 29771195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 29781195e687SMark J Musante mosconfig); 2979468c413aSTim Haley if (load_error == 0) 2980468c413aSTim Haley return (0); 2981468c413aSTim Haley 2982468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2983468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2984468c413aSTim Haley 2985468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2986468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2987468c413aSTim Haley 2988c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2989468c413aSTim Haley nvlist_free(config); 2990468c413aSTim Haley return (load_error); 2991468c413aSTim Haley } 2992468c413aSTim Haley 2993ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2994ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 29951195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2996ad135b5dSChristopher Siden } else { 2997ad135b5dSChristopher Siden /* 2998ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2999ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 3000ad135b5dSChristopher Siden * to rewind. 3001ad135b5dSChristopher Siden */ 3002ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 3003ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 3004ad135b5dSChristopher Siden } 3005468c413aSTim Haley 3006c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 3007c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 3008c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 3009c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 3010468c413aSTim Haley 3011c8ee1847SVictor Latushkin /* 3012c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 3013c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 3014c8ee1847SVictor Latushkin */ 3015c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 3016c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 3017c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 3018468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 3019468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 3020468c413aSTim Haley } 3021468c413aSTim Haley 3022468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 3023468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 3024468c413aSTim Haley 3025468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 3026468c413aSTim Haley spa_config_set(spa, config); 3027468c413aSTim Haley 3028ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 3029ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 3030ad135b5dSChristopher Siden return (rewind_error); 3031ad135b5dSChristopher Siden } else { 3032ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 3033ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 3034ad135b5dSChristopher Siden spa->spa_load_info); 3035ad135b5dSChristopher Siden 3036ad135b5dSChristopher Siden /* Restore the initial load info */ 3037ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 3038ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 3039ad135b5dSChristopher Siden 3040ad135b5dSChristopher Siden return (load_error); 3041ad135b5dSChristopher Siden } 3042468c413aSTim Haley } 3043468c413aSTim Haley 3044fa9e4066Sahrens /* 3045fa9e4066Sahrens * Pool Open/Import 3046fa9e4066Sahrens * 3047fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 3048fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 3049fa9e4066Sahrens * case of an open, the pool configuration will exist in the 30503d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 3051fa9e4066Sahrens * 3052fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 3053fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 3054fa9e4066Sahrens * ambiguous state. 3055fa9e4066Sahrens */ 3056fa9e4066Sahrens static int 3057468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 3058468c413aSTim Haley nvlist_t **config) 3059fa9e4066Sahrens { 3060fa9e4066Sahrens spa_t *spa; 30614b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 3062fa9e4066Sahrens int error; 3063fa9e4066Sahrens int locked = B_FALSE; 3064fa9e4066Sahrens 3065fa9e4066Sahrens *spapp = NULL; 3066fa9e4066Sahrens 3067fa9e4066Sahrens /* 3068fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 3069fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 3070fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 3071fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 3072fa9e4066Sahrens */ 3073fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 3074fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3075fa9e4066Sahrens locked = B_TRUE; 3076fa9e4066Sahrens } 3077fa9e4066Sahrens 3078fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 3079fa9e4066Sahrens if (locked) 3080fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3081be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3082fa9e4066Sahrens } 3083468c413aSTim Haley 3084fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 30854b44c88cSTim Haley zpool_rewind_policy_t policy; 30864b44c88cSTim Haley 30874b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 30884b44c88cSTim Haley &policy); 30894b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 30904b44c88cSTim Haley state = SPA_LOAD_RECOVER; 3091fa9e4066Sahrens 30928ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3093fa9e4066Sahrens 3094468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3095468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 3096468c413aSTim Haley 3097468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 3098c8ee1847SVictor Latushkin policy.zrp_request); 3099fa9e4066Sahrens 3100fa9e4066Sahrens if (error == EBADF) { 3101fa9e4066Sahrens /* 3102560e6e96Seschrock * If vdev_validate() returns failure (indicated by 3103560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 3104560e6e96Seschrock * that the pool has been exported or destroyed. If 3105560e6e96Seschrock * this is the case, the config cache is out of sync and 3106560e6e96Seschrock * we should remove the pool from the namespace. 3107fa9e4066Sahrens */ 3108fa9e4066Sahrens spa_unload(spa); 3109fa9e4066Sahrens spa_deactivate(spa); 3110c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 3111fa9e4066Sahrens spa_remove(spa); 3112fa9e4066Sahrens if (locked) 3113fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3114be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 3115ea8dc4b6Seschrock } 3116ea8dc4b6Seschrock 3117ea8dc4b6Seschrock if (error) { 3118fa9e4066Sahrens /* 3119fa9e4066Sahrens * We can't open the pool, but we still have useful 3120fa9e4066Sahrens * information: the state of each vdev after the 3121fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 3122fa9e4066Sahrens */ 31234b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 3124468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 3125468c413aSTim Haley KM_SLEEP) == 0); 31264b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 31274b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 31284b964adaSGeorge Wilson spa->spa_load_info) == 0); 31294b964adaSGeorge Wilson } 3130fa9e4066Sahrens spa_unload(spa); 3131fa9e4066Sahrens spa_deactivate(spa); 3132468c413aSTim Haley spa->spa_last_open_failed = error; 3133fa9e4066Sahrens if (locked) 3134fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3135fa9e4066Sahrens *spapp = NULL; 3136fa9e4066Sahrens return (error); 3137fa9e4066Sahrens } 3138fa9e4066Sahrens } 3139fa9e4066Sahrens 3140fa9e4066Sahrens spa_open_ref(spa, tag); 31413d7072f8Seschrock 3142468c413aSTim Haley if (config != NULL) 3143468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3144468c413aSTim Haley 31454b964adaSGeorge Wilson /* 31464b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 31474b964adaSGeorge Wilson * gathered while doing the load. 31484b964adaSGeorge Wilson */ 31494b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 31504b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 31514b964adaSGeorge Wilson spa->spa_load_info) == 0); 31524b964adaSGeorge Wilson } 31534b964adaSGeorge Wilson 3154a33cae98STim Haley if (locked) { 3155a33cae98STim Haley spa->spa_last_open_failed = 0; 3156a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 3157a33cae98STim Haley spa->spa_load_txg = 0; 3158fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3159a33cae98STim Haley } 3160fa9e4066Sahrens 3161fa9e4066Sahrens *spapp = spa; 3162fa9e4066Sahrens 3163fa9e4066Sahrens return (0); 3164fa9e4066Sahrens } 3165fa9e4066Sahrens 3166468c413aSTim Haley int 3167468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 3168468c413aSTim Haley nvlist_t **config) 3169468c413aSTim Haley { 3170468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 3171468c413aSTim Haley } 3172468c413aSTim Haley 3173fa9e4066Sahrens int 3174fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 3175fa9e4066Sahrens { 3176468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 3177fa9e4066Sahrens } 3178fa9e4066Sahrens 3179ea8dc4b6Seschrock /* 3180ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 3181ea8dc4b6Seschrock * preventing it from being exported or destroyed. 3182ea8dc4b6Seschrock */ 3183ea8dc4b6Seschrock spa_t * 3184ea8dc4b6Seschrock spa_inject_addref(char *name) 3185ea8dc4b6Seschrock { 3186ea8dc4b6Seschrock spa_t *spa; 3187ea8dc4b6Seschrock 3188ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3189ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 3190ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3191ea8dc4b6Seschrock return (NULL); 3192ea8dc4b6Seschrock } 3193ea8dc4b6Seschrock spa->spa_inject_ref++; 3194ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3195ea8dc4b6Seschrock 3196ea8dc4b6Seschrock return (spa); 3197ea8dc4b6Seschrock } 3198ea8dc4b6Seschrock 3199ea8dc4b6Seschrock void 3200ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 3201ea8dc4b6Seschrock { 3202ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3203ea8dc4b6Seschrock spa->spa_inject_ref--; 3204ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3205ea8dc4b6Seschrock } 3206ea8dc4b6Seschrock 3207fa94a07fSbrendan /* 3208fa94a07fSbrendan * Add spares device information to the nvlist. 3209fa94a07fSbrendan */ 321099653d4eSeschrock static void 321199653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 321299653d4eSeschrock { 321399653d4eSeschrock nvlist_t **spares; 321499653d4eSeschrock uint_t i, nspares; 321599653d4eSeschrock nvlist_t *nvroot; 321699653d4eSeschrock uint64_t guid; 321799653d4eSeschrock vdev_stat_t *vs; 321899653d4eSeschrock uint_t vsc; 321939c23413Seschrock uint64_t pool; 322099653d4eSeschrock 32216809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32226809eb4eSEric Schrock 3223fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 322499653d4eSeschrock return; 322599653d4eSeschrock 322699653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 322799653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3228fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 322999653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 323099653d4eSeschrock if (nspares != 0) { 323199653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 323299653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 323399653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 323499653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 323599653d4eSeschrock 323699653d4eSeschrock /* 323799653d4eSeschrock * Go through and find any spares which have since been 323899653d4eSeschrock * repurposed as an active spare. If this is the case, update 323999653d4eSeschrock * their status appropriately. 324099653d4eSeschrock */ 324199653d4eSeschrock for (i = 0; i < nspares; i++) { 324299653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 324399653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 324489a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 324589a89ebfSlling pool != 0ULL) { 324699653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 32473f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 324899653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 324999653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 325099653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 325199653d4eSeschrock } 325299653d4eSeschrock } 325399653d4eSeschrock } 325499653d4eSeschrock } 325599653d4eSeschrock 3256fa94a07fSbrendan /* 3257fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3258fa94a07fSbrendan */ 3259fa94a07fSbrendan static void 3260fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3261fa94a07fSbrendan { 3262fa94a07fSbrendan nvlist_t **l2cache; 3263fa94a07fSbrendan uint_t i, j, nl2cache; 3264fa94a07fSbrendan nvlist_t *nvroot; 3265fa94a07fSbrendan uint64_t guid; 3266fa94a07fSbrendan vdev_t *vd; 3267fa94a07fSbrendan vdev_stat_t *vs; 3268fa94a07fSbrendan uint_t vsc; 3269fa94a07fSbrendan 32706809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 32716809eb4eSEric Schrock 3272fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3273fa94a07fSbrendan return; 3274fa94a07fSbrendan 3275fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3276fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3277fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3278fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3279fa94a07fSbrendan if (nl2cache != 0) { 3280fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3281fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3282fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3283fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3284fa94a07fSbrendan 3285fa94a07fSbrendan /* 3286fa94a07fSbrendan * Update level 2 cache device stats. 3287fa94a07fSbrendan */ 3288fa94a07fSbrendan 3289fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3290fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3291fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3292fa94a07fSbrendan 3293fa94a07fSbrendan vd = NULL; 3294fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3295fa94a07fSbrendan if (guid == 3296fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3297fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3298fa94a07fSbrendan break; 3299fa94a07fSbrendan } 3300fa94a07fSbrendan } 3301fa94a07fSbrendan ASSERT(vd != NULL); 3302fa94a07fSbrendan 3303fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 33043f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 33053f9d6ad7SLin Ling == 0); 3306fa94a07fSbrendan vdev_get_stats(vd, vs); 3307fa94a07fSbrendan } 3308fa94a07fSbrendan } 3309fa94a07fSbrendan } 3310fa94a07fSbrendan 3311ad135b5dSChristopher Siden static void 3312ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3313ad135b5dSChristopher Siden { 3314ad135b5dSChristopher Siden nvlist_t *features; 3315ad135b5dSChristopher Siden zap_cursor_t zc; 3316ad135b5dSChristopher Siden zap_attribute_t za; 3317ad135b5dSChristopher Siden 3318ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3319ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3320ad135b5dSChristopher Siden 3321ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3322ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3323ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3324ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3325ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3326ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3327ad135b5dSChristopher Siden za.za_num_integers == 1); 3328b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3329ad135b5dSChristopher Siden za.za_first_integer)); 3330ad135b5dSChristopher Siden } 3331ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3332ad135b5dSChristopher Siden } 3333ad135b5dSChristopher Siden 3334ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3335ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3336ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3337ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3338ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3339ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3340ad135b5dSChristopher Siden za.za_num_integers == 1); 3341b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3342ad135b5dSChristopher Siden za.za_first_integer)); 3343ad135b5dSChristopher Siden } 3344ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3345ad135b5dSChristopher Siden } 3346ad135b5dSChristopher Siden 3347ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3348ad135b5dSChristopher Siden features) == 0); 3349ad135b5dSChristopher Siden nvlist_free(features); 3350ad135b5dSChristopher Siden } 3351ad135b5dSChristopher Siden 3352fa9e4066Sahrens int 3353ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3354ad135b5dSChristopher Siden char *altroot, size_t buflen) 3355fa9e4066Sahrens { 3356fa9e4066Sahrens int error; 3357fa9e4066Sahrens spa_t *spa; 3358fa9e4066Sahrens 3359fa9e4066Sahrens *config = NULL; 3360468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3361fa9e4066Sahrens 33626809eb4eSEric Schrock if (spa != NULL) { 33636809eb4eSEric Schrock /* 33646809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 33656809eb4eSEric Schrock * or l2cache devices could change and the config would be 33666809eb4eSEric Schrock * self-inconsistent. 33676809eb4eSEric Schrock */ 33686809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3369ea8dc4b6Seschrock 33706809eb4eSEric Schrock if (*config != NULL) { 337111027bc7STim Haley uint64_t loadtimes[2]; 337211027bc7STim Haley 337311027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 337411027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 337511027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 337611027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 337711027bc7STim Haley 3378e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 33796809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 33806809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3381e14bb325SJeff Bonwick 33826809eb4eSEric Schrock if (spa_suspended(spa)) 33836809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 33846809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 33856809eb4eSEric Schrock spa->spa_failmode) == 0); 33866809eb4eSEric Schrock 33876809eb4eSEric Schrock spa_add_spares(spa, *config); 33886809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3389ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 33906809eb4eSEric Schrock } 339199653d4eSeschrock } 339299653d4eSeschrock 3393ea8dc4b6Seschrock /* 3394ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3395ea8dc4b6Seschrock * and call spa_lookup() directly. 3396ea8dc4b6Seschrock */ 3397ea8dc4b6Seschrock if (altroot) { 3398ea8dc4b6Seschrock if (spa == NULL) { 3399ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3400ea8dc4b6Seschrock spa = spa_lookup(name); 3401ea8dc4b6Seschrock if (spa) 3402ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3403ea8dc4b6Seschrock else 3404ea8dc4b6Seschrock altroot[0] = '\0'; 3405ea8dc4b6Seschrock spa = NULL; 3406ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3407ea8dc4b6Seschrock } else { 3408ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3409ea8dc4b6Seschrock } 3410ea8dc4b6Seschrock } 3411ea8dc4b6Seschrock 34126809eb4eSEric Schrock if (spa != NULL) { 34136809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3414fa9e4066Sahrens spa_close(spa, FTAG); 34156809eb4eSEric Schrock } 3416fa9e4066Sahrens 3417fa9e4066Sahrens return (error); 3418fa9e4066Sahrens } 3419fa9e4066Sahrens 342099653d4eSeschrock /* 3421fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3422fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3423fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3424fa94a07fSbrendan * specified, as long as they are well-formed. 342599653d4eSeschrock */ 342699653d4eSeschrock static int 3427fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3428fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3429fa94a07fSbrendan vdev_labeltype_t label) 343099653d4eSeschrock { 3431fa94a07fSbrendan nvlist_t **dev; 3432fa94a07fSbrendan uint_t i, ndev; 343399653d4eSeschrock vdev_t *vd; 343499653d4eSeschrock int error; 343599653d4eSeschrock 3436e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3437e14bb325SJeff Bonwick 343899653d4eSeschrock /* 3439fa94a07fSbrendan * It's acceptable to have no devs specified. 344099653d4eSeschrock */ 3441fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 344299653d4eSeschrock return (0); 344399653d4eSeschrock 3444fa94a07fSbrendan if (ndev == 0) 3445be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 344699653d4eSeschrock 344799653d4eSeschrock /* 3448fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3449fa94a07fSbrendan * device type. 345099653d4eSeschrock */ 3451fa94a07fSbrendan if (spa_version(spa) < version) 3452be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 345399653d4eSeschrock 345439c23413Seschrock /* 3455fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 345639c23413Seschrock * checking. 345739c23413Seschrock */ 3458fa94a07fSbrendan sav->sav_pending = dev; 3459fa94a07fSbrendan sav->sav_npending = ndev; 346039c23413Seschrock 3461fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3462fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 346399653d4eSeschrock mode)) != 0) 346439c23413Seschrock goto out; 346599653d4eSeschrock 346699653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 346799653d4eSeschrock vdev_free(vd); 3468be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 346939c23413Seschrock goto out; 347099653d4eSeschrock } 347199653d4eSeschrock 3472fa94a07fSbrendan /* 3473e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3474e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3475fa94a07fSbrendan */ 3476e14bb325SJeff Bonwick #ifdef _KERNEL 3477fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3478fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3479be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK); 3480cd0837ccSGeorge Wilson vdev_free(vd); 3481fa94a07fSbrendan goto out; 3482fa94a07fSbrendan } 3483e14bb325SJeff Bonwick #endif 348499653d4eSeschrock vd->vdev_top = vd; 348599653d4eSeschrock 348639c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3487fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3488fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 348939c23413Seschrock vd->vdev_guid) == 0); 349039c23413Seschrock } 349199653d4eSeschrock 349299653d4eSeschrock vdev_free(vd); 349339c23413Seschrock 3494fa94a07fSbrendan if (error && 3495fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 349639c23413Seschrock goto out; 349739c23413Seschrock else 349839c23413Seschrock error = 0; 349999653d4eSeschrock } 350099653d4eSeschrock 350139c23413Seschrock out: 3502fa94a07fSbrendan sav->sav_pending = NULL; 3503fa94a07fSbrendan sav->sav_npending = 0; 350439c23413Seschrock return (error); 350599653d4eSeschrock } 350699653d4eSeschrock 3507fa94a07fSbrendan static int 3508fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3509fa94a07fSbrendan { 3510fa94a07fSbrendan int error; 3511fa94a07fSbrendan 3512e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3513e14bb325SJeff Bonwick 3514fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3515fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3516fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3517fa94a07fSbrendan return (error); 3518fa94a07fSbrendan } 3519fa94a07fSbrendan 3520fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3521fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3522fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3523fa94a07fSbrendan } 3524fa94a07fSbrendan 3525fa94a07fSbrendan static void 3526fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3527fa94a07fSbrendan const char *config) 3528fa94a07fSbrendan { 3529fa94a07fSbrendan int i; 3530fa94a07fSbrendan 3531fa94a07fSbrendan if (sav->sav_config != NULL) { 3532fa94a07fSbrendan nvlist_t **olddevs; 3533fa94a07fSbrendan uint_t oldndevs; 3534fa94a07fSbrendan nvlist_t **newdevs; 3535fa94a07fSbrendan 3536fa94a07fSbrendan /* 3537fa94a07fSbrendan * Generate new dev list by concatentating with the 3538fa94a07fSbrendan * current dev list. 3539fa94a07fSbrendan */ 3540fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3541fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3542fa94a07fSbrendan 3543fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3544fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3545fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3546fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3547fa94a07fSbrendan KM_SLEEP) == 0); 3548fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3549fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3550fa94a07fSbrendan KM_SLEEP) == 0); 3551fa94a07fSbrendan 3552fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3553fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3554fa94a07fSbrendan 3555fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3556fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3557fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3558fa94a07fSbrendan nvlist_free(newdevs[i]); 3559fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3560fa94a07fSbrendan } else { 3561fa94a07fSbrendan /* 3562fa94a07fSbrendan * Generate a new dev list. 3563fa94a07fSbrendan */ 3564fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3565fa94a07fSbrendan KM_SLEEP) == 0); 3566fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3567fa94a07fSbrendan devs, ndevs) == 0); 3568fa94a07fSbrendan } 3569fa94a07fSbrendan } 3570fa94a07fSbrendan 3571fa94a07fSbrendan /* 3572fa94a07fSbrendan * Stop and drop level 2 ARC devices 3573fa94a07fSbrendan */ 3574fa94a07fSbrendan void 3575fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3576fa94a07fSbrendan { 3577fa94a07fSbrendan vdev_t *vd; 3578fa94a07fSbrendan int i; 3579fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3580fa94a07fSbrendan 3581fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3582fa94a07fSbrendan uint64_t pool; 3583fa94a07fSbrendan 3584fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3585fa94a07fSbrendan ASSERT(vd != NULL); 3586fa94a07fSbrendan 35878ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 35888ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3589fa94a07fSbrendan l2arc_remove_vdev(vd); 3590fa94a07fSbrendan } 3591fa94a07fSbrendan } 3592fa94a07fSbrendan 3593fa9e4066Sahrens /* 3594fa9e4066Sahrens * Pool Creation 3595fa9e4066Sahrens */ 3596fa9e4066Sahrens int 3597990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 35984445fffbSMatthew Ahrens nvlist_t *zplprops) 3599fa9e4066Sahrens { 3600fa9e4066Sahrens spa_t *spa; 3601990b4856Slling char *altroot = NULL; 36020373e76bSbonwick vdev_t *rvd; 3603fa9e4066Sahrens dsl_pool_t *dp; 3604fa9e4066Sahrens dmu_tx_t *tx; 3605573ca77eSGeorge Wilson int error = 0; 3606fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3607fa94a07fSbrendan nvlist_t **spares, **l2cache; 3608fa94a07fSbrendan uint_t nspares, nl2cache; 3609cde58dbcSMatthew Ahrens uint64_t version, obj; 3610ad135b5dSChristopher Siden boolean_t has_features; 3611fa9e4066Sahrens 3612fa9e4066Sahrens /* 3613fa9e4066Sahrens * If this pool already exists, return failure. 3614fa9e4066Sahrens */ 3615fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3616fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3617fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3618be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 3619fa9e4066Sahrens } 3620fa9e4066Sahrens 3621fa9e4066Sahrens /* 3622fa9e4066Sahrens * Allocate a new spa_t structure. 3623fa9e4066Sahrens */ 3624990b4856Slling (void) nvlist_lookup_string(props, 3625990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3626468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 36278ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3628fa9e4066Sahrens 3629990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3630990b4856Slling spa_deactivate(spa); 3631990b4856Slling spa_remove(spa); 3632c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3633990b4856Slling return (error); 3634990b4856Slling } 3635990b4856Slling 3636ad135b5dSChristopher Siden has_features = B_FALSE; 3637ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3638ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3639ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3640ad135b5dSChristopher Siden has_features = B_TRUE; 3641ad135b5dSChristopher Siden } 3642ad135b5dSChristopher Siden 3643ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3644ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3645990b4856Slling version = SPA_VERSION; 3646ad135b5dSChristopher Siden } 3647ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3648b24ab676SJeff Bonwick 3649b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3650b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3651990b4856Slling spa->spa_uberblock.ub_version = version; 3652fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 36530f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_CREATE; 3654fa9e4066Sahrens 365554d692b7SGeorge Wilson /* 365654d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 365754d692b7SGeorge Wilson */ 36586f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 36596f834bc1SMatthew Ahrens KM_SLEEP); 36606f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) { 36616f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 36626f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 36636f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER); 36646f834bc1SMatthew Ahrens } 366554d692b7SGeorge Wilson 36660373e76bSbonwick /* 36670373e76bSbonwick * Create the root vdev. 36680373e76bSbonwick */ 3669e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 36700373e76bSbonwick 367199653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 36720373e76bSbonwick 367399653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 367499653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 36750373e76bSbonwick 3676b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 3677be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 367899653d4eSeschrock 367999653d4eSeschrock if (error == 0 && 368099653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3681fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 368299653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3683573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3684573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3685573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3686573ca77eSGeorge Wilson } 36870373e76bSbonwick } 36880373e76bSbonwick 3689e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3690fa9e4066Sahrens 369199653d4eSeschrock if (error != 0) { 3692fa9e4066Sahrens spa_unload(spa); 3693fa9e4066Sahrens spa_deactivate(spa); 3694fa9e4066Sahrens spa_remove(spa); 3695fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3696fa9e4066Sahrens return (error); 3697fa9e4066Sahrens } 3698fa9e4066Sahrens 369999653d4eSeschrock /* 370099653d4eSeschrock * Get the list of spares, if specified. 370199653d4eSeschrock */ 370299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 370399653d4eSeschrock &spares, &nspares) == 0) { 3704fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 370599653d4eSeschrock KM_SLEEP) == 0); 3706fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 370799653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3708e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 370999653d4eSeschrock spa_load_spares(spa); 3710e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3711fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3712fa94a07fSbrendan } 3713fa94a07fSbrendan 3714fa94a07fSbrendan /* 3715fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3716fa94a07fSbrendan */ 3717fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3718fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3719fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3720fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3721fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3722fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3723e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3724fa94a07fSbrendan spa_load_l2cache(spa); 3725e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3726fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 372799653d4eSeschrock } 372899653d4eSeschrock 3729ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 37300a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3731fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3732ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3733fa9e4066Sahrens 3734485bbbf5SGeorge Wilson /* 3735485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3736485bbbf5SGeorge Wilson */ 3737485bbbf5SGeorge Wilson ddt_create(spa); 3738485bbbf5SGeorge Wilson 3739485bbbf5SGeorge Wilson spa_update_dspace(spa); 3740485bbbf5SGeorge Wilson 3741fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3742fa9e4066Sahrens 3743fa9e4066Sahrens /* 3744fa9e4066Sahrens * Create the pool config object. 3745fa9e4066Sahrens */ 3746fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3747f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3748fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3749fa9e4066Sahrens 3750ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3751fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3752ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3753ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3754ea8dc4b6Seschrock } 3755fa9e4066Sahrens 3756ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3757ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3758ad135b5dSChristopher Siden 37593f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 37603f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 37613f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 37623f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 37633f9d6ad7SLin Ling } 37643f9d6ad7SLin Ling 3765990b4856Slling /* Newly created pools with the right version are always deflated. */ 3766990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3767990b4856Slling spa->spa_deflate = TRUE; 3768990b4856Slling if (zap_add(spa->spa_meta_objset, 3769990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3770990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3771990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3772990b4856Slling } 377399653d4eSeschrock } 377499653d4eSeschrock 3775fa9e4066Sahrens /* 3776cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3777fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3778fa9e4066Sahrens * keeps changing. 3779fa9e4066Sahrens */ 3780cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3781cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3782cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3783ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3784cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3785cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3786cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3787ea8dc4b6Seschrock } 3788b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3789cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3790fa9e4066Sahrens 379106eeb2adSek /* 379206eeb2adSek * Create the pool's history object. 379306eeb2adSek */ 3794990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3795990b4856Slling spa_history_create_obj(spa, tx); 3796990b4856Slling 379745818ee1SMatthew Ahrens /* 379845818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on. 379945818ee1SMatthew Ahrens */ 380045818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 380145818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes)); 380245818ee1SMatthew Ahrens 3803990b4856Slling /* 3804990b4856Slling * Set pool properties. 3805990b4856Slling */ 3806990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3807990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 38080a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3809573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3810b24ab676SJeff Bonwick 3811379c004dSEric Schrock if (props != NULL) { 3812379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38133b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3814379c004dSEric Schrock } 381506eeb2adSek 3816fa9e4066Sahrens dmu_tx_commit(tx); 3817fa9e4066Sahrens 3818fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3819fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3820fa9e4066Sahrens 3821fa9e4066Sahrens /* 3822fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3823fa9e4066Sahrens * bean counters are appropriately updated. 3824fa9e4066Sahrens */ 3825fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3826fa9e4066Sahrens 3827c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3828*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 3829fa9e4066Sahrens 38304445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3831228975ccSek 3832bc9014e6SJustin Gibbs /* 3833bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed 3834bc9014e6SJustin Gibbs * and are making their way through the eviction process. 3835bc9014e6SJustin Gibbs */ 3836bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 3837088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 38380f7643c7SGeorge Wilson spa->spa_load_state = SPA_LOAD_NONE; 3839088f3894Sahrens 3840daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3841daaa36a7SGeorge Wilson 3842fa9e4066Sahrens return (0); 3843fa9e4066Sahrens } 3844fa9e4066Sahrens 3845e7cbe64fSgw #ifdef _KERNEL 3846e7cbe64fSgw /* 384721ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 384821ecdf64SLin Ling * during the system boot up time. 3849e7cbe64fSgw */ 385021ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 385121ecdf64SLin Ling 385221ecdf64SLin Ling static nvlist_t * 385321ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3854e7cbe64fSgw { 385521ecdf64SLin Ling nvlist_t *config; 3856e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3857e7cbe64fSgw uint64_t pgid; 3858e7cbe64fSgw 385921ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 386021ecdf64SLin Ling return (NULL); 386121ecdf64SLin Ling 3862e7cbe64fSgw /* 3863e7cbe64fSgw * Add this top-level vdev to the child array. 3864e7cbe64fSgw */ 386521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 386621ecdf64SLin Ling &nvtop) == 0); 386721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 386821ecdf64SLin Ling &pgid) == 0); 386921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3870e7cbe64fSgw 3871e7cbe64fSgw /* 3872e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3873e7cbe64fSgw */ 3874e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 387521ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 387621ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3877e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3878e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3879e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3880e7cbe64fSgw &nvtop, 1) == 0); 3881e7cbe64fSgw 3882e7cbe64fSgw /* 3883e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3884e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3885e7cbe64fSgw */ 3886e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3887e7cbe64fSgw nvlist_free(nvroot); 388821ecdf64SLin Ling return (config); 3889e7cbe64fSgw } 3890e7cbe64fSgw 3891e7cbe64fSgw /* 389221ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 389321ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 389421ecdf64SLin Ling * device has a more recent txg. 3895051aabe6Staylor */ 389621ecdf64SLin Ling static void 389721ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3898051aabe6Staylor { 3899573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 390021ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3901051aabe6Staylor 390221ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 390321ecdf64SLin Ling nvlist_t *label; 390421ecdf64SLin Ling uint64_t label_txg; 3905051aabe6Staylor 390621ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 390721ecdf64SLin Ling &label) != 0) 390821ecdf64SLin Ling return; 3909051aabe6Staylor 391021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 391121ecdf64SLin Ling &label_txg) == 0); 3912051aabe6Staylor 391321ecdf64SLin Ling /* 391421ecdf64SLin Ling * Do we have a better boot device? 391521ecdf64SLin Ling */ 391621ecdf64SLin Ling if (label_txg > *txg) { 391721ecdf64SLin Ling *txg = label_txg; 391821ecdf64SLin Ling *avd = vd; 3919051aabe6Staylor } 392021ecdf64SLin Ling nvlist_free(label); 3921051aabe6Staylor } 3922051aabe6Staylor } 3923051aabe6Staylor 3924e7cbe64fSgw /* 3925e7cbe64fSgw * Import a root pool. 3926e7cbe64fSgw * 3927051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3928051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3929051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3930e7cbe64fSgw * 3931e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3932e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3933e7cbe64fSgw * e.g. 3934e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3935e7cbe64fSgw */ 3936e7cbe64fSgw int 3937051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3938e7cbe64fSgw { 393921ecdf64SLin Ling spa_t *spa; 394021ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 394121ecdf64SLin Ling nvlist_t *config, *nvtop; 394221ecdf64SLin Ling uint64_t guid, txg; 3943e7cbe64fSgw char *pname; 3944e7cbe64fSgw int error; 3945e7cbe64fSgw 3946e7cbe64fSgw /* 394721ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3948e7cbe64fSgw */ 3949dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3950dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3951dedec472SJack Meng if (config == NULL) { 3952dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3953dedec472SJack Meng /* iscsi boot */ 3954dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3955dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3956dedec472SJack Meng } 3957dedec472SJack Meng } 3958dedec472SJack Meng #endif 3959dedec472SJack Meng if (config == NULL) { 3960ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 396121ecdf64SLin Ling devpath); 3962be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 396321ecdf64SLin Ling } 3964e7cbe64fSgw 396521ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 396621ecdf64SLin Ling &pname) == 0); 396721ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3968e7cbe64fSgw 39696809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 39706809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 39716809eb4eSEric Schrock /* 39726809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 39736809eb4eSEric Schrock * can replace it with the correct config we just read in. 39746809eb4eSEric Schrock */ 39756809eb4eSEric Schrock spa_remove(spa); 39766809eb4eSEric Schrock } 39776809eb4eSEric Schrock 3978468c413aSTim Haley spa = spa_add(pname, config, NULL); 39796809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 39804b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3981e7cbe64fSgw 398221ecdf64SLin Ling /* 398321ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 398421ecdf64SLin Ling */ 398521ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 398621ecdf64SLin Ling &nvtop) == 0); 398721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 398821ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 398921ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 399021ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 399121ecdf64SLin Ling if (error) { 399221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 399321ecdf64SLin Ling nvlist_free(config); 399421ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 399521ecdf64SLin Ling pname); 399621ecdf64SLin Ling return (error); 399721ecdf64SLin Ling } 399821ecdf64SLin Ling 399921ecdf64SLin Ling /* 400021ecdf64SLin Ling * Get the boot vdev. 400121ecdf64SLin Ling */ 400221ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 400321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 400421ecdf64SLin Ling (u_longlong_t)guid); 4005be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 400621ecdf64SLin Ling goto out; 400721ecdf64SLin Ling } 4008e7cbe64fSgw 400921ecdf64SLin Ling /* 401021ecdf64SLin Ling * Determine if there is a better boot device. 401121ecdf64SLin Ling */ 401221ecdf64SLin Ling avd = bvd; 401321ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 401421ecdf64SLin Ling if (avd != bvd) { 401521ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 401621ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 4017be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 401821ecdf64SLin Ling goto out; 401921ecdf64SLin Ling } 4020e7cbe64fSgw 402121ecdf64SLin Ling /* 402221ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 402321ecdf64SLin Ling * we're booting off the active spare. 402421ecdf64SLin Ling */ 402521ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 402621ecdf64SLin Ling !bvd->vdev_isspare) { 402721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 402821ecdf64SLin Ling "try booting from '%s'", 4029cb04b873SMark J Musante bvd->vdev_parent-> 4030cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 4031be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 403221ecdf64SLin Ling goto out; 403321ecdf64SLin Ling } 403421ecdf64SLin Ling 403521ecdf64SLin Ling error = 0; 403621ecdf64SLin Ling out: 403721ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 403821ecdf64SLin Ling vdev_free(rvd); 403921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 404021ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 404121ecdf64SLin Ling 404221ecdf64SLin Ling nvlist_free(config); 4043e7cbe64fSgw return (error); 4044e7cbe64fSgw } 404521ecdf64SLin Ling 4046e7cbe64fSgw #endif 4047e7cbe64fSgw 40486809eb4eSEric Schrock /* 40496809eb4eSEric Schrock * Import a non-root pool into the system. 40506809eb4eSEric Schrock */ 4051c5904d13Seschrock int 40524b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 4053c5904d13Seschrock { 40546809eb4eSEric Schrock spa_t *spa; 40556809eb4eSEric Schrock char *altroot = NULL; 4056468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 4057468c413aSTim Haley zpool_rewind_policy_t policy; 4058f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 4059f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 40606809eb4eSEric Schrock int error; 40616809eb4eSEric Schrock nvlist_t *nvroot; 40626809eb4eSEric Schrock nvlist_t **spares, **l2cache; 40636809eb4eSEric Schrock uint_t nspares, nl2cache; 40646809eb4eSEric Schrock 40656809eb4eSEric Schrock /* 40666809eb4eSEric Schrock * If a pool with this name exists, return failure. 40676809eb4eSEric Schrock */ 40686809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 40691195e687SMark J Musante if (spa_lookup(pool) != NULL) { 40706809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 4071be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 40726809eb4eSEric Schrock } 40736809eb4eSEric Schrock 40746809eb4eSEric Schrock /* 40756809eb4eSEric Schrock * Create and initialize the spa structure. 40766809eb4eSEric Schrock */ 40776809eb4eSEric Schrock (void) nvlist_lookup_string(props, 40786809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 4079f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 4080f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 4081f9af39baSGeorge Wilson if (readonly) 4082f9af39baSGeorge Wilson mode = FREAD; 4083468c413aSTim Haley spa = spa_add(pool, config, altroot); 40844b964adaSGeorge Wilson spa->spa_import_flags = flags; 40854b964adaSGeorge Wilson 40864b964adaSGeorge Wilson /* 40874b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 40884b964adaSGeorge Wilson * as if it had been loaded at boot. 40894b964adaSGeorge Wilson */ 40904b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 40914b964adaSGeorge Wilson if (props != NULL) 40924b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 40934b964adaSGeorge Wilson 40944b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 4095*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 40964b964adaSGeorge Wilson 40974b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 40984b964adaSGeorge Wilson return (0); 40994b964adaSGeorge Wilson } 41004b964adaSGeorge Wilson 4101f9af39baSGeorge Wilson spa_activate(spa, mode); 41026809eb4eSEric Schrock 410325f89ee2SJeff Bonwick /* 410425f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 410525f89ee2SJeff Bonwick */ 410625f89ee2SJeff Bonwick spa_async_suspend(spa); 410725f89ee2SJeff Bonwick 41084b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 41094b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 41104b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 41114b964adaSGeorge Wilson 41126809eb4eSEric Schrock /* 41136809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 41146809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 41156809eb4eSEric Schrock * doing an import. 41166809eb4eSEric Schrock */ 4117468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 4118468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 41194b964adaSGeorge Wilson 4120468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 4121c8ee1847SVictor Latushkin policy.zrp_request); 4122468c413aSTim Haley 4123468c413aSTim Haley /* 41244b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 41254b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 4126468c413aSTim Haley */ 41274b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 41284b964adaSGeorge Wilson spa->spa_load_info) == 0); 41296809eb4eSEric Schrock 41306809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41316809eb4eSEric Schrock /* 41326809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 41336809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 41346809eb4eSEric Schrock */ 41356809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 41366809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 41376809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 41386809eb4eSEric Schrock spa_load_spares(spa); 41396809eb4eSEric Schrock } 41406809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 41416809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 41426809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 41436809eb4eSEric Schrock spa_load_l2cache(spa); 41446809eb4eSEric Schrock } 41456809eb4eSEric Schrock 41466809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 41476809eb4eSEric Schrock &nvroot) == 0); 41486809eb4eSEric Schrock if (error == 0) 41496809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41506809eb4eSEric Schrock VDEV_ALLOC_SPARE); 41516809eb4eSEric Schrock if (error == 0) 41526809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 41536809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 41546809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41556809eb4eSEric Schrock 41566809eb4eSEric Schrock if (props != NULL) 41576809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 41586809eb4eSEric Schrock 41596809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 41606809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 41616809eb4eSEric Schrock spa_unload(spa); 41626809eb4eSEric Schrock spa_deactivate(spa); 41636809eb4eSEric Schrock spa_remove(spa); 41646809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 41656809eb4eSEric Schrock return (error); 41666809eb4eSEric Schrock } 41676809eb4eSEric Schrock 4168955ef359SLin Ling spa_async_resume(spa); 4169955ef359SLin Ling 41706809eb4eSEric Schrock /* 41716809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 41726809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 41736809eb4eSEric Schrock */ 41746809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 41756809eb4eSEric Schrock &spares, &nspares) == 0) { 41766809eb4eSEric Schrock if (spa->spa_spares.sav_config) 41776809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 41786809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 41796809eb4eSEric Schrock else 41806809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 41816809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41826809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 41836809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 41846809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 41856809eb4eSEric Schrock spa_load_spares(spa); 41866809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 41876809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 41886809eb4eSEric Schrock } 41896809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 41906809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 41916809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 41926809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 41936809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 41946809eb4eSEric Schrock else 41956809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 41966809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 41976809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 41986809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 41996809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 42006809eb4eSEric Schrock spa_load_l2cache(spa); 42016809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 42026809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 42036809eb4eSEric Schrock } 42046809eb4eSEric Schrock 4205b693757aSEric Schrock /* 4206b693757aSEric Schrock * Check for any removed devices. 4207b693757aSEric Schrock */ 4208b693757aSEric Schrock if (spa->spa_autoreplace) { 4209b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 4210b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 4211b693757aSEric Schrock } 4212b693757aSEric Schrock 42136809eb4eSEric Schrock if (spa_writeable(spa)) { 42146809eb4eSEric Schrock /* 42156809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 42166809eb4eSEric Schrock */ 4217bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 42186809eb4eSEric Schrock } 42196809eb4eSEric Schrock 4220573ca77eSGeorge Wilson /* 4221573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 4222573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 4223573ca77eSGeorge Wilson */ 4224573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4225573ca77eSGeorge Wilson 42264445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 42276809eb4eSEric Schrock 4228*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 422914372834SHans Rosenfeld 423014372834SHans Rosenfeld mutex_exit(&spa_namespace_lock); 423114372834SHans Rosenfeld 42326809eb4eSEric Schrock return (0); 4233c5904d13Seschrock } 4234c5904d13Seschrock 4235fa9e4066Sahrens nvlist_t * 4236fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 4237fa9e4066Sahrens { 4238fa9e4066Sahrens nvlist_t *config = NULL; 4239fa9e4066Sahrens char *poolname; 4240fa9e4066Sahrens spa_t *spa; 4241fa9e4066Sahrens uint64_t state; 42427b7154beSLin Ling int error; 4243fa9e4066Sahrens 4244fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 4245fa9e4066Sahrens return (NULL); 4246fa9e4066Sahrens 4247fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 4248fa9e4066Sahrens return (NULL); 4249fa9e4066Sahrens 4250fa9e4066Sahrens /* 42510373e76bSbonwick * Create and initialize the spa structure. 4252fa9e4066Sahrens */ 42530373e76bSbonwick mutex_enter(&spa_namespace_lock); 4254468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 42558ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 4256fa9e4066Sahrens 4257fa9e4066Sahrens /* 42580373e76bSbonwick * Pass off the heavy lifting to spa_load(). 4259ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 4260ecc2d604Sbonwick * is actually the one to trust when doing an import. 4261fa9e4066Sahrens */ 42621195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 4263fa9e4066Sahrens 4264fa9e4066Sahrens /* 4265fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 4266fa9e4066Sahrens */ 4267fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 4268fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4269fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4270fa9e4066Sahrens poolname) == 0); 4271fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4272fa9e4066Sahrens state) == 0); 427395173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 427495173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4275ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4276ad135b5dSChristopher Siden spa->spa_load_info) == 0); 427799653d4eSeschrock 4278e7cbe64fSgw /* 4279e7cbe64fSgw * If the bootfs property exists on this pool then we 4280e7cbe64fSgw * copy it out so that external consumers can tell which 4281e7cbe64fSgw * pools are bootable. 4282e7cbe64fSgw */ 42837b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4284e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4285e7cbe64fSgw 4286e7cbe64fSgw /* 4287e7cbe64fSgw * We have to play games with the name since the 4288e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4289e7cbe64fSgw */ 4290e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4291e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4292e7cbe64fSgw char *cp; 4293e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4294e7cbe64fSgw 4295e7cbe64fSgw cp = strchr(tmpname, '/'); 4296e7cbe64fSgw if (cp == NULL) { 4297e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4298e7cbe64fSgw MAXPATHLEN); 4299e7cbe64fSgw } else { 4300e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4301e7cbe64fSgw "%s/%s", poolname, ++cp); 4302e7cbe64fSgw } 4303e7cbe64fSgw VERIFY(nvlist_add_string(config, 4304e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4305e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4306e7cbe64fSgw } 4307e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4308e7cbe64fSgw } 4309e7cbe64fSgw 431099653d4eSeschrock /* 4311fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 431299653d4eSeschrock */ 43136809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 431499653d4eSeschrock spa_add_spares(spa, config); 4315fa94a07fSbrendan spa_add_l2cache(spa, config); 43166809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4317fa9e4066Sahrens } 4318fa9e4066Sahrens 4319fa9e4066Sahrens spa_unload(spa); 4320fa9e4066Sahrens spa_deactivate(spa); 4321fa9e4066Sahrens spa_remove(spa); 4322fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4323fa9e4066Sahrens 4324fa9e4066Sahrens return (config); 4325fa9e4066Sahrens } 4326fa9e4066Sahrens 4327fa9e4066Sahrens /* 4328fa9e4066Sahrens * Pool export/destroy 4329fa9e4066Sahrens * 4330fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4331fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4332fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4333394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4334394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4335fa9e4066Sahrens */ 4336fa9e4066Sahrens static int 433789a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4338394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4339fa9e4066Sahrens { 4340fa9e4066Sahrens spa_t *spa; 4341fa9e4066Sahrens 434244cd46caSbillm if (oldconfig) 434344cd46caSbillm *oldconfig = NULL; 434444cd46caSbillm 43458ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4346be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 4347fa9e4066Sahrens 4348fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4349fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4350fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4351be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 4352fa9e4066Sahrens } 4353fa9e4066Sahrens 4354ea8dc4b6Seschrock /* 4355ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4356ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4357ea8dc4b6Seschrock */ 4358ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4359ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4360ea8dc4b6Seschrock spa_async_suspend(spa); 4361ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4362ea8dc4b6Seschrock spa_close(spa, FTAG); 4363ea8dc4b6Seschrock 4364fa9e4066Sahrens /* 4365fa9e4066Sahrens * The pool will be in core if it's openable, 4366fa9e4066Sahrens * in which case we can modify its state. 4367fa9e4066Sahrens */ 4368fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4369fa9e4066Sahrens /* 4370fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4371fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4372fa9e4066Sahrens */ 4373fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4374bc9014e6SJustin Gibbs spa_evicting_os_wait(spa); 4375fa9e4066Sahrens 4376ea8dc4b6Seschrock /* 4377ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4378ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4379ea8dc4b6Seschrock * fault injection handlers. 4380ea8dc4b6Seschrock */ 4381ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4382ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4383ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4384ea8dc4b6Seschrock spa_async_resume(spa); 4385fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4386be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 4387fa9e4066Sahrens } 4388fa9e4066Sahrens 438989a89ebfSlling /* 439089a89ebfSlling * A pool cannot be exported if it has an active shared spare. 439189a89ebfSlling * This is to prevent other pools stealing the active spare 439289a89ebfSlling * from an exported pool. At user's own will, such pool can 439389a89ebfSlling * be forcedly exported. 439489a89ebfSlling */ 439589a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 439689a89ebfSlling spa_has_active_shared_spare(spa)) { 439789a89ebfSlling spa_async_resume(spa); 439889a89ebfSlling mutex_exit(&spa_namespace_lock); 4399be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 440089a89ebfSlling } 440189a89ebfSlling 4402fa9e4066Sahrens /* 4403fa9e4066Sahrens * We want this to be reflected on every label, 4404fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4405fa9e4066Sahrens * final sync that pushes these changes out. 4406fa9e4066Sahrens */ 4407394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4408e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4409ea8dc4b6Seschrock spa->spa_state = new_state; 44103f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 44113f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4412ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4413e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4414ea8dc4b6Seschrock } 4415fa9e4066Sahrens } 4416fa9e4066Sahrens 4417*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 44183d7072f8Seschrock 4419fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4420fa9e4066Sahrens spa_unload(spa); 4421fa9e4066Sahrens spa_deactivate(spa); 4422fa9e4066Sahrens } 4423fa9e4066Sahrens 442444cd46caSbillm if (oldconfig && spa->spa_config) 442544cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 442644cd46caSbillm 4427ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4428394ab0cbSGeorge Wilson if (!hardforce) 4429394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4430ea8dc4b6Seschrock spa_remove(spa); 4431ea8dc4b6Seschrock } 4432fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4433fa9e4066Sahrens 4434fa9e4066Sahrens return (0); 4435fa9e4066Sahrens } 4436fa9e4066Sahrens 4437fa9e4066Sahrens /* 4438fa9e4066Sahrens * Destroy a storage pool. 4439fa9e4066Sahrens */ 4440fa9e4066Sahrens int 4441fa9e4066Sahrens spa_destroy(char *pool) 4442fa9e4066Sahrens { 4443394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4444394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4445fa9e4066Sahrens } 4446fa9e4066Sahrens 4447fa9e4066Sahrens /* 4448fa9e4066Sahrens * Export a storage pool. 4449fa9e4066Sahrens */ 4450fa9e4066Sahrens int 4451394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4452394ab0cbSGeorge Wilson boolean_t hardforce) 4453fa9e4066Sahrens { 4454394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4455394ab0cbSGeorge Wilson force, hardforce)); 4456fa9e4066Sahrens } 4457fa9e4066Sahrens 4458ea8dc4b6Seschrock /* 4459ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4460ea8dc4b6Seschrock * from the namespace in any way. 4461ea8dc4b6Seschrock */ 4462ea8dc4b6Seschrock int 4463ea8dc4b6Seschrock spa_reset(char *pool) 4464ea8dc4b6Seschrock { 446589a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4466394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4467ea8dc4b6Seschrock } 4468ea8dc4b6Seschrock 4469fa9e4066Sahrens /* 4470fa9e4066Sahrens * ========================================================================== 4471fa9e4066Sahrens * Device manipulation 4472fa9e4066Sahrens * ========================================================================== 4473fa9e4066Sahrens */ 4474fa9e4066Sahrens 4475fa9e4066Sahrens /* 44768654d025Sperrin * Add a device to a storage pool. 4477fa9e4066Sahrens */ 4478fa9e4066Sahrens int 4479fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4480fa9e4066Sahrens { 448188ecc943SGeorge Wilson uint64_t txg, id; 44828ad4d6ddSJeff Bonwick int error; 4483fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 44840e34b6a7Sbonwick vdev_t *vd, *tvd; 4485fa94a07fSbrendan nvlist_t **spares, **l2cache; 4486fa94a07fSbrendan uint_t nspares, nl2cache; 4487fa9e4066Sahrens 4488f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4489f9af39baSGeorge Wilson 4490fa9e4066Sahrens txg = spa_vdev_enter(spa); 4491fa9e4066Sahrens 449299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 449399653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 449499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4495fa9e4066Sahrens 4496e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 449799653d4eSeschrock 4498fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4499fa94a07fSbrendan &nspares) != 0) 450099653d4eSeschrock nspares = 0; 450199653d4eSeschrock 4502fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4503fa94a07fSbrendan &nl2cache) != 0) 4504fa94a07fSbrendan nl2cache = 0; 4505fa94a07fSbrendan 4506e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4507fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4508fa9e4066Sahrens 4509e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4510e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4511e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 451299653d4eSeschrock 451339c23413Seschrock /* 4514fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4515fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 451639c23413Seschrock */ 4517e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 451839c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 451939c23413Seschrock 452039c23413Seschrock /* 452139c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 452239c23413Seschrock */ 45238ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 452488ecc943SGeorge Wilson 452588ecc943SGeorge Wilson /* 452688ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 452788ecc943SGeorge Wilson */ 452888ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 452988ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 453088ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 453188ecc943SGeorge Wilson break; 453288ecc943SGeorge Wilson } 453388ecc943SGeorge Wilson } 453439c23413Seschrock tvd = vd->vdev_child[c]; 453539c23413Seschrock vdev_remove_child(vd, tvd); 453688ecc943SGeorge Wilson tvd->vdev_id = id; 453739c23413Seschrock vdev_add_child(rvd, tvd); 453839c23413Seschrock vdev_config_dirty(tvd); 453939c23413Seschrock } 454039c23413Seschrock 454199653d4eSeschrock if (nspares != 0) { 4542fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4543fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 454499653d4eSeschrock spa_load_spares(spa); 4545fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4546fa94a07fSbrendan } 4547fa94a07fSbrendan 4548fa94a07fSbrendan if (nl2cache != 0) { 4549fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4550fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4551fa94a07fSbrendan spa_load_l2cache(spa); 4552fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4553fa9e4066Sahrens } 4554fa9e4066Sahrens 4555fa9e4066Sahrens /* 45560e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 45570e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 45580e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 45590e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 45600e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 45610e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 45620373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 45630e34b6a7Sbonwick * 45640e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 45650e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 45660e34b6a7Sbonwick * steps will be completed the next time we load the pool. 45670e34b6a7Sbonwick */ 45680373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 45690e34b6a7Sbonwick 45700373e76bSbonwick mutex_enter(&spa_namespace_lock); 45710373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 4572*ce1577b0SDave Eddy spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 45730373e76bSbonwick mutex_exit(&spa_namespace_lock); 4574fa9e4066Sahrens 45750373e76bSbonwick return (0); 4576fa9e4066Sahrens } 4577fa9e4066Sahrens 4578fa9e4066Sahrens /* 4579fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4580fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4581fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4582fa9e4066Sahrens * 4583fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4584fa9e4066Sahrens * existing device; in this case the two devices are made into their own 45853d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4586fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4587fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4588fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4589fa9e4066Sahrens * is automatically detached. 4590fa9e4066Sahrens */ 4591fa9e4066Sahrens int 4592ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4593fa9e4066Sahrens { 45943f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4595fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4596fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 459799653d4eSeschrock vdev_ops_t *pvops; 45989b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 45999b3f6b42SEric Kustarz int newvd_isspare; 46009b3f6b42SEric Kustarz int error; 4601fa9e4066Sahrens 4602f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4603f9af39baSGeorge Wilson 4604fa9e4066Sahrens txg = spa_vdev_enter(spa); 4605fa9e4066Sahrens 4606c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4607fa9e4066Sahrens 4608fa9e4066Sahrens if (oldvd == NULL) 4609fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4610fa9e4066Sahrens 46110e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 46120e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 46130e34b6a7Sbonwick 4614fa9e4066Sahrens pvd = oldvd->vdev_parent; 4615fa9e4066Sahrens 461699653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4617cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 46183d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 46193d7072f8Seschrock 46203d7072f8Seschrock if (newrootvd->vdev_children != 1) 4621fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4622fa9e4066Sahrens 4623fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4624fa9e4066Sahrens 4625fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4626fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4627fa9e4066Sahrens 462899653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4629fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4630fa9e4066Sahrens 46318654d025Sperrin /* 46328654d025Sperrin * Spares can't replace logs 46338654d025Sperrin */ 4634ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 46358654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 46368654d025Sperrin 463799653d4eSeschrock if (!replacing) { 463899653d4eSeschrock /* 463999653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 464099653d4eSeschrock * vdev. 464199653d4eSeschrock */ 464299653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 464399653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 464499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 464599653d4eSeschrock 464699653d4eSeschrock pvops = &vdev_mirror_ops; 464799653d4eSeschrock } else { 464899653d4eSeschrock /* 464999653d4eSeschrock * Active hot spares can only be replaced by inactive hot 465099653d4eSeschrock * spares. 465199653d4eSeschrock */ 465299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4653cb04b873SMark J Musante oldvd->vdev_isspare && 465499653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 465599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 465699653d4eSeschrock 465799653d4eSeschrock /* 465899653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 465999653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 466039c23413Seschrock * want to create a replacing vdev. The user is not allowed to 466139c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 466239c23413Seschrock * the same (spare replaces spare, non-spare replaces 466339c23413Seschrock * non-spare). 466499653d4eSeschrock */ 4665cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4666cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 466799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4668cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4669cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 467039c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4671cb04b873SMark J Musante } 4672cb04b873SMark J Musante 4673cb04b873SMark J Musante if (newvd->vdev_isspare) 467499653d4eSeschrock pvops = &vdev_spare_ops; 467599653d4eSeschrock else 467699653d4eSeschrock pvops = &vdev_replacing_ops; 467799653d4eSeschrock } 467899653d4eSeschrock 46792a79c5feSlling /* 4680573ca77eSGeorge Wilson * Make sure the new device is big enough. 46812a79c5feSlling */ 4682573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4683fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4684fa9e4066Sahrens 4685ecc2d604Sbonwick /* 4686ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4687ecc2d604Sbonwick * than the top-level vdev. 4688ecc2d604Sbonwick */ 4689ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4690fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4691fa9e4066Sahrens 4692fa9e4066Sahrens /* 4693fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4694fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4695fa9e4066Sahrens */ 4696fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4697fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4698fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4699fa9e4066Sahrens KM_SLEEP); 4700fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4701fa9e4066Sahrens newvd->vdev_path, "old"); 4702fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4703fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4704fa9e4066Sahrens oldvd->vdev_devid = NULL; 4705fa9e4066Sahrens } 4706fa9e4066Sahrens } 4707fa9e4066Sahrens 4708cb04b873SMark J Musante /* mark the device being resilvered */ 4709b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg; 4710cb04b873SMark J Musante 4711fa9e4066Sahrens /* 471299653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 471399653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4714fa9e4066Sahrens */ 4715fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4716fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4717fa9e4066Sahrens 4718fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4719fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4720fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4721fa9e4066Sahrens 4722fa9e4066Sahrens /* 4723fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4724fa9e4066Sahrens */ 4725fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4726fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 472788ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4728fa9e4066Sahrens vdev_add_child(pvd, newvd); 4729fa9e4066Sahrens 4730fa9e4066Sahrens tvd = newvd->vdev_top; 4731fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4732fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4733fa9e4066Sahrens 4734fa9e4066Sahrens vdev_config_dirty(tvd); 4735fa9e4066Sahrens 4736fa9e4066Sahrens /* 47373f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 47383f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 47393f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4740fa9e4066Sahrens */ 47413f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4742fa9e4066Sahrens 47433f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 47443f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4745fa9e4066Sahrens 47466809eb4eSEric Schrock if (newvd->vdev_isspare) { 474739c23413Seschrock spa_spare_activate(newvd); 4748*ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 47496809eb4eSEric Schrock } 47506809eb4eSEric Schrock 4751e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4752e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 47539b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4754ea8dc4b6Seschrock 4755fa9e4066Sahrens /* 4756fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4757fa9e4066Sahrens */ 4758ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4759fa9e4066Sahrens 47603f9d6ad7SLin Ling /* 47610713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to 47620713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the 47630713e232SGeorge Wilson * respective datasets. 47643f9d6ad7SLin Ling */ 47653f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 47663f9d6ad7SLin Ling 476714372834SHans Rosenfeld if (spa->spa_bootfs) 4768*ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 476914372834SHans Rosenfeld 4770*ce1577b0SDave Eddy spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 477114372834SHans Rosenfeld 47723f9d6ad7SLin Ling /* 47733f9d6ad7SLin Ling * Commit the config 47743f9d6ad7SLin Ling */ 47753f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4776fa9e4066Sahrens 47774445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 47783f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4779c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4780c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4781c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 47829b3f6b42SEric Kustarz 47839b3f6b42SEric Kustarz spa_strfree(oldvdpath); 47849b3f6b42SEric Kustarz spa_strfree(newvdpath); 47859b3f6b42SEric Kustarz 4786fa9e4066Sahrens return (0); 4787fa9e4066Sahrens } 4788fa9e4066Sahrens 4789fa9e4066Sahrens /* 4790fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4791f7170741SWill Andrews * 4792fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4793fa9e4066Sahrens * is a replacing vdev. 4794fa9e4066Sahrens */ 4795fa9e4066Sahrens int 47968ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4797fa9e4066Sahrens { 4798fa9e4066Sahrens uint64_t txg; 47998ad4d6ddSJeff Bonwick int error; 4800fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4801fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 480299653d4eSeschrock boolean_t unspare = B_FALSE; 4803d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 48041195e687SMark J Musante char *vdpath; 4805fa9e4066Sahrens 4806f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4807f9af39baSGeorge Wilson 4808fa9e4066Sahrens txg = spa_vdev_enter(spa); 4809fa9e4066Sahrens 4810c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4811fa9e4066Sahrens 4812fa9e4066Sahrens if (vd == NULL) 4813fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4814fa9e4066Sahrens 48150e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 48160e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 48170e34b6a7Sbonwick 4818fa9e4066Sahrens pvd = vd->vdev_parent; 4819fa9e4066Sahrens 48208ad4d6ddSJeff Bonwick /* 48218ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 48228ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 48238ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 48248ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 48258ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 48268ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 48278ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 48288ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 48298ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 48308ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 48318ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 48328ad4d6ddSJeff Bonwick */ 48338ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 48348ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 48358ad4d6ddSJeff Bonwick 4836fa9e4066Sahrens /* 4837cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 483899653d4eSeschrock */ 4839cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4840cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4841cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 484299653d4eSeschrock 484399653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4844e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4845fa9e4066Sahrens 4846fa9e4066Sahrens /* 484799653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4848fa9e4066Sahrens */ 4849fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 485099653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 485199653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4852fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4853fa9e4066Sahrens 4854fa9e4066Sahrens /* 48558ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 48568ad4d6ddSJeff Bonwick * we cannot safely detach it. 4857fa9e4066Sahrens */ 48588ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4859fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4860fa9e4066Sahrens 48618ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4862fa9e4066Sahrens 4863bf82a41bSeschrock /* 4864bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4865bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4866bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4867bf82a41bSeschrock */ 4868cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4869cb04b873SMark J Musante vd->vdev_path != NULL) { 4870cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4871cb04b873SMark J Musante 4872cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4873cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4874cb04b873SMark J Musante 4875cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4876cb04b873SMark J Musante continue; 4877cb04b873SMark J Musante 4878cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4879cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4880cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4881cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4882cb04b873SMark J Musante break; 4883cb04b873SMark J Musante } 4884bf82a41bSeschrock } 4885bf82a41bSeschrock } 4886bf82a41bSeschrock 488799653d4eSeschrock /* 488899653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 488999653d4eSeschrock * that the spare should become a real disk, and be removed from the 489099653d4eSeschrock * active spare list for the pool. 489199653d4eSeschrock */ 489299653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4893cb04b873SMark J Musante vd->vdev_id == 0 && 4894cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 489599653d4eSeschrock unspare = B_TRUE; 489699653d4eSeschrock 4897fa9e4066Sahrens /* 4898fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4899fa9e4066Sahrens * This must be done after all other error cases are handled, 4900fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4901fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4902fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4903fa9e4066Sahrens * it's being detached! 4904fa9e4066Sahrens */ 490539c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4906fa9e4066Sahrens 4907fa9e4066Sahrens /* 4908fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4909fa9e4066Sahrens */ 4910fa9e4066Sahrens vdev_remove_child(pvd, vd); 4911fa9e4066Sahrens vdev_compact_children(pvd); 4912fa9e4066Sahrens 4913fa9e4066Sahrens /* 4914fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4915fa9e4066Sahrens */ 4916cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4917fa9e4066Sahrens 491899653d4eSeschrock /* 491999653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 49208ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 49218ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 49228ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 49238ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 49248ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 49258ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 492699653d4eSeschrock */ 492799653d4eSeschrock if (unspare) { 492899653d4eSeschrock ASSERT(cvd->vdev_isspare); 492939c23413Seschrock spa_spare_remove(cvd); 493099653d4eSeschrock unspare_guid = cvd->vdev_guid; 49318ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4932cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 493399653d4eSeschrock } 493499653d4eSeschrock 4935fa9e4066Sahrens /* 4936fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4937fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4938fa9e4066Sahrens */ 4939cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4940cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4941cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4942fa9e4066Sahrens vdev_remove_parent(cvd); 4943cb04b873SMark J Musante } 4944cb04b873SMark J Musante 4945fa9e4066Sahrens 4946fa9e4066Sahrens /* 4947fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4948fa9e4066Sahrens * may have been the previous top-level vdev. 4949fa9e4066Sahrens */ 4950fa9e4066Sahrens tvd = cvd->vdev_top; 4951fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4952fa9e4066Sahrens 4953fa9e4066Sahrens /* 495439c23413Seschrock * Reevaluate the parent vdev state. 4955fa9e4066Sahrens */ 49563d7072f8Seschrock vdev_propagate_state(cvd); 4957fa9e4066Sahrens 4958fa9e4066Sahrens /* 4959573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4960573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4961573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4962573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4963573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4964fa9e4066Sahrens */ 4965573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4966573ca77eSGeorge Wilson vdev_reopen(tvd); 4967573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4968573ca77eSGeorge Wilson } 4969fa9e4066Sahrens 4970fa9e4066Sahrens vdev_config_dirty(tvd); 4971fa9e4066Sahrens 4972fa9e4066Sahrens /* 497339c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 497439c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 497539c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 497639c23413Seschrock * prevent vd from being accessed after it's freed. 4977fa9e4066Sahrens */ 49781195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 49798ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4980fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4981ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4982ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4983fa9e4066Sahrens 4984*ce1577b0SDave Eddy spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 49853d7072f8Seschrock 4986cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4987cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4988cb04b873SMark J Musante 498999653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 499099653d4eSeschrock 49914445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 49921195e687SMark J Musante "vdev=%s", vdpath); 49931195e687SMark J Musante spa_strfree(vdpath); 49941195e687SMark J Musante 499599653d4eSeschrock /* 499639c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 499739c23413Seschrock * then we want to go through and remove the device from the hot spare 499839c23413Seschrock * list of every other pool. 499999653d4eSeschrock */ 500099653d4eSeschrock if (unspare) { 5001cb04b873SMark J Musante spa_t *altspa = NULL; 5002cb04b873SMark J Musante 500399653d4eSeschrock mutex_enter(&spa_namespace_lock); 5004cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 5005cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 5006cb04b873SMark J Musante altspa == spa) 500799653d4eSeschrock continue; 5008cb04b873SMark J Musante 5009cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 50109af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 5011cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 50129af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 5013cb04b873SMark J Musante spa_close(altspa, FTAG); 501499653d4eSeschrock } 501599653d4eSeschrock mutex_exit(&spa_namespace_lock); 5016cb04b873SMark J Musante 5017cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 5018cb04b873SMark J Musante spa_vdev_resilver_done(spa); 501999653d4eSeschrock } 502099653d4eSeschrock 5021cb04b873SMark J Musante /* all done with the spa; OK to release */ 5022cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 5023cb04b873SMark J Musante spa_close(spa, FTAG); 5024cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 5025cb04b873SMark J Musante 502699653d4eSeschrock return (error); 502799653d4eSeschrock } 502899653d4eSeschrock 50291195e687SMark J Musante /* 50301195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 50311195e687SMark J Musante */ 50321195e687SMark J Musante int 50331195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 50341195e687SMark J Musante nvlist_t *props, boolean_t exp) 50351195e687SMark J Musante { 50361195e687SMark J Musante int error = 0; 50371195e687SMark J Musante uint64_t txg, *glist; 50381195e687SMark J Musante spa_t *newspa; 50391195e687SMark J Musante uint_t c, children, lastlog; 50401195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 50411195e687SMark J Musante dmu_tx_t *tx; 50421195e687SMark J Musante char *altroot = NULL; 50431195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 50441195e687SMark J Musante boolean_t activate_slog; 50451195e687SMark J Musante 5046f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 50471195e687SMark J Musante 50481195e687SMark J Musante txg = spa_vdev_enter(spa); 50491195e687SMark J Musante 50501195e687SMark J Musante /* clear the log and flush everything up to now */ 50511195e687SMark J Musante activate_slog = spa_passivate_log(spa); 50521195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 50531195e687SMark J Musante error = spa_offline_log(spa); 50541195e687SMark J Musante txg = spa_vdev_config_enter(spa); 50551195e687SMark J Musante 50561195e687SMark J Musante if (activate_slog) 50571195e687SMark J Musante spa_activate_log(spa); 50581195e687SMark J Musante 50591195e687SMark J Musante if (error != 0) 50601195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 50611195e687SMark J Musante 50621195e687SMark J Musante /* check new spa name before going any further */ 50631195e687SMark J Musante if (spa_lookup(newname) != NULL) 50641195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 50651195e687SMark J Musante 50661195e687SMark J Musante /* 50671195e687SMark J Musante * scan through all the children to ensure they're all mirrors 50681195e687SMark J Musante */ 50691195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 50701195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 50711195e687SMark J Musante &children) != 0) 50721195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50731195e687SMark J Musante 50741195e687SMark J Musante /* first, check to ensure we've got the right child count */ 50751195e687SMark J Musante rvd = spa->spa_root_vdev; 50761195e687SMark J Musante lastlog = 0; 50771195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 50781195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 50791195e687SMark J Musante 50801195e687SMark J Musante /* don't count the holes & logs as children */ 50811195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 50821195e687SMark J Musante if (lastlog == 0) 50831195e687SMark J Musante lastlog = c; 50841195e687SMark J Musante continue; 50851195e687SMark J Musante } 50861195e687SMark J Musante 50871195e687SMark J Musante lastlog = 0; 50881195e687SMark J Musante } 50891195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 50901195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50911195e687SMark J Musante 50921195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 50931195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 50941195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 50951195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 50961195e687SMark J Musante 50971195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 50981195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 50991195e687SMark J Musante 51001195e687SMark J Musante /* then, loop over each vdev and validate it */ 51011195e687SMark J Musante for (c = 0; c < children; c++) { 51021195e687SMark J Musante uint64_t is_hole = 0; 51031195e687SMark J Musante 51041195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 51051195e687SMark J Musante &is_hole); 51061195e687SMark J Musante 51071195e687SMark J Musante if (is_hole != 0) { 51081195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 51091195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 51101195e687SMark J Musante continue; 51111195e687SMark J Musante } else { 5112be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51131195e687SMark J Musante break; 51141195e687SMark J Musante } 51151195e687SMark J Musante } 51161195e687SMark J Musante 51171195e687SMark J Musante /* which disk is going to be split? */ 51181195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 51191195e687SMark J Musante &glist[c]) != 0) { 5120be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51211195e687SMark J Musante break; 51221195e687SMark J Musante } 51231195e687SMark J Musante 51241195e687SMark J Musante /* look it up in the spa */ 51251195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 51261195e687SMark J Musante if (vml[c] == NULL) { 5127be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV); 51281195e687SMark J Musante break; 51291195e687SMark J Musante } 51301195e687SMark J Musante 51311195e687SMark J Musante /* make sure there's nothing stopping the split */ 51321195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 51331195e687SMark J Musante vml[c]->vdev_islog || 51341195e687SMark J Musante vml[c]->vdev_ishole || 51351195e687SMark J Musante vml[c]->vdev_isspare || 51361195e687SMark J Musante vml[c]->vdev_isl2cache || 51371195e687SMark J Musante !vdev_writeable(vml[c]) || 5138d41c4376SMark J Musante vml[c]->vdev_children != 0 || 51391195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 51401195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 5141be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 51421195e687SMark J Musante break; 51431195e687SMark J Musante } 51441195e687SMark J Musante 51451195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 5146be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 51471195e687SMark J Musante break; 51481195e687SMark J Musante } 51491195e687SMark J Musante 51501195e687SMark J Musante /* we need certain info from the top level */ 51511195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 51521195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 51531195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 51541195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 51551195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 51561195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 51571195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 51581195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 5159215198a6SJoe Stein 5160215198a6SJoe Stein /* transfer per-vdev ZAPs */ 5161215198a6SJoe Stein ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 5162215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5163215198a6SJoe Stein ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 5164215198a6SJoe Stein 5165215198a6SJoe Stein ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 5166215198a6SJoe Stein VERIFY0(nvlist_add_uint64(child[c], 5167215198a6SJoe Stein ZPOOL_CONFIG_VDEV_TOP_ZAP, 5168215198a6SJoe Stein vml[c]->vdev_parent->vdev_top_zap)); 51691195e687SMark J Musante } 51701195e687SMark J Musante 51711195e687SMark J Musante if (error != 0) { 51721195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 51731195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51741195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 51751195e687SMark J Musante } 51761195e687SMark J Musante 51771195e687SMark J Musante /* stop writers from using the disks */ 51781195e687SMark J Musante for (c = 0; c < children; c++) { 51791195e687SMark J Musante if (vml[c] != NULL) 51801195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 51811195e687SMark J Musante } 51821195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 51831195e687SMark J Musante 51841195e687SMark J Musante /* 51851195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 51861195e687SMark J Musante * will disappear once the config is regenerated. 51871195e687SMark J Musante */ 51881195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 51891195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 51901195e687SMark J Musante glist, children) == 0); 51911195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 51921195e687SMark J Musante 519398295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 51941195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 51951195e687SMark J Musante nvl) == 0); 519698295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 51971195e687SMark J Musante spa->spa_config_splitting = nvl; 51981195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 51991195e687SMark J Musante 52001195e687SMark J Musante /* configure and create the new pool */ 52011195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 52021195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 52031195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 52041195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 52051195e687SMark J Musante spa_version(spa)) == 0); 52061195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 52071195e687SMark J Musante spa->spa_config_txg) == 0); 52081195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 52091195e687SMark J Musante spa_generate_guid(NULL)) == 0); 5210215198a6SJoe Stein VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 52111195e687SMark J Musante (void) nvlist_lookup_string(props, 52121195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 52131195e687SMark J Musante 5214d41c4376SMark J Musante /* add the new pool to the namespace */ 52151195e687SMark J Musante newspa = spa_add(newname, config, altroot); 5216215198a6SJoe Stein newspa->spa_avz_action = AVZ_ACTION_REBUILD; 52171195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 52181195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 52191195e687SMark J Musante 52201195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 52211195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 52221195e687SMark J Musante 52231195e687SMark J Musante if (zio_injection_enabled) 52241195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 52251195e687SMark J Musante 52261195e687SMark J Musante spa_activate(newspa, spa_mode_global); 52271195e687SMark J Musante spa_async_suspend(newspa); 52281195e687SMark J Musante 52291195e687SMark J Musante /* create the new pool from the disks of the original pool */ 52301195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 52311195e687SMark J Musante if (error) 52321195e687SMark J Musante goto out; 52331195e687SMark J Musante 52341195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 52351195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 52361195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 52371195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 52381195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 52391195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 52401195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 52411195e687SMark J Musante B_TRUE)); 52421195e687SMark J Musante } 52431195e687SMark J Musante 52441195e687SMark J Musante /* set the props */ 52451195e687SMark J Musante if (props != NULL) { 52461195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 52471195e687SMark J Musante error = spa_prop_set(newspa, props); 52481195e687SMark J Musante if (error) 52491195e687SMark J Musante goto out; 52501195e687SMark J Musante } 52511195e687SMark J Musante 52521195e687SMark J Musante /* flush everything */ 52531195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 52541195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 52551195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 52561195e687SMark J Musante 52571195e687SMark J Musante if (zio_injection_enabled) 52581195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 52591195e687SMark J Musante 52601195e687SMark J Musante spa_async_resume(newspa); 52611195e687SMark J Musante 52621195e687SMark J Musante /* finally, update the original pool's config */ 52631195e687SMark J Musante txg = spa_vdev_config_enter(spa); 52641195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 52651195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 52661195e687SMark J Musante if (error != 0) 52671195e687SMark J Musante dmu_tx_abort(tx); 52681195e687SMark J Musante for (c = 0; c < children; c++) { 52691195e687SMark J Musante if (vml[c] != NULL) { 52701195e687SMark J Musante vdev_split(vml[c]); 52711195e687SMark J Musante if (error == 0) 52724445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 52734445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 5274215198a6SJoe Stein 52751195e687SMark J Musante vdev_free(vml[c]); 52761195e687SMark J Musante } 52771195e687SMark J Musante } 5278215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_REBUILD; 52791195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 52801195e687SMark J Musante spa->spa_config_splitting = NULL; 52811195e687SMark J Musante nvlist_free(nvl); 52821195e687SMark J Musante if (error == 0) 52831195e687SMark J Musante dmu_tx_commit(tx); 52841195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 52851195e687SMark J Musante 52861195e687SMark J Musante if (zio_injection_enabled) 52871195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 52881195e687SMark J Musante 52891195e687SMark J Musante /* split is complete; log a history record */ 52904445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 52914445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 52921195e687SMark J Musante 52931195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 52941195e687SMark J Musante 52951195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 52961195e687SMark J Musante if (exp) 52971195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 52981195e687SMark J Musante B_FALSE, B_FALSE); 52991195e687SMark J Musante 53001195e687SMark J Musante return (error); 53011195e687SMark J Musante 53021195e687SMark J Musante out: 53031195e687SMark J Musante spa_unload(newspa); 53041195e687SMark J Musante spa_deactivate(newspa); 53051195e687SMark J Musante spa_remove(newspa); 53061195e687SMark J Musante 53071195e687SMark J Musante txg = spa_vdev_config_enter(spa); 530898295d61SMark J Musante 530998295d61SMark J Musante /* re-online all offlined disks */ 531098295d61SMark J Musante for (c = 0; c < children; c++) { 531198295d61SMark J Musante if (vml[c] != NULL) 531298295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 531398295d61SMark J Musante } 531498295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 531598295d61SMark J Musante 53161195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 53171195e687SMark J Musante spa->spa_config_splitting = NULL; 5318d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 53191195e687SMark J Musante 53201195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 53211195e687SMark J Musante return (error); 53221195e687SMark J Musante } 53231195e687SMark J Musante 5324e14bb325SJeff Bonwick static nvlist_t * 5325e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 532699653d4eSeschrock { 5327e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5328e14bb325SJeff Bonwick uint64_t guid; 532999653d4eSeschrock 5330e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5331e14bb325SJeff Bonwick &guid) == 0); 533299653d4eSeschrock 5333e14bb325SJeff Bonwick if (guid == target_guid) 5334e14bb325SJeff Bonwick return (nvpp[i]); 533599653d4eSeschrock } 533699653d4eSeschrock 5337e14bb325SJeff Bonwick return (NULL); 5338fa94a07fSbrendan } 5339fa94a07fSbrendan 5340e14bb325SJeff Bonwick static void 5341e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 53420f7643c7SGeorge Wilson nvlist_t *dev_to_remove) 5343fa94a07fSbrendan { 5344e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5345fa94a07fSbrendan 5346e14bb325SJeff Bonwick if (count > 1) 5347e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5348fa94a07fSbrendan 5349e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5350e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5351e14bb325SJeff Bonwick continue; 5352e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5353fa94a07fSbrendan } 5354fa94a07fSbrendan 5355e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5356e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5357fa94a07fSbrendan 5358e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5359e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5360fa94a07fSbrendan 5361e14bb325SJeff Bonwick if (count > 1) 5362e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5363fa94a07fSbrendan } 5364fa94a07fSbrendan 536588ecc943SGeorge Wilson /* 536688ecc943SGeorge Wilson * Evacuate the device. 536788ecc943SGeorge Wilson */ 53683f9d6ad7SLin Ling static int 536988ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 537088ecc943SGeorge Wilson { 537188ecc943SGeorge Wilson uint64_t txg; 53723f9d6ad7SLin Ling int error = 0; 537388ecc943SGeorge Wilson 537488ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 537588ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5376b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 537788ecc943SGeorge Wilson 537888ecc943SGeorge Wilson /* 537988ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 538088ecc943SGeorge Wilson * since we need to do I/O but we do keep the 538188ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 538288ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 538388ecc943SGeorge Wilson */ 538488ecc943SGeorge Wilson if (vd->vdev_islog) { 53853f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 53863f9d6ad7SLin Ling error = spa_offline_log(spa); 5387a1521560SJeff Bonwick } else { 5388be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 538988ecc943SGeorge Wilson } 539088ecc943SGeorge Wilson 5391a1521560SJeff Bonwick if (error) 5392a1521560SJeff Bonwick return (error); 5393a1521560SJeff Bonwick 539488ecc943SGeorge Wilson /* 5395a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5396a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 539788ecc943SGeorge Wilson */ 5398fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 539988ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 540088ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 54010713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg); 540288ecc943SGeorge Wilson vdev_config_dirty(vd); 540388ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 540488ecc943SGeorge Wilson 540588ecc943SGeorge Wilson return (0); 540688ecc943SGeorge Wilson } 540788ecc943SGeorge Wilson 540888ecc943SGeorge Wilson /* 540988ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 541088ecc943SGeorge Wilson */ 54113f9d6ad7SLin Ling static void 5412a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 541388ecc943SGeorge Wilson { 541488ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 541588ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 541688ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 541788ecc943SGeorge Wilson 541888ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 541988ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5420b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 542188ecc943SGeorge Wilson 54223f9d6ad7SLin Ling /* 54233f9d6ad7SLin Ling * Only remove any devices which are empty. 54243f9d6ad7SLin Ling */ 54253f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 54263f9d6ad7SLin Ling return; 54273f9d6ad7SLin Ling 542888ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5429b24ab676SJeff Bonwick 5430b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5431b24ab676SJeff Bonwick vdev_state_clean(vd); 5432b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5433b24ab676SJeff Bonwick vdev_config_clean(vd); 5434b24ab676SJeff Bonwick 543588ecc943SGeorge Wilson vdev_free(vd); 543688ecc943SGeorge Wilson 543788ecc943SGeorge Wilson if (last_vdev) { 543888ecc943SGeorge Wilson vdev_compact_children(rvd); 543988ecc943SGeorge Wilson } else { 544088ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 544188ecc943SGeorge Wilson vdev_add_child(rvd, vd); 544288ecc943SGeorge Wilson } 5443fcbfa62bSLin Ling vdev_config_dirty(rvd); 5444fcbfa62bSLin Ling 5445fcbfa62bSLin Ling /* 5446fcbfa62bSLin Ling * Reassess the health of our root vdev. 5447fcbfa62bSLin Ling */ 5448fcbfa62bSLin Ling vdev_reopen(rvd); 544988ecc943SGeorge Wilson } 545088ecc943SGeorge Wilson 54513f9d6ad7SLin Ling /* 54523f9d6ad7SLin Ling * Remove a device from the pool - 54533f9d6ad7SLin Ling * 54543f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 54553f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 54563f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 54573f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 54583f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 5459f7170741SWill Andrews * 5460f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC 5461f7170741SWill Andrews * devices. 5462fa94a07fSbrendan */ 5463fa94a07fSbrendan int 5464fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5465fa94a07fSbrendan { 5466fa94a07fSbrendan vdev_t *vd; 5467b72b6bb1SAlan Somers sysevent_t *ev = NULL; 5468a1521560SJeff Bonwick metaslab_group_t *mg; 5469e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 54708ad4d6ddSJeff Bonwick uint64_t txg = 0; 547188ecc943SGeorge Wilson uint_t nspares, nl2cache; 5472fa94a07fSbrendan int error = 0; 54738ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5474fa94a07fSbrendan 5475f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5476f9af39baSGeorge Wilson 54778ad4d6ddSJeff Bonwick if (!locked) 54788ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5479fa94a07fSbrendan 5480c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5481fa94a07fSbrendan 5482fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5483fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5484e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5485e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5486e14bb325SJeff Bonwick /* 5487e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5488e14bb325SJeff Bonwick * in this pool. 5489e14bb325SJeff Bonwick */ 5490e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5491b72b6bb1SAlan Somers if (vd == NULL) 5492b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5493*ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, 5494*ce1577b0SDave Eddy ESC_ZFS_VDEV_REMOVE_AUX); 5495e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5496e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5497e14bb325SJeff Bonwick spa_load_spares(spa); 5498e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5499e14bb325SJeff Bonwick } else { 5500be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY); 5501e14bb325SJeff Bonwick } 5502e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5503fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5504e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5505e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5506e14bb325SJeff Bonwick /* 5507e14bb325SJeff Bonwick * Cache devices can always be removed. 5508e14bb325SJeff Bonwick */ 5509b72b6bb1SAlan Somers vd = spa_lookup_by_guid(spa, guid, B_TRUE); 5510*ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX); 5511e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5512e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5513fa94a07fSbrendan spa_load_l2cache(spa); 5514fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 551588ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 551688ecc943SGeorge Wilson ASSERT(!locked); 5517b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 551888ecc943SGeorge Wilson 5519a1521560SJeff Bonwick mg = vd->vdev_mg; 5520a1521560SJeff Bonwick 552188ecc943SGeorge Wilson /* 5522a1521560SJeff Bonwick * Stop allocating from this vdev. 552388ecc943SGeorge Wilson */ 5524a1521560SJeff Bonwick metaslab_group_passivate(mg); 552588ecc943SGeorge Wilson 5526b24ab676SJeff Bonwick /* 5527b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5528b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5529b24ab676SJeff Bonwick */ 5530b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5531b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5532b24ab676SJeff Bonwick 5533a1521560SJeff Bonwick /* 5534a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5535a1521560SJeff Bonwick */ 5536a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5537a1521560SJeff Bonwick 553888ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 553988ecc943SGeorge Wilson 5540a1521560SJeff Bonwick /* 5541a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5542a1521560SJeff Bonwick */ 5543a1521560SJeff Bonwick if (error) { 5544a1521560SJeff Bonwick metaslab_group_activate(mg); 5545a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5546a1521560SJeff Bonwick } 5547a1521560SJeff Bonwick 5548a1521560SJeff Bonwick /* 5549a1521560SJeff Bonwick * Clean up the vdev namespace. 5550a1521560SJeff Bonwick */ 5551*ce1577b0SDave Eddy ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_DEV); 5552a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 555388ecc943SGeorge Wilson 5554e14bb325SJeff Bonwick } else if (vd != NULL) { 5555e14bb325SJeff Bonwick /* 5556e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5557e14bb325SJeff Bonwick */ 5558be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 5559e14bb325SJeff Bonwick } else { 5560e14bb325SJeff Bonwick /* 5561e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5562e14bb325SJeff Bonwick */ 5563be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT); 5564fa94a07fSbrendan } 556599653d4eSeschrock 55668ad4d6ddSJeff Bonwick if (!locked) 556763364b0eSAlan Somers error = spa_vdev_exit(spa, NULL, txg, error); 55688ad4d6ddSJeff Bonwick 5569b72b6bb1SAlan Somers if (ev) 5570b72b6bb1SAlan Somers spa_event_post(ev); 5571b72b6bb1SAlan Somers 55728ad4d6ddSJeff Bonwick return (error); 5573fa9e4066Sahrens } 5574fa9e4066Sahrens 5575fa9e4066Sahrens /* 55763d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 5577f7170741SWill Andrews * currently spared, so we can detach it. 5578fa9e4066Sahrens */ 5579ea8dc4b6Seschrock static vdev_t * 55803d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5581fa9e4066Sahrens { 5582ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5583fa9e4066Sahrens 5584573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 55853d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5586ea8dc4b6Seschrock if (oldvd != NULL) 5587ea8dc4b6Seschrock return (oldvd); 5588ea8dc4b6Seschrock } 5589fa9e4066Sahrens 55903d7072f8Seschrock /* 5591cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5592cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5593cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5594cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5595cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5596cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 55973d7072f8Seschrock */ 5598cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5599cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5600cb04b873SMark J Musante 5601cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5602ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5603ea8dc4b6Seschrock 56048ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5605e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 56068ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5607ea8dc4b6Seschrock return (oldvd); 5608fa9e4066Sahrens } 5609ea8dc4b6Seschrock 56103d7072f8Seschrock /* 56113d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 56123d7072f8Seschrock */ 5613cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5614cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5615cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5616cb04b873SMark J Musante 5617cb04b873SMark J Musante if (last->vdev_unspare) { 5618cb04b873SMark J Musante oldvd = first; 5619cb04b873SMark J Musante newvd = last; 5620cb04b873SMark J Musante } else if (first->vdev_unspare) { 5621cb04b873SMark J Musante oldvd = last; 5622cb04b873SMark J Musante newvd = first; 5623cb04b873SMark J Musante } else { 5624cb04b873SMark J Musante oldvd = NULL; 5625cb04b873SMark J Musante } 56263d7072f8Seschrock 5627cb04b873SMark J Musante if (oldvd != NULL && 56288ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5629e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5630cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 56313d7072f8Seschrock return (oldvd); 5632cb04b873SMark J Musante 5633cb04b873SMark J Musante /* 5634cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5635cb04b873SMark J Musante * and those spares are not required, then we want to 5636cb04b873SMark J Musante * attempt to free them up now so that they can be used 5637cb04b873SMark J Musante * by other pools. Once we're back down to a single 5638cb04b873SMark J Musante * disk+spare, we stop removing them. 5639cb04b873SMark J Musante */ 5640cb04b873SMark J Musante if (vd->vdev_children > 2) { 5641cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5642cb04b873SMark J Musante 5643cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5644cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5645cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5646cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5647cb04b873SMark J Musante return (newvd); 56483d7072f8Seschrock } 56493d7072f8Seschrock } 56503d7072f8Seschrock 5651ea8dc4b6Seschrock return (NULL); 5652fa9e4066Sahrens } 5653fa9e4066Sahrens 5654ea8dc4b6Seschrock static void 56553d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5656fa9e4066Sahrens { 56578ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 56588ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5659ea8dc4b6Seschrock 56608ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5661ea8dc4b6Seschrock 56623d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 56638ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 56648ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5665ea8dc4b6Seschrock guid = vd->vdev_guid; 56668ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 56678ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 56688ad4d6ddSJeff Bonwick sguid = 0; 566999653d4eSeschrock /* 567099653d4eSeschrock * If we have just finished replacing a hot spared device, then 567199653d4eSeschrock * we need to detach the parent's first child (the original hot 567299653d4eSeschrock * spare) as well. 567399653d4eSeschrock */ 5674cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5675cb04b873SMark J Musante ppvd->vdev_children == 2) { 567699653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 56778ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 567899653d4eSeschrock } 5679b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 5680b4952e17SGeorge Wilson 56818ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 56828ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5683ea8dc4b6Seschrock return; 56848ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 568599653d4eSeschrock return; 56868ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5687fa9e4066Sahrens } 5688fa9e4066Sahrens 56898ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5690fa9e4066Sahrens } 5691fa9e4066Sahrens 5692c67d9675Seschrock /* 5693b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5694c67d9675Seschrock */ 5695c67d9675Seschrock int 56966809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 56976809eb4eSEric Schrock boolean_t ispath) 5698c67d9675Seschrock { 5699c5904d13Seschrock vdev_t *vd; 5700208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5701c67d9675Seschrock 5702f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5703f9af39baSGeorge Wilson 5704b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5705c67d9675Seschrock 57066809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5707b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5708c67d9675Seschrock 57090e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5710b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 57110e34b6a7Sbonwick 57126809eb4eSEric Schrock if (ispath) { 5713208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5714208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5715208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5716208044b8SGeorge Wilson sync = B_TRUE; 5717208044b8SGeorge Wilson } 57186809eb4eSEric Schrock } else { 5719208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5720208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5721208044b8SGeorge Wilson sync = B_TRUE; 5722208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 57236809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5724208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5725208044b8SGeorge Wilson sync = B_TRUE; 5726208044b8SGeorge Wilson } 57276809eb4eSEric Schrock } 5728c67d9675Seschrock 5729208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5730c67d9675Seschrock } 5731c67d9675Seschrock 57326809eb4eSEric Schrock int 57336809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 57346809eb4eSEric Schrock { 57356809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 57366809eb4eSEric Schrock } 57376809eb4eSEric Schrock 57386809eb4eSEric Schrock int 57396809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 57406809eb4eSEric Schrock { 57416809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 57426809eb4eSEric Schrock } 57436809eb4eSEric Schrock 5744fa9e4066Sahrens /* 5745fa9e4066Sahrens * ========================================================================== 57463f9d6ad7SLin Ling * SPA Scanning 5747fa9e4066Sahrens * ========================================================================== 5748fa9e4066Sahrens */ 5749fa9e4066Sahrens 5750ea8dc4b6Seschrock int 57513f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5752fa9e4066Sahrens { 5753e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57543f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 5755be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 57563f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 57573f9d6ad7SLin Ling } 5758bb8b5132Sek 57593f9d6ad7SLin Ling int 57603f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 57613f9d6ad7SLin Ling { 57623f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 57633f9d6ad7SLin Ling 57643f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5765be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 5766fa9e4066Sahrens 5767fa9e4066Sahrens /* 5768088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5769088f3894Sahrens * writeable leaf device, we have nothing to do. 5770fa9e4066Sahrens */ 57713f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5772088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5773088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5774ea8dc4b6Seschrock return (0); 5775ea8dc4b6Seschrock } 5776fa9e4066Sahrens 57773f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5778fa9e4066Sahrens } 5779fa9e4066Sahrens 5780ea8dc4b6Seschrock /* 5781ea8dc4b6Seschrock * ========================================================================== 5782ea8dc4b6Seschrock * SPA async task processing 5783ea8dc4b6Seschrock * ========================================================================== 5784ea8dc4b6Seschrock */ 5785ea8dc4b6Seschrock 5786ea8dc4b6Seschrock static void 57873d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5788fa9e4066Sahrens { 578949cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 579098d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 579198d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 579249cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 57931d713200SEric Schrock 57941d713200SEric Schrock /* 57951d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 57961d713200SEric Schrock * vdev_clear() as that will cause us to throw away 57971d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 57981d713200SEric Schrock * device, all of which is a waste. 57991d713200SEric Schrock */ 58001d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 58011d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 58021d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 58031d713200SEric Schrock 5804e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5805ea8dc4b6Seschrock } 580649cf58c0SBrendan Gregg - Sun Microsystems 5807e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 580849cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5809ea8dc4b6Seschrock } 5810fa9e4066Sahrens 5811e14bb325SJeff Bonwick static void 5812e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5813e14bb325SJeff Bonwick { 5814e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 581598d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5816e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5817e14bb325SJeff Bonwick } 5818e14bb325SJeff Bonwick 5819e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5820e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5821e14bb325SJeff Bonwick } 5822e14bb325SJeff Bonwick 5823573ca77eSGeorge Wilson static void 5824573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5825573ca77eSGeorge Wilson { 5826573ca77eSGeorge Wilson sysevent_id_t eid; 5827573ca77eSGeorge Wilson nvlist_t *attr; 5828573ca77eSGeorge Wilson char *physpath; 5829573ca77eSGeorge Wilson 5830573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5831573ca77eSGeorge Wilson return; 5832573ca77eSGeorge Wilson 5833573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5834573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5835573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5836573ca77eSGeorge Wilson } 5837573ca77eSGeorge Wilson 5838573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5839573ca77eSGeorge Wilson return; 5840573ca77eSGeorge Wilson 5841573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5842573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5843573ca77eSGeorge Wilson 5844573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5845573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5846573ca77eSGeorge Wilson 5847573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5848573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5849573ca77eSGeorge Wilson 5850573ca77eSGeorge Wilson nvlist_free(attr); 5851573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5852573ca77eSGeorge Wilson } 5853573ca77eSGeorge Wilson 5854ea8dc4b6Seschrock static void 5855ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5856ea8dc4b6Seschrock { 5857e14bb325SJeff Bonwick int tasks; 5858ea8dc4b6Seschrock 5859ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5860ea8dc4b6Seschrock 5861ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5862ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5863ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5864ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5865ea8dc4b6Seschrock 58660373e76bSbonwick /* 58670373e76bSbonwick * See if the config needs to be updated. 58680373e76bSbonwick */ 58690373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5870b24ab676SJeff Bonwick uint64_t old_space, new_space; 5871573ca77eSGeorge Wilson 58720373e76bSbonwick mutex_enter(&spa_namespace_lock); 5873b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 58740373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5875b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 58760373e76bSbonwick mutex_exit(&spa_namespace_lock); 5877573ca77eSGeorge Wilson 5878573ca77eSGeorge Wilson /* 5879573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5880573ca77eSGeorge Wilson * then log an internal history event. 5881573ca77eSGeorge Wilson */ 5882b24ab676SJeff Bonwick if (new_space != old_space) { 58834445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5884c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5885b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5886573ca77eSGeorge Wilson } 58870373e76bSbonwick } 58880373e76bSbonwick 5889ea8dc4b6Seschrock /* 58903d7072f8Seschrock * See if any devices need to be marked REMOVED. 5891ea8dc4b6Seschrock */ 5892e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 58938f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 58943d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5895e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 589649cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5897e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 589849cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5899e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5900e14bb325SJeff Bonwick } 5901e14bb325SJeff Bonwick 5902573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5903573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5904573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5905573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5906573ca77eSGeorge Wilson } 5907573ca77eSGeorge Wilson 5908e14bb325SJeff Bonwick /* 5909e14bb325SJeff Bonwick * See if any devices need to be probed. 5910e14bb325SJeff Bonwick */ 5911e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 59128f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5913e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5914e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 59153d7072f8Seschrock } 5916ea8dc4b6Seschrock 5917ea8dc4b6Seschrock /* 5918ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5919ea8dc4b6Seschrock */ 59203d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 59213d7072f8Seschrock spa_vdev_resilver_done(spa); 5922fa9e4066Sahrens 5923ea8dc4b6Seschrock /* 5924ea8dc4b6Seschrock * Kick off a resilver. 5925ea8dc4b6Seschrock */ 5926088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 59273f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5928ea8dc4b6Seschrock 5929ea8dc4b6Seschrock /* 5930ea8dc4b6Seschrock * Let the world know that we're done. 5931ea8dc4b6Seschrock */ 5932ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5933ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5934ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5935ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5936ea8dc4b6Seschrock thread_exit(); 5937ea8dc4b6Seschrock } 5938ea8dc4b6Seschrock 5939ea8dc4b6Seschrock void 5940ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5941ea8dc4b6Seschrock { 5942ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5943ea8dc4b6Seschrock spa->spa_async_suspended++; 5944ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5945ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5946ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5947ea8dc4b6Seschrock } 5948ea8dc4b6Seschrock 5949ea8dc4b6Seschrock void 5950ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5951ea8dc4b6Seschrock { 5952ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5953ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5954ea8dc4b6Seschrock spa->spa_async_suspended--; 5955ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5956ea8dc4b6Seschrock } 5957ea8dc4b6Seschrock 59583cb69f73SWill Andrews static boolean_t 59593cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa) 59603cb69f73SWill Andrews { 59613cb69f73SWill Andrews uint_t non_config_tasks; 59623cb69f73SWill Andrews uint_t config_task; 59633cb69f73SWill Andrews boolean_t config_task_suspended; 59643cb69f73SWill Andrews 59653cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 59663cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 59673cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) { 59683cb69f73SWill Andrews config_task_suspended = B_FALSE; 59693cb69f73SWill Andrews } else { 59703cb69f73SWill Andrews config_task_suspended = 59713cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) < 59723cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC); 59733cb69f73SWill Andrews } 59743cb69f73SWill Andrews 59753cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended)); 59763cb69f73SWill Andrews } 59773cb69f73SWill Andrews 5978ea8dc4b6Seschrock static void 5979ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5980ea8dc4b6Seschrock { 5981ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 59823cb69f73SWill Andrews if (spa_async_tasks_pending(spa) && 59833cb69f73SWill Andrews !spa->spa_async_suspended && 59840373e76bSbonwick spa->spa_async_thread == NULL && 59853cb69f73SWill Andrews rootdir != NULL) 5986ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5987ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5988ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5989ea8dc4b6Seschrock } 5990ea8dc4b6Seschrock 5991ea8dc4b6Seschrock void 5992ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5993ea8dc4b6Seschrock { 59943f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5995ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5996ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5997ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5998fa9e4066Sahrens } 5999fa9e4066Sahrens 6000fa9e4066Sahrens /* 6001fa9e4066Sahrens * ========================================================================== 6002fa9e4066Sahrens * SPA syncing routines 6003fa9e4066Sahrens * ========================================================================== 6004fa9e4066Sahrens */ 6005fa9e4066Sahrens 6006cde58dbcSMatthew Ahrens static int 6007cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6008cde58dbcSMatthew Ahrens { 6009cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 6010cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 6011cde58dbcSMatthew Ahrens return (0); 6012b24ab676SJeff Bonwick } 6013b24ab676SJeff Bonwick 6014cde58dbcSMatthew Ahrens static int 6015cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6016b24ab676SJeff Bonwick { 6017b24ab676SJeff Bonwick zio_t *zio = arg; 6018b24ab676SJeff Bonwick 6019b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 6020b24ab676SJeff Bonwick zio->io_flags)); 6021cde58dbcSMatthew Ahrens return (0); 6022fa9e4066Sahrens } 6023fa9e4066Sahrens 602469962b56SMatthew Ahrens /* 602569962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 602669962b56SMatthew Ahrens * amount of time spent syncing frees. 602769962b56SMatthew Ahrens */ 602869962b56SMatthew Ahrens static void 602969962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 603069962b56SMatthew Ahrens { 603169962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 603269962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 603369962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0); 603469962b56SMatthew Ahrens } 603569962b56SMatthew Ahrens 603669962b56SMatthew Ahrens /* 603769962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the 603869962b56SMatthew Ahrens * amount of time spent syncing deferred frees. 603969962b56SMatthew Ahrens */ 604069962b56SMatthew Ahrens static void 604169962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 604269962b56SMatthew Ahrens { 604369962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 604469962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 604569962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 604669962b56SMatthew Ahrens VERIFY0(zio_wait(zio)); 604769962b56SMatthew Ahrens } 604869962b56SMatthew Ahrens 604969962b56SMatthew Ahrens 6050fa9e4066Sahrens static void 605199653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 6052fa9e4066Sahrens { 6053fa9e4066Sahrens char *packed = NULL; 6054f7991ba4STim Haley size_t bufsize; 6055fa9e4066Sahrens size_t nvsize = 0; 6056fa9e4066Sahrens dmu_buf_t *db; 6057fa9e4066Sahrens 605899653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 6059fa9e4066Sahrens 6060f7991ba4STim Haley /* 6061f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 606243466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and 6063f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 6064f7991ba4STim Haley */ 6065ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 6066f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 6067fa9e4066Sahrens 606899653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 6069ea8dc4b6Seschrock KM_SLEEP) == 0); 6070f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 6071fa9e4066Sahrens 6072f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 6073fa9e4066Sahrens 6074f7991ba4STim Haley kmem_free(packed, bufsize); 6075fa9e4066Sahrens 607699653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 6077fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 6078fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 6079ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 6080fa9e4066Sahrens } 6081fa9e4066Sahrens 608299653d4eSeschrock static void 6083fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 6084fa94a07fSbrendan const char *config, const char *entry) 608599653d4eSeschrock { 608699653d4eSeschrock nvlist_t *nvroot; 6087fa94a07fSbrendan nvlist_t **list; 608899653d4eSeschrock int i; 608999653d4eSeschrock 6090fa94a07fSbrendan if (!sav->sav_sync) 609199653d4eSeschrock return; 609299653d4eSeschrock 609399653d4eSeschrock /* 6094fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 6095fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 60963d7072f8Seschrock * valid and the vdevs are labeled appropriately. 609799653d4eSeschrock */ 6098fa94a07fSbrendan if (sav->sav_object == 0) { 6099fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 6100fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 6101fa94a07fSbrendan sizeof (uint64_t), tx); 610299653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 6103fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 6104fa94a07fSbrendan &sav->sav_object, tx) == 0); 610599653d4eSeschrock } 610699653d4eSeschrock 610799653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 6108fa94a07fSbrendan if (sav->sav_count == 0) { 6109fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 611099653d4eSeschrock } else { 6111fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 6112fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6113fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 61143f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 6115fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 6116fa94a07fSbrendan sav->sav_count) == 0); 6117fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6118fa94a07fSbrendan nvlist_free(list[i]); 6119fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 612099653d4eSeschrock } 612199653d4eSeschrock 6122fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 612306eeb2adSek nvlist_free(nvroot); 612499653d4eSeschrock 6125fa94a07fSbrendan sav->sav_sync = B_FALSE; 612699653d4eSeschrock } 612799653d4eSeschrock 6128215198a6SJoe Stein /* 6129215198a6SJoe Stein * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 6130215198a6SJoe Stein * The all-vdev ZAP must be empty. 6131215198a6SJoe Stein */ 6132215198a6SJoe Stein static void 6133215198a6SJoe Stein spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 6134215198a6SJoe Stein { 6135215198a6SJoe Stein spa_t *spa = vd->vdev_spa; 6136215198a6SJoe Stein if (vd->vdev_top_zap != 0) { 6137215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6138215198a6SJoe Stein vd->vdev_top_zap, tx)); 6139215198a6SJoe Stein } 6140215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) { 6141215198a6SJoe Stein VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 6142215198a6SJoe Stein vd->vdev_leaf_zap, tx)); 6143215198a6SJoe Stein } 6144215198a6SJoe Stein for (uint64_t i = 0; i < vd->vdev_children; i++) { 6145215198a6SJoe Stein spa_avz_build(vd->vdev_child[i], avz, tx); 6146215198a6SJoe Stein } 6147215198a6SJoe Stein } 6148215198a6SJoe Stein 614999653d4eSeschrock static void 615099653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 615199653d4eSeschrock { 615299653d4eSeschrock nvlist_t *config; 615399653d4eSeschrock 6154215198a6SJoe Stein /* 6155215198a6SJoe Stein * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 6156215198a6SJoe Stein * its config may not be dirty but we still need to build per-vdev ZAPs. 6157215198a6SJoe Stein * Similarly, if the pool is being assembled (e.g. after a split), we 6158215198a6SJoe Stein * need to rebuild the AVZ although the config may not be dirty. 6159215198a6SJoe Stein */ 6160215198a6SJoe Stein if (list_is_empty(&spa->spa_config_dirty_list) && 6161215198a6SJoe Stein spa->spa_avz_action == AVZ_ACTION_NONE) 616299653d4eSeschrock return; 616399653d4eSeschrock 6164e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6165e14bb325SJeff Bonwick 6166215198a6SJoe Stein ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 6167555da511SPaul Dagnelie spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 6168215198a6SJoe Stein spa->spa_all_vdev_zaps != 0); 6169215198a6SJoe Stein 6170215198a6SJoe Stein if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 6171215198a6SJoe Stein /* Make and build the new AVZ */ 6172215198a6SJoe Stein uint64_t new_avz = zap_create(spa->spa_meta_objset, 6173215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 6174215198a6SJoe Stein spa_avz_build(spa->spa_root_vdev, new_avz, tx); 6175215198a6SJoe Stein 6176215198a6SJoe Stein /* Diff old AVZ with new one */ 6177215198a6SJoe Stein zap_cursor_t zc; 6178215198a6SJoe Stein zap_attribute_t za; 6179215198a6SJoe Stein 6180215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6181215198a6SJoe Stein spa->spa_all_vdev_zaps); 6182215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6183215198a6SJoe Stein zap_cursor_advance(&zc)) { 6184215198a6SJoe Stein uint64_t vdzap = za.za_first_integer; 6185215198a6SJoe Stein if (zap_lookup_int(spa->spa_meta_objset, new_avz, 6186215198a6SJoe Stein vdzap) == ENOENT) { 6187215198a6SJoe Stein /* 6188215198a6SJoe Stein * ZAP is listed in old AVZ but not in new one; 6189215198a6SJoe Stein * destroy it 6190215198a6SJoe Stein */ 6191215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 6192215198a6SJoe Stein tx)); 6193215198a6SJoe Stein } 6194215198a6SJoe Stein } 6195215198a6SJoe Stein 6196215198a6SJoe Stein zap_cursor_fini(&zc); 6197215198a6SJoe Stein 6198215198a6SJoe Stein /* Destroy the old AVZ */ 6199215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6200215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6201215198a6SJoe Stein 6202215198a6SJoe Stein /* Replace the old AVZ in the dir obj with the new one */ 6203215198a6SJoe Stein VERIFY0(zap_update(spa->spa_meta_objset, 6204215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 6205215198a6SJoe Stein sizeof (new_avz), 1, &new_avz, tx)); 6206215198a6SJoe Stein 6207215198a6SJoe Stein spa->spa_all_vdev_zaps = new_avz; 6208215198a6SJoe Stein } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 6209215198a6SJoe Stein zap_cursor_t zc; 6210215198a6SJoe Stein zap_attribute_t za; 6211215198a6SJoe Stein 6212215198a6SJoe Stein /* Walk through the AVZ and destroy all listed ZAPs */ 6213215198a6SJoe Stein for (zap_cursor_init(&zc, spa->spa_meta_objset, 6214215198a6SJoe Stein spa->spa_all_vdev_zaps); 6215215198a6SJoe Stein zap_cursor_retrieve(&zc, &za) == 0; 6216215198a6SJoe Stein zap_cursor_advance(&zc)) { 6217215198a6SJoe Stein uint64_t zap = za.za_first_integer; 6218215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 6219215198a6SJoe Stein } 6220215198a6SJoe Stein 6221215198a6SJoe Stein zap_cursor_fini(&zc); 6222215198a6SJoe Stein 6223215198a6SJoe Stein /* Destroy and unlink the AVZ itself */ 6224215198a6SJoe Stein VERIFY0(zap_destroy(spa->spa_meta_objset, 6225215198a6SJoe Stein spa->spa_all_vdev_zaps, tx)); 6226215198a6SJoe Stein VERIFY0(zap_remove(spa->spa_meta_objset, 6227215198a6SJoe Stein DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 6228215198a6SJoe Stein spa->spa_all_vdev_zaps = 0; 6229215198a6SJoe Stein } 6230215198a6SJoe Stein 6231215198a6SJoe Stein if (spa->spa_all_vdev_zaps == 0) { 6232215198a6SJoe Stein spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 6233215198a6SJoe Stein DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 6234215198a6SJoe Stein DMU_POOL_VDEV_ZAP_MAP, tx); 6235215198a6SJoe Stein } 6236215198a6SJoe Stein spa->spa_avz_action = AVZ_ACTION_NONE; 6237215198a6SJoe Stein 6238215198a6SJoe Stein /* Create ZAPs for vdevs that don't have them. */ 6239215198a6SJoe Stein vdev_construct_zaps(spa->spa_root_vdev, tx); 6240215198a6SJoe Stein 6241e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 6242e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 6243e14bb325SJeff Bonwick 624425345e46SGeorge Wilson /* 624525345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 624625345e46SGeorge Wilson * the config object gets updated with the correct version. 624725345e46SGeorge Wilson */ 624825345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 624925345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 625025345e46SGeorge Wilson spa->spa_uberblock.ub_version); 625125345e46SGeorge Wilson 6252e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 625399653d4eSeschrock 6254aab83bb8SJosef 'Jeff' Sipek nvlist_free(spa->spa_config_syncing); 625599653d4eSeschrock spa->spa_config_syncing = config; 625699653d4eSeschrock 625799653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 625899653d4eSeschrock } 625999653d4eSeschrock 6260ad135b5dSChristopher Siden static void 62613b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 6262ad135b5dSChristopher Siden { 62633b2aab18SMatthew Ahrens uint64_t *versionp = arg; 62643b2aab18SMatthew Ahrens uint64_t version = *versionp; 62653b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6266ad135b5dSChristopher Siden 6267ad135b5dSChristopher Siden /* 6268ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 6269ad135b5dSChristopher Siden */ 6270ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 6271ad135b5dSChristopher Siden 627262eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 6273ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 6274ad135b5dSChristopher Siden 6275ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 6276ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 62774445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 6278ad135b5dSChristopher Siden } 6279ad135b5dSChristopher Siden 6280990b4856Slling /* 6281990b4856Slling * Set zpool properties. 6282990b4856Slling */ 6283b1b8ab34Slling static void 62843b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 6285b1b8ab34Slling { 62863b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 62873b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 6288b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 6289ad135b5dSChristopher Siden nvpair_t *elem = NULL; 6290b1b8ab34Slling 6291e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 6292e14bb325SJeff Bonwick 6293990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 6294ad135b5dSChristopher Siden uint64_t intval; 6295ad135b5dSChristopher Siden char *strval, *fname; 6296ad135b5dSChristopher Siden zpool_prop_t prop; 6297ad135b5dSChristopher Siden const char *propname; 6298ad135b5dSChristopher Siden zprop_type_t proptype; 62992acef22dSMatthew Ahrens spa_feature_t fid; 6300ad135b5dSChristopher Siden 6301990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 6302ad135b5dSChristopher Siden case ZPROP_INVAL: 6303ad135b5dSChristopher Siden /* 6304ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 6305ad135b5dSChristopher Siden */ 6306ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 6307ad135b5dSChristopher Siden 6308ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 63092acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid)); 6310ad135b5dSChristopher Siden 63112acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx); 63124445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63134445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 6314ad135b5dSChristopher Siden break; 6315ad135b5dSChristopher Siden 6316990b4856Slling case ZPOOL_PROP_VERSION: 63170713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6318990b4856Slling /* 6319ad135b5dSChristopher Siden * The version is synced seperatly before other 6320ad135b5dSChristopher Siden * properties and should be correct by now. 6321990b4856Slling */ 6322ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 6323ecd6cf80Smarks break; 6324990b4856Slling 6325990b4856Slling case ZPOOL_PROP_ALTROOT: 6326990b4856Slling /* 6327990b4856Slling * 'altroot' is a non-persistent property. It should 6328990b4856Slling * have been set temporarily at creation or import time. 6329990b4856Slling */ 6330990b4856Slling ASSERT(spa->spa_root != NULL); 6331b1b8ab34Slling break; 63323d7072f8Seschrock 6333f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 63342f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 6335990b4856Slling /* 6336f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 6337f9af39baSGeorge Wilson * properties. 6338990b4856Slling */ 63393d7072f8Seschrock break; 63408704186eSDan McDonald case ZPOOL_PROP_COMMENT: 63410713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63428704186eSDan McDonald if (spa->spa_comment != NULL) 63438704186eSDan McDonald spa_strfree(spa->spa_comment); 63448704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 63458704186eSDan McDonald /* 63468704186eSDan McDonald * We need to dirty the configuration on all the vdevs 63478704186eSDan McDonald * so that their labels get updated. It's unnecessary 63488704186eSDan McDonald * to do this for pool creation since the vdev's 63498704186eSDan McDonald * configuratoin has already been dirtied. 63508704186eSDan McDonald */ 63518704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 63528704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 63534445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63544445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 63558704186eSDan McDonald break; 6356990b4856Slling default: 6357990b4856Slling /* 6358990b4856Slling * Set pool property values in the poolprops mos object. 6359990b4856Slling */ 6360990b4856Slling if (spa->spa_pool_props_object == 0) { 6361ad135b5dSChristopher Siden spa->spa_pool_props_object = 6362ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 6363990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 6364ad135b5dSChristopher Siden tx); 6365990b4856Slling } 6366990b4856Slling 6367990b4856Slling /* normalize the property name */ 6368990b4856Slling propname = zpool_prop_to_name(prop); 6369990b4856Slling proptype = zpool_prop_get_type(prop); 6370990b4856Slling 6371990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 6372990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 63730713e232SGeorge Wilson strval = fnvpair_value_string(elem); 63740713e232SGeorge Wilson VERIFY0(zap_update(mos, 6375990b4856Slling spa->spa_pool_props_object, propname, 63760713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx)); 63774445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63784445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 6379990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 63800713e232SGeorge Wilson intval = fnvpair_value_uint64(elem); 6381990b4856Slling 6382990b4856Slling if (proptype == PROP_TYPE_INDEX) { 6383990b4856Slling const char *unused; 63840713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string( 63850713e232SGeorge Wilson prop, intval, &unused)); 6386990b4856Slling } 63870713e232SGeorge Wilson VERIFY0(zap_update(mos, 6388990b4856Slling spa->spa_pool_props_object, propname, 63890713e232SGeorge Wilson 8, 1, &intval, tx)); 63904445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 63914445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 6392990b4856Slling } else { 6393990b4856Slling ASSERT(0); /* not allowed */ 6394990b4856Slling } 6395990b4856Slling 63960a4e9518Sgw switch (prop) { 63970a4e9518Sgw case ZPOOL_PROP_DELEGATION: 6398990b4856Slling spa->spa_delegation = intval; 63990a4e9518Sgw break; 64000a4e9518Sgw case ZPOOL_PROP_BOOTFS: 6401990b4856Slling spa->spa_bootfs = intval; 64020a4e9518Sgw break; 64030a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 64040a4e9518Sgw spa->spa_failmode = intval; 64050a4e9518Sgw break; 6406573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 6407573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 6408b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 6409b98131cfSEric Taylor spa_async_request(spa, 6410b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 6411573ca77eSGeorge Wilson break; 6412b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 6413b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 6414b24ab676SJeff Bonwick break; 64150a4e9518Sgw default: 64160a4e9518Sgw break; 64170a4e9518Sgw } 6418990b4856Slling } 6419990b4856Slling 6420b1b8ab34Slling } 6421e14bb325SJeff Bonwick 6422e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 6423b1b8ab34Slling } 6424b1b8ab34Slling 6425cde58dbcSMatthew Ahrens /* 6426cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 6427cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 6428cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 6429cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 6430cde58dbcSMatthew Ahrens * tasks. 6431cde58dbcSMatthew Ahrens */ 6432cde58dbcSMatthew Ahrens static void 6433cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 6434cde58dbcSMatthew Ahrens { 6435cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6436cde58dbcSMatthew Ahrens 6437cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 6438cde58dbcSMatthew Ahrens 64393b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 64403b2aab18SMatthew Ahrens 6441cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 6442cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 6443cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6444cde58dbcSMatthew Ahrens 6445cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6446cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6447cde58dbcSMatthew Ahrens } 6448cde58dbcSMatthew Ahrens 6449cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6450cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6451cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6452cde58dbcSMatthew Ahrens } 6453cde58dbcSMatthew Ahrens 6454cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6455cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6456cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6457cde58dbcSMatthew Ahrens 6458cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6459cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6460cde58dbcSMatthew Ahrens } 6461ad135b5dSChristopher Siden 6462ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6463ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6464ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6465ad135b5dSChristopher Siden } 6466b8289d24SDaniil Lunev 6467b8289d24SDaniil Lunev /* 6468b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 6469b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added 6470b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have 6471b8289d24SDaniil Lunev * this feature active 6472b8289d24SDaniil Lunev */ 6473b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6474b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa, 6475b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6476b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa, 6477b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS); 6478b8289d24SDaniil Lunev 6479b8289d24SDaniil Lunev if (lz4_en && !lz4_ac) 6480b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 6481b8289d24SDaniil Lunev } 648245818ee1SMatthew Ahrens 648345818ee1SMatthew Ahrens /* 648445818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the 648545818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since 648645818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible. 648745818ee1SMatthew Ahrens */ 648845818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 648945818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) { 649045818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset, 649145818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 649245818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes), 649345818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx)); 649445818ee1SMatthew Ahrens } 649545818ee1SMatthew Ahrens 64963b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6497cde58dbcSMatthew Ahrens } 6498cde58dbcSMatthew Ahrens 6499fa9e4066Sahrens /* 6500fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6501fa9e4066Sahrens * part of the process, so we iterate until it converges. 6502fa9e4066Sahrens */ 6503fa9e4066Sahrens void 6504fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6505fa9e4066Sahrens { 6506fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6507fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6508b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 65090373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6510fa9e4066Sahrens vdev_t *vd; 6511fa9e4066Sahrens dmu_tx_t *tx; 6512e14bb325SJeff Bonwick int error; 65130f7643c7SGeorge Wilson uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 65140f7643c7SGeorge Wilson zfs_vdev_queue_depth_pct / 100; 6515fa9e4066Sahrens 6516f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6517f9af39baSGeorge Wilson 6518fa9e4066Sahrens /* 6519fa9e4066Sahrens * Lock out configuration changes. 6520fa9e4066Sahrens */ 6521e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6522fa9e4066Sahrens 6523fa9e4066Sahrens spa->spa_syncing_txg = txg; 6524fa9e4066Sahrens spa->spa_sync_pass = 0; 6525fa9e4066Sahrens 65260f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 65270f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 65280f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 65290f7643c7SGeorge Wilson 6530e14bb325SJeff Bonwick /* 6531e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6532e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6533e14bb325SJeff Bonwick */ 6534e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 65358ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 65368ad4d6ddSJeff Bonwick /* 65378ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 65388ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 65398ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 65408ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 65418ad4d6ddSJeff Bonwick * into the root vdev tree. 65428ad4d6ddSJeff Bonwick */ 65438ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65448ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 65458ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 65468ad4d6ddSJeff Bonwick vdev_state_clean(vd); 65478ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 65488ad4d6ddSJeff Bonwick } 65498ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 65508ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6551e14bb325SJeff Bonwick } 6552e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6553e14bb325SJeff Bonwick 655499653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 655599653d4eSeschrock 6556283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6557283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6558283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6559283b8460SGeorge.Wilson 656099653d4eSeschrock /* 6561e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 656299653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 656399653d4eSeschrock */ 6564e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6565e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 656699653d4eSeschrock int i; 656799653d4eSeschrock 656899653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 656999653d4eSeschrock vd = rvd->vdev_child[i]; 657099653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 657199653d4eSeschrock break; 657299653d4eSeschrock } 657399653d4eSeschrock if (i == rvd->vdev_children) { 657499653d4eSeschrock spa->spa_deflate = TRUE; 657599653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 657699653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 657799653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 657899653d4eSeschrock } 657999653d4eSeschrock } 658099653d4eSeschrock 65810f7643c7SGeorge Wilson /* 65820f7643c7SGeorge Wilson * Set the top-level vdev's max queue depth. Evaluate each 65830f7643c7SGeorge Wilson * top-level's async write queue depth in case it changed. 65840f7643c7SGeorge Wilson * The max queue depth will not change in the middle of syncing 65850f7643c7SGeorge Wilson * out this txg. 65860f7643c7SGeorge Wilson */ 65870f7643c7SGeorge Wilson uint64_t queue_depth_total = 0; 65880f7643c7SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 65890f7643c7SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 65900f7643c7SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg; 65910f7643c7SGeorge Wilson 65920f7643c7SGeorge Wilson if (mg == NULL || mg->mg_class != spa_normal_class(spa) || 65930f7643c7SGeorge Wilson !metaslab_group_initialized(mg)) 65940f7643c7SGeorge Wilson continue; 65950f7643c7SGeorge Wilson 65960f7643c7SGeorge Wilson /* 65970f7643c7SGeorge Wilson * It is safe to do a lock-free check here because only async 65980f7643c7SGeorge Wilson * allocations look at mg_max_alloc_queue_depth, and async 65990f7643c7SGeorge Wilson * allocations all happen from spa_sync(). 66000f7643c7SGeorge Wilson */ 66010f7643c7SGeorge Wilson ASSERT0(refcount_count(&mg->mg_alloc_queue_depth)); 66020f7643c7SGeorge Wilson mg->mg_max_alloc_queue_depth = max_queue_depth; 66030f7643c7SGeorge Wilson queue_depth_total += mg->mg_max_alloc_queue_depth; 66040f7643c7SGeorge Wilson } 66050f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 66060f7643c7SGeorge Wilson ASSERT0(refcount_count(&mc->mc_alloc_slots)); 66070f7643c7SGeorge Wilson mc->mc_alloc_max_slots = queue_depth_total; 66080f7643c7SGeorge Wilson mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 66090f7643c7SGeorge Wilson 66100f7643c7SGeorge Wilson ASSERT3U(mc->mc_alloc_max_slots, <=, 66110f7643c7SGeorge Wilson max_queue_depth * rvd->vdev_children); 66120f7643c7SGeorge Wilson 6613fa9e4066Sahrens /* 6614fa9e4066Sahrens * Iterate to convergence. 6615fa9e4066Sahrens */ 6616fa9e4066Sahrens do { 6617b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6618fa9e4066Sahrens 6619fa9e4066Sahrens spa_sync_config_object(spa, tx); 6620fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6621fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6622fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6623fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6624ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6625fa9e4066Sahrens dsl_pool_sync(dp, txg); 6626fa9e4066Sahrens 662701f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 662869962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx); 6629b24ab676SJeff Bonwick } else { 6630231aab85SMatthew Ahrens /* 6631231aab85SMatthew Ahrens * We can not defer frees in pass 1, because 6632231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1. 6633231aab85SMatthew Ahrens */ 6634231aab85SMatthew Ahrens ASSERT3U(pass, >, 1); 6635cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 663669962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx); 6637fa9e4066Sahrens } 6638fa9e4066Sahrens 6639b24ab676SJeff Bonwick ddt_sync(spa, txg); 66403f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6641afee20e4SGeorge Wilson 6642b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6643b24ab676SJeff Bonwick vdev_sync(vd, txg); 6644b24ab676SJeff Bonwick 6645231aab85SMatthew Ahrens if (pass == 1) { 6646cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6647231aab85SMatthew Ahrens ASSERT3U(txg, >=, 6648231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth); 6649231aab85SMatthew Ahrens /* 6650231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty 6651231aab85SMatthew Ahrens * because we could have marked the MOS dirty 6652231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we 6653231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We 6654231aab85SMatthew Ahrens * need to check the uberblock's rootbp because 6655231aab85SMatthew Ahrens * it is updated if we have synced out dirty 6656231aab85SMatthew Ahrens * data (though in this case the MOS will most 6657231aab85SMatthew Ahrens * likely also be dirty due to second order 6658231aab85SMatthew Ahrens * effects, we don't want to rely on that here). 6659231aab85SMatthew Ahrens */ 6660231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg && 6661231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) { 6662231aab85SMatthew Ahrens /* 6663231aab85SMatthew Ahrens * Nothing changed on the first pass, 6664231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid 6665231aab85SMatthew Ahrens * syncing deferred frees, so that we 6666231aab85SMatthew Ahrens * can keep this TXG as a no-op. 6667231aab85SMatthew Ahrens */ 6668231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, 6669231aab85SMatthew Ahrens txg)); 6670231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6671231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 6672231aab85SMatthew Ahrens break; 6673231aab85SMatthew Ahrens } 6674231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx); 6675231aab85SMatthew Ahrens } 6676fa9e4066Sahrens 6677cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6678fa9e4066Sahrens 6679215198a6SJoe Stein if (!list_is_empty(&spa->spa_config_dirty_list)) { 6680215198a6SJoe Stein /* 6681215198a6SJoe Stein * Make sure that the number of ZAPs for all the vdevs matches 6682215198a6SJoe Stein * the number of ZAPs in the per-vdev ZAP list. This only gets 6683215198a6SJoe Stein * called if the config is dirty; otherwise there may be 6684215198a6SJoe Stein * outstanding AVZ operations that weren't completed in 6685215198a6SJoe Stein * spa_sync_config_object. 6686215198a6SJoe Stein */ 6687215198a6SJoe Stein uint64_t all_vdev_zap_entry_count; 6688215198a6SJoe Stein ASSERT0(zap_count(spa->spa_meta_objset, 6689215198a6SJoe Stein spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 6690215198a6SJoe Stein ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 6691215198a6SJoe Stein all_vdev_zap_entry_count); 6692215198a6SJoe Stein } 6693215198a6SJoe Stein 6694fa9e4066Sahrens /* 6695fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6696fa9e4066Sahrens * to commit the transaction group. 66970373e76bSbonwick * 669817f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 669917f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6700e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6701e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 67020373e76bSbonwick */ 6703e14bb325SJeff Bonwick for (;;) { 6704e14bb325SJeff Bonwick /* 6705e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6706e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6707e14bb325SJeff Bonwick */ 6708e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6709e14bb325SJeff Bonwick 6710e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6711e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6712e14bb325SJeff Bonwick int svdcount = 0; 6713e14bb325SJeff Bonwick int children = rvd->vdev_children; 6714e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6715e14bb325SJeff Bonwick 6716573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6717e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6718e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6719e14bb325SJeff Bonwick continue; 6720e14bb325SJeff Bonwick svd[svdcount++] = vd; 6721e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6722e14bb325SJeff Bonwick break; 6723e14bb325SJeff Bonwick } 6724eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg); 6725e14bb325SJeff Bonwick } else { 6726e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 6727eb5bb584SWill Andrews rvd->vdev_children, txg); 67280373e76bSbonwick } 6729e14bb325SJeff Bonwick 6730dfbb9432SGeorge Wilson if (error == 0) 6731dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6732dfbb9432SGeorge Wilson 6733e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6734e14bb325SJeff Bonwick 6735e14bb325SJeff Bonwick if (error == 0) 6736e14bb325SJeff Bonwick break; 6737e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6738e14bb325SJeff Bonwick zio_resume_wait(spa); 67390373e76bSbonwick } 674099653d4eSeschrock dmu_tx_commit(tx); 674199653d4eSeschrock 6742283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6743283b8460SGeorge.Wilson 67440373e76bSbonwick /* 67450373e76bSbonwick * Clear the dirty config list. 6746fa9e4066Sahrens */ 6747e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 67480373e76bSbonwick vdev_config_clean(vd); 67490373e76bSbonwick 67500373e76bSbonwick /* 67510373e76bSbonwick * Now that the new config has synced transactionally, 67520373e76bSbonwick * let it become visible to the config cache. 67530373e76bSbonwick */ 67540373e76bSbonwick if (spa->spa_config_syncing != NULL) { 67550373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 67560373e76bSbonwick spa->spa_config_txg = txg; 67570373e76bSbonwick spa->spa_config_syncing = NULL; 67580373e76bSbonwick } 6759fa9e4066Sahrens 6760b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6761fa9e4066Sahrens 67620f7643c7SGeorge Wilson mutex_enter(&spa->spa_alloc_lock); 67630f7643c7SGeorge Wilson VERIFY0(avl_numnodes(&spa->spa_alloc_tree)); 67640f7643c7SGeorge Wilson mutex_exit(&spa->spa_alloc_lock); 67650f7643c7SGeorge Wilson 6766fa9e4066Sahrens /* 6767fa9e4066Sahrens * Update usable space statistics. 6768fa9e4066Sahrens */ 6769fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6770fa9e4066Sahrens vdev_sync_done(vd, txg); 6771fa9e4066Sahrens 6772485bbbf5SGeorge Wilson spa_update_dspace(spa); 6773485bbbf5SGeorge Wilson 6774fa9e4066Sahrens /* 6775fa9e4066Sahrens * It had better be the case that we didn't dirty anything 677699653d4eSeschrock * since vdev_config_sync(). 6777fa9e4066Sahrens */ 6778fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6779fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6780fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6781b24ab676SJeff Bonwick 6782b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6783fa9e4066Sahrens 678443297f97SGeorge Wilson /* 678543297f97SGeorge Wilson * Update the last synced uberblock here. We want to do this at 678643297f97SGeorge Wilson * the end of spa_sync() so that consumers of spa_last_synced_txg() 678743297f97SGeorge Wilson * will be guaranteed that all the processing associated with 678843297f97SGeorge Wilson * that txg has been completed. 678943297f97SGeorge Wilson */ 679043297f97SGeorge Wilson spa->spa_ubsync = spa->spa_uberblock; 6791e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6792ea8dc4b6Seschrock 6793468c413aSTim Haley spa_handle_ignored_writes(spa); 6794468c413aSTim Haley 6795ea8dc4b6Seschrock /* 6796ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6797ea8dc4b6Seschrock */ 6798ea8dc4b6Seschrock spa_async_dispatch(spa); 6799fa9e4066Sahrens } 6800fa9e4066Sahrens 6801fa9e4066Sahrens /* 6802fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6803fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6804fa9e4066Sahrens * sync. 6805fa9e4066Sahrens */ 6806fa9e4066Sahrens void 6807fa9e4066Sahrens spa_sync_allpools(void) 6808fa9e4066Sahrens { 6809fa9e4066Sahrens spa_t *spa = NULL; 6810fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6811fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6812f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6813f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6814fa9e4066Sahrens continue; 6815fa9e4066Sahrens spa_open_ref(spa, FTAG); 6816fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6817fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6818fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6819fa9e4066Sahrens spa_close(spa, FTAG); 6820fa9e4066Sahrens } 6821fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6822fa9e4066Sahrens } 6823fa9e4066Sahrens 6824fa9e4066Sahrens /* 6825fa9e4066Sahrens * ========================================================================== 6826fa9e4066Sahrens * Miscellaneous routines 6827fa9e4066Sahrens * ========================================================================== 6828fa9e4066Sahrens */ 6829fa9e4066Sahrens 6830fa9e4066Sahrens /* 6831fa9e4066Sahrens * Remove all pools in the system. 6832fa9e4066Sahrens */ 6833fa9e4066Sahrens void 6834fa9e4066Sahrens spa_evict_all(void) 6835fa9e4066Sahrens { 6836fa9e4066Sahrens spa_t *spa; 6837fa9e4066Sahrens 6838fa9e4066Sahrens /* 6839fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6840fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6841fa9e4066Sahrens */ 6842fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6843fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6844fa9e4066Sahrens /* 6845ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6846ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6847ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6848fa9e4066Sahrens */ 6849fa9e4066Sahrens spa_open_ref(spa, FTAG); 6850fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6851ea8dc4b6Seschrock spa_async_suspend(spa); 6852fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6853fa9e4066Sahrens spa_close(spa, FTAG); 6854fa9e4066Sahrens 6855fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6856fa9e4066Sahrens spa_unload(spa); 6857fa9e4066Sahrens spa_deactivate(spa); 6858fa9e4066Sahrens } 6859fa9e4066Sahrens spa_remove(spa); 6860fa9e4066Sahrens } 6861fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6862fa9e4066Sahrens } 6863ea8dc4b6Seschrock 6864ea8dc4b6Seschrock vdev_t * 68656809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6866ea8dc4b6Seschrock { 6867c5904d13Seschrock vdev_t *vd; 6868c5904d13Seschrock int i; 6869c5904d13Seschrock 6870c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6871c5904d13Seschrock return (vd); 6872c5904d13Seschrock 68736809eb4eSEric Schrock if (aux) { 6874c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6875c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 68766809eb4eSEric Schrock if (vd->vdev_guid == guid) 68776809eb4eSEric Schrock return (vd); 68786809eb4eSEric Schrock } 68796809eb4eSEric Schrock 68806809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 68816809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6882c5904d13Seschrock if (vd->vdev_guid == guid) 6883c5904d13Seschrock return (vd); 6884c5904d13Seschrock } 6885c5904d13Seschrock } 6886c5904d13Seschrock 6887c5904d13Seschrock return (NULL); 6888ea8dc4b6Seschrock } 6889eaca9bbdSeschrock 6890eaca9bbdSeschrock void 6891990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6892eaca9bbdSeschrock { 6893f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6894f9af39baSGeorge Wilson 6895e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6896eaca9bbdSeschrock 6897eaca9bbdSeschrock /* 6898eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6899eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6900eaca9bbdSeschrock * possible. 6901eaca9bbdSeschrock */ 690262eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 69035d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 6904eaca9bbdSeschrock 6905990b4856Slling spa->spa_uberblock.ub_version = version; 6906eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6907eaca9bbdSeschrock 6908e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 690999653d4eSeschrock 691099653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 691199653d4eSeschrock } 691299653d4eSeschrock 691399653d4eSeschrock boolean_t 691499653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 691599653d4eSeschrock { 691699653d4eSeschrock int i; 691739c23413Seschrock uint64_t spareguid; 6918fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 691999653d4eSeschrock 6920fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6921fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 692299653d4eSeschrock return (B_TRUE); 692399653d4eSeschrock 6924fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6925fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6926fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 692739c23413Seschrock return (B_TRUE); 692839c23413Seschrock } 692939c23413Seschrock 693099653d4eSeschrock return (B_FALSE); 6931eaca9bbdSeschrock } 6932b1b8ab34Slling 693389a89ebfSlling /* 693489a89ebfSlling * Check if a pool has an active shared spare device. 693589a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 693689a89ebfSlling */ 693789a89ebfSlling static boolean_t 693889a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 693989a89ebfSlling { 694089a89ebfSlling int i, refcnt; 694189a89ebfSlling uint64_t pool; 694289a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 694389a89ebfSlling 694489a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 694589a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 694689a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 694789a89ebfSlling refcnt > 2) 694889a89ebfSlling return (B_TRUE); 694989a89ebfSlling } 695089a89ebfSlling 695189a89ebfSlling return (B_FALSE); 695289a89ebfSlling } 695389a89ebfSlling 6954b72b6bb1SAlan Somers static sysevent_t * 6955*ce1577b0SDave Eddy spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 69563d7072f8Seschrock { 6957b72b6bb1SAlan Somers sysevent_t *ev = NULL; 69583d7072f8Seschrock #ifdef _KERNEL 69593d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 69603d7072f8Seschrock sysevent_value_t value; 69613d7072f8Seschrock 69623d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 69633d7072f8Seschrock SE_SLEEP); 6964b72b6bb1SAlan Somers ASSERT(ev != NULL); 69653d7072f8Seschrock 69663d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69673d7072f8Seschrock value.value.sv_string = spa_name(spa); 69683d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 69693d7072f8Seschrock goto done; 69703d7072f8Seschrock 69713d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69723d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 69733d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 69743d7072f8Seschrock goto done; 69753d7072f8Seschrock 69763d7072f8Seschrock if (vd) { 69773d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 69783d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 69793d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 69803d7072f8Seschrock SE_SLEEP) != 0) 69813d7072f8Seschrock goto done; 69823d7072f8Seschrock 69833d7072f8Seschrock if (vd->vdev_path) { 69843d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 69853d7072f8Seschrock value.value.sv_string = vd->vdev_path; 69863d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 69873d7072f8Seschrock &value, SE_SLEEP) != 0) 69883d7072f8Seschrock goto done; 69893d7072f8Seschrock } 69903d7072f8Seschrock } 69913d7072f8Seschrock 6992*ce1577b0SDave Eddy if (hist_nvl != NULL) { 6993*ce1577b0SDave Eddy fnvlist_merge((nvlist_t *)attr, hist_nvl); 6994*ce1577b0SDave Eddy } 6995*ce1577b0SDave Eddy 6996b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6997b01c3b58Seschrock goto done; 6998b01c3b58Seschrock attr = NULL; 6999b01c3b58Seschrock 70003d7072f8Seschrock done: 70013d7072f8Seschrock if (attr) 70023d7072f8Seschrock sysevent_free_attr(attr); 7003b72b6bb1SAlan Somers 7004b72b6bb1SAlan Somers #endif 7005b72b6bb1SAlan Somers return (ev); 7006b72b6bb1SAlan Somers } 7007b72b6bb1SAlan Somers 7008b72b6bb1SAlan Somers static void 7009b72b6bb1SAlan Somers spa_event_post(sysevent_t *ev) 7010b72b6bb1SAlan Somers { 7011b72b6bb1SAlan Somers #ifdef _KERNEL 7012b72b6bb1SAlan Somers sysevent_id_t eid; 7013b72b6bb1SAlan Somers 7014b72b6bb1SAlan Somers (void) log_sysevent(ev, SE_SLEEP, &eid); 70153d7072f8Seschrock sysevent_free(ev); 70163d7072f8Seschrock #endif 70173d7072f8Seschrock } 7018b72b6bb1SAlan Somers 7019b72b6bb1SAlan Somers /* 7020b72b6bb1SAlan Somers * Post a sysevent corresponding to the given event. The 'name' must be one of 7021b72b6bb1SAlan Somers * the event definitions in sys/sysevent/eventdefs.h. The payload will be 7022*ce1577b0SDave Eddy * filled in from the spa and (optionally) the vdev and history nvl. This 7023*ce1577b0SDave Eddy * doesn't do anything in the userland libzpool, as we don't want consumers to 7024*ce1577b0SDave Eddy * misinterpret ztest or zdb as real changes. 7025b72b6bb1SAlan Somers */ 7026b72b6bb1SAlan Somers void 7027*ce1577b0SDave Eddy spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 7028b72b6bb1SAlan Somers { 7029*ce1577b0SDave Eddy spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 7030b72b6bb1SAlan Somers } 7031