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. 245aeb9474SGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 254263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 265aeb9474SGarrett D'Amore */ 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 30fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 31fa9e4066Sahrens * pool. 32fa9e4066Sahrens */ 33fa9e4066Sahrens 34fa9e4066Sahrens #include <sys/zfs_context.h> 35ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 36fa9e4066Sahrens #include <sys/spa_impl.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zio_checksum.h> 39fa9e4066Sahrens #include <sys/dmu.h> 40fa9e4066Sahrens #include <sys/dmu_tx.h> 41fa9e4066Sahrens #include <sys/zap.h> 42fa9e4066Sahrens #include <sys/zil.h> 43b24ab676SJeff Bonwick #include <sys/ddt.h> 44fa9e4066Sahrens #include <sys/vdev_impl.h> 45fa9e4066Sahrens #include <sys/metaslab.h> 4688ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 47fa9e4066Sahrens #include <sys/uberblock_impl.h> 48fa9e4066Sahrens #include <sys/txg.h> 49fa9e4066Sahrens #include <sys/avl.h> 50fa9e4066Sahrens #include <sys/dmu_traverse.h> 51b1b8ab34Slling #include <sys/dmu_objset.h> 52fa9e4066Sahrens #include <sys/unique.h> 53fa9e4066Sahrens #include <sys/dsl_pool.h> 54b1b8ab34Slling #include <sys/dsl_dataset.h> 55fa9e4066Sahrens #include <sys/dsl_dir.h> 56fa9e4066Sahrens #include <sys/dsl_prop.h> 57b1b8ab34Slling #include <sys/dsl_synctask.h> 58fa9e4066Sahrens #include <sys/fs/zfs.h> 59fa94a07fSbrendan #include <sys/arc.h> 60fa9e4066Sahrens #include <sys/callb.h> 6195173954Sek #include <sys/systeminfo.h> 62e7cbe64fSgw #include <sys/spa_boot.h> 63573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 643f9d6ad7SLin Ling #include <sys/dsl_scan.h> 65ad135b5dSChristopher Siden #include <sys/zfeature.h> 66fa9e4066Sahrens 675679c89fSjv #ifdef _KERNEL 68dedec472SJack Meng #include <sys/bootprops.h> 6935a5a358SJonathan Adams #include <sys/callb.h> 7035a5a358SJonathan Adams #include <sys/cpupart.h> 7135a5a358SJonathan Adams #include <sys/pool.h> 7235a5a358SJonathan Adams #include <sys/sysdc.h> 7335a5a358SJonathan Adams #include <sys/zone.h> 745679c89fSjv #endif /* _KERNEL */ 755679c89fSjv 76990b4856Slling #include "zfs_prop.h" 77b7b97454Sperrin #include "zfs_comutil.h" 78990b4856Slling 7935a5a358SJonathan Adams typedef enum zti_modes { 802e0c549eSJonathan Adams zti_mode_fixed, /* value is # of threads (min 1) */ 812e0c549eSJonathan Adams zti_mode_online_percent, /* value is % of online CPUs */ 8235a5a358SJonathan Adams zti_mode_batch, /* cpu-intensive; value is ignored */ 8380eb36f2SGeorge Wilson zti_mode_null, /* don't create a taskq */ 842e0c549eSJonathan Adams zti_nmodes 8535a5a358SJonathan Adams } zti_modes_t; 86416e0cd8Sek 8780eb36f2SGeorge Wilson #define ZTI_FIX(n) { zti_mode_fixed, (n) } 8880eb36f2SGeorge Wilson #define ZTI_PCT(n) { zti_mode_online_percent, (n) } 8935a5a358SJonathan Adams #define ZTI_BATCH { zti_mode_batch, 0 } 9080eb36f2SGeorge Wilson #define ZTI_NULL { zti_mode_null, 0 } 912e0c549eSJonathan Adams 9280eb36f2SGeorge Wilson #define ZTI_ONE ZTI_FIX(1) 932e0c549eSJonathan Adams 942e0c549eSJonathan Adams typedef struct zio_taskq_info { 9580eb36f2SGeorge Wilson enum zti_modes zti_mode; 9680eb36f2SGeorge Wilson uint_t zti_value; 972e0c549eSJonathan Adams } zio_taskq_info_t; 982e0c549eSJonathan Adams 992e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 10035a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1012e0c549eSJonathan Adams }; 1022e0c549eSJonathan Adams 10380eb36f2SGeorge Wilson /* 10480eb36f2SGeorge Wilson * Define the taskq threads for the following I/O types: 10580eb36f2SGeorge Wilson * NULL, READ, WRITE, FREE, CLAIM, and IOCTL 10680eb36f2SGeorge Wilson */ 10780eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 10880eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 10980eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11035a5a358SJonathan Adams { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, 11135a5a358SJonathan Adams { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) }, 112c7cd2421SGeorge Wilson { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11380eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 11480eb36f2SGeorge Wilson { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, 1152e0c549eSJonathan Adams }; 1162e0c549eSJonathan Adams 117ad135b5dSChristopher Siden static dsl_syncfunc_t spa_sync_version; 1183f9d6ad7SLin Ling static dsl_syncfunc_t spa_sync_props; 119dfbb9432SGeorge Wilson static dsl_checkfunc_t spa_change_guid_check; 120dfbb9432SGeorge Wilson static dsl_syncfunc_t spa_change_guid_sync; 12189a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1221195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1231195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1241195e687SMark J Musante char **ereport); 125cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 126990b4856Slling 12735a5a358SJonathan Adams uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 12835a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 12935a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 13035a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 13135a5a358SJonathan Adams 13235a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 13301f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 13435a5a358SJonathan Adams 13535a5a358SJonathan Adams /* 13635a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 13735a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 13835a5a358SJonathan Adams */ 13935a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 14035a5a358SJonathan Adams 141990b4856Slling /* 142990b4856Slling * ========================================================================== 143990b4856Slling * SPA properties routines 144990b4856Slling * ========================================================================== 145990b4856Slling */ 146990b4856Slling 147990b4856Slling /* 148990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 149990b4856Slling */ 1509d82f4f6Slling static void 151990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 152990b4856Slling uint64_t intval, zprop_source_t src) 153990b4856Slling { 154990b4856Slling const char *propname = zpool_prop_to_name(prop); 155990b4856Slling nvlist_t *propval; 156990b4856Slling 1579d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1589d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 159990b4856Slling 1609d82f4f6Slling if (strval != NULL) 1619d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1629d82f4f6Slling else 1639d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 164990b4856Slling 1659d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 166990b4856Slling nvlist_free(propval); 167990b4856Slling } 168990b4856Slling 169990b4856Slling /* 170990b4856Slling * Get property values from the spa configuration. 171990b4856Slling */ 1729d82f4f6Slling static void 173990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 174990b4856Slling { 1754263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 176ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 177379c004dSEric Schrock uint64_t size; 178485bbbf5SGeorge Wilson uint64_t alloc; 1794263d13fSGeorge Wilson uint64_t space; 180990b4856Slling uint64_t cap, version; 181990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 182c5904d13Seschrock spa_config_dirent_t *dp; 183990b4856Slling 184e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 185e14bb325SJeff Bonwick 1864263d13fSGeorge Wilson if (rvd != NULL) { 187485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 188b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 189379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 190379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 191485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 192485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 193485bbbf5SGeorge Wilson size - alloc, src); 1944263d13fSGeorge Wilson 1954263d13fSGeorge Wilson space = 0; 1964263d13fSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 1974263d13fSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 1984263d13fSGeorge Wilson space += tvd->vdev_max_asize - tvd->vdev_asize; 1994263d13fSGeorge Wilson } 2004263d13fSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space, 2014263d13fSGeorge Wilson src); 2024263d13fSGeorge Wilson 203f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 204f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 205379c004dSEric Schrock 206485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 207379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 208379c004dSEric Schrock 209b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 210b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 211b24ab676SJeff Bonwick 212379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2134263d13fSGeorge Wilson rvd->vdev_state, src); 214379c004dSEric Schrock 215379c004dSEric Schrock version = spa_version(spa); 216379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 217379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 218379c004dSEric Schrock else 219379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 220379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 221379c004dSEric Schrock } 222990b4856Slling 223ad135b5dSChristopher Siden if (pool != NULL) { 224ad135b5dSChristopher Siden dsl_dir_t *freedir = pool->dp_free_dir; 225ad135b5dSChristopher Siden 226ad135b5dSChristopher Siden /* 227ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 228ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 229ad135b5dSChristopher Siden */ 230ad135b5dSChristopher Siden if (freedir != NULL) { 231ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 232ad135b5dSChristopher Siden freedir->dd_phys->dd_used_bytes, src); 233ad135b5dSChristopher Siden } else { 234ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 235ad135b5dSChristopher Siden NULL, 0, src); 236ad135b5dSChristopher Siden } 237ad135b5dSChristopher Siden } 238ad135b5dSChristopher Siden 2399d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 240990b4856Slling 2418704186eSDan McDonald if (spa->spa_comment != NULL) { 2428704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2438704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2448704186eSDan McDonald } 2458704186eSDan McDonald 2469d82f4f6Slling if (spa->spa_root != NULL) 2479d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2489d82f4f6Slling 0, ZPROP_SRC_LOCAL); 249990b4856Slling 250c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 251c5904d13Seschrock if (dp->scd_path == NULL) { 2529d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 253c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 254c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2559d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 256c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2572f8aaab3Seschrock } 2582f8aaab3Seschrock } 259990b4856Slling } 260990b4856Slling 261990b4856Slling /* 262990b4856Slling * Get zpool property values. 263990b4856Slling */ 264990b4856Slling int 265990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 266990b4856Slling { 267b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 268990b4856Slling zap_cursor_t zc; 269990b4856Slling zap_attribute_t za; 270990b4856Slling int err; 271990b4856Slling 2729d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 273990b4856Slling 274e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 275e14bb325SJeff Bonwick 276990b4856Slling /* 277990b4856Slling * Get properties from the spa config. 278990b4856Slling */ 2799d82f4f6Slling spa_prop_get_config(spa, nvp); 280990b4856Slling 281990b4856Slling /* If no pool property object, no more prop to get. */ 282afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 283990b4856Slling mutex_exit(&spa->spa_props_lock); 284990b4856Slling return (0); 285990b4856Slling } 286990b4856Slling 287990b4856Slling /* 288990b4856Slling * Get properties from the MOS pool property object. 289990b4856Slling */ 290990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 291990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 292990b4856Slling zap_cursor_advance(&zc)) { 293990b4856Slling uint64_t intval = 0; 294990b4856Slling char *strval = NULL; 295990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 296990b4856Slling zpool_prop_t prop; 297990b4856Slling 298990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 299990b4856Slling continue; 300990b4856Slling 301990b4856Slling switch (za.za_integer_length) { 302990b4856Slling case 8: 303990b4856Slling /* integer property */ 304990b4856Slling if (za.za_first_integer != 305990b4856Slling zpool_prop_default_numeric(prop)) 306990b4856Slling src = ZPROP_SRC_LOCAL; 307990b4856Slling 308990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 309990b4856Slling dsl_pool_t *dp; 310990b4856Slling dsl_dataset_t *ds = NULL; 311990b4856Slling 312990b4856Slling dp = spa_get_dsl(spa); 313990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 314745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 315745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 316990b4856Slling rw_exit(&dp->dp_config_rwlock); 317990b4856Slling break; 318990b4856Slling } 319990b4856Slling 320990b4856Slling strval = kmem_alloc( 321990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 322990b4856Slling KM_SLEEP); 323990b4856Slling dsl_dataset_name(ds, strval); 324745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 325990b4856Slling rw_exit(&dp->dp_config_rwlock); 326990b4856Slling } else { 327990b4856Slling strval = NULL; 328990b4856Slling intval = za.za_first_integer; 329990b4856Slling } 330990b4856Slling 3319d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 332990b4856Slling 333990b4856Slling if (strval != NULL) 334990b4856Slling kmem_free(strval, 335990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 336990b4856Slling 337990b4856Slling break; 338990b4856Slling 339990b4856Slling case 1: 340990b4856Slling /* string property */ 341990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 342990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 343990b4856Slling za.za_name, 1, za.za_num_integers, strval); 344990b4856Slling if (err) { 345990b4856Slling kmem_free(strval, za.za_num_integers); 346990b4856Slling break; 347990b4856Slling } 3489d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 349990b4856Slling kmem_free(strval, za.za_num_integers); 350990b4856Slling break; 351990b4856Slling 352990b4856Slling default: 353990b4856Slling break; 354990b4856Slling } 355990b4856Slling } 356990b4856Slling zap_cursor_fini(&zc); 357990b4856Slling mutex_exit(&spa->spa_props_lock); 358990b4856Slling out: 359990b4856Slling if (err && err != ENOENT) { 360990b4856Slling nvlist_free(*nvp); 3619d82f4f6Slling *nvp = NULL; 362990b4856Slling return (err); 363990b4856Slling } 364990b4856Slling 365990b4856Slling return (0); 366990b4856Slling } 367990b4856Slling 368990b4856Slling /* 369990b4856Slling * Validate the given pool properties nvlist and modify the list 370990b4856Slling * for the property values to be set. 371990b4856Slling */ 372990b4856Slling static int 373990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 374990b4856Slling { 375990b4856Slling nvpair_t *elem; 376990b4856Slling int error = 0, reset_bootfs = 0; 377d5285caeSGeorge Wilson uint64_t objnum = 0; 378ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 379990b4856Slling 380990b4856Slling elem = NULL; 381990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 382990b4856Slling uint64_t intval; 383ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 384ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 385ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 386ad135b5dSChristopher Siden 387ad135b5dSChristopher Siden switch (prop) { 388ad135b5dSChristopher Siden case ZPROP_INVAL: 389ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 390ad135b5dSChristopher Siden error = EINVAL; 391ad135b5dSChristopher Siden break; 392ad135b5dSChristopher Siden } 393990b4856Slling 394ad135b5dSChristopher Siden /* 395ad135b5dSChristopher Siden * Sanitize the input. 396ad135b5dSChristopher Siden */ 397ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 398ad135b5dSChristopher Siden error = EINVAL; 399ad135b5dSChristopher Siden break; 400ad135b5dSChristopher Siden } 401990b4856Slling 402ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 403ad135b5dSChristopher Siden error = EINVAL; 404ad135b5dSChristopher Siden break; 405ad135b5dSChristopher Siden } 406ad135b5dSChristopher Siden 407ad135b5dSChristopher Siden if (intval != 0) { 408ad135b5dSChristopher Siden error = EINVAL; 409ad135b5dSChristopher Siden break; 410ad135b5dSChristopher Siden } 411ad135b5dSChristopher Siden 412ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 413ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 414ad135b5dSChristopher Siden error = EINVAL; 415ad135b5dSChristopher Siden break; 416ad135b5dSChristopher Siden } 417ad135b5dSChristopher Siden 418ad135b5dSChristopher Siden has_feature = B_TRUE; 419ad135b5dSChristopher Siden break; 420990b4856Slling 421990b4856Slling case ZPOOL_PROP_VERSION: 422990b4856Slling error = nvpair_value_uint64(elem, &intval); 423990b4856Slling if (!error && 424ad135b5dSChristopher Siden (intval < spa_version(spa) || 425ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 426ad135b5dSChristopher Siden has_feature)) 427990b4856Slling error = EINVAL; 428990b4856Slling break; 429990b4856Slling 430990b4856Slling case ZPOOL_PROP_DELEGATION: 431990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 432d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 433573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 434990b4856Slling error = nvpair_value_uint64(elem, &intval); 435990b4856Slling if (!error && intval > 1) 436990b4856Slling error = EINVAL; 437990b4856Slling break; 438990b4856Slling 439990b4856Slling case ZPOOL_PROP_BOOTFS: 44025f89ee2SJeff Bonwick /* 44125f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 44225f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 44325f89ee2SJeff Bonwick * the bootfs property cannot be set. 44425f89ee2SJeff Bonwick */ 445990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 446990b4856Slling error = ENOTSUP; 447990b4856Slling break; 448990b4856Slling } 449990b4856Slling 450990b4856Slling /* 45115e6edf1Sgw * Make sure the vdev config is bootable 452990b4856Slling */ 45315e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 454990b4856Slling error = ENOTSUP; 455990b4856Slling break; 456990b4856Slling } 457990b4856Slling 458990b4856Slling reset_bootfs = 1; 459990b4856Slling 460990b4856Slling error = nvpair_value_string(elem, &strval); 461990b4856Slling 462990b4856Slling if (!error) { 463ad135b5dSChristopher Siden objset_t *os; 46415e6edf1Sgw uint64_t compress; 46515e6edf1Sgw 466990b4856Slling if (strval == NULL || strval[0] == '\0') { 467990b4856Slling objnum = zpool_prop_default_numeric( 468990b4856Slling ZPOOL_PROP_BOOTFS); 469990b4856Slling break; 470990b4856Slling } 471990b4856Slling 472503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 473990b4856Slling break; 47415e6edf1Sgw 475503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 476503ad85cSMatthew Ahrens 477503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 478503ad85cSMatthew Ahrens error = ENOTSUP; 479503ad85cSMatthew Ahrens } else if ((error = dsl_prop_get_integer(strval, 48015e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 48115e6edf1Sgw &compress, NULL)) == 0 && 48215e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 48315e6edf1Sgw error = ENOTSUP; 48415e6edf1Sgw } else { 48515e6edf1Sgw objnum = dmu_objset_id(os); 48615e6edf1Sgw } 487503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 488990b4856Slling } 489990b4856Slling break; 490e14bb325SJeff Bonwick 4910a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 4920a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 4930a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 4940a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 4950a4e9518Sgw error = EINVAL; 4960a4e9518Sgw 4970a4e9518Sgw /* 4980a4e9518Sgw * This is a special case which only occurs when 4990a4e9518Sgw * the pool has completely failed. This allows 5000a4e9518Sgw * the user to change the in-core failmode property 5010a4e9518Sgw * without syncing it out to disk (I/Os might 5020a4e9518Sgw * currently be blocked). We do this by returning 5030a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5040a4e9518Sgw * into thinking we encountered a property validation 5050a4e9518Sgw * error. 5060a4e9518Sgw */ 507e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5080a4e9518Sgw spa->spa_failmode = intval; 5090a4e9518Sgw error = EIO; 5100a4e9518Sgw } 5110a4e9518Sgw break; 5122f8aaab3Seschrock 5132f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5142f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5152f8aaab3Seschrock break; 5162f8aaab3Seschrock 5172f8aaab3Seschrock if (strval[0] == '\0') 5182f8aaab3Seschrock break; 5192f8aaab3Seschrock 5202f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5212f8aaab3Seschrock break; 5222f8aaab3Seschrock 5232f8aaab3Seschrock if (strval[0] != '/') { 5242f8aaab3Seschrock error = EINVAL; 5252f8aaab3Seschrock break; 5262f8aaab3Seschrock } 5272f8aaab3Seschrock 5282f8aaab3Seschrock slash = strrchr(strval, '/'); 5292f8aaab3Seschrock ASSERT(slash != NULL); 5302f8aaab3Seschrock 5312f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5322f8aaab3Seschrock strcmp(slash, "/..") == 0) 5332f8aaab3Seschrock error = EINVAL; 5342f8aaab3Seschrock break; 535b24ab676SJeff Bonwick 5368704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5378704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5388704186eSDan McDonald break; 5398704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5408704186eSDan McDonald /* 5418704186eSDan McDonald * The kernel doesn't have an easy isprint() 5428704186eSDan McDonald * check. For this kernel check, we merely 5438704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5448704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5458704186eSDan McDonald */ 5468704186eSDan McDonald if (*check >= 0x7f) { 5478704186eSDan McDonald error = EINVAL; 5488704186eSDan McDonald break; 5498704186eSDan McDonald } 5508704186eSDan McDonald check++; 5518704186eSDan McDonald } 5528704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5538704186eSDan McDonald error = E2BIG; 5548704186eSDan McDonald break; 5558704186eSDan McDonald 556b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 557b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 558b24ab676SJeff Bonwick error = ENOTSUP; 559b24ab676SJeff Bonwick else 560b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 561b24ab676SJeff Bonwick if (error == 0 && 562b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 563b24ab676SJeff Bonwick error = EINVAL; 564b24ab676SJeff Bonwick break; 565990b4856Slling } 566990b4856Slling 567990b4856Slling if (error) 568990b4856Slling break; 569990b4856Slling } 570990b4856Slling 571990b4856Slling if (!error && reset_bootfs) { 572990b4856Slling error = nvlist_remove(props, 573990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 574990b4856Slling 575990b4856Slling if (!error) { 576990b4856Slling error = nvlist_add_uint64(props, 577990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 578990b4856Slling } 579990b4856Slling } 580990b4856Slling 581990b4856Slling return (error); 582990b4856Slling } 583990b4856Slling 584379c004dSEric Schrock void 585379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 586379c004dSEric Schrock { 587379c004dSEric Schrock char *cachefile; 588379c004dSEric Schrock spa_config_dirent_t *dp; 589379c004dSEric Schrock 590379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 591379c004dSEric Schrock &cachefile) != 0) 592379c004dSEric Schrock return; 593379c004dSEric Schrock 594379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 595379c004dSEric Schrock KM_SLEEP); 596379c004dSEric Schrock 597379c004dSEric Schrock if (cachefile[0] == '\0') 598379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 599379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 600379c004dSEric Schrock dp->scd_path = NULL; 601379c004dSEric Schrock else 602379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 603379c004dSEric Schrock 604379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 605379c004dSEric Schrock if (need_sync) 606379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 607379c004dSEric Schrock } 608379c004dSEric Schrock 609990b4856Slling int 610990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 611990b4856Slling { 612990b4856Slling int error; 613ad135b5dSChristopher Siden nvpair_t *elem = NULL; 614379c004dSEric Schrock boolean_t need_sync = B_FALSE; 615990b4856Slling 616990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 617990b4856Slling return (error); 618990b4856Slling 619379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 620ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 621379c004dSEric Schrock 622f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 623f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 624f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 625379c004dSEric Schrock continue; 626379c004dSEric Schrock 627ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 628ad135b5dSChristopher Siden uint64_t ver; 629ad135b5dSChristopher Siden 630ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 631ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 632ad135b5dSChristopher Siden } else { 633ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 634ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 635ad135b5dSChristopher Siden need_sync = B_TRUE; 636ad135b5dSChristopher Siden } 637ad135b5dSChristopher Siden 638ad135b5dSChristopher Siden /* Save time if the version is already set. */ 639ad135b5dSChristopher Siden if (ver == spa_version(spa)) 640ad135b5dSChristopher Siden continue; 641ad135b5dSChristopher Siden 642ad135b5dSChristopher Siden /* 643ad135b5dSChristopher Siden * In addition to the pool directory object, we might 644ad135b5dSChristopher Siden * create the pool properties object, the features for 645ad135b5dSChristopher Siden * read object, the features for write object, or the 646ad135b5dSChristopher Siden * feature descriptions object. 647ad135b5dSChristopher Siden */ 648ad135b5dSChristopher Siden error = dsl_sync_task_do(spa_get_dsl(spa), NULL, 649ad135b5dSChristopher Siden spa_sync_version, spa, &ver, 6); 650ad135b5dSChristopher Siden if (error) 651ad135b5dSChristopher Siden return (error); 652ad135b5dSChristopher Siden continue; 653ad135b5dSChristopher Siden } 654ad135b5dSChristopher Siden 655379c004dSEric Schrock need_sync = B_TRUE; 656379c004dSEric Schrock break; 657379c004dSEric Schrock } 658379c004dSEric Schrock 659ad135b5dSChristopher Siden if (need_sync) { 660379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 661ad135b5dSChristopher Siden spa, nvp, 6)); 662ad135b5dSChristopher Siden } 663ad135b5dSChristopher Siden 664ad135b5dSChristopher Siden return (0); 665990b4856Slling } 666990b4856Slling 667990b4856Slling /* 668990b4856Slling * If the bootfs property value is dsobj, clear it. 669990b4856Slling */ 670990b4856Slling void 671990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 672990b4856Slling { 673990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 674990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 675990b4856Slling spa->spa_pool_props_object, 676990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 677990b4856Slling spa->spa_bootfs = 0; 678990b4856Slling } 679990b4856Slling } 680990b4856Slling 681dfbb9432SGeorge Wilson /*ARGSUSED*/ 682dfbb9432SGeorge Wilson static int 683dfbb9432SGeorge Wilson spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx) 684dfbb9432SGeorge Wilson { 685dfbb9432SGeorge Wilson spa_t *spa = arg1; 686dfbb9432SGeorge Wilson uint64_t *newguid = arg2; 687dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 688dfbb9432SGeorge Wilson uint64_t vdev_state; 689dfbb9432SGeorge Wilson 690dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 691dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 692dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 693dfbb9432SGeorge Wilson 694dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 695dfbb9432SGeorge Wilson return (ENXIO); 696dfbb9432SGeorge Wilson 697dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 698dfbb9432SGeorge Wilson 699dfbb9432SGeorge Wilson return (0); 700dfbb9432SGeorge Wilson } 701dfbb9432SGeorge Wilson 702dfbb9432SGeorge Wilson static void 703dfbb9432SGeorge Wilson spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx) 704dfbb9432SGeorge Wilson { 705dfbb9432SGeorge Wilson spa_t *spa = arg1; 706dfbb9432SGeorge Wilson uint64_t *newguid = arg2; 707dfbb9432SGeorge Wilson uint64_t oldguid; 708dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 709dfbb9432SGeorge Wilson 710dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 711dfbb9432SGeorge Wilson 712dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 713dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 714dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 715dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 716dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 717dfbb9432SGeorge Wilson 718*20128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 719dfbb9432SGeorge Wilson oldguid, *newguid); 720dfbb9432SGeorge Wilson } 721dfbb9432SGeorge Wilson 722e9103aaeSGarrett D'Amore /* 723e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 724e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 725e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 726e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 727e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 728e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 729e9103aaeSGarrett D'Amore * sysevent to update any watchers. 730e9103aaeSGarrett D'Amore */ 731e9103aaeSGarrett D'Amore int 732e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 733e9103aaeSGarrett D'Amore { 734dfbb9432SGeorge Wilson int error; 735dfbb9432SGeorge Wilson uint64_t guid; 736e9103aaeSGarrett D'Amore 737dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 738dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 739e9103aaeSGarrett D'Amore 740dfbb9432SGeorge Wilson error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check, 741dfbb9432SGeorge Wilson spa_change_guid_sync, spa, &guid, 5); 742e9103aaeSGarrett D'Amore 743dfbb9432SGeorge Wilson if (error == 0) { 744dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 745dfbb9432SGeorge Wilson spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); 746dfbb9432SGeorge Wilson } 747e9103aaeSGarrett D'Amore 748dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 749e9103aaeSGarrett D'Amore 750dfbb9432SGeorge Wilson return (error); 751e9103aaeSGarrett D'Amore } 752e9103aaeSGarrett D'Amore 753fa9e4066Sahrens /* 754fa9e4066Sahrens * ========================================================================== 755fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 756fa9e4066Sahrens * ========================================================================== 757fa9e4066Sahrens */ 758fa9e4066Sahrens 759ea8dc4b6Seschrock static int 760ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 761ea8dc4b6Seschrock { 762ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 763ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 764ea8dc4b6Seschrock int ret; 765ea8dc4b6Seschrock 766ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 767ea8dc4b6Seschrock sizeof (zbookmark_t)); 768ea8dc4b6Seschrock 769ea8dc4b6Seschrock if (ret < 0) 770ea8dc4b6Seschrock return (-1); 771ea8dc4b6Seschrock else if (ret > 0) 772ea8dc4b6Seschrock return (1); 773ea8dc4b6Seschrock else 774ea8dc4b6Seschrock return (0); 775ea8dc4b6Seschrock } 776ea8dc4b6Seschrock 777ea8dc4b6Seschrock /* 778ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 779ea8dc4b6Seschrock * re-initializes them in the process. 780ea8dc4b6Seschrock */ 781ea8dc4b6Seschrock void 782ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 783ea8dc4b6Seschrock { 784ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 785ea8dc4b6Seschrock 786ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 787ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 788ea8dc4b6Seschrock 789ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 790ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 791ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 792ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 793ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 794ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 795ea8dc4b6Seschrock } 796ea8dc4b6Seschrock 79735a5a358SJonathan Adams static taskq_t * 79835a5a358SJonathan Adams spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, 79935a5a358SJonathan Adams uint_t value) 800fa9e4066Sahrens { 8015aeb9474SGarrett D'Amore uint_t flags = 0; 80235a5a358SJonathan Adams boolean_t batch = B_FALSE; 803fa9e4066Sahrens 80435a5a358SJonathan Adams switch (mode) { 80535a5a358SJonathan Adams case zti_mode_null: 80635a5a358SJonathan Adams return (NULL); /* no taskq needed */ 807fa9e4066Sahrens 80835a5a358SJonathan Adams case zti_mode_fixed: 80935a5a358SJonathan Adams ASSERT3U(value, >=, 1); 81035a5a358SJonathan Adams value = MAX(value, 1); 81135a5a358SJonathan Adams break; 812fa9e4066Sahrens 81335a5a358SJonathan Adams case zti_mode_batch: 81435a5a358SJonathan Adams batch = B_TRUE; 81535a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 81635a5a358SJonathan Adams value = zio_taskq_batch_pct; 81735a5a358SJonathan Adams break; 81835a5a358SJonathan Adams 81935a5a358SJonathan Adams case zti_mode_online_percent: 82035a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT; 82135a5a358SJonathan Adams break; 82235a5a358SJonathan Adams 82335a5a358SJonathan Adams default: 82435a5a358SJonathan Adams panic("unrecognized mode for %s taskq (%u:%u) in " 82535a5a358SJonathan Adams "spa_activate()", 82635a5a358SJonathan Adams name, mode, value); 82735a5a358SJonathan Adams break; 82835a5a358SJonathan Adams } 82935a5a358SJonathan Adams 83035a5a358SJonathan Adams if (zio_taskq_sysdc && spa->spa_proc != &p0) { 83135a5a358SJonathan Adams if (batch) 83235a5a358SJonathan Adams flags |= TASKQ_DC_BATCH; 83335a5a358SJonathan Adams 83435a5a358SJonathan Adams return (taskq_create_sysdc(name, value, 50, INT_MAX, 83535a5a358SJonathan Adams spa->spa_proc, zio_taskq_basedc, flags)); 83635a5a358SJonathan Adams } 83735a5a358SJonathan Adams return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, 83835a5a358SJonathan Adams spa->spa_proc, flags)); 83935a5a358SJonathan Adams } 84035a5a358SJonathan Adams 84135a5a358SJonathan Adams static void 84235a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 84335a5a358SJonathan Adams { 844e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 845e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 84680eb36f2SGeorge Wilson const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 84780eb36f2SGeorge Wilson enum zti_modes mode = ztip->zti_mode; 84880eb36f2SGeorge Wilson uint_t value = ztip->zti_value; 8492e0c549eSJonathan Adams char name[32]; 8502e0c549eSJonathan Adams 8512e0c549eSJonathan Adams (void) snprintf(name, sizeof (name), 85280eb36f2SGeorge Wilson "%s_%s", zio_type_name[t], zio_taskq_types[q]); 8532e0c549eSJonathan Adams 85435a5a358SJonathan Adams spa->spa_zio_taskq[t][q] = 85535a5a358SJonathan Adams spa_taskq_create(spa, name, mode, value); 85635a5a358SJonathan Adams } 85735a5a358SJonathan Adams } 85835a5a358SJonathan Adams } 85935a5a358SJonathan Adams 86035a5a358SJonathan Adams #ifdef _KERNEL 86135a5a358SJonathan Adams static void 86235a5a358SJonathan Adams spa_thread(void *arg) 86335a5a358SJonathan Adams { 86435a5a358SJonathan Adams callb_cpr_t cprinfo; 8652e0c549eSJonathan Adams 86635a5a358SJonathan Adams spa_t *spa = arg; 86735a5a358SJonathan Adams user_t *pu = PTOU(curproc); 8682e0c549eSJonathan Adams 86935a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 87035a5a358SJonathan Adams spa->spa_name); 8712e0c549eSJonathan Adams 87235a5a358SJonathan Adams ASSERT(curproc != &p0); 87335a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 87435a5a358SJonathan Adams "zpool-%s", spa->spa_name); 87535a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 8762e0c549eSJonathan Adams 87735a5a358SJonathan Adams /* bind this thread to the requested psrset */ 87835a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 87935a5a358SJonathan Adams pool_lock(); 88035a5a358SJonathan Adams mutex_enter(&cpu_lock); 88135a5a358SJonathan Adams mutex_enter(&pidlock); 88235a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 88380eb36f2SGeorge Wilson 88435a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 88535a5a358SJonathan Adams 0, NULL, NULL) == 0) { 88635a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 88735a5a358SJonathan Adams } else { 88835a5a358SJonathan Adams cmn_err(CE_WARN, 88935a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 89035a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 89135a5a358SJonathan Adams } 89235a5a358SJonathan Adams 89335a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 89435a5a358SJonathan Adams mutex_exit(&pidlock); 89535a5a358SJonathan Adams mutex_exit(&cpu_lock); 89635a5a358SJonathan Adams pool_unlock(); 89735a5a358SJonathan Adams } 89835a5a358SJonathan Adams 89935a5a358SJonathan Adams if (zio_taskq_sysdc) { 90035a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 90135a5a358SJonathan Adams } 90235a5a358SJonathan Adams 90335a5a358SJonathan Adams spa->spa_proc = curproc; 90435a5a358SJonathan Adams spa->spa_did = curthread->t_did; 90535a5a358SJonathan Adams 90635a5a358SJonathan Adams spa_create_zio_taskqs(spa); 90735a5a358SJonathan Adams 90835a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 90935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 91035a5a358SJonathan Adams 91135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 91235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 91335a5a358SJonathan Adams 91435a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 91535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 91635a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 91735a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 91835a5a358SJonathan Adams 91935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 92035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 92135a5a358SJonathan Adams spa->spa_proc = &p0; 92235a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 92335a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 92435a5a358SJonathan Adams 92535a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 92635a5a358SJonathan Adams lwp_exit(); 92735a5a358SJonathan Adams } 92835a5a358SJonathan Adams #endif 92935a5a358SJonathan Adams 93035a5a358SJonathan Adams /* 93135a5a358SJonathan Adams * Activate an uninitialized pool. 93235a5a358SJonathan Adams */ 93335a5a358SJonathan Adams static void 93435a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 93535a5a358SJonathan Adams { 93635a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 93735a5a358SJonathan Adams 93835a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 93935a5a358SJonathan Adams spa->spa_mode = mode; 94035a5a358SJonathan Adams 94135a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 94235a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 94335a5a358SJonathan Adams 94435a5a358SJonathan Adams /* Try to create a covering process */ 94535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 94635a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 94735a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 94835a5a358SJonathan Adams spa->spa_did = 0; 94935a5a358SJonathan Adams 95035a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 95135a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 95235a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 95335a5a358SJonathan Adams NULL, 0) == 0) { 95435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 95535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 95635a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 95735a5a358SJonathan Adams &spa->spa_proc_lock); 9582e0c549eSJonathan Adams } 95935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 96035a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 96135a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 96235a5a358SJonathan Adams } else { 96335a5a358SJonathan Adams #ifdef _KERNEL 96435a5a358SJonathan Adams cmn_err(CE_WARN, 96535a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 96635a5a358SJonathan Adams spa->spa_name); 96735a5a358SJonathan Adams #endif 968e14bb325SJeff Bonwick } 969fa9e4066Sahrens } 97035a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 97135a5a358SJonathan Adams 97235a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 97335a5a358SJonathan Adams if (spa->spa_proc == &p0) { 97435a5a358SJonathan Adams spa_create_zio_taskqs(spa); 97535a5a358SJonathan Adams } 976fa9e4066Sahrens 977e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 978e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 979e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 980e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 981fa9e4066Sahrens 982fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 983fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 984ea8dc4b6Seschrock 985ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 986ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 987ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 988ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 989ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 990ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 991fa9e4066Sahrens } 992fa9e4066Sahrens 993fa9e4066Sahrens /* 994fa9e4066Sahrens * Opposite of spa_activate(). 995fa9e4066Sahrens */ 996fa9e4066Sahrens static void 997fa9e4066Sahrens spa_deactivate(spa_t *spa) 998fa9e4066Sahrens { 999fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1000fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1001fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 100225f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1003fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1004fa9e4066Sahrens 1005fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1006fa9e4066Sahrens 1007e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1008e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1009fa9e4066Sahrens 1010e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1011e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 101280eb36f2SGeorge Wilson if (spa->spa_zio_taskq[t][q] != NULL) 101380eb36f2SGeorge Wilson taskq_destroy(spa->spa_zio_taskq[t][q]); 1014e14bb325SJeff Bonwick spa->spa_zio_taskq[t][q] = NULL; 1015e14bb325SJeff Bonwick } 1016fa9e4066Sahrens } 1017fa9e4066Sahrens 1018fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1019fa9e4066Sahrens spa->spa_normal_class = NULL; 1020fa9e4066Sahrens 10218654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 10228654d025Sperrin spa->spa_log_class = NULL; 10238654d025Sperrin 1024ea8dc4b6Seschrock /* 1025ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1026ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1027ea8dc4b6Seschrock */ 1028ea8dc4b6Seschrock spa_errlog_drain(spa); 1029ea8dc4b6Seschrock 1030ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1031ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1032ea8dc4b6Seschrock 1033fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 103435a5a358SJonathan Adams 103535a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 103635a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 103735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 103835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 103935a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 104035a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 104135a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 104235a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 104335a5a358SJonathan Adams } 104435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 104535a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 104635a5a358SJonathan Adams } 104735a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 104835a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 104935a5a358SJonathan Adams 105035a5a358SJonathan Adams /* 105135a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 105235a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 105335a5a358SJonathan Adams * it. 105435a5a358SJonathan Adams */ 105535a5a358SJonathan Adams if (spa->spa_did != 0) { 105635a5a358SJonathan Adams thread_join(spa->spa_did); 105735a5a358SJonathan Adams spa->spa_did = 0; 105835a5a358SJonathan Adams } 1059fa9e4066Sahrens } 1060fa9e4066Sahrens 1061fa9e4066Sahrens /* 1062fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1063fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1064fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1065fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1066fa9e4066Sahrens */ 106799653d4eSeschrock static int 106899653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 106999653d4eSeschrock uint_t id, int atype) 1070fa9e4066Sahrens { 1071fa9e4066Sahrens nvlist_t **child; 1072573ca77eSGeorge Wilson uint_t children; 107399653d4eSeschrock int error; 1074fa9e4066Sahrens 107599653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 107699653d4eSeschrock return (error); 1077fa9e4066Sahrens 107899653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 107999653d4eSeschrock return (0); 1080fa9e4066Sahrens 1081e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1082e14bb325SJeff Bonwick &child, &children); 1083e14bb325SJeff Bonwick 1084e14bb325SJeff Bonwick if (error == ENOENT) 1085e14bb325SJeff Bonwick return (0); 1086e14bb325SJeff Bonwick 1087e14bb325SJeff Bonwick if (error) { 108899653d4eSeschrock vdev_free(*vdp); 108999653d4eSeschrock *vdp = NULL; 109099653d4eSeschrock return (EINVAL); 1091fa9e4066Sahrens } 1092fa9e4066Sahrens 1093573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 109499653d4eSeschrock vdev_t *vd; 109599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 109699653d4eSeschrock atype)) != 0) { 109799653d4eSeschrock vdev_free(*vdp); 109899653d4eSeschrock *vdp = NULL; 109999653d4eSeschrock return (error); 1100fa9e4066Sahrens } 1101fa9e4066Sahrens } 1102fa9e4066Sahrens 110399653d4eSeschrock ASSERT(*vdp != NULL); 110499653d4eSeschrock 110599653d4eSeschrock return (0); 1106fa9e4066Sahrens } 1107fa9e4066Sahrens 1108fa9e4066Sahrens /* 1109fa9e4066Sahrens * Opposite of spa_load(). 1110fa9e4066Sahrens */ 1111fa9e4066Sahrens static void 1112fa9e4066Sahrens spa_unload(spa_t *spa) 1113fa9e4066Sahrens { 111499653d4eSeschrock int i; 111599653d4eSeschrock 1116e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1117e14bb325SJeff Bonwick 1118ea8dc4b6Seschrock /* 1119ea8dc4b6Seschrock * Stop async tasks. 1120ea8dc4b6Seschrock */ 1121ea8dc4b6Seschrock spa_async_suspend(spa); 1122ea8dc4b6Seschrock 1123fa9e4066Sahrens /* 1124fa9e4066Sahrens * Stop syncing. 1125fa9e4066Sahrens */ 1126fa9e4066Sahrens if (spa->spa_sync_on) { 1127fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1128fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1129fa9e4066Sahrens } 1130fa9e4066Sahrens 1131fa9e4066Sahrens /* 1132e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1133fa9e4066Sahrens */ 113454d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 113554d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 113654d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 113754d692b7SGeorge Wilson } 1138fa9e4066Sahrens 1139cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1140cde58dbcSMatthew Ahrens 1141fa9e4066Sahrens /* 1142fa9e4066Sahrens * Close the dsl pool. 1143fa9e4066Sahrens */ 1144fa9e4066Sahrens if (spa->spa_dsl_pool) { 1145fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1146fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1147afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1148fa9e4066Sahrens } 1149fa9e4066Sahrens 1150b24ab676SJeff Bonwick ddt_unload(spa); 1151b24ab676SJeff Bonwick 11528ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 11538ad4d6ddSJeff Bonwick 11548ad4d6ddSJeff Bonwick /* 11558ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 11568ad4d6ddSJeff Bonwick */ 11578ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 11588ad4d6ddSJeff Bonwick 1159fa9e4066Sahrens /* 1160fa9e4066Sahrens * Close all vdevs. 1161fa9e4066Sahrens */ 11620e34b6a7Sbonwick if (spa->spa_root_vdev) 1163fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 11640e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 1165ea8dc4b6Seschrock 1166fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1167fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1168fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1169fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1170fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1171fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 117299653d4eSeschrock } 1173fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1174fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1175fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1176fa94a07fSbrendan } 11772ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1178fa94a07fSbrendan 1179cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1180cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1181fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1182cd0837ccSGeorge Wilson } 1183fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1184fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1185fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1186fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1187fa94a07fSbrendan } 1188fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1189fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1190fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 119199653d4eSeschrock } 11922ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 119399653d4eSeschrock 1194ea8dc4b6Seschrock spa->spa_async_suspended = 0; 11958ad4d6ddSJeff Bonwick 11968704186eSDan McDonald if (spa->spa_comment != NULL) { 11978704186eSDan McDonald spa_strfree(spa->spa_comment); 11988704186eSDan McDonald spa->spa_comment = NULL; 11998704186eSDan McDonald } 12008704186eSDan McDonald 12018ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1202fa9e4066Sahrens } 1203fa9e4066Sahrens 120499653d4eSeschrock /* 120599653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 120699653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1207fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1208fa94a07fSbrendan * then re-generate a more complete list including status information. 120999653d4eSeschrock */ 121099653d4eSeschrock static void 121199653d4eSeschrock spa_load_spares(spa_t *spa) 121299653d4eSeschrock { 121399653d4eSeschrock nvlist_t **spares; 121499653d4eSeschrock uint_t nspares; 121599653d4eSeschrock int i; 121639c23413Seschrock vdev_t *vd, *tvd; 121799653d4eSeschrock 1218e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1219e14bb325SJeff Bonwick 122099653d4eSeschrock /* 122199653d4eSeschrock * First, close and free any existing spare vdevs. 122299653d4eSeschrock */ 1223fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1224fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 122539c23413Seschrock 122639c23413Seschrock /* Undo the call to spa_activate() below */ 1227c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1228c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 122939c23413Seschrock spa_spare_remove(tvd); 123039c23413Seschrock vdev_close(vd); 123139c23413Seschrock vdev_free(vd); 123299653d4eSeschrock } 123339c23413Seschrock 1234fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1235fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1236fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 123799653d4eSeschrock 1238fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 123999653d4eSeschrock nspares = 0; 124099653d4eSeschrock else 1241fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 124299653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 124399653d4eSeschrock 1244fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1245fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 124699653d4eSeschrock 124799653d4eSeschrock if (nspares == 0) 124899653d4eSeschrock return; 124999653d4eSeschrock 125099653d4eSeschrock /* 125199653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 125239c23413Seschrock * process. For each spare, there is potentially two different vdev_t 125339c23413Seschrock * structures associated with it: one in the list of spares (used only 125439c23413Seschrock * for basic validation purposes) and one in the active vdev 125539c23413Seschrock * configuration (if it's spared in). During this phase we open and 125639c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 125739c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 125899653d4eSeschrock */ 1259fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1260fa94a07fSbrendan KM_SLEEP); 1261fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 126299653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 126399653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 126499653d4eSeschrock ASSERT(vd != NULL); 126599653d4eSeschrock 1266fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 126799653d4eSeschrock 1268c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1269c5904d13Seschrock B_FALSE)) != NULL) { 127039c23413Seschrock if (!tvd->vdev_isspare) 127139c23413Seschrock spa_spare_add(tvd); 127239c23413Seschrock 127339c23413Seschrock /* 127439c23413Seschrock * We only mark the spare active if we were successfully 127539c23413Seschrock * able to load the vdev. Otherwise, importing a pool 127639c23413Seschrock * with a bad active spare would result in strange 127739c23413Seschrock * behavior, because multiple pool would think the spare 127839c23413Seschrock * is actively in use. 127939c23413Seschrock * 128039c23413Seschrock * There is a vulnerability here to an equally bizarre 128139c23413Seschrock * circumstance, where a dead active spare is later 128239c23413Seschrock * brought back to life (onlined or otherwise). Given 128339c23413Seschrock * the rarity of this scenario, and the extra complexity 128439c23413Seschrock * it adds, we ignore the possibility. 128539c23413Seschrock */ 128639c23413Seschrock if (!vdev_is_dead(tvd)) 128739c23413Seschrock spa_spare_activate(tvd); 128839c23413Seschrock } 128939c23413Seschrock 1290e14bb325SJeff Bonwick vd->vdev_top = vd; 12916809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1292e14bb325SJeff Bonwick 129399653d4eSeschrock if (vdev_open(vd) != 0) 129499653d4eSeschrock continue; 129599653d4eSeschrock 1296fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1297fa94a07fSbrendan spa_spare_add(vd); 129899653d4eSeschrock } 129999653d4eSeschrock 130099653d4eSeschrock /* 130199653d4eSeschrock * Recompute the stashed list of spares, with status information 130299653d4eSeschrock * this time. 130399653d4eSeschrock */ 1304fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 130599653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 130699653d4eSeschrock 1307fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1308fa94a07fSbrendan KM_SLEEP); 1309fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1310fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 13113f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1312fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1313fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1314fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 131599653d4eSeschrock nvlist_free(spares[i]); 1316fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1317fa94a07fSbrendan } 1318fa94a07fSbrendan 1319fa94a07fSbrendan /* 1320fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1321fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1322fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1323fa94a07fSbrendan * then re-generate a more complete list including status information. 1324fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1325fa94a07fSbrendan * not re-opened. 1326fa94a07fSbrendan */ 1327fa94a07fSbrendan static void 1328fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1329fa94a07fSbrendan { 1330fa94a07fSbrendan nvlist_t **l2cache; 1331fa94a07fSbrendan uint_t nl2cache; 1332fa94a07fSbrendan int i, j, oldnvdevs; 1333573ca77eSGeorge Wilson uint64_t guid; 1334fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1335fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1336fa94a07fSbrendan 1337e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1338e14bb325SJeff Bonwick 1339fa94a07fSbrendan if (sav->sav_config != NULL) { 1340fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1341fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1342fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1343fa94a07fSbrendan } else { 1344fa94a07fSbrendan nl2cache = 0; 1345d5285caeSGeorge Wilson newvdevs = NULL; 1346fa94a07fSbrendan } 1347fa94a07fSbrendan 1348fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1349fa94a07fSbrendan oldnvdevs = sav->sav_count; 1350fa94a07fSbrendan sav->sav_vdevs = NULL; 1351fa94a07fSbrendan sav->sav_count = 0; 1352fa94a07fSbrendan 1353fa94a07fSbrendan /* 1354fa94a07fSbrendan * Process new nvlist of vdevs. 1355fa94a07fSbrendan */ 1356fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1357fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1358fa94a07fSbrendan &guid) == 0); 1359fa94a07fSbrendan 1360fa94a07fSbrendan newvdevs[i] = NULL; 1361fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1362fa94a07fSbrendan vd = oldvdevs[j]; 1363fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1364fa94a07fSbrendan /* 1365fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1366fa94a07fSbrendan */ 1367fa94a07fSbrendan newvdevs[i] = vd; 1368fa94a07fSbrendan oldvdevs[j] = NULL; 1369fa94a07fSbrendan break; 1370fa94a07fSbrendan } 1371fa94a07fSbrendan } 1372fa94a07fSbrendan 1373fa94a07fSbrendan if (newvdevs[i] == NULL) { 1374fa94a07fSbrendan /* 1375fa94a07fSbrendan * Create new vdev 1376fa94a07fSbrendan */ 1377fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1378fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1379fa94a07fSbrendan ASSERT(vd != NULL); 1380fa94a07fSbrendan newvdevs[i] = vd; 1381fa94a07fSbrendan 1382fa94a07fSbrendan /* 1383fa94a07fSbrendan * Commit this vdev as an l2cache device, 1384fa94a07fSbrendan * even if it fails to open. 1385fa94a07fSbrendan */ 1386fa94a07fSbrendan spa_l2cache_add(vd); 1387fa94a07fSbrendan 1388c5904d13Seschrock vd->vdev_top = vd; 1389c5904d13Seschrock vd->vdev_aux = sav; 1390c5904d13Seschrock 1391c5904d13Seschrock spa_l2cache_activate(vd); 1392c5904d13Seschrock 1393fa94a07fSbrendan if (vdev_open(vd) != 0) 1394fa94a07fSbrendan continue; 1395fa94a07fSbrendan 1396fa94a07fSbrendan (void) vdev_validate_aux(vd); 1397fa94a07fSbrendan 1398573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1399573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1400fa94a07fSbrendan } 1401fa94a07fSbrendan } 1402fa94a07fSbrendan 1403fa94a07fSbrendan /* 1404fa94a07fSbrendan * Purge vdevs that were dropped 1405fa94a07fSbrendan */ 1406fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1407fa94a07fSbrendan uint64_t pool; 1408fa94a07fSbrendan 1409fa94a07fSbrendan vd = oldvdevs[i]; 1410fa94a07fSbrendan if (vd != NULL) { 1411cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1412cd0837ccSGeorge Wilson 14138ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 14148ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1415fa94a07fSbrendan l2arc_remove_vdev(vd); 1416cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1417cd0837ccSGeorge Wilson vdev_free(vd); 1418fa94a07fSbrendan } 1419fa94a07fSbrendan } 1420fa94a07fSbrendan 1421fa94a07fSbrendan if (oldvdevs) 1422fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1423fa94a07fSbrendan 1424fa94a07fSbrendan if (sav->sav_config == NULL) 1425fa94a07fSbrendan goto out; 1426fa94a07fSbrendan 1427fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1428fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1429fa94a07fSbrendan 1430fa94a07fSbrendan /* 1431fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1432fa94a07fSbrendan * information this time. 1433fa94a07fSbrendan */ 1434fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1435fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1436fa94a07fSbrendan 1437fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1438fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1439fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 14403f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1441fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1442fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1443fa94a07fSbrendan out: 1444fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1445fa94a07fSbrendan nvlist_free(l2cache[i]); 1446fa94a07fSbrendan if (sav->sav_count) 1447fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 144899653d4eSeschrock } 144999653d4eSeschrock 145099653d4eSeschrock static int 145199653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 145299653d4eSeschrock { 145399653d4eSeschrock dmu_buf_t *db; 145499653d4eSeschrock char *packed = NULL; 145599653d4eSeschrock size_t nvsize = 0; 145699653d4eSeschrock int error; 145799653d4eSeschrock *value = NULL; 145899653d4eSeschrock 145999653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 146099653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 146199653d4eSeschrock dmu_buf_rele(db, FTAG); 146299653d4eSeschrock 146399653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 14647bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 14657bfdf011SNeil Perrin DMU_READ_PREFETCH); 146699653d4eSeschrock if (error == 0) 146799653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 146899653d4eSeschrock kmem_free(packed, nvsize); 146999653d4eSeschrock 147099653d4eSeschrock return (error); 147199653d4eSeschrock } 147299653d4eSeschrock 14733d7072f8Seschrock /* 14743d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 14753d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 14763d7072f8Seschrock */ 14773d7072f8Seschrock static void 14783d7072f8Seschrock spa_check_removed(vdev_t *vd) 14793d7072f8Seschrock { 1480573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 14813d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 14823d7072f8Seschrock 14833d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 14843d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 14853d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 14863d7072f8Seschrock } 14873d7072f8Seschrock } 14883d7072f8Seschrock 1489e6ca193dSGeorge Wilson /* 14904b964adaSGeorge Wilson * Validate the current config against the MOS config 1491e6ca193dSGeorge Wilson */ 14924b964adaSGeorge Wilson static boolean_t 14934b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1494e6ca193dSGeorge Wilson { 14954b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 14964b964adaSGeorge Wilson nvlist_t *nv; 14974b964adaSGeorge Wilson 14984b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 14994b964adaSGeorge Wilson 15004b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15014b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 15024b964adaSGeorge Wilson 15034b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1504e6ca193dSGeorge Wilson 150588ecc943SGeorge Wilson /* 15064b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 15074b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 15084b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 150988ecc943SGeorge Wilson */ 15104b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 15114b964adaSGeorge Wilson nvlist_t **child, *nv; 15124b964adaSGeorge Wilson uint64_t idx = 0; 15134b964adaSGeorge Wilson 15144b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 15154b964adaSGeorge Wilson KM_SLEEP); 15164b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1517e6ca193dSGeorge Wilson 15184b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 15194b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 15204b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 15214b964adaSGeorge Wilson 15224b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 15234b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 15244b964adaSGeorge Wilson mtvd->vdev_islog) 15254b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 15264b964adaSGeorge Wilson B_FALSE, 0); 15274b964adaSGeorge Wilson } 15284b964adaSGeorge Wilson 15294b964adaSGeorge Wilson if (idx) { 15304b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 15314b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 15324b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 15334b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 15344b964adaSGeorge Wilson 15354b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 15364b964adaSGeorge Wilson nvlist_free(child[i]); 15374b964adaSGeorge Wilson } 15384b964adaSGeorge Wilson nvlist_free(nv); 15394b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 15404b964adaSGeorge Wilson } 15414b964adaSGeorge Wilson 15424b964adaSGeorge Wilson /* 15434b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 15444b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 15454b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 15464b964adaSGeorge Wilson */ 154788ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 15484b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 15494b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 15504b964adaSGeorge Wilson 15514b964adaSGeorge Wilson /* 15524b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 15534b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 15544b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 15554b964adaSGeorge Wilson */ 15564b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 15574b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 15584b964adaSGeorge Wilson 15594b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 15604b964adaSGeorge Wilson continue; 15614b964adaSGeorge Wilson 15624b964adaSGeorge Wilson /* 15634b964adaSGeorge Wilson * Device specific actions. 15644b964adaSGeorge Wilson */ 15654b964adaSGeorge Wilson if (mtvd->vdev_islog) { 15664b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 15674b964adaSGeorge Wilson } else { 15684b964adaSGeorge Wilson /* 15694b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 15704b964adaSGeorge Wilson * support we should be able to handle 15714b964adaSGeorge Wilson * missing data devices by transitioning 15724b964adaSGeorge Wilson * the pool to readonly. 15734b964adaSGeorge Wilson */ 15744b964adaSGeorge Wilson continue; 15754b964adaSGeorge Wilson } 15764b964adaSGeorge Wilson 15774b964adaSGeorge Wilson /* 15784b964adaSGeorge Wilson * Swap the missing vdev with the data we were 15794b964adaSGeorge Wilson * able to obtain from the MOS config. 15804b964adaSGeorge Wilson */ 15814b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 15824b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 15834b964adaSGeorge Wilson 15844b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 15854b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 15864b964adaSGeorge Wilson 15874b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 15884b964adaSGeorge Wilson vdev_load(mtvd); 15894b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15904b964adaSGeorge Wilson 15914b964adaSGeorge Wilson vdev_reopen(rvd); 15924b964adaSGeorge Wilson } else if (mtvd->vdev_islog) { 15934b964adaSGeorge Wilson /* 15944b964adaSGeorge Wilson * Load the slog device's state from the MOS config 15954b964adaSGeorge Wilson * since it's possible that the label does not 15964b964adaSGeorge Wilson * contain the most up-to-date information. 15974b964adaSGeorge Wilson */ 15984b964adaSGeorge Wilson vdev_load_log_state(tvd, mtvd); 15994b964adaSGeorge Wilson vdev_reopen(tvd); 16004b964adaSGeorge Wilson } 1601e6ca193dSGeorge Wilson } 16024b964adaSGeorge Wilson vdev_free(mrvd); 160388ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 16044b964adaSGeorge Wilson 16054b964adaSGeorge Wilson /* 16064b964adaSGeorge Wilson * Ensure we were able to validate the config. 16074b964adaSGeorge Wilson */ 16084b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1609e6ca193dSGeorge Wilson } 1610e6ca193dSGeorge Wilson 1611b87f3af3Sperrin /* 1612b87f3af3Sperrin * Check for missing log devices 1613b87f3af3Sperrin */ 16144b964adaSGeorge Wilson static int 1615b87f3af3Sperrin spa_check_logs(spa_t *spa) 1616b87f3af3Sperrin { 1617b87f3af3Sperrin switch (spa->spa_log_state) { 1618b87f3af3Sperrin case SPA_LOG_MISSING: 1619b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1620b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1621b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1622b87f3af3Sperrin DS_FIND_CHILDREN)) { 16231195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1624b87f3af3Sperrin return (1); 1625b87f3af3Sperrin } 1626b87f3af3Sperrin break; 1627b87f3af3Sperrin } 1628b87f3af3Sperrin return (0); 1629b87f3af3Sperrin } 1630b87f3af3Sperrin 16311195e687SMark J Musante static boolean_t 16321195e687SMark J Musante spa_passivate_log(spa_t *spa) 16331195e687SMark J Musante { 16341195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 16351195e687SMark J Musante boolean_t slog_found = B_FALSE; 16361195e687SMark J Musante 16371195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 16381195e687SMark J Musante 16391195e687SMark J Musante if (!spa_has_slogs(spa)) 16401195e687SMark J Musante return (B_FALSE); 16411195e687SMark J Musante 16421195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 16431195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 16441195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 16451195e687SMark J Musante 16461195e687SMark J Musante if (tvd->vdev_islog) { 16471195e687SMark J Musante metaslab_group_passivate(mg); 16481195e687SMark J Musante slog_found = B_TRUE; 16491195e687SMark J Musante } 16501195e687SMark J Musante } 16511195e687SMark J Musante 16521195e687SMark J Musante return (slog_found); 16531195e687SMark J Musante } 16541195e687SMark J Musante 16551195e687SMark J Musante static void 16561195e687SMark J Musante spa_activate_log(spa_t *spa) 16571195e687SMark J Musante { 16581195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 16591195e687SMark J Musante 16601195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 16611195e687SMark J Musante 16621195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 16631195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 16641195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 16651195e687SMark J Musante 16661195e687SMark J Musante if (tvd->vdev_islog) 16671195e687SMark J Musante metaslab_group_activate(mg); 16681195e687SMark J Musante } 16691195e687SMark J Musante } 16701195e687SMark J Musante 16711195e687SMark J Musante int 16721195e687SMark J Musante spa_offline_log(spa_t *spa) 16731195e687SMark J Musante { 16741195e687SMark J Musante int error = 0; 16751195e687SMark J Musante 16761195e687SMark J Musante if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 16771195e687SMark J Musante NULL, DS_FIND_CHILDREN)) == 0) { 16781195e687SMark J Musante 16791195e687SMark J Musante /* 16801195e687SMark J Musante * We successfully offlined the log device, sync out the 16811195e687SMark J Musante * current txg so that the "stubby" block can be removed 16821195e687SMark J Musante * by zil_sync(). 16831195e687SMark J Musante */ 16841195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 16851195e687SMark J Musante } 16861195e687SMark J Musante return (error); 16871195e687SMark J Musante } 16881195e687SMark J Musante 1689b693757aSEric Schrock static void 1690b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1691b693757aSEric Schrock { 1692b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1693b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1694b693757aSEric Schrock } 1695b693757aSEric Schrock 1696b24ab676SJeff Bonwick void 1697b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1698b24ab676SJeff Bonwick { 1699b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1700b24ab676SJeff Bonwick 1701b24ab676SJeff Bonwick if (zio->io_error) 1702b24ab676SJeff Bonwick return; 1703b24ab676SJeff Bonwick 1704b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1705b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1706b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1707b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1708b24ab676SJeff Bonwick } 1709b24ab676SJeff Bonwick 1710468c413aSTim Haley typedef struct spa_load_error { 1711c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1712468c413aSTim Haley uint64_t sle_data_count; 1713468c413aSTim Haley } spa_load_error_t; 1714468c413aSTim Haley 1715468c413aSTim Haley static void 1716468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1717468c413aSTim Haley { 1718468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1719468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1720468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1721468c413aSTim Haley int error = zio->io_error; 1722468c413aSTim Haley 1723468c413aSTim Haley if (error) { 1724ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1725468c413aSTim Haley type != DMU_OT_INTENT_LOG) 1726c8ee1847SVictor Latushkin atomic_add_64(&sle->sle_meta_count, 1); 1727468c413aSTim Haley else 1728468c413aSTim Haley atomic_add_64(&sle->sle_data_count, 1); 1729468c413aSTim Haley } 1730468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1731468c413aSTim Haley } 1732468c413aSTim Haley 1733468c413aSTim Haley /*ARGSUSED*/ 1734468c413aSTim Haley static int 1735b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 17361b912ec7SGeorge Wilson const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1737468c413aSTim Haley { 1738468c413aSTim Haley if (bp != NULL) { 1739468c413aSTim Haley zio_t *rio = arg; 1740468c413aSTim Haley size_t size = BP_GET_PSIZE(bp); 1741468c413aSTim Haley void *data = zio_data_buf_alloc(size); 1742468c413aSTim Haley 1743468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size, 1744468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1745468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1746468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1747468c413aSTim Haley } 1748468c413aSTim Haley return (0); 1749468c413aSTim Haley } 1750468c413aSTim Haley 1751468c413aSTim Haley static int 1752468c413aSTim Haley spa_load_verify(spa_t *spa) 1753468c413aSTim Haley { 1754468c413aSTim Haley zio_t *rio; 1755468c413aSTim Haley spa_load_error_t sle = { 0 }; 1756468c413aSTim Haley zpool_rewind_policy_t policy; 1757468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1758468c413aSTim Haley int error; 1759468c413aSTim Haley 1760c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1761c8ee1847SVictor Latushkin 1762c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1763c8ee1847SVictor Latushkin return (0); 1764c8ee1847SVictor Latushkin 1765468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1766468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1767468c413aSTim Haley 1768bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg, 1769bbfd46c4SJeff Bonwick TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 1770468c413aSTim Haley 1771468c413aSTim Haley (void) zio_wait(rio); 1772468c413aSTim Haley 1773c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1774468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1775468c413aSTim Haley 1776c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1777468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 17784b964adaSGeorge Wilson int64_t loss = 0; 17794b964adaSGeorge Wilson 1780468c413aSTim Haley verify_ok = B_TRUE; 1781468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1782468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 17834b964adaSGeorge Wilson 17844b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 17854b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 17864b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 17874b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 17884b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 17894b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 17904b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1791a33cae98STim Haley } else { 1792a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1793468c413aSTim Haley } 1794468c413aSTim Haley 1795468c413aSTim Haley if (error) { 1796468c413aSTim Haley if (error != ENXIO && error != EIO) 1797468c413aSTim Haley error = EIO; 1798468c413aSTim Haley return (error); 1799468c413aSTim Haley } 1800468c413aSTim Haley 1801468c413aSTim Haley return (verify_ok ? 0 : EIO); 1802468c413aSTim Haley } 1803468c413aSTim Haley 18041195e687SMark J Musante /* 18051195e687SMark J Musante * Find a value in the pool props object. 18061195e687SMark J Musante */ 18071195e687SMark J Musante static void 18081195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 18091195e687SMark J Musante { 18101195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 18111195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 18121195e687SMark J Musante } 18131195e687SMark J Musante 18141195e687SMark J Musante /* 18151195e687SMark J Musante * Find a value in the pool directory object. 18161195e687SMark J Musante */ 18171195e687SMark J Musante static int 18181195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 18191195e687SMark J Musante { 18201195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 18211195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 18221195e687SMark J Musante } 18231195e687SMark J Musante 18241195e687SMark J Musante static int 18251195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 18261195e687SMark J Musante { 18271195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 18281195e687SMark J Musante return (err); 18291195e687SMark J Musante } 18301195e687SMark J Musante 18311195e687SMark J Musante /* 18321195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 18331195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 18341195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 18351195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 18361195e687SMark J Musante * 18371195e687SMark J Musante * This function determines what to do with that list: either rejoin 18381195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 18391195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 18401195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 18411195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 18421195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 18431195e687SMark J Musante * 1844d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1845d41c4376SMark J Musante * the original pool. 18461195e687SMark J Musante */ 18471195e687SMark J Musante static void 18481195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 18491195e687SMark J Musante { 18501195e687SMark J Musante uint_t extracted; 18511195e687SMark J Musante uint64_t *glist; 18521195e687SMark J Musante uint_t i, gcount; 18531195e687SMark J Musante nvlist_t *nvl; 18541195e687SMark J Musante vdev_t **vd; 18551195e687SMark J Musante boolean_t attempt_reopen; 18561195e687SMark J Musante 18571195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 18581195e687SMark J Musante return; 18591195e687SMark J Musante 18601195e687SMark J Musante /* check that the config is complete */ 18611195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 18621195e687SMark J Musante &glist, &gcount) != 0) 18631195e687SMark J Musante return; 18641195e687SMark J Musante 18651195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 18661195e687SMark J Musante 18671195e687SMark J Musante /* attempt to online all the vdevs & validate */ 18681195e687SMark J Musante attempt_reopen = B_TRUE; 18691195e687SMark J Musante for (i = 0; i < gcount; i++) { 18701195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 18711195e687SMark J Musante continue; 18721195e687SMark J Musante 18731195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 18741195e687SMark J Musante if (vd[i] == NULL) { 18751195e687SMark J Musante /* 18761195e687SMark J Musante * Don't bother attempting to reopen the disks; 18771195e687SMark J Musante * just do the split. 18781195e687SMark J Musante */ 18791195e687SMark J Musante attempt_reopen = B_FALSE; 18801195e687SMark J Musante } else { 18811195e687SMark J Musante /* attempt to re-online it */ 18821195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 18831195e687SMark J Musante } 18841195e687SMark J Musante } 18851195e687SMark J Musante 18861195e687SMark J Musante if (attempt_reopen) { 18871195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 18881195e687SMark J Musante 18891195e687SMark J Musante /* check each device to see what state it's in */ 18901195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 18911195e687SMark J Musante if (vd[i] != NULL && 18921195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 18931195e687SMark J Musante break; 18941195e687SMark J Musante ++extracted; 18951195e687SMark J Musante } 18961195e687SMark J Musante } 18971195e687SMark J Musante 18981195e687SMark J Musante /* 18991195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 19001195e687SMark J Musante * even attempted to look at them, then we split them off for 19011195e687SMark J Musante * good. 19021195e687SMark J Musante */ 19031195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 19041195e687SMark J Musante for (i = 0; i < gcount; i++) 19051195e687SMark J Musante if (vd[i] != NULL) 19061195e687SMark J Musante vdev_split(vd[i]); 19071195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 19081195e687SMark J Musante } 19091195e687SMark J Musante 19101195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 19111195e687SMark J Musante } 19121195e687SMark J Musante 19131195e687SMark J Musante static int 19141195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 19151195e687SMark J Musante boolean_t mosconfig) 19161195e687SMark J Musante { 19171195e687SMark J Musante nvlist_t *config = spa->spa_config; 19181195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 19198704186eSDan McDonald char *comment; 19201195e687SMark J Musante int error; 19211195e687SMark J Musante uint64_t pool_guid; 19221195e687SMark J Musante nvlist_t *nvl; 19231195e687SMark J Musante 19241195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 19251195e687SMark J Musante return (EINVAL); 19261195e687SMark J Musante 19278704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 19288704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 19298704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 19308704186eSDan McDonald 19311195e687SMark J Musante /* 19321195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 19331195e687SMark J Musante * it's not present treat it as the initial version. 19341195e687SMark J Musante */ 19351195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 19361195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 19371195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 19381195e687SMark J Musante 19391195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 19401195e687SMark J Musante &spa->spa_config_txg); 19411195e687SMark J Musante 19421195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 19431195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 19441195e687SMark J Musante error = EEXIST; 19451195e687SMark J Musante } else { 1946e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 19471195e687SMark J Musante 19481195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 19491195e687SMark J Musante &nvl) == 0) { 19501195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 19511195e687SMark J Musante KM_SLEEP) == 0); 19521195e687SMark J Musante } 19531195e687SMark J Musante 1954ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 1955ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 1956ad135b5dSChristopher Siden 195711027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 19581195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 19591195e687SMark J Musante mosconfig, &ereport); 19601195e687SMark J Musante } 19611195e687SMark J Musante 19621195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 196311027bc7STim Haley if (error) { 196411027bc7STim Haley if (error != EEXIST) { 196511027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 196611027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 196711027bc7STim Haley } 196811027bc7STim Haley if (error != EBADF) { 196911027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 197011027bc7STim Haley } 197111027bc7STim Haley } 19721195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 19731195e687SMark J Musante spa->spa_ena = 0; 19741195e687SMark J Musante 19751195e687SMark J Musante return (error); 19761195e687SMark J Musante } 19771195e687SMark J Musante 1978fa9e4066Sahrens /* 1979fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 1980ea8dc4b6Seschrock * source of configuration information. 1981fa9e4066Sahrens */ 1982fa9e4066Sahrens static int 19831195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 19841195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 19851195e687SMark J Musante char **ereport) 1986fa9e4066Sahrens { 1987fa9e4066Sahrens int error = 0; 1988871a9500SMark J Musante nvlist_t *nvroot = NULL; 1989ad135b5dSChristopher Siden nvlist_t *label; 1990fa9e4066Sahrens vdev_t *rvd; 1991fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 19924b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 19938ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 19941195e687SMark J Musante int parse; 1995cde58dbcSMatthew Ahrens uint64_t obj; 1996ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 1997fa9e4066Sahrens 19988ad4d6ddSJeff Bonwick /* 19998ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 20008ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 20018ad4d6ddSJeff Bonwick */ 20028ad4d6ddSJeff Bonwick if (!mosconfig) 20038ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 20048ad4d6ddSJeff Bonwick 2005e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2006e14bb325SJeff Bonwick 2007ea8dc4b6Seschrock spa->spa_load_state = state; 20080373e76bSbonwick 20091195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 20101195e687SMark J Musante return (EINVAL); 2011fa9e4066Sahrens 20121195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 20131195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2014b5989ec7Seschrock 201554d692b7SGeorge Wilson /* 201654d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 201754d692b7SGeorge Wilson */ 201825f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 201925f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 202054d692b7SGeorge Wilson 2021fa9e4066Sahrens /* 202299653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 202399653d4eSeschrock * value that will be returned by spa_version() since parsing the 202499653d4eSeschrock * configuration requires knowing the version number. 2025fa9e4066Sahrens */ 2026e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20271195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2028e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2029fa9e4066Sahrens 203099653d4eSeschrock if (error != 0) 20311195e687SMark J Musante return (error); 2032fa9e4066Sahrens 20330e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 20341195e687SMark J Musante 20351195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 20361195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 20371195e687SMark J Musante } 2038fa9e4066Sahrens 2039fa9e4066Sahrens /* 2040fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2041fa9e4066Sahrens */ 2042e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 20430bf246f5Smc error = vdev_open(rvd); 2044e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 20450bf246f5Smc if (error != 0) 20461195e687SMark J Musante return (error); 2047fa9e4066Sahrens 2048560e6e96Seschrock /* 204977e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 205077e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 205177e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 20521195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 205377e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 205477e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 205577e3a39cSMark J Musante * the vdev config. 20561195e687SMark J Musante * 20571195e687SMark J Musante * If we're assembling a new pool that's been split off from an 20581195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 20591195e687SMark J Musante * validation for now. 2060560e6e96Seschrock */ 20611195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 20621195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2063d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 20641195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2065560e6e96Seschrock 20661195e687SMark J Musante if (error != 0) 20671195e687SMark J Musante return (error); 20681195e687SMark J Musante 20691195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 20701195e687SMark J Musante return (ENXIO); 2071560e6e96Seschrock } 2072560e6e96Seschrock 2073fa9e4066Sahrens /* 2074fa9e4066Sahrens * Find the best uberblock. 2075fa9e4066Sahrens */ 2076ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2077fa9e4066Sahrens 2078fa9e4066Sahrens /* 2079fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2080fa9e4066Sahrens */ 2081ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2082ad135b5dSChristopher Siden nvlist_free(label); 20831195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2084ad135b5dSChristopher Siden } 2085ea8dc4b6Seschrock 2086ea8dc4b6Seschrock /* 2087ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2088ea8dc4b6Seschrock */ 2089ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2090ad135b5dSChristopher Siden nvlist_free(label); 20911195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2092ad135b5dSChristopher Siden } 2093ad135b5dSChristopher Siden 2094ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2095ad135b5dSChristopher Siden nvlist_t *features; 2096ad135b5dSChristopher Siden 2097ad135b5dSChristopher Siden /* 2098ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2099ad135b5dSChristopher Siden * MOS in the label, return failure. 2100ad135b5dSChristopher Siden */ 2101ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2102ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2103ad135b5dSChristopher Siden nvlist_free(label); 2104ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2105ad135b5dSChristopher Siden ENXIO)); 2106ad135b5dSChristopher Siden } 2107ad135b5dSChristopher Siden 2108ad135b5dSChristopher Siden /* 2109ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2110ad135b5dSChristopher Siden * from the label. 2111ad135b5dSChristopher Siden */ 2112ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2113ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2114ad135b5dSChristopher Siden } 2115ad135b5dSChristopher Siden 2116ad135b5dSChristopher Siden nvlist_free(label); 2117ad135b5dSChristopher Siden 2118ad135b5dSChristopher Siden /* 2119ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2120ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2121ad135b5dSChristopher Siden * cannot open a pool. 2122ad135b5dSChristopher Siden */ 2123ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2124ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2125ad135b5dSChristopher Siden 2126ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2127ad135b5dSChristopher Siden 0); 2128ad135b5dSChristopher Siden 2129ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2130ad135b5dSChristopher Siden NULL); nvp != NULL; 2131ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2132ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2133ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2134ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2135ad135b5dSChristopher Siden } 2136ad135b5dSChristopher Siden } 2137ad135b5dSChristopher Siden 2138ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2139ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2140ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2141ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2142ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2143ad135b5dSChristopher Siden ENOTSUP)); 2144ad135b5dSChristopher Siden } 2145ad135b5dSChristopher Siden 2146ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2147ad135b5dSChristopher Siden } 2148fa9e4066Sahrens 2149fa9e4066Sahrens /* 2150fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 21514b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 21524b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 21534b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 21544b964adaSGeorge Wilson * can handle missing vdevs. 2155fa9e4066Sahrens */ 21564b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 21574b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 21581195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 21591195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 21601195e687SMark J Musante 21611195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 21621195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21631195e687SMark J Musante spa_try_repair(spa, config); 21641195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 21651195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 21661195e687SMark J Musante spa->spa_config_splitting = NULL; 2167fa9e4066Sahrens } 2168fa9e4066Sahrens 2169fa9e4066Sahrens /* 2170fa9e4066Sahrens * Initialize internal SPA structures. 2171fa9e4066Sahrens */ 2172fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2173fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2174468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2175c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2176468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2177468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2178b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 21793f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2180b24ab676SJeff Bonwick 2181ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 21821195e687SMark J Musante if (error) 21831195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2184fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2185fa9e4066Sahrens 21861195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 21871195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2188fa9e4066Sahrens 2189ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2190ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 219157221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2192ad135b5dSChristopher Siden 2193ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2194ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2195ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2196ad135b5dSChristopher Siden } 2197ad135b5dSChristopher Siden 2198ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2199ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2200ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2201ad135b5dSChristopher Siden } 2202ad135b5dSChristopher Siden 2203ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2204ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2205ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2206ad135b5dSChristopher Siden } 2207ad135b5dSChristopher Siden 220857221772SChristopher Siden enabled_feat = fnvlist_alloc(); 220957221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2210ad135b5dSChristopher Siden 2211ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2212ad135b5dSChristopher Siden spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj, 221357221772SChristopher Siden unsup_feat, enabled_feat)) 2214ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2215ad135b5dSChristopher Siden 2216ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 2217ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2218ad135b5dSChristopher Siden spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj, 221957221772SChristopher Siden unsup_feat, enabled_feat)) { 2220ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 222157221772SChristopher Siden } 2222ad135b5dSChristopher Siden } 2223ad135b5dSChristopher Siden 222457221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 222557221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 222657221772SChristopher Siden 2227ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 222857221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 222957221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2230ad135b5dSChristopher Siden } 2231ad135b5dSChristopher Siden 223257221772SChristopher Siden fnvlist_free(enabled_feat); 223357221772SChristopher Siden fnvlist_free(unsup_feat); 2234ad135b5dSChristopher Siden 2235ad135b5dSChristopher Siden if (!missing_feat_read) { 2236ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2237ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2238ad135b5dSChristopher Siden } 2239ad135b5dSChristopher Siden 2240ad135b5dSChristopher Siden /* 2241ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2242ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2243ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2244ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2245ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2246ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2247ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2248ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2249ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2250ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2251ad135b5dSChristopher Siden * available for open in read-only mode. 2252ad135b5dSChristopher Siden * 2253ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2254ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2255ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2256ad135b5dSChristopher Siden * userland in order to know whether to display the 2257ad135b5dSChristopher Siden * abovementioned note. 2258ad135b5dSChristopher Siden */ 2259ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2260ad135b5dSChristopher Siden spa_writeable(spa))) { 2261ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2262ad135b5dSChristopher Siden ENOTSUP)); 2263ad135b5dSChristopher Siden } 2264ad135b5dSChristopher Siden } 2265ad135b5dSChristopher Siden 2266ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2267ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2268ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2269ad135b5dSChristopher Siden if (error != 0) 2270ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2271ad135b5dSChristopher Siden 2272fa9e4066Sahrens if (!mosconfig) { 227395173954Sek uint64_t hostid; 2274871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2275871a9500SMark J Musante 2276871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2277871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2278fa9e4066Sahrens 227988ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 228077650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 228195173954Sek char *hostname; 228295173954Sek unsigned long myhostid = 0; 228395173954Sek 228488ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 228595173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 228695173954Sek 22875679c89fSjv #ifdef _KERNEL 22885679c89fSjv myhostid = zone_get_hostid(NULL); 22895679c89fSjv #else /* _KERNEL */ 22905679c89fSjv /* 22915679c89fSjv * We're emulating the system's hostid in userland, so 22925679c89fSjv * we can't use zone_get_hostid(). 22935679c89fSjv */ 229495173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 22955679c89fSjv #endif /* _KERNEL */ 229617194a52Slling if (hostid != 0 && myhostid != 0 && 22975679c89fSjv hostid != myhostid) { 2298871a9500SMark J Musante nvlist_free(nvconfig); 229995173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 230095173954Sek "loaded as it was last accessed by " 230177650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2302654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2303e14bb325SJeff Bonwick spa_name(spa), hostname, 230495173954Sek (unsigned long)hostid); 23051195e687SMark J Musante return (EBADF); 230695173954Sek } 230795173954Sek } 2308c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2309c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2310c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2311c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 231295173954Sek 231388ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2314fa9e4066Sahrens spa_unload(spa); 2315fa9e4066Sahrens spa_deactivate(spa); 23168ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2317fa9e4066Sahrens 23181195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2319fa9e4066Sahrens } 2320fa9e4066Sahrens 2321cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2322cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2323cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2324cde58dbcSMatthew Ahrens if (error != 0) 23251195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2326fa9e4066Sahrens 232799653d4eSeschrock /* 232899653d4eSeschrock * Load the bit that tells us to use the new accounting function 232999653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 233099653d4eSeschrock * be present. 233199653d4eSeschrock */ 23321195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 23331195e687SMark J Musante if (error != 0 && error != ENOENT) 23341195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 233599653d4eSeschrock 23363f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 23373f9d6ad7SLin Ling &spa->spa_creation_version); 23383f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 23393f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 23403f9d6ad7SLin Ling 2341fa9e4066Sahrens /* 2342ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2343ea8dc4b6Seschrock * not be present. 2344fa9e4066Sahrens */ 23451195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 23461195e687SMark J Musante if (error != 0 && error != ENOENT) 23471195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2348ea8dc4b6Seschrock 23491195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 23501195e687SMark J Musante &spa->spa_errlog_scrub); 23511195e687SMark J Musante if (error != 0 && error != ENOENT) 23521195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2353ea8dc4b6Seschrock 235406eeb2adSek /* 235506eeb2adSek * Load the history object. If we have an older pool, this 235606eeb2adSek * will not be present. 235706eeb2adSek */ 23581195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 23591195e687SMark J Musante if (error != 0 && error != ENOENT) 23601195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 23611195e687SMark J Musante 23621195e687SMark J Musante /* 23631195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 23641195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 23651195e687SMark J Musante * devices. 23661195e687SMark J Musante */ 236706eeb2adSek 236899653d4eSeschrock /* 236999653d4eSeschrock * Load any hot spares for this pool. 237099653d4eSeschrock */ 23711195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 23721195e687SMark J Musante if (error != 0 && error != ENOENT) 23731195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 23741195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2375e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2376fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 23771195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 23781195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 237999653d4eSeschrock 2380e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 238199653d4eSeschrock spa_load_spares(spa); 2382e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 23831195e687SMark J Musante } else if (error == 0) { 23841195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 238599653d4eSeschrock } 238699653d4eSeschrock 2387fa94a07fSbrendan /* 2388fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2389fa94a07fSbrendan */ 23901195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2391fa94a07fSbrendan &spa->spa_l2cache.sav_object); 23921195e687SMark J Musante if (error != 0 && error != ENOENT) 23931195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 23941195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2395fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2396fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 23971195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 23981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2399fa94a07fSbrendan 2400e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2401fa94a07fSbrendan spa_load_l2cache(spa); 2402e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24031195e687SMark J Musante } else if (error == 0) { 24041195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2405fa94a07fSbrendan } 2406fa94a07fSbrendan 2407990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2408ecd6cf80Smarks 24091195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 24101195e687SMark J Musante if (error && error != ENOENT) 24111195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2412b1b8ab34Slling 2413b1b8ab34Slling if (error == 0) { 24141195e687SMark J Musante uint64_t autoreplace; 24151195e687SMark J Musante 24161195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 24171195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 24181195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 24191195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 24201195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 24211195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 24221195e687SMark J Musante &spa->spa_dedup_ditto); 24231195e687SMark J Musante 2424b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2425b1b8ab34Slling } 2426b1b8ab34Slling 24273d7072f8Seschrock /* 24283d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 24293d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 24303d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 24313d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 24323d7072f8Seschrock * over. 24333d7072f8Seschrock */ 2434b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 24353d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2436b693757aSEric Schrock /* 2437b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2438b693757aSEric Schrock * at this point we're using the spare definitions from 2439b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2440b693757aSEric Schrock */ 2441b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2442b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2443b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2444b693757aSEric Schrock } 2445b693757aSEric Schrock } 24463d7072f8Seschrock 2447ea8dc4b6Seschrock /* 2448560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2449ea8dc4b6Seschrock */ 2450560e6e96Seschrock vdev_load(rvd); 24510373e76bSbonwick 2452fa9e4066Sahrens /* 2453fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2454fa9e4066Sahrens */ 2455e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2456fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2457e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2458fa9e4066Sahrens 2459b24ab676SJeff Bonwick /* 2460b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2461b24ab676SJeff Bonwick */ 2462b24ab676SJeff Bonwick error = ddt_load(spa); 24631195e687SMark J Musante if (error != 0) 24641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2465b24ab676SJeff Bonwick 2466485bbbf5SGeorge Wilson spa_update_dspace(spa); 2467485bbbf5SGeorge Wilson 2468b24ab676SJeff Bonwick /* 24694b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 24704b964adaSGeorge Wilson * information which might be missing. If we fail to validate 24714b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 24724b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 24734b964adaSGeorge Wilson * over. 2474b24ab676SJeff Bonwick */ 24751195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2476871a9500SMark J Musante nvlist_t *nvconfig; 2477871a9500SMark J Musante 2478871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2479871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2480871a9500SMark J Musante 24814b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 24824b964adaSGeorge Wilson nvlist_free(nvconfig); 24834b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 24844b964adaSGeorge Wilson ENXIO)); 24854b964adaSGeorge Wilson } 24861195e687SMark J Musante nvlist_free(nvconfig); 24871195e687SMark J Musante 24884b964adaSGeorge Wilson /* 2489ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 24904b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 24914b964adaSGeorge Wilson * more toplevel vdevs are faulted. 24924b964adaSGeorge Wilson */ 24934b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 24944b964adaSGeorge Wilson return (ENXIO); 24954b964adaSGeorge Wilson 24961195e687SMark J Musante if (spa_check_logs(spa)) { 24971195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 24981195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 24991195e687SMark J Musante } 2500b24ab676SJeff Bonwick } 2501b24ab676SJeff Bonwick 2502ad135b5dSChristopher Siden if (missing_feat_write) { 2503ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2504ad135b5dSChristopher Siden 2505ad135b5dSChristopher Siden /* 2506ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2507ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2508ad135b5dSChristopher Siden * information and can return to userland. 2509ad135b5dSChristopher Siden */ 2510ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2511ad135b5dSChristopher Siden } 2512ad135b5dSChristopher Siden 25134b964adaSGeorge Wilson /* 25144b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 25154b964adaSGeorge Wilson * to start pushing transactions. 25164b964adaSGeorge Wilson */ 25174b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 25184b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 25194b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 25204b964adaSGeorge Wilson error)); 25214b964adaSGeorge Wilson } 25224b964adaSGeorge Wilson 2523468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2524468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 25255dabedeeSbonwick dmu_tx_t *tx; 25260373e76bSbonwick int need_update = B_FALSE; 25278ad4d6ddSJeff Bonwick 25288ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 25295dabedeeSbonwick 25300373e76bSbonwick /* 25310373e76bSbonwick * Claim log blocks that haven't been committed yet. 25320373e76bSbonwick * This must all happen in a single txg. 2533b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2534b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2535468c413aSTim Haley * Price of rollback is that we abandon the log. 25360373e76bSbonwick */ 2537b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2538b24ab676SJeff Bonwick 25395dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2540fa9e4066Sahrens spa_first_txg(spa)); 2541e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 25420b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2543fa9e4066Sahrens dmu_tx_commit(tx); 2544fa9e4066Sahrens 2545b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2546b24ab676SJeff Bonwick 25471195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2548fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2549fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2550fa9e4066Sahrens 2551fa9e4066Sahrens /* 2552b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2553b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2554b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2555b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2556b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2557fa9e4066Sahrens */ 2558b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 25590e34b6a7Sbonwick 25600e34b6a7Sbonwick /* 25610373e76bSbonwick * If the config cache is stale, or we have uninitialized 25620373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2563bc758434SLin Ling * 25644b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2565bc758434SLin Ling * in-core spa_config and update the disk labels. 25660e34b6a7Sbonwick */ 25670373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 25684b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 25694b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 25704b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 25710373e76bSbonwick need_update = B_TRUE; 25720373e76bSbonwick 25738ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 25740373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 25750373e76bSbonwick need_update = B_TRUE; 25760e34b6a7Sbonwick 25770e34b6a7Sbonwick /* 25780373e76bSbonwick * Update the config cache asychronously in case we're the 25790373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 25800e34b6a7Sbonwick */ 25810373e76bSbonwick if (need_update) 25820373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 25838ad4d6ddSJeff Bonwick 25848ad4d6ddSJeff Bonwick /* 25858ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 25868ad4d6ddSJeff Bonwick */ 25873f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 25883f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 25898ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2590503ad85cSMatthew Ahrens 25914445fffbSMatthew Ahrens /* 25924445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 25934445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 25944445fffbSMatthew Ahrens */ 25954445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 25964445fffbSMatthew Ahrens 2597503ad85cSMatthew Ahrens /* 2598503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2599503ad85cSMatthew Ahrens */ 2600503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2601503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2602ca45db41SChris Kirby 2603ca45db41SChris Kirby /* 2604ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2605ca45db41SChris Kirby */ 2606ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2607fa9e4066Sahrens } 2608fa9e4066Sahrens 26091195e687SMark J Musante return (0); 2610fa9e4066Sahrens } 2611fa9e4066Sahrens 2612468c413aSTim Haley static int 2613468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2614468c413aSTim Haley { 2615f9af39baSGeorge Wilson int mode = spa->spa_mode; 2616f9af39baSGeorge Wilson 2617468c413aSTim Haley spa_unload(spa); 2618468c413aSTim Haley spa_deactivate(spa); 2619468c413aSTim Haley 2620468c413aSTim Haley spa->spa_load_max_txg--; 2621468c413aSTim Haley 2622f9af39baSGeorge Wilson spa_activate(spa, mode); 2623468c413aSTim Haley spa_async_suspend(spa); 2624468c413aSTim Haley 26251195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2626468c413aSTim Haley } 2627468c413aSTim Haley 2628ad135b5dSChristopher Siden /* 2629ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2630ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2631ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2632ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2633ad135b5dSChristopher Siden * spa_load(). 2634ad135b5dSChristopher Siden */ 2635468c413aSTim Haley static int 2636468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2637c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2638468c413aSTim Haley { 2639ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2640468c413aSTim Haley nvlist_t *config = NULL; 2641468c413aSTim Haley int load_error, rewind_error; 2642c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2643468c413aSTim Haley uint64_t min_txg; 2644468c413aSTim Haley 2645a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2646468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 26471195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2648a33cae98STim Haley } else { 2649468c413aSTim Haley spa->spa_load_max_txg = max_request; 2650a33cae98STim Haley } 2651468c413aSTim Haley 26521195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 26531195e687SMark J Musante mosconfig); 2654468c413aSTim Haley if (load_error == 0) 2655468c413aSTim Haley return (0); 2656468c413aSTim Haley 2657468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2658468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2659468c413aSTim Haley 2660468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2661468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2662468c413aSTim Haley 2663c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2664468c413aSTim Haley nvlist_free(config); 2665468c413aSTim Haley return (load_error); 2666468c413aSTim Haley } 2667468c413aSTim Haley 2668ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2669ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 26701195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2671ad135b5dSChristopher Siden } else { 2672ad135b5dSChristopher Siden /* 2673ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2674ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2675ad135b5dSChristopher Siden * to rewind. 2676ad135b5dSChristopher Siden */ 2677ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 2678ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2679ad135b5dSChristopher Siden } 2680468c413aSTim Haley 2681c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2682c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2683c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2684c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2685468c413aSTim Haley 2686c8ee1847SVictor Latushkin /* 2687c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2688c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2689c8ee1847SVictor Latushkin */ 2690c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2691c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2692c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2693468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2694468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2695468c413aSTim Haley } 2696468c413aSTim Haley 2697468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2698468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2699468c413aSTim Haley 2700468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2701468c413aSTim Haley spa_config_set(spa, config); 2702468c413aSTim Haley 2703ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2704ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 2705ad135b5dSChristopher Siden return (rewind_error); 2706ad135b5dSChristopher Siden } else { 2707ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 2708ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 2709ad135b5dSChristopher Siden spa->spa_load_info); 2710ad135b5dSChristopher Siden 2711ad135b5dSChristopher Siden /* Restore the initial load info */ 2712ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 2713ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 2714ad135b5dSChristopher Siden 2715ad135b5dSChristopher Siden return (load_error); 2716ad135b5dSChristopher Siden } 2717468c413aSTim Haley } 2718468c413aSTim Haley 2719fa9e4066Sahrens /* 2720fa9e4066Sahrens * Pool Open/Import 2721fa9e4066Sahrens * 2722fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2723fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2724fa9e4066Sahrens * case of an open, the pool configuration will exist in the 27253d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2726fa9e4066Sahrens * 2727fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2728fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2729fa9e4066Sahrens * ambiguous state. 2730fa9e4066Sahrens */ 2731fa9e4066Sahrens static int 2732468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2733468c413aSTim Haley nvlist_t **config) 2734fa9e4066Sahrens { 2735fa9e4066Sahrens spa_t *spa; 27364b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 2737fa9e4066Sahrens int error; 2738fa9e4066Sahrens int locked = B_FALSE; 2739fa9e4066Sahrens 2740fa9e4066Sahrens *spapp = NULL; 2741fa9e4066Sahrens 2742fa9e4066Sahrens /* 2743fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2744fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2745fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2746fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2747fa9e4066Sahrens */ 2748fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2749fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2750fa9e4066Sahrens locked = B_TRUE; 2751fa9e4066Sahrens } 2752fa9e4066Sahrens 2753fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2754fa9e4066Sahrens if (locked) 2755fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2756fa9e4066Sahrens return (ENOENT); 2757fa9e4066Sahrens } 2758468c413aSTim Haley 2759fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 27604b44c88cSTim Haley zpool_rewind_policy_t policy; 27614b44c88cSTim Haley 27624b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 27634b44c88cSTim Haley &policy); 27644b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 27654b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2766fa9e4066Sahrens 27678ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2768fa9e4066Sahrens 2769468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2770468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2771468c413aSTim Haley 2772468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2773c8ee1847SVictor Latushkin policy.zrp_request); 2774fa9e4066Sahrens 2775fa9e4066Sahrens if (error == EBADF) { 2776fa9e4066Sahrens /* 2777560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2778560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2779560e6e96Seschrock * that the pool has been exported or destroyed. If 2780560e6e96Seschrock * this is the case, the config cache is out of sync and 2781560e6e96Seschrock * we should remove the pool from the namespace. 2782fa9e4066Sahrens */ 2783fa9e4066Sahrens spa_unload(spa); 2784fa9e4066Sahrens spa_deactivate(spa); 2785c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2786fa9e4066Sahrens spa_remove(spa); 2787fa9e4066Sahrens if (locked) 2788fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2789fa9e4066Sahrens return (ENOENT); 2790ea8dc4b6Seschrock } 2791ea8dc4b6Seschrock 2792ea8dc4b6Seschrock if (error) { 2793fa9e4066Sahrens /* 2794fa9e4066Sahrens * We can't open the pool, but we still have useful 2795fa9e4066Sahrens * information: the state of each vdev after the 2796fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2797fa9e4066Sahrens */ 27984b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 2799468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2800468c413aSTim Haley KM_SLEEP) == 0); 28014b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 28024b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 28034b964adaSGeorge Wilson spa->spa_load_info) == 0); 28044b964adaSGeorge Wilson } 2805fa9e4066Sahrens spa_unload(spa); 2806fa9e4066Sahrens spa_deactivate(spa); 2807468c413aSTim Haley spa->spa_last_open_failed = error; 2808fa9e4066Sahrens if (locked) 2809fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2810fa9e4066Sahrens *spapp = NULL; 2811fa9e4066Sahrens return (error); 2812fa9e4066Sahrens } 2813fa9e4066Sahrens } 2814fa9e4066Sahrens 2815fa9e4066Sahrens spa_open_ref(spa, tag); 28163d7072f8Seschrock 2817468c413aSTim Haley if (config != NULL) 2818468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2819468c413aSTim Haley 28204b964adaSGeorge Wilson /* 28214b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 28224b964adaSGeorge Wilson * gathered while doing the load. 28234b964adaSGeorge Wilson */ 28244b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 28254b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 28264b964adaSGeorge Wilson spa->spa_load_info) == 0); 28274b964adaSGeorge Wilson } 28284b964adaSGeorge Wilson 2829a33cae98STim Haley if (locked) { 2830a33cae98STim Haley spa->spa_last_open_failed = 0; 2831a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 2832a33cae98STim Haley spa->spa_load_txg = 0; 2833fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2834a33cae98STim Haley } 2835fa9e4066Sahrens 2836fa9e4066Sahrens *spapp = spa; 2837fa9e4066Sahrens 2838fa9e4066Sahrens return (0); 2839fa9e4066Sahrens } 2840fa9e4066Sahrens 2841468c413aSTim Haley int 2842468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 2843468c413aSTim Haley nvlist_t **config) 2844468c413aSTim Haley { 2845468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 2846468c413aSTim Haley } 2847468c413aSTim Haley 2848fa9e4066Sahrens int 2849fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2850fa9e4066Sahrens { 2851468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 2852fa9e4066Sahrens } 2853fa9e4066Sahrens 2854ea8dc4b6Seschrock /* 2855ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 2856ea8dc4b6Seschrock * preventing it from being exported or destroyed. 2857ea8dc4b6Seschrock */ 2858ea8dc4b6Seschrock spa_t * 2859ea8dc4b6Seschrock spa_inject_addref(char *name) 2860ea8dc4b6Seschrock { 2861ea8dc4b6Seschrock spa_t *spa; 2862ea8dc4b6Seschrock 2863ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2864ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 2865ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2866ea8dc4b6Seschrock return (NULL); 2867ea8dc4b6Seschrock } 2868ea8dc4b6Seschrock spa->spa_inject_ref++; 2869ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2870ea8dc4b6Seschrock 2871ea8dc4b6Seschrock return (spa); 2872ea8dc4b6Seschrock } 2873ea8dc4b6Seschrock 2874ea8dc4b6Seschrock void 2875ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 2876ea8dc4b6Seschrock { 2877ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2878ea8dc4b6Seschrock spa->spa_inject_ref--; 2879ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2880ea8dc4b6Seschrock } 2881ea8dc4b6Seschrock 2882fa94a07fSbrendan /* 2883fa94a07fSbrendan * Add spares device information to the nvlist. 2884fa94a07fSbrendan */ 288599653d4eSeschrock static void 288699653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 288799653d4eSeschrock { 288899653d4eSeschrock nvlist_t **spares; 288999653d4eSeschrock uint_t i, nspares; 289099653d4eSeschrock nvlist_t *nvroot; 289199653d4eSeschrock uint64_t guid; 289299653d4eSeschrock vdev_stat_t *vs; 289399653d4eSeschrock uint_t vsc; 289439c23413Seschrock uint64_t pool; 289599653d4eSeschrock 28966809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 28976809eb4eSEric Schrock 2898fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 289999653d4eSeschrock return; 290099653d4eSeschrock 290199653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 290299653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2903fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 290499653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 290599653d4eSeschrock if (nspares != 0) { 290699653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 290799653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 290899653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 290999653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 291099653d4eSeschrock 291199653d4eSeschrock /* 291299653d4eSeschrock * Go through and find any spares which have since been 291399653d4eSeschrock * repurposed as an active spare. If this is the case, update 291499653d4eSeschrock * their status appropriately. 291599653d4eSeschrock */ 291699653d4eSeschrock for (i = 0; i < nspares; i++) { 291799653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 291899653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 291989a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 292089a89ebfSlling pool != 0ULL) { 292199653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 29223f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 292399653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 292499653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 292599653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 292699653d4eSeschrock } 292799653d4eSeschrock } 292899653d4eSeschrock } 292999653d4eSeschrock } 293099653d4eSeschrock 2931fa94a07fSbrendan /* 2932fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 2933fa94a07fSbrendan */ 2934fa94a07fSbrendan static void 2935fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 2936fa94a07fSbrendan { 2937fa94a07fSbrendan nvlist_t **l2cache; 2938fa94a07fSbrendan uint_t i, j, nl2cache; 2939fa94a07fSbrendan nvlist_t *nvroot; 2940fa94a07fSbrendan uint64_t guid; 2941fa94a07fSbrendan vdev_t *vd; 2942fa94a07fSbrendan vdev_stat_t *vs; 2943fa94a07fSbrendan uint_t vsc; 2944fa94a07fSbrendan 29456809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 29466809eb4eSEric Schrock 2947fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 2948fa94a07fSbrendan return; 2949fa94a07fSbrendan 2950fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 2951fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2952fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 2953fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2954fa94a07fSbrendan if (nl2cache != 0) { 2955fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 2956fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2957fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 2958fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2959fa94a07fSbrendan 2960fa94a07fSbrendan /* 2961fa94a07fSbrendan * Update level 2 cache device stats. 2962fa94a07fSbrendan */ 2963fa94a07fSbrendan 2964fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 2965fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 2966fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 2967fa94a07fSbrendan 2968fa94a07fSbrendan vd = NULL; 2969fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 2970fa94a07fSbrendan if (guid == 2971fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 2972fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 2973fa94a07fSbrendan break; 2974fa94a07fSbrendan } 2975fa94a07fSbrendan } 2976fa94a07fSbrendan ASSERT(vd != NULL); 2977fa94a07fSbrendan 2978fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 29793f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 29803f9d6ad7SLin Ling == 0); 2981fa94a07fSbrendan vdev_get_stats(vd, vs); 2982fa94a07fSbrendan } 2983fa94a07fSbrendan } 2984fa94a07fSbrendan } 2985fa94a07fSbrendan 2986ad135b5dSChristopher Siden static void 2987ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 2988ad135b5dSChristopher Siden { 2989ad135b5dSChristopher Siden nvlist_t *features; 2990ad135b5dSChristopher Siden zap_cursor_t zc; 2991ad135b5dSChristopher Siden zap_attribute_t za; 2992ad135b5dSChristopher Siden 2993ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 2994ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2995ad135b5dSChristopher Siden 2996ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 2997ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 2998ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 2999ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3000ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3001ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3002ad135b5dSChristopher Siden za.za_num_integers == 1); 3003b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3004ad135b5dSChristopher Siden za.za_first_integer)); 3005ad135b5dSChristopher Siden } 3006ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3007ad135b5dSChristopher Siden } 3008ad135b5dSChristopher Siden 3009ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3010ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3011ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3012ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3013ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3014ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3015ad135b5dSChristopher Siden za.za_num_integers == 1); 3016b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3017ad135b5dSChristopher Siden za.za_first_integer)); 3018ad135b5dSChristopher Siden } 3019ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3020ad135b5dSChristopher Siden } 3021ad135b5dSChristopher Siden 3022ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3023ad135b5dSChristopher Siden features) == 0); 3024ad135b5dSChristopher Siden nvlist_free(features); 3025ad135b5dSChristopher Siden } 3026ad135b5dSChristopher Siden 3027fa9e4066Sahrens int 3028ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3029ad135b5dSChristopher Siden char *altroot, size_t buflen) 3030fa9e4066Sahrens { 3031fa9e4066Sahrens int error; 3032fa9e4066Sahrens spa_t *spa; 3033fa9e4066Sahrens 3034fa9e4066Sahrens *config = NULL; 3035468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3036fa9e4066Sahrens 30376809eb4eSEric Schrock if (spa != NULL) { 30386809eb4eSEric Schrock /* 30396809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 30406809eb4eSEric Schrock * or l2cache devices could change and the config would be 30416809eb4eSEric Schrock * self-inconsistent. 30426809eb4eSEric Schrock */ 30436809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3044ea8dc4b6Seschrock 30456809eb4eSEric Schrock if (*config != NULL) { 304611027bc7STim Haley uint64_t loadtimes[2]; 304711027bc7STim Haley 304811027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 304911027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 305011027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 305111027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 305211027bc7STim Haley 3053e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 30546809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 30556809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3056e14bb325SJeff Bonwick 30576809eb4eSEric Schrock if (spa_suspended(spa)) 30586809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 30596809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 30606809eb4eSEric Schrock spa->spa_failmode) == 0); 30616809eb4eSEric Schrock 30626809eb4eSEric Schrock spa_add_spares(spa, *config); 30636809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3064ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 30656809eb4eSEric Schrock } 306699653d4eSeschrock } 306799653d4eSeschrock 3068ea8dc4b6Seschrock /* 3069ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3070ea8dc4b6Seschrock * and call spa_lookup() directly. 3071ea8dc4b6Seschrock */ 3072ea8dc4b6Seschrock if (altroot) { 3073ea8dc4b6Seschrock if (spa == NULL) { 3074ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3075ea8dc4b6Seschrock spa = spa_lookup(name); 3076ea8dc4b6Seschrock if (spa) 3077ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3078ea8dc4b6Seschrock else 3079ea8dc4b6Seschrock altroot[0] = '\0'; 3080ea8dc4b6Seschrock spa = NULL; 3081ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3082ea8dc4b6Seschrock } else { 3083ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3084ea8dc4b6Seschrock } 3085ea8dc4b6Seschrock } 3086ea8dc4b6Seschrock 30876809eb4eSEric Schrock if (spa != NULL) { 30886809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3089fa9e4066Sahrens spa_close(spa, FTAG); 30906809eb4eSEric Schrock } 3091fa9e4066Sahrens 3092fa9e4066Sahrens return (error); 3093fa9e4066Sahrens } 3094fa9e4066Sahrens 309599653d4eSeschrock /* 3096fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3097fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3098fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3099fa94a07fSbrendan * specified, as long as they are well-formed. 310099653d4eSeschrock */ 310199653d4eSeschrock static int 3102fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3103fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3104fa94a07fSbrendan vdev_labeltype_t label) 310599653d4eSeschrock { 3106fa94a07fSbrendan nvlist_t **dev; 3107fa94a07fSbrendan uint_t i, ndev; 310899653d4eSeschrock vdev_t *vd; 310999653d4eSeschrock int error; 311099653d4eSeschrock 3111e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3112e14bb325SJeff Bonwick 311399653d4eSeschrock /* 3114fa94a07fSbrendan * It's acceptable to have no devs specified. 311599653d4eSeschrock */ 3116fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 311799653d4eSeschrock return (0); 311899653d4eSeschrock 3119fa94a07fSbrendan if (ndev == 0) 312099653d4eSeschrock return (EINVAL); 312199653d4eSeschrock 312299653d4eSeschrock /* 3123fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3124fa94a07fSbrendan * device type. 312599653d4eSeschrock */ 3126fa94a07fSbrendan if (spa_version(spa) < version) 312799653d4eSeschrock return (ENOTSUP); 312899653d4eSeschrock 312939c23413Seschrock /* 3130fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 313139c23413Seschrock * checking. 313239c23413Seschrock */ 3133fa94a07fSbrendan sav->sav_pending = dev; 3134fa94a07fSbrendan sav->sav_npending = ndev; 313539c23413Seschrock 3136fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3137fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 313899653d4eSeschrock mode)) != 0) 313939c23413Seschrock goto out; 314099653d4eSeschrock 314199653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 314299653d4eSeschrock vdev_free(vd); 314339c23413Seschrock error = EINVAL; 314439c23413Seschrock goto out; 314599653d4eSeschrock } 314699653d4eSeschrock 3147fa94a07fSbrendan /* 3148e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3149e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3150fa94a07fSbrendan */ 3151e14bb325SJeff Bonwick #ifdef _KERNEL 3152fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3153fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3154fa94a07fSbrendan error = ENOTBLK; 3155cd0837ccSGeorge Wilson vdev_free(vd); 3156fa94a07fSbrendan goto out; 3157fa94a07fSbrendan } 3158e14bb325SJeff Bonwick #endif 315999653d4eSeschrock vd->vdev_top = vd; 316099653d4eSeschrock 316139c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3162fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3163fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 316439c23413Seschrock vd->vdev_guid) == 0); 316539c23413Seschrock } 316699653d4eSeschrock 316799653d4eSeschrock vdev_free(vd); 316839c23413Seschrock 3169fa94a07fSbrendan if (error && 3170fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 317139c23413Seschrock goto out; 317239c23413Seschrock else 317339c23413Seschrock error = 0; 317499653d4eSeschrock } 317599653d4eSeschrock 317639c23413Seschrock out: 3177fa94a07fSbrendan sav->sav_pending = NULL; 3178fa94a07fSbrendan sav->sav_npending = 0; 317939c23413Seschrock return (error); 318099653d4eSeschrock } 318199653d4eSeschrock 3182fa94a07fSbrendan static int 3183fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3184fa94a07fSbrendan { 3185fa94a07fSbrendan int error; 3186fa94a07fSbrendan 3187e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3188e14bb325SJeff Bonwick 3189fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3190fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3191fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3192fa94a07fSbrendan return (error); 3193fa94a07fSbrendan } 3194fa94a07fSbrendan 3195fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3196fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3197fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3198fa94a07fSbrendan } 3199fa94a07fSbrendan 3200fa94a07fSbrendan static void 3201fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3202fa94a07fSbrendan const char *config) 3203fa94a07fSbrendan { 3204fa94a07fSbrendan int i; 3205fa94a07fSbrendan 3206fa94a07fSbrendan if (sav->sav_config != NULL) { 3207fa94a07fSbrendan nvlist_t **olddevs; 3208fa94a07fSbrendan uint_t oldndevs; 3209fa94a07fSbrendan nvlist_t **newdevs; 3210fa94a07fSbrendan 3211fa94a07fSbrendan /* 3212fa94a07fSbrendan * Generate new dev list by concatentating with the 3213fa94a07fSbrendan * current dev list. 3214fa94a07fSbrendan */ 3215fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3216fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3217fa94a07fSbrendan 3218fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3219fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3220fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3221fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3222fa94a07fSbrendan KM_SLEEP) == 0); 3223fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3224fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3225fa94a07fSbrendan KM_SLEEP) == 0); 3226fa94a07fSbrendan 3227fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3228fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3229fa94a07fSbrendan 3230fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3231fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3232fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3233fa94a07fSbrendan nvlist_free(newdevs[i]); 3234fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3235fa94a07fSbrendan } else { 3236fa94a07fSbrendan /* 3237fa94a07fSbrendan * Generate a new dev list. 3238fa94a07fSbrendan */ 3239fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3240fa94a07fSbrendan KM_SLEEP) == 0); 3241fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3242fa94a07fSbrendan devs, ndevs) == 0); 3243fa94a07fSbrendan } 3244fa94a07fSbrendan } 3245fa94a07fSbrendan 3246fa94a07fSbrendan /* 3247fa94a07fSbrendan * Stop and drop level 2 ARC devices 3248fa94a07fSbrendan */ 3249fa94a07fSbrendan void 3250fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3251fa94a07fSbrendan { 3252fa94a07fSbrendan vdev_t *vd; 3253fa94a07fSbrendan int i; 3254fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3255fa94a07fSbrendan 3256fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3257fa94a07fSbrendan uint64_t pool; 3258fa94a07fSbrendan 3259fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3260fa94a07fSbrendan ASSERT(vd != NULL); 3261fa94a07fSbrendan 32628ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 32638ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3264fa94a07fSbrendan l2arc_remove_vdev(vd); 3265fa94a07fSbrendan } 3266fa94a07fSbrendan } 3267fa94a07fSbrendan 3268fa9e4066Sahrens /* 3269fa9e4066Sahrens * Pool Creation 3270fa9e4066Sahrens */ 3271fa9e4066Sahrens int 3272990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 32734445fffbSMatthew Ahrens nvlist_t *zplprops) 3274fa9e4066Sahrens { 3275fa9e4066Sahrens spa_t *spa; 3276990b4856Slling char *altroot = NULL; 32770373e76bSbonwick vdev_t *rvd; 3278fa9e4066Sahrens dsl_pool_t *dp; 3279fa9e4066Sahrens dmu_tx_t *tx; 3280573ca77eSGeorge Wilson int error = 0; 3281fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3282fa94a07fSbrendan nvlist_t **spares, **l2cache; 3283fa94a07fSbrendan uint_t nspares, nl2cache; 3284cde58dbcSMatthew Ahrens uint64_t version, obj; 3285ad135b5dSChristopher Siden boolean_t has_features; 3286fa9e4066Sahrens 3287fa9e4066Sahrens /* 3288fa9e4066Sahrens * If this pool already exists, return failure. 3289fa9e4066Sahrens */ 3290fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3291fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3292fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3293fa9e4066Sahrens return (EEXIST); 3294fa9e4066Sahrens } 3295fa9e4066Sahrens 3296fa9e4066Sahrens /* 3297fa9e4066Sahrens * Allocate a new spa_t structure. 3298fa9e4066Sahrens */ 3299990b4856Slling (void) nvlist_lookup_string(props, 3300990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3301468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 33028ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3303fa9e4066Sahrens 3304990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3305990b4856Slling spa_deactivate(spa); 3306990b4856Slling spa_remove(spa); 3307c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3308990b4856Slling return (error); 3309990b4856Slling } 3310990b4856Slling 3311ad135b5dSChristopher Siden has_features = B_FALSE; 3312ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3313ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3314ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3315ad135b5dSChristopher Siden has_features = B_TRUE; 3316ad135b5dSChristopher Siden } 3317ad135b5dSChristopher Siden 3318ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3319ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3320990b4856Slling version = SPA_VERSION; 3321ad135b5dSChristopher Siden } 3322ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3323b24ab676SJeff Bonwick 3324b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3325b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3326990b4856Slling spa->spa_uberblock.ub_version = version; 3327fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3328fa9e4066Sahrens 332954d692b7SGeorge Wilson /* 333054d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 333154d692b7SGeorge Wilson */ 333225f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 333325f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 333454d692b7SGeorge Wilson 33350373e76bSbonwick /* 33360373e76bSbonwick * Create the root vdev. 33370373e76bSbonwick */ 3338e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 33390373e76bSbonwick 334099653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 33410373e76bSbonwick 334299653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 334399653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 33440373e76bSbonwick 3345b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 33460373e76bSbonwick error = EINVAL; 334799653d4eSeschrock 334899653d4eSeschrock if (error == 0 && 334999653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3350fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 335199653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3352573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3353573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3354573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3355573ca77eSGeorge Wilson } 33560373e76bSbonwick } 33570373e76bSbonwick 3358e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3359fa9e4066Sahrens 336099653d4eSeschrock if (error != 0) { 3361fa9e4066Sahrens spa_unload(spa); 3362fa9e4066Sahrens spa_deactivate(spa); 3363fa9e4066Sahrens spa_remove(spa); 3364fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3365fa9e4066Sahrens return (error); 3366fa9e4066Sahrens } 3367fa9e4066Sahrens 336899653d4eSeschrock /* 336999653d4eSeschrock * Get the list of spares, if specified. 337099653d4eSeschrock */ 337199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 337299653d4eSeschrock &spares, &nspares) == 0) { 3373fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 337499653d4eSeschrock KM_SLEEP) == 0); 3375fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 337699653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3377e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 337899653d4eSeschrock spa_load_spares(spa); 3379e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3380fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3381fa94a07fSbrendan } 3382fa94a07fSbrendan 3383fa94a07fSbrendan /* 3384fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3385fa94a07fSbrendan */ 3386fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3387fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3388fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3389fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3390fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3391fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3392e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3393fa94a07fSbrendan spa_load_l2cache(spa); 3394e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3395fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 339699653d4eSeschrock } 339799653d4eSeschrock 3398ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 33990a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3400fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3401ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3402fa9e4066Sahrens 3403485bbbf5SGeorge Wilson /* 3404485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3405485bbbf5SGeorge Wilson */ 3406485bbbf5SGeorge Wilson ddt_create(spa); 3407485bbbf5SGeorge Wilson 3408485bbbf5SGeorge Wilson spa_update_dspace(spa); 3409485bbbf5SGeorge Wilson 3410fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3411fa9e4066Sahrens 3412fa9e4066Sahrens /* 3413fa9e4066Sahrens * Create the pool config object. 3414fa9e4066Sahrens */ 3415fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3416f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3417fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3418fa9e4066Sahrens 3419ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3420fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3421ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3422ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3423ea8dc4b6Seschrock } 3424fa9e4066Sahrens 3425ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3426ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3427ad135b5dSChristopher Siden 34283f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 34293f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 34303f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 34313f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 34323f9d6ad7SLin Ling } 34333f9d6ad7SLin Ling 3434990b4856Slling /* Newly created pools with the right version are always deflated. */ 3435990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3436990b4856Slling spa->spa_deflate = TRUE; 3437990b4856Slling if (zap_add(spa->spa_meta_objset, 3438990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3439990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3440990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3441990b4856Slling } 344299653d4eSeschrock } 344399653d4eSeschrock 3444fa9e4066Sahrens /* 3445cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3446fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3447fa9e4066Sahrens * keeps changing. 3448fa9e4066Sahrens */ 3449cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3450cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3451cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3452ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3453cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3454cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3455cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3456ea8dc4b6Seschrock } 3457b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3458cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3459fa9e4066Sahrens 346006eeb2adSek /* 346106eeb2adSek * Create the pool's history object. 346206eeb2adSek */ 3463990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3464990b4856Slling spa_history_create_obj(spa, tx); 3465990b4856Slling 3466990b4856Slling /* 3467990b4856Slling * Set pool properties. 3468990b4856Slling */ 3469990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3470990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 34710a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3472573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3473b24ab676SJeff Bonwick 3474379c004dSEric Schrock if (props != NULL) { 3475379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 34763f9d6ad7SLin Ling spa_sync_props(spa, props, tx); 3477379c004dSEric Schrock } 347806eeb2adSek 3479fa9e4066Sahrens dmu_tx_commit(tx); 3480fa9e4066Sahrens 3481fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3482fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3483fa9e4066Sahrens 3484fa9e4066Sahrens /* 3485fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3486fa9e4066Sahrens * bean counters are appropriately updated. 3487fa9e4066Sahrens */ 3488fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3489fa9e4066Sahrens 3490c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3491fa9e4066Sahrens 34924445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3493228975ccSek 3494088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 3495088f3894Sahrens 3496daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3497daaa36a7SGeorge Wilson 3498fa9e4066Sahrens return (0); 3499fa9e4066Sahrens } 3500fa9e4066Sahrens 3501e7cbe64fSgw #ifdef _KERNEL 3502e7cbe64fSgw /* 350321ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 350421ecdf64SLin Ling * during the system boot up time. 3505e7cbe64fSgw */ 350621ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 350721ecdf64SLin Ling 350821ecdf64SLin Ling static nvlist_t * 350921ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3510e7cbe64fSgw { 351121ecdf64SLin Ling nvlist_t *config; 3512e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3513e7cbe64fSgw uint64_t pgid; 3514e7cbe64fSgw 351521ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 351621ecdf64SLin Ling return (NULL); 351721ecdf64SLin Ling 3518e7cbe64fSgw /* 3519e7cbe64fSgw * Add this top-level vdev to the child array. 3520e7cbe64fSgw */ 352121ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 352221ecdf64SLin Ling &nvtop) == 0); 352321ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 352421ecdf64SLin Ling &pgid) == 0); 352521ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3526e7cbe64fSgw 3527e7cbe64fSgw /* 3528e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3529e7cbe64fSgw */ 3530e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 353121ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 353221ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3533e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3534e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3535e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3536e7cbe64fSgw &nvtop, 1) == 0); 3537e7cbe64fSgw 3538e7cbe64fSgw /* 3539e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3540e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3541e7cbe64fSgw */ 3542e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3543e7cbe64fSgw nvlist_free(nvroot); 354421ecdf64SLin Ling return (config); 3545e7cbe64fSgw } 3546e7cbe64fSgw 3547e7cbe64fSgw /* 354821ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 354921ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 355021ecdf64SLin Ling * device has a more recent txg. 3551051aabe6Staylor */ 355221ecdf64SLin Ling static void 355321ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3554051aabe6Staylor { 3555573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 355621ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3557051aabe6Staylor 355821ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 355921ecdf64SLin Ling nvlist_t *label; 356021ecdf64SLin Ling uint64_t label_txg; 3561051aabe6Staylor 356221ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 356321ecdf64SLin Ling &label) != 0) 356421ecdf64SLin Ling return; 3565051aabe6Staylor 356621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 356721ecdf64SLin Ling &label_txg) == 0); 3568051aabe6Staylor 356921ecdf64SLin Ling /* 357021ecdf64SLin Ling * Do we have a better boot device? 357121ecdf64SLin Ling */ 357221ecdf64SLin Ling if (label_txg > *txg) { 357321ecdf64SLin Ling *txg = label_txg; 357421ecdf64SLin Ling *avd = vd; 3575051aabe6Staylor } 357621ecdf64SLin Ling nvlist_free(label); 3577051aabe6Staylor } 3578051aabe6Staylor } 3579051aabe6Staylor 3580e7cbe64fSgw /* 3581e7cbe64fSgw * Import a root pool. 3582e7cbe64fSgw * 3583051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3584051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3585051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3586e7cbe64fSgw * 3587e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3588e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3589e7cbe64fSgw * e.g. 3590e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3591e7cbe64fSgw */ 3592e7cbe64fSgw int 3593051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3594e7cbe64fSgw { 359521ecdf64SLin Ling spa_t *spa; 359621ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 359721ecdf64SLin Ling nvlist_t *config, *nvtop; 359821ecdf64SLin Ling uint64_t guid, txg; 3599e7cbe64fSgw char *pname; 3600e7cbe64fSgw int error; 3601e7cbe64fSgw 3602e7cbe64fSgw /* 360321ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3604e7cbe64fSgw */ 3605dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3606dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3607dedec472SJack Meng if (config == NULL) { 3608dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3609dedec472SJack Meng /* iscsi boot */ 3610dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3611dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3612dedec472SJack Meng } 3613dedec472SJack Meng } 3614dedec472SJack Meng #endif 3615dedec472SJack Meng if (config == NULL) { 3616ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 361721ecdf64SLin Ling devpath); 361821ecdf64SLin Ling return (EIO); 361921ecdf64SLin Ling } 3620e7cbe64fSgw 362121ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 362221ecdf64SLin Ling &pname) == 0); 362321ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3624e7cbe64fSgw 36256809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 36266809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 36276809eb4eSEric Schrock /* 36286809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 36296809eb4eSEric Schrock * can replace it with the correct config we just read in. 36306809eb4eSEric Schrock */ 36316809eb4eSEric Schrock spa_remove(spa); 36326809eb4eSEric Schrock } 36336809eb4eSEric Schrock 3634468c413aSTim Haley spa = spa_add(pname, config, NULL); 36356809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 36364b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3637e7cbe64fSgw 363821ecdf64SLin Ling /* 363921ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 364021ecdf64SLin Ling */ 364121ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 364221ecdf64SLin Ling &nvtop) == 0); 364321ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 364421ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 364521ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 364621ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 364721ecdf64SLin Ling if (error) { 364821ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 364921ecdf64SLin Ling nvlist_free(config); 365021ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 365121ecdf64SLin Ling pname); 365221ecdf64SLin Ling return (error); 365321ecdf64SLin Ling } 365421ecdf64SLin Ling 365521ecdf64SLin Ling /* 365621ecdf64SLin Ling * Get the boot vdev. 365721ecdf64SLin Ling */ 365821ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 365921ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 366021ecdf64SLin Ling (u_longlong_t)guid); 366121ecdf64SLin Ling error = ENOENT; 366221ecdf64SLin Ling goto out; 366321ecdf64SLin Ling } 3664e7cbe64fSgw 366521ecdf64SLin Ling /* 366621ecdf64SLin Ling * Determine if there is a better boot device. 366721ecdf64SLin Ling */ 366821ecdf64SLin Ling avd = bvd; 366921ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 367021ecdf64SLin Ling if (avd != bvd) { 367121ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 367221ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 367321ecdf64SLin Ling error = EINVAL; 367421ecdf64SLin Ling goto out; 367521ecdf64SLin Ling } 3676e7cbe64fSgw 367721ecdf64SLin Ling /* 367821ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 367921ecdf64SLin Ling * we're booting off the active spare. 368021ecdf64SLin Ling */ 368121ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 368221ecdf64SLin Ling !bvd->vdev_isspare) { 368321ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 368421ecdf64SLin Ling "try booting from '%s'", 3685cb04b873SMark J Musante bvd->vdev_parent-> 3686cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 368721ecdf64SLin Ling error = EINVAL; 368821ecdf64SLin Ling goto out; 368921ecdf64SLin Ling } 369021ecdf64SLin Ling 369121ecdf64SLin Ling error = 0; 369221ecdf64SLin Ling out: 369321ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 369421ecdf64SLin Ling vdev_free(rvd); 369521ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 369621ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 369721ecdf64SLin Ling 369821ecdf64SLin Ling nvlist_free(config); 3699e7cbe64fSgw return (error); 3700e7cbe64fSgw } 370121ecdf64SLin Ling 3702e7cbe64fSgw #endif 3703e7cbe64fSgw 37046809eb4eSEric Schrock /* 37056809eb4eSEric Schrock * Import a non-root pool into the system. 37066809eb4eSEric Schrock */ 3707c5904d13Seschrock int 37084b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 3709c5904d13Seschrock { 37106809eb4eSEric Schrock spa_t *spa; 37116809eb4eSEric Schrock char *altroot = NULL; 3712468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3713468c413aSTim Haley zpool_rewind_policy_t policy; 3714f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 3715f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 37166809eb4eSEric Schrock int error; 37176809eb4eSEric Schrock nvlist_t *nvroot; 37186809eb4eSEric Schrock nvlist_t **spares, **l2cache; 37196809eb4eSEric Schrock uint_t nspares, nl2cache; 37206809eb4eSEric Schrock 37216809eb4eSEric Schrock /* 37226809eb4eSEric Schrock * If a pool with this name exists, return failure. 37236809eb4eSEric Schrock */ 37246809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 37251195e687SMark J Musante if (spa_lookup(pool) != NULL) { 37266809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 37276809eb4eSEric Schrock return (EEXIST); 37286809eb4eSEric Schrock } 37296809eb4eSEric Schrock 37306809eb4eSEric Schrock /* 37316809eb4eSEric Schrock * Create and initialize the spa structure. 37326809eb4eSEric Schrock */ 37336809eb4eSEric Schrock (void) nvlist_lookup_string(props, 37346809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3735f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 3736f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3737f9af39baSGeorge Wilson if (readonly) 3738f9af39baSGeorge Wilson mode = FREAD; 3739468c413aSTim Haley spa = spa_add(pool, config, altroot); 37404b964adaSGeorge Wilson spa->spa_import_flags = flags; 37414b964adaSGeorge Wilson 37424b964adaSGeorge Wilson /* 37434b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 37444b964adaSGeorge Wilson * as if it had been loaded at boot. 37454b964adaSGeorge Wilson */ 37464b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 37474b964adaSGeorge Wilson if (props != NULL) 37484b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 37494b964adaSGeorge Wilson 37504b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 37514b964adaSGeorge Wilson 37524b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 37534445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 37544b964adaSGeorge Wilson 37554b964adaSGeorge Wilson return (0); 37564b964adaSGeorge Wilson } 37574b964adaSGeorge Wilson 3758f9af39baSGeorge Wilson spa_activate(spa, mode); 37596809eb4eSEric Schrock 376025f89ee2SJeff Bonwick /* 376125f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 376225f89ee2SJeff Bonwick */ 376325f89ee2SJeff Bonwick spa_async_suspend(spa); 376425f89ee2SJeff Bonwick 37654b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 37664b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 37674b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 37684b964adaSGeorge Wilson 37696809eb4eSEric Schrock /* 37706809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 37716809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 37726809eb4eSEric Schrock * doing an import. 37736809eb4eSEric Schrock */ 3774468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3775468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 37764b964adaSGeorge Wilson 3777468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3778c8ee1847SVictor Latushkin policy.zrp_request); 3779468c413aSTim Haley 3780468c413aSTim Haley /* 37814b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 37824b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 3783468c413aSTim Haley */ 37844b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 37854b964adaSGeorge Wilson spa->spa_load_info) == 0); 37866809eb4eSEric Schrock 37876809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 37886809eb4eSEric Schrock /* 37896809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 37906809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 37916809eb4eSEric Schrock */ 37926809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 37936809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 37946809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 37956809eb4eSEric Schrock spa_load_spares(spa); 37966809eb4eSEric Schrock } 37976809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 37986809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 37996809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 38006809eb4eSEric Schrock spa_load_l2cache(spa); 38016809eb4eSEric Schrock } 38026809eb4eSEric Schrock 38036809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 38046809eb4eSEric Schrock &nvroot) == 0); 38056809eb4eSEric Schrock if (error == 0) 38066809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38076809eb4eSEric Schrock VDEV_ALLOC_SPARE); 38086809eb4eSEric Schrock if (error == 0) 38096809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38106809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 38116809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 38126809eb4eSEric Schrock 38136809eb4eSEric Schrock if (props != NULL) 38146809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38156809eb4eSEric Schrock 38166809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 38176809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 38186809eb4eSEric Schrock spa_unload(spa); 38196809eb4eSEric Schrock spa_deactivate(spa); 38206809eb4eSEric Schrock spa_remove(spa); 38216809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38226809eb4eSEric Schrock return (error); 38236809eb4eSEric Schrock } 38246809eb4eSEric Schrock 3825955ef359SLin Ling spa_async_resume(spa); 3826955ef359SLin Ling 38276809eb4eSEric Schrock /* 38286809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 38296809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 38306809eb4eSEric Schrock */ 38316809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 38326809eb4eSEric Schrock &spares, &nspares) == 0) { 38336809eb4eSEric Schrock if (spa->spa_spares.sav_config) 38346809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 38356809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 38366809eb4eSEric Schrock else 38376809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 38386809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 38396809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 38406809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 38416809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 38426809eb4eSEric Schrock spa_load_spares(spa); 38436809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 38446809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 38456809eb4eSEric Schrock } 38466809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 38476809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 38486809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 38496809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 38506809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 38516809eb4eSEric Schrock else 38526809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 38536809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 38546809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 38556809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 38566809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 38576809eb4eSEric Schrock spa_load_l2cache(spa); 38586809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 38596809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 38606809eb4eSEric Schrock } 38616809eb4eSEric Schrock 3862b693757aSEric Schrock /* 3863b693757aSEric Schrock * Check for any removed devices. 3864b693757aSEric Schrock */ 3865b693757aSEric Schrock if (spa->spa_autoreplace) { 3866b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3867b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3868b693757aSEric Schrock } 3869b693757aSEric Schrock 38706809eb4eSEric Schrock if (spa_writeable(spa)) { 38716809eb4eSEric Schrock /* 38726809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 38736809eb4eSEric Schrock */ 3874bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 38756809eb4eSEric Schrock } 38766809eb4eSEric Schrock 3877573ca77eSGeorge Wilson /* 3878573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 3879573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 3880573ca77eSGeorge Wilson */ 3881573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 3882573ca77eSGeorge Wilson 38836809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38844445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 38856809eb4eSEric Schrock 38866809eb4eSEric Schrock return (0); 3887c5904d13Seschrock } 3888c5904d13Seschrock 3889fa9e4066Sahrens nvlist_t * 3890fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 3891fa9e4066Sahrens { 3892fa9e4066Sahrens nvlist_t *config = NULL; 3893fa9e4066Sahrens char *poolname; 3894fa9e4066Sahrens spa_t *spa; 3895fa9e4066Sahrens uint64_t state; 38967b7154beSLin Ling int error; 3897fa9e4066Sahrens 3898fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3899fa9e4066Sahrens return (NULL); 3900fa9e4066Sahrens 3901fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3902fa9e4066Sahrens return (NULL); 3903fa9e4066Sahrens 3904fa9e4066Sahrens /* 39050373e76bSbonwick * Create and initialize the spa structure. 3906fa9e4066Sahrens */ 39070373e76bSbonwick mutex_enter(&spa_namespace_lock); 3908468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 39098ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 3910fa9e4066Sahrens 3911fa9e4066Sahrens /* 39120373e76bSbonwick * Pass off the heavy lifting to spa_load(). 3913ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 3914ecc2d604Sbonwick * is actually the one to trust when doing an import. 3915fa9e4066Sahrens */ 39161195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3917fa9e4066Sahrens 3918fa9e4066Sahrens /* 3919fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 3920fa9e4066Sahrens */ 3921fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 3922fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3923fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3924fa9e4066Sahrens poolname) == 0); 3925fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3926fa9e4066Sahrens state) == 0); 392795173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 392895173954Sek spa->spa_uberblock.ub_timestamp) == 0); 3929ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 3930ad135b5dSChristopher Siden spa->spa_load_info) == 0); 393199653d4eSeschrock 3932e7cbe64fSgw /* 3933e7cbe64fSgw * If the bootfs property exists on this pool then we 3934e7cbe64fSgw * copy it out so that external consumers can tell which 3935e7cbe64fSgw * pools are bootable. 3936e7cbe64fSgw */ 39377b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 3938e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3939e7cbe64fSgw 3940e7cbe64fSgw /* 3941e7cbe64fSgw * We have to play games with the name since the 3942e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 3943e7cbe64fSgw */ 3944e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 3945e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 3946e7cbe64fSgw char *cp; 3947e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3948e7cbe64fSgw 3949e7cbe64fSgw cp = strchr(tmpname, '/'); 3950e7cbe64fSgw if (cp == NULL) { 3951e7cbe64fSgw (void) strlcpy(dsname, tmpname, 3952e7cbe64fSgw MAXPATHLEN); 3953e7cbe64fSgw } else { 3954e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 3955e7cbe64fSgw "%s/%s", poolname, ++cp); 3956e7cbe64fSgw } 3957e7cbe64fSgw VERIFY(nvlist_add_string(config, 3958e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 3959e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 3960e7cbe64fSgw } 3961e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 3962e7cbe64fSgw } 3963e7cbe64fSgw 396499653d4eSeschrock /* 3965fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 396699653d4eSeschrock */ 39676809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 396899653d4eSeschrock spa_add_spares(spa, config); 3969fa94a07fSbrendan spa_add_l2cache(spa, config); 39706809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3971fa9e4066Sahrens } 3972fa9e4066Sahrens 3973fa9e4066Sahrens spa_unload(spa); 3974fa9e4066Sahrens spa_deactivate(spa); 3975fa9e4066Sahrens spa_remove(spa); 3976fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3977fa9e4066Sahrens 3978fa9e4066Sahrens return (config); 3979fa9e4066Sahrens } 3980fa9e4066Sahrens 3981fa9e4066Sahrens /* 3982fa9e4066Sahrens * Pool export/destroy 3983fa9e4066Sahrens * 3984fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 3985fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 3986fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 3987394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 3988394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 3989fa9e4066Sahrens */ 3990fa9e4066Sahrens static int 399189a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 3992394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 3993fa9e4066Sahrens { 3994fa9e4066Sahrens spa_t *spa; 3995fa9e4066Sahrens 399644cd46caSbillm if (oldconfig) 399744cd46caSbillm *oldconfig = NULL; 399844cd46caSbillm 39998ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4000fa9e4066Sahrens return (EROFS); 4001fa9e4066Sahrens 4002fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4003fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4004fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4005fa9e4066Sahrens return (ENOENT); 4006fa9e4066Sahrens } 4007fa9e4066Sahrens 4008ea8dc4b6Seschrock /* 4009ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4010ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4011ea8dc4b6Seschrock */ 4012ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4013ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4014ea8dc4b6Seschrock spa_async_suspend(spa); 4015ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4016ea8dc4b6Seschrock spa_close(spa, FTAG); 4017ea8dc4b6Seschrock 4018fa9e4066Sahrens /* 4019fa9e4066Sahrens * The pool will be in core if it's openable, 4020fa9e4066Sahrens * in which case we can modify its state. 4021fa9e4066Sahrens */ 4022fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4023fa9e4066Sahrens /* 4024fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4025fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4026fa9e4066Sahrens */ 4027fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4028fa9e4066Sahrens 4029ea8dc4b6Seschrock /* 4030ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4031ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4032ea8dc4b6Seschrock * fault injection handlers. 4033ea8dc4b6Seschrock */ 4034ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4035ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4036ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4037ea8dc4b6Seschrock spa_async_resume(spa); 4038fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4039fa9e4066Sahrens return (EBUSY); 4040fa9e4066Sahrens } 4041fa9e4066Sahrens 404289a89ebfSlling /* 404389a89ebfSlling * A pool cannot be exported if it has an active shared spare. 404489a89ebfSlling * This is to prevent other pools stealing the active spare 404589a89ebfSlling * from an exported pool. At user's own will, such pool can 404689a89ebfSlling * be forcedly exported. 404789a89ebfSlling */ 404889a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 404989a89ebfSlling spa_has_active_shared_spare(spa)) { 405089a89ebfSlling spa_async_resume(spa); 405189a89ebfSlling mutex_exit(&spa_namespace_lock); 405289a89ebfSlling return (EXDEV); 405389a89ebfSlling } 405489a89ebfSlling 4055fa9e4066Sahrens /* 4056fa9e4066Sahrens * We want this to be reflected on every label, 4057fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4058fa9e4066Sahrens * final sync that pushes these changes out. 4059fa9e4066Sahrens */ 4060394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4061e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4062ea8dc4b6Seschrock spa->spa_state = new_state; 40633f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 40643f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4065ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4066e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4067ea8dc4b6Seschrock } 4068fa9e4066Sahrens } 4069fa9e4066Sahrens 40703d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 40713d7072f8Seschrock 4072fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4073fa9e4066Sahrens spa_unload(spa); 4074fa9e4066Sahrens spa_deactivate(spa); 4075fa9e4066Sahrens } 4076fa9e4066Sahrens 407744cd46caSbillm if (oldconfig && spa->spa_config) 407844cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 407944cd46caSbillm 4080ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4081394ab0cbSGeorge Wilson if (!hardforce) 4082394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4083ea8dc4b6Seschrock spa_remove(spa); 4084ea8dc4b6Seschrock } 4085fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4086fa9e4066Sahrens 4087fa9e4066Sahrens return (0); 4088fa9e4066Sahrens } 4089fa9e4066Sahrens 4090fa9e4066Sahrens /* 4091fa9e4066Sahrens * Destroy a storage pool. 4092fa9e4066Sahrens */ 4093fa9e4066Sahrens int 4094fa9e4066Sahrens spa_destroy(char *pool) 4095fa9e4066Sahrens { 4096394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4097394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4098fa9e4066Sahrens } 4099fa9e4066Sahrens 4100fa9e4066Sahrens /* 4101fa9e4066Sahrens * Export a storage pool. 4102fa9e4066Sahrens */ 4103fa9e4066Sahrens int 4104394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4105394ab0cbSGeorge Wilson boolean_t hardforce) 4106fa9e4066Sahrens { 4107394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4108394ab0cbSGeorge Wilson force, hardforce)); 4109fa9e4066Sahrens } 4110fa9e4066Sahrens 4111ea8dc4b6Seschrock /* 4112ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4113ea8dc4b6Seschrock * from the namespace in any way. 4114ea8dc4b6Seschrock */ 4115ea8dc4b6Seschrock int 4116ea8dc4b6Seschrock spa_reset(char *pool) 4117ea8dc4b6Seschrock { 411889a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4119394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4120ea8dc4b6Seschrock } 4121ea8dc4b6Seschrock 4122fa9e4066Sahrens /* 4123fa9e4066Sahrens * ========================================================================== 4124fa9e4066Sahrens * Device manipulation 4125fa9e4066Sahrens * ========================================================================== 4126fa9e4066Sahrens */ 4127fa9e4066Sahrens 4128fa9e4066Sahrens /* 41298654d025Sperrin * Add a device to a storage pool. 4130fa9e4066Sahrens */ 4131fa9e4066Sahrens int 4132fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4133fa9e4066Sahrens { 413488ecc943SGeorge Wilson uint64_t txg, id; 41358ad4d6ddSJeff Bonwick int error; 4136fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 41370e34b6a7Sbonwick vdev_t *vd, *tvd; 4138fa94a07fSbrendan nvlist_t **spares, **l2cache; 4139fa94a07fSbrendan uint_t nspares, nl2cache; 4140fa9e4066Sahrens 4141f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4142f9af39baSGeorge Wilson 4143fa9e4066Sahrens txg = spa_vdev_enter(spa); 4144fa9e4066Sahrens 414599653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 414699653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 414799653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4148fa9e4066Sahrens 4149e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 415099653d4eSeschrock 4151fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4152fa94a07fSbrendan &nspares) != 0) 415399653d4eSeschrock nspares = 0; 415499653d4eSeschrock 4155fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4156fa94a07fSbrendan &nl2cache) != 0) 4157fa94a07fSbrendan nl2cache = 0; 4158fa94a07fSbrendan 4159e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4160fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4161fa9e4066Sahrens 4162e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4163e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4164e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 416599653d4eSeschrock 416639c23413Seschrock /* 4167fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4168fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 416939c23413Seschrock */ 4170e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 417139c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 417239c23413Seschrock 417339c23413Seschrock /* 417439c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 417539c23413Seschrock */ 41768ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 417788ecc943SGeorge Wilson 417888ecc943SGeorge Wilson /* 417988ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 418088ecc943SGeorge Wilson */ 418188ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 418288ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 418388ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 418488ecc943SGeorge Wilson break; 418588ecc943SGeorge Wilson } 418688ecc943SGeorge Wilson } 418739c23413Seschrock tvd = vd->vdev_child[c]; 418839c23413Seschrock vdev_remove_child(vd, tvd); 418988ecc943SGeorge Wilson tvd->vdev_id = id; 419039c23413Seschrock vdev_add_child(rvd, tvd); 419139c23413Seschrock vdev_config_dirty(tvd); 419239c23413Seschrock } 419339c23413Seschrock 419499653d4eSeschrock if (nspares != 0) { 4195fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4196fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 419799653d4eSeschrock spa_load_spares(spa); 4198fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4199fa94a07fSbrendan } 4200fa94a07fSbrendan 4201fa94a07fSbrendan if (nl2cache != 0) { 4202fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4203fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4204fa94a07fSbrendan spa_load_l2cache(spa); 4205fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4206fa9e4066Sahrens } 4207fa9e4066Sahrens 4208fa9e4066Sahrens /* 42090e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 42100e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 42110e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 42120e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 42130e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 42140e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 42150373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 42160e34b6a7Sbonwick * 42170e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 42180e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 42190e34b6a7Sbonwick * steps will be completed the next time we load the pool. 42200e34b6a7Sbonwick */ 42210373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 42220e34b6a7Sbonwick 42230373e76bSbonwick mutex_enter(&spa_namespace_lock); 42240373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 42250373e76bSbonwick mutex_exit(&spa_namespace_lock); 4226fa9e4066Sahrens 42270373e76bSbonwick return (0); 4228fa9e4066Sahrens } 4229fa9e4066Sahrens 4230fa9e4066Sahrens /* 4231fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4232fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4233fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4234fa9e4066Sahrens * 4235fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4236fa9e4066Sahrens * existing device; in this case the two devices are made into their own 42373d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4238fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4239fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4240fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4241fa9e4066Sahrens * is automatically detached. 4242fa9e4066Sahrens */ 4243fa9e4066Sahrens int 4244ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4245fa9e4066Sahrens { 42463f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4247fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4248fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 424999653d4eSeschrock vdev_ops_t *pvops; 42509b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 42519b3f6b42SEric Kustarz int newvd_isspare; 42529b3f6b42SEric Kustarz int error; 4253fa9e4066Sahrens 4254f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4255f9af39baSGeorge Wilson 4256fa9e4066Sahrens txg = spa_vdev_enter(spa); 4257fa9e4066Sahrens 4258c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4259fa9e4066Sahrens 4260fa9e4066Sahrens if (oldvd == NULL) 4261fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4262fa9e4066Sahrens 42630e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 42640e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 42650e34b6a7Sbonwick 4266fa9e4066Sahrens pvd = oldvd->vdev_parent; 4267fa9e4066Sahrens 426899653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4269cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 42703d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 42713d7072f8Seschrock 42723d7072f8Seschrock if (newrootvd->vdev_children != 1) 4273fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4274fa9e4066Sahrens 4275fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4276fa9e4066Sahrens 4277fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4278fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4279fa9e4066Sahrens 428099653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4281fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4282fa9e4066Sahrens 42838654d025Sperrin /* 42848654d025Sperrin * Spares can't replace logs 42858654d025Sperrin */ 4286ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 42878654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 42888654d025Sperrin 428999653d4eSeschrock if (!replacing) { 429099653d4eSeschrock /* 429199653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 429299653d4eSeschrock * vdev. 429399653d4eSeschrock */ 429499653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 429599653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 429699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 429799653d4eSeschrock 429899653d4eSeschrock pvops = &vdev_mirror_ops; 429999653d4eSeschrock } else { 430099653d4eSeschrock /* 430199653d4eSeschrock * Active hot spares can only be replaced by inactive hot 430299653d4eSeschrock * spares. 430399653d4eSeschrock */ 430499653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4305cb04b873SMark J Musante oldvd->vdev_isspare && 430699653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 430799653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 430899653d4eSeschrock 430999653d4eSeschrock /* 431099653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 431199653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 431239c23413Seschrock * want to create a replacing vdev. The user is not allowed to 431339c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 431439c23413Seschrock * the same (spare replaces spare, non-spare replaces 431539c23413Seschrock * non-spare). 431699653d4eSeschrock */ 4317cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4318cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 431999653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4320cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4321cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 432239c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4323cb04b873SMark J Musante } 4324cb04b873SMark J Musante 4325cb04b873SMark J Musante if (newvd->vdev_isspare) 432699653d4eSeschrock pvops = &vdev_spare_ops; 432799653d4eSeschrock else 432899653d4eSeschrock pvops = &vdev_replacing_ops; 432999653d4eSeschrock } 433099653d4eSeschrock 43312a79c5feSlling /* 4332573ca77eSGeorge Wilson * Make sure the new device is big enough. 43332a79c5feSlling */ 4334573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4335fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4336fa9e4066Sahrens 4337ecc2d604Sbonwick /* 4338ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4339ecc2d604Sbonwick * than the top-level vdev. 4340ecc2d604Sbonwick */ 4341ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4342fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4343fa9e4066Sahrens 4344fa9e4066Sahrens /* 4345fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4346fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4347fa9e4066Sahrens */ 4348fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4349fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4350fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4351fa9e4066Sahrens KM_SLEEP); 4352fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4353fa9e4066Sahrens newvd->vdev_path, "old"); 4354fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4355fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4356fa9e4066Sahrens oldvd->vdev_devid = NULL; 4357fa9e4066Sahrens } 4358fa9e4066Sahrens } 4359fa9e4066Sahrens 4360cb04b873SMark J Musante /* mark the device being resilvered */ 4361cb04b873SMark J Musante newvd->vdev_resilvering = B_TRUE; 4362cb04b873SMark J Musante 4363fa9e4066Sahrens /* 436499653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 436599653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4366fa9e4066Sahrens */ 4367fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4368fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4369fa9e4066Sahrens 4370fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4371fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4372fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4373fa9e4066Sahrens 4374fa9e4066Sahrens /* 4375fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4376fa9e4066Sahrens */ 4377fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4378fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 437988ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4380fa9e4066Sahrens vdev_add_child(pvd, newvd); 4381fa9e4066Sahrens 4382fa9e4066Sahrens tvd = newvd->vdev_top; 4383fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4384fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4385fa9e4066Sahrens 4386fa9e4066Sahrens vdev_config_dirty(tvd); 4387fa9e4066Sahrens 4388fa9e4066Sahrens /* 43893f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 43903f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 43913f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4392fa9e4066Sahrens */ 43933f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4394fa9e4066Sahrens 43953f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 43963f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4397fa9e4066Sahrens 43986809eb4eSEric Schrock if (newvd->vdev_isspare) { 439939c23413Seschrock spa_spare_activate(newvd); 44006809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 44016809eb4eSEric Schrock } 44026809eb4eSEric Schrock 4403e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4404e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 44059b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4406ea8dc4b6Seschrock 4407fa9e4066Sahrens /* 4408fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4409fa9e4066Sahrens */ 4410ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4411fa9e4066Sahrens 44123f9d6ad7SLin Ling /* 44133f9d6ad7SLin Ling * Restart the resilver 44143f9d6ad7SLin Ling */ 44153f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 44163f9d6ad7SLin Ling 44173f9d6ad7SLin Ling /* 44183f9d6ad7SLin Ling * Commit the config 44193f9d6ad7SLin Ling */ 44203f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4421fa9e4066Sahrens 44224445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 44233f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4424c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4425c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4426c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 44279b3f6b42SEric Kustarz 44289b3f6b42SEric Kustarz spa_strfree(oldvdpath); 44299b3f6b42SEric Kustarz spa_strfree(newvdpath); 44309b3f6b42SEric Kustarz 4431943e9869SLori Alt if (spa->spa_bootfs) 4432943e9869SLori Alt spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 4433943e9869SLori Alt 4434fa9e4066Sahrens return (0); 4435fa9e4066Sahrens } 4436fa9e4066Sahrens 4437fa9e4066Sahrens /* 4438fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4439fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4440fa9e4066Sahrens * is a replacing vdev. 4441fa9e4066Sahrens */ 4442fa9e4066Sahrens int 44438ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4444fa9e4066Sahrens { 4445fa9e4066Sahrens uint64_t txg; 44468ad4d6ddSJeff Bonwick int error; 4447fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4448fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 444999653d4eSeschrock boolean_t unspare = B_FALSE; 4450d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 44511195e687SMark J Musante char *vdpath; 4452fa9e4066Sahrens 4453f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4454f9af39baSGeorge Wilson 4455fa9e4066Sahrens txg = spa_vdev_enter(spa); 4456fa9e4066Sahrens 4457c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4458fa9e4066Sahrens 4459fa9e4066Sahrens if (vd == NULL) 4460fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4461fa9e4066Sahrens 44620e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 44630e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 44640e34b6a7Sbonwick 4465fa9e4066Sahrens pvd = vd->vdev_parent; 4466fa9e4066Sahrens 44678ad4d6ddSJeff Bonwick /* 44688ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 44698ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 44708ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 44718ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 44728ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 44738ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 44748ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 44758ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 44768ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 44778ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 44788ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 44798ad4d6ddSJeff Bonwick */ 44808ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 44818ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 44828ad4d6ddSJeff Bonwick 4483fa9e4066Sahrens /* 4484cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 448599653d4eSeschrock */ 4486cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4487cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4488cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 448999653d4eSeschrock 449099653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4491e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4492fa9e4066Sahrens 4493fa9e4066Sahrens /* 449499653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4495fa9e4066Sahrens */ 4496fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 449799653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 449899653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4499fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4500fa9e4066Sahrens 4501fa9e4066Sahrens /* 45028ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 45038ad4d6ddSJeff Bonwick * we cannot safely detach it. 4504fa9e4066Sahrens */ 45058ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4506fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4507fa9e4066Sahrens 45088ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4509fa9e4066Sahrens 4510bf82a41bSeschrock /* 4511bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4512bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4513bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4514bf82a41bSeschrock */ 4515cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4516cb04b873SMark J Musante vd->vdev_path != NULL) { 4517cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4518cb04b873SMark J Musante 4519cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4520cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4521cb04b873SMark J Musante 4522cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4523cb04b873SMark J Musante continue; 4524cb04b873SMark J Musante 4525cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4526cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4527cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4528cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4529cb04b873SMark J Musante break; 4530cb04b873SMark J Musante } 4531bf82a41bSeschrock } 4532bf82a41bSeschrock } 4533bf82a41bSeschrock 453499653d4eSeschrock /* 453599653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 453699653d4eSeschrock * that the spare should become a real disk, and be removed from the 453799653d4eSeschrock * active spare list for the pool. 453899653d4eSeschrock */ 453999653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4540cb04b873SMark J Musante vd->vdev_id == 0 && 4541cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 454299653d4eSeschrock unspare = B_TRUE; 454399653d4eSeschrock 4544fa9e4066Sahrens /* 4545fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4546fa9e4066Sahrens * This must be done after all other error cases are handled, 4547fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4548fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4549fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4550fa9e4066Sahrens * it's being detached! 4551fa9e4066Sahrens */ 455239c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4553fa9e4066Sahrens 4554fa9e4066Sahrens /* 4555fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4556fa9e4066Sahrens */ 4557fa9e4066Sahrens vdev_remove_child(pvd, vd); 4558fa9e4066Sahrens vdev_compact_children(pvd); 4559fa9e4066Sahrens 4560fa9e4066Sahrens /* 4561fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4562fa9e4066Sahrens */ 4563cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4564fa9e4066Sahrens 456599653d4eSeschrock /* 456699653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 45678ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 45688ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 45698ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 45708ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 45718ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 45728ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 457399653d4eSeschrock */ 457499653d4eSeschrock if (unspare) { 457599653d4eSeschrock ASSERT(cvd->vdev_isspare); 457639c23413Seschrock spa_spare_remove(cvd); 457799653d4eSeschrock unspare_guid = cvd->vdev_guid; 45788ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4579cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 458099653d4eSeschrock } 458199653d4eSeschrock 4582fa9e4066Sahrens /* 4583fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4584fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4585fa9e4066Sahrens */ 4586cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4587cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4588cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4589fa9e4066Sahrens vdev_remove_parent(cvd); 4590cb04b873SMark J Musante cvd->vdev_resilvering = B_FALSE; 4591cb04b873SMark J Musante } 4592cb04b873SMark J Musante 4593fa9e4066Sahrens 4594fa9e4066Sahrens /* 4595fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4596fa9e4066Sahrens * may have been the previous top-level vdev. 4597fa9e4066Sahrens */ 4598fa9e4066Sahrens tvd = cvd->vdev_top; 4599fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4600fa9e4066Sahrens 4601fa9e4066Sahrens /* 460239c23413Seschrock * Reevaluate the parent vdev state. 4603fa9e4066Sahrens */ 46043d7072f8Seschrock vdev_propagate_state(cvd); 4605fa9e4066Sahrens 4606fa9e4066Sahrens /* 4607573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4608573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4609573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4610573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4611573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4612fa9e4066Sahrens */ 4613573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4614573ca77eSGeorge Wilson vdev_reopen(tvd); 4615573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4616573ca77eSGeorge Wilson } 4617fa9e4066Sahrens 4618fa9e4066Sahrens vdev_config_dirty(tvd); 4619fa9e4066Sahrens 4620fa9e4066Sahrens /* 462139c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 462239c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 462339c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 462439c23413Seschrock * prevent vd from being accessed after it's freed. 4625fa9e4066Sahrens */ 46261195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 46278ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4628fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4629ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4630ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4631fa9e4066Sahrens 46323d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 46333d7072f8Seschrock 4634cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4635cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4636cb04b873SMark J Musante 463799653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 463899653d4eSeschrock 46394445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 46401195e687SMark J Musante "vdev=%s", vdpath); 46411195e687SMark J Musante spa_strfree(vdpath); 46421195e687SMark J Musante 464399653d4eSeschrock /* 464439c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 464539c23413Seschrock * then we want to go through and remove the device from the hot spare 464639c23413Seschrock * list of every other pool. 464799653d4eSeschrock */ 464899653d4eSeschrock if (unspare) { 4649cb04b873SMark J Musante spa_t *altspa = NULL; 4650cb04b873SMark J Musante 465199653d4eSeschrock mutex_enter(&spa_namespace_lock); 4652cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4653cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4654cb04b873SMark J Musante altspa == spa) 465599653d4eSeschrock continue; 4656cb04b873SMark J Musante 4657cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 46589af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4659cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 46609af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4661cb04b873SMark J Musante spa_close(altspa, FTAG); 466299653d4eSeschrock } 466399653d4eSeschrock mutex_exit(&spa_namespace_lock); 4664cb04b873SMark J Musante 4665cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4666cb04b873SMark J Musante spa_vdev_resilver_done(spa); 466799653d4eSeschrock } 466899653d4eSeschrock 4669cb04b873SMark J Musante /* all done with the spa; OK to release */ 4670cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 4671cb04b873SMark J Musante spa_close(spa, FTAG); 4672cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 4673cb04b873SMark J Musante 467499653d4eSeschrock return (error); 467599653d4eSeschrock } 467699653d4eSeschrock 46771195e687SMark J Musante /* 46781195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 46791195e687SMark J Musante */ 46801195e687SMark J Musante int 46811195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 46821195e687SMark J Musante nvlist_t *props, boolean_t exp) 46831195e687SMark J Musante { 46841195e687SMark J Musante int error = 0; 46851195e687SMark J Musante uint64_t txg, *glist; 46861195e687SMark J Musante spa_t *newspa; 46871195e687SMark J Musante uint_t c, children, lastlog; 46881195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 46891195e687SMark J Musante dmu_tx_t *tx; 46901195e687SMark J Musante char *altroot = NULL; 46911195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 46921195e687SMark J Musante boolean_t activate_slog; 46931195e687SMark J Musante 4694f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 46951195e687SMark J Musante 46961195e687SMark J Musante txg = spa_vdev_enter(spa); 46971195e687SMark J Musante 46981195e687SMark J Musante /* clear the log and flush everything up to now */ 46991195e687SMark J Musante activate_slog = spa_passivate_log(spa); 47001195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 47011195e687SMark J Musante error = spa_offline_log(spa); 47021195e687SMark J Musante txg = spa_vdev_config_enter(spa); 47031195e687SMark J Musante 47041195e687SMark J Musante if (activate_slog) 47051195e687SMark J Musante spa_activate_log(spa); 47061195e687SMark J Musante 47071195e687SMark J Musante if (error != 0) 47081195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 47091195e687SMark J Musante 47101195e687SMark J Musante /* check new spa name before going any further */ 47111195e687SMark J Musante if (spa_lookup(newname) != NULL) 47121195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 47131195e687SMark J Musante 47141195e687SMark J Musante /* 47151195e687SMark J Musante * scan through all the children to ensure they're all mirrors 47161195e687SMark J Musante */ 47171195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 47181195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 47191195e687SMark J Musante &children) != 0) 47201195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47211195e687SMark J Musante 47221195e687SMark J Musante /* first, check to ensure we've got the right child count */ 47231195e687SMark J Musante rvd = spa->spa_root_vdev; 47241195e687SMark J Musante lastlog = 0; 47251195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 47261195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 47271195e687SMark J Musante 47281195e687SMark J Musante /* don't count the holes & logs as children */ 47291195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 47301195e687SMark J Musante if (lastlog == 0) 47311195e687SMark J Musante lastlog = c; 47321195e687SMark J Musante continue; 47331195e687SMark J Musante } 47341195e687SMark J Musante 47351195e687SMark J Musante lastlog = 0; 47361195e687SMark J Musante } 47371195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 47381195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47391195e687SMark J Musante 47401195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 47411195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 47421195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 47431195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47441195e687SMark J Musante 47451195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 47461195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 47471195e687SMark J Musante 47481195e687SMark J Musante /* then, loop over each vdev and validate it */ 47491195e687SMark J Musante for (c = 0; c < children; c++) { 47501195e687SMark J Musante uint64_t is_hole = 0; 47511195e687SMark J Musante 47521195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 47531195e687SMark J Musante &is_hole); 47541195e687SMark J Musante 47551195e687SMark J Musante if (is_hole != 0) { 47561195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 47571195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 47581195e687SMark J Musante continue; 47591195e687SMark J Musante } else { 47601195e687SMark J Musante error = EINVAL; 47611195e687SMark J Musante break; 47621195e687SMark J Musante } 47631195e687SMark J Musante } 47641195e687SMark J Musante 47651195e687SMark J Musante /* which disk is going to be split? */ 47661195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 47671195e687SMark J Musante &glist[c]) != 0) { 47681195e687SMark J Musante error = EINVAL; 47691195e687SMark J Musante break; 47701195e687SMark J Musante } 47711195e687SMark J Musante 47721195e687SMark J Musante /* look it up in the spa */ 47731195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 47741195e687SMark J Musante if (vml[c] == NULL) { 47751195e687SMark J Musante error = ENODEV; 47761195e687SMark J Musante break; 47771195e687SMark J Musante } 47781195e687SMark J Musante 47791195e687SMark J Musante /* make sure there's nothing stopping the split */ 47801195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 47811195e687SMark J Musante vml[c]->vdev_islog || 47821195e687SMark J Musante vml[c]->vdev_ishole || 47831195e687SMark J Musante vml[c]->vdev_isspare || 47841195e687SMark J Musante vml[c]->vdev_isl2cache || 47851195e687SMark J Musante !vdev_writeable(vml[c]) || 4786d41c4376SMark J Musante vml[c]->vdev_children != 0 || 47871195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 47881195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 47891195e687SMark J Musante error = EINVAL; 47901195e687SMark J Musante break; 47911195e687SMark J Musante } 47921195e687SMark J Musante 47931195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 47941195e687SMark J Musante error = EBUSY; 47951195e687SMark J Musante break; 47961195e687SMark J Musante } 47971195e687SMark J Musante 47981195e687SMark J Musante /* we need certain info from the top level */ 47991195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 48001195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 48011195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 48021195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 48031195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 48041195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 48051195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 48061195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 48071195e687SMark J Musante } 48081195e687SMark J Musante 48091195e687SMark J Musante if (error != 0) { 48101195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 48111195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 48121195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 48131195e687SMark J Musante } 48141195e687SMark J Musante 48151195e687SMark J Musante /* stop writers from using the disks */ 48161195e687SMark J Musante for (c = 0; c < children; c++) { 48171195e687SMark J Musante if (vml[c] != NULL) 48181195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 48191195e687SMark J Musante } 48201195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 48211195e687SMark J Musante 48221195e687SMark J Musante /* 48231195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 48241195e687SMark J Musante * will disappear once the config is regenerated. 48251195e687SMark J Musante */ 48261195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 48271195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 48281195e687SMark J Musante glist, children) == 0); 48291195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 48301195e687SMark J Musante 483198295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 48321195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 48331195e687SMark J Musante nvl) == 0); 483498295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 48351195e687SMark J Musante spa->spa_config_splitting = nvl; 48361195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 48371195e687SMark J Musante 48381195e687SMark J Musante /* configure and create the new pool */ 48391195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 48401195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 48411195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 48421195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 48431195e687SMark J Musante spa_version(spa)) == 0); 48441195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 48451195e687SMark J Musante spa->spa_config_txg) == 0); 48461195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 48471195e687SMark J Musante spa_generate_guid(NULL)) == 0); 48481195e687SMark J Musante (void) nvlist_lookup_string(props, 48491195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 48501195e687SMark J Musante 4851d41c4376SMark J Musante /* add the new pool to the namespace */ 48521195e687SMark J Musante newspa = spa_add(newname, config, altroot); 48531195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 48541195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 48551195e687SMark J Musante 48561195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 48571195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 48581195e687SMark J Musante 48591195e687SMark J Musante if (zio_injection_enabled) 48601195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 48611195e687SMark J Musante 48621195e687SMark J Musante spa_activate(newspa, spa_mode_global); 48631195e687SMark J Musante spa_async_suspend(newspa); 48641195e687SMark J Musante 48651195e687SMark J Musante /* create the new pool from the disks of the original pool */ 48661195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 48671195e687SMark J Musante if (error) 48681195e687SMark J Musante goto out; 48691195e687SMark J Musante 48701195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 48711195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 48721195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 48731195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 48741195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 48751195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 48761195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 48771195e687SMark J Musante B_TRUE)); 48781195e687SMark J Musante } 48791195e687SMark J Musante 48801195e687SMark J Musante /* set the props */ 48811195e687SMark J Musante if (props != NULL) { 48821195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 48831195e687SMark J Musante error = spa_prop_set(newspa, props); 48841195e687SMark J Musante if (error) 48851195e687SMark J Musante goto out; 48861195e687SMark J Musante } 48871195e687SMark J Musante 48881195e687SMark J Musante /* flush everything */ 48891195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 48901195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 48911195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 48921195e687SMark J Musante 48931195e687SMark J Musante if (zio_injection_enabled) 48941195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 48951195e687SMark J Musante 48961195e687SMark J Musante spa_async_resume(newspa); 48971195e687SMark J Musante 48981195e687SMark J Musante /* finally, update the original pool's config */ 48991195e687SMark J Musante txg = spa_vdev_config_enter(spa); 49001195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 49011195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 49021195e687SMark J Musante if (error != 0) 49031195e687SMark J Musante dmu_tx_abort(tx); 49041195e687SMark J Musante for (c = 0; c < children; c++) { 49051195e687SMark J Musante if (vml[c] != NULL) { 49061195e687SMark J Musante vdev_split(vml[c]); 49071195e687SMark J Musante if (error == 0) 49084445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 49094445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 49101195e687SMark J Musante vdev_free(vml[c]); 49111195e687SMark J Musante } 49121195e687SMark J Musante } 49131195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 49141195e687SMark J Musante spa->spa_config_splitting = NULL; 49151195e687SMark J Musante nvlist_free(nvl); 49161195e687SMark J Musante if (error == 0) 49171195e687SMark J Musante dmu_tx_commit(tx); 49181195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 49191195e687SMark J Musante 49201195e687SMark J Musante if (zio_injection_enabled) 49211195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 49221195e687SMark J Musante 49231195e687SMark J Musante /* split is complete; log a history record */ 49244445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 49254445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 49261195e687SMark J Musante 49271195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 49281195e687SMark J Musante 49291195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 49301195e687SMark J Musante if (exp) 49311195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 49321195e687SMark J Musante B_FALSE, B_FALSE); 49331195e687SMark J Musante 49341195e687SMark J Musante return (error); 49351195e687SMark J Musante 49361195e687SMark J Musante out: 49371195e687SMark J Musante spa_unload(newspa); 49381195e687SMark J Musante spa_deactivate(newspa); 49391195e687SMark J Musante spa_remove(newspa); 49401195e687SMark J Musante 49411195e687SMark J Musante txg = spa_vdev_config_enter(spa); 494298295d61SMark J Musante 494398295d61SMark J Musante /* re-online all offlined disks */ 494498295d61SMark J Musante for (c = 0; c < children; c++) { 494598295d61SMark J Musante if (vml[c] != NULL) 494698295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 494798295d61SMark J Musante } 494898295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 494998295d61SMark J Musante 49501195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 49511195e687SMark J Musante spa->spa_config_splitting = NULL; 4952d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 49531195e687SMark J Musante 49541195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 49551195e687SMark J Musante return (error); 49561195e687SMark J Musante } 49571195e687SMark J Musante 4958e14bb325SJeff Bonwick static nvlist_t * 4959e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 496099653d4eSeschrock { 4961e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 4962e14bb325SJeff Bonwick uint64_t guid; 496399653d4eSeschrock 4964e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 4965e14bb325SJeff Bonwick &guid) == 0); 496699653d4eSeschrock 4967e14bb325SJeff Bonwick if (guid == target_guid) 4968e14bb325SJeff Bonwick return (nvpp[i]); 496999653d4eSeschrock } 497099653d4eSeschrock 4971e14bb325SJeff Bonwick return (NULL); 4972fa94a07fSbrendan } 4973fa94a07fSbrendan 4974e14bb325SJeff Bonwick static void 4975e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 4976e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 4977fa94a07fSbrendan { 4978e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 4979fa94a07fSbrendan 4980e14bb325SJeff Bonwick if (count > 1) 4981e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 4982fa94a07fSbrendan 4983e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 4984e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 4985e14bb325SJeff Bonwick continue; 4986e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 4987fa94a07fSbrendan } 4988fa94a07fSbrendan 4989e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 4990e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 4991fa94a07fSbrendan 4992e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 4993e14bb325SJeff Bonwick nvlist_free(newdev[i]); 4994fa94a07fSbrendan 4995e14bb325SJeff Bonwick if (count > 1) 4996e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 4997fa94a07fSbrendan } 4998fa94a07fSbrendan 499988ecc943SGeorge Wilson /* 500088ecc943SGeorge Wilson * Evacuate the device. 500188ecc943SGeorge Wilson */ 50023f9d6ad7SLin Ling static int 500388ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 500488ecc943SGeorge Wilson { 500588ecc943SGeorge Wilson uint64_t txg; 50063f9d6ad7SLin Ling int error = 0; 500788ecc943SGeorge Wilson 500888ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 500988ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5010b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 501188ecc943SGeorge Wilson 501288ecc943SGeorge Wilson /* 501388ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 501488ecc943SGeorge Wilson * since we need to do I/O but we do keep the 501588ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 501688ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 501788ecc943SGeorge Wilson */ 501888ecc943SGeorge Wilson if (vd->vdev_islog) { 50193f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 50203f9d6ad7SLin Ling error = spa_offline_log(spa); 5021a1521560SJeff Bonwick } else { 50223f9d6ad7SLin Ling error = ENOTSUP; 502388ecc943SGeorge Wilson } 502488ecc943SGeorge Wilson 5025a1521560SJeff Bonwick if (error) 5026a1521560SJeff Bonwick return (error); 5027a1521560SJeff Bonwick 502888ecc943SGeorge Wilson /* 5029a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5030a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 503188ecc943SGeorge Wilson */ 5032fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 503388ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 503488ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 503588ecc943SGeorge Wilson vdev_dirty(vd, 0, NULL, txg); 503688ecc943SGeorge Wilson vdev_config_dirty(vd); 503788ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 503888ecc943SGeorge Wilson 503988ecc943SGeorge Wilson return (0); 504088ecc943SGeorge Wilson } 504188ecc943SGeorge Wilson 504288ecc943SGeorge Wilson /* 504388ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 504488ecc943SGeorge Wilson */ 50453f9d6ad7SLin Ling static void 5046a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 504788ecc943SGeorge Wilson { 504888ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 504988ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 505088ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 505188ecc943SGeorge Wilson 505288ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 505388ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5054b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 505588ecc943SGeorge Wilson 50563f9d6ad7SLin Ling /* 50573f9d6ad7SLin Ling * Only remove any devices which are empty. 50583f9d6ad7SLin Ling */ 50593f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 50603f9d6ad7SLin Ling return; 50613f9d6ad7SLin Ling 506288ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5063b24ab676SJeff Bonwick 5064b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5065b24ab676SJeff Bonwick vdev_state_clean(vd); 5066b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5067b24ab676SJeff Bonwick vdev_config_clean(vd); 5068b24ab676SJeff Bonwick 506988ecc943SGeorge Wilson vdev_free(vd); 507088ecc943SGeorge Wilson 507188ecc943SGeorge Wilson if (last_vdev) { 507288ecc943SGeorge Wilson vdev_compact_children(rvd); 507388ecc943SGeorge Wilson } else { 507488ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 507588ecc943SGeorge Wilson vdev_add_child(rvd, vd); 507688ecc943SGeorge Wilson } 5077fcbfa62bSLin Ling vdev_config_dirty(rvd); 5078fcbfa62bSLin Ling 5079fcbfa62bSLin Ling /* 5080fcbfa62bSLin Ling * Reassess the health of our root vdev. 5081fcbfa62bSLin Ling */ 5082fcbfa62bSLin Ling vdev_reopen(rvd); 508388ecc943SGeorge Wilson } 508488ecc943SGeorge Wilson 50853f9d6ad7SLin Ling /* 50863f9d6ad7SLin Ling * Remove a device from the pool - 50873f9d6ad7SLin Ling * 50883f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 50893f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 50903f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 50913f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 50923f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 50933f9d6ad7SLin Ling */ 50943f9d6ad7SLin Ling 5095fa94a07fSbrendan /* 5096fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 509788ecc943SGeorge Wilson * spares, slogs, and level 2 ARC devices. 5098fa94a07fSbrendan */ 5099fa94a07fSbrendan int 5100fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5101fa94a07fSbrendan { 5102fa94a07fSbrendan vdev_t *vd; 5103a1521560SJeff Bonwick metaslab_group_t *mg; 5104e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 51058ad4d6ddSJeff Bonwick uint64_t txg = 0; 510688ecc943SGeorge Wilson uint_t nspares, nl2cache; 5107fa94a07fSbrendan int error = 0; 51088ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5109fa94a07fSbrendan 5110f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5111f9af39baSGeorge Wilson 51128ad4d6ddSJeff Bonwick if (!locked) 51138ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5114fa94a07fSbrendan 5115c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5116fa94a07fSbrendan 5117fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5118fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5119e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5120e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5121e14bb325SJeff Bonwick /* 5122e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5123e14bb325SJeff Bonwick * in this pool. 5124e14bb325SJeff Bonwick */ 5125e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5126e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5127e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5128e14bb325SJeff Bonwick spa_load_spares(spa); 5129e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5130e14bb325SJeff Bonwick } else { 5131e14bb325SJeff Bonwick error = EBUSY; 5132e14bb325SJeff Bonwick } 5133e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5134fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5135e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5136e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5137e14bb325SJeff Bonwick /* 5138e14bb325SJeff Bonwick * Cache devices can always be removed. 5139e14bb325SJeff Bonwick */ 5140e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5141e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5142fa94a07fSbrendan spa_load_l2cache(spa); 5143fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 514488ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 514588ecc943SGeorge Wilson ASSERT(!locked); 5146b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 514788ecc943SGeorge Wilson 514888ecc943SGeorge Wilson /* 514988ecc943SGeorge Wilson * XXX - Once we have bp-rewrite this should 515088ecc943SGeorge Wilson * become the common case. 515188ecc943SGeorge Wilson */ 515288ecc943SGeorge Wilson 5153a1521560SJeff Bonwick mg = vd->vdev_mg; 5154a1521560SJeff Bonwick 515588ecc943SGeorge Wilson /* 5156a1521560SJeff Bonwick * Stop allocating from this vdev. 515788ecc943SGeorge Wilson */ 5158a1521560SJeff Bonwick metaslab_group_passivate(mg); 515988ecc943SGeorge Wilson 5160b24ab676SJeff Bonwick /* 5161b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5162b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5163b24ab676SJeff Bonwick */ 5164b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5165b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5166b24ab676SJeff Bonwick 5167a1521560SJeff Bonwick /* 5168a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5169a1521560SJeff Bonwick */ 5170a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5171a1521560SJeff Bonwick 517288ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 517388ecc943SGeorge Wilson 5174a1521560SJeff Bonwick /* 5175a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5176a1521560SJeff Bonwick */ 5177a1521560SJeff Bonwick if (error) { 5178a1521560SJeff Bonwick metaslab_group_activate(mg); 5179a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5180a1521560SJeff Bonwick } 5181a1521560SJeff Bonwick 5182a1521560SJeff Bonwick /* 5183a1521560SJeff Bonwick * Clean up the vdev namespace. 5184a1521560SJeff Bonwick */ 5185a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 518688ecc943SGeorge Wilson 5187e14bb325SJeff Bonwick } else if (vd != NULL) { 5188e14bb325SJeff Bonwick /* 5189e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5190e14bb325SJeff Bonwick */ 5191e14bb325SJeff Bonwick error = ENOTSUP; 5192e14bb325SJeff Bonwick } else { 5193e14bb325SJeff Bonwick /* 5194e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5195e14bb325SJeff Bonwick */ 5196e14bb325SJeff Bonwick error = ENOENT; 5197fa94a07fSbrendan } 519899653d4eSeschrock 51998ad4d6ddSJeff Bonwick if (!locked) 52008ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 52018ad4d6ddSJeff Bonwick 52028ad4d6ddSJeff Bonwick return (error); 5203fa9e4066Sahrens } 5204fa9e4066Sahrens 5205fa9e4066Sahrens /* 52063d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 52073d7072f8Seschrock * current spared, so we can detach it. 5208fa9e4066Sahrens */ 5209ea8dc4b6Seschrock static vdev_t * 52103d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5211fa9e4066Sahrens { 5212ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5213fa9e4066Sahrens 5214573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 52153d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5216ea8dc4b6Seschrock if (oldvd != NULL) 5217ea8dc4b6Seschrock return (oldvd); 5218ea8dc4b6Seschrock } 5219fa9e4066Sahrens 52203d7072f8Seschrock /* 5221cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5222cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5223cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5224cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5225cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5226cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 52273d7072f8Seschrock */ 5228cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5229cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5230cb04b873SMark J Musante 5231cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5232ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5233ea8dc4b6Seschrock 52348ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5235e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 52368ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5237ea8dc4b6Seschrock return (oldvd); 5238fa9e4066Sahrens } 5239ea8dc4b6Seschrock 52403d7072f8Seschrock /* 52413d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 52423d7072f8Seschrock */ 5243cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5244cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5245cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5246cb04b873SMark J Musante 5247cb04b873SMark J Musante if (last->vdev_unspare) { 5248cb04b873SMark J Musante oldvd = first; 5249cb04b873SMark J Musante newvd = last; 5250cb04b873SMark J Musante } else if (first->vdev_unspare) { 5251cb04b873SMark J Musante oldvd = last; 5252cb04b873SMark J Musante newvd = first; 5253cb04b873SMark J Musante } else { 5254cb04b873SMark J Musante oldvd = NULL; 5255cb04b873SMark J Musante } 52563d7072f8Seschrock 5257cb04b873SMark J Musante if (oldvd != NULL && 52588ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5259e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5260cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 52613d7072f8Seschrock return (oldvd); 5262cb04b873SMark J Musante 5263cb04b873SMark J Musante /* 5264cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5265cb04b873SMark J Musante * and those spares are not required, then we want to 5266cb04b873SMark J Musante * attempt to free them up now so that they can be used 5267cb04b873SMark J Musante * by other pools. Once we're back down to a single 5268cb04b873SMark J Musante * disk+spare, we stop removing them. 5269cb04b873SMark J Musante */ 5270cb04b873SMark J Musante if (vd->vdev_children > 2) { 5271cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5272cb04b873SMark J Musante 5273cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5274cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5275cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5276cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5277cb04b873SMark J Musante return (newvd); 52783d7072f8Seschrock } 52793d7072f8Seschrock } 52803d7072f8Seschrock 5281ea8dc4b6Seschrock return (NULL); 5282fa9e4066Sahrens } 5283fa9e4066Sahrens 5284ea8dc4b6Seschrock static void 52853d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5286fa9e4066Sahrens { 52878ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 52888ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5289ea8dc4b6Seschrock 52908ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5291ea8dc4b6Seschrock 52923d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 52938ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 52948ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5295ea8dc4b6Seschrock guid = vd->vdev_guid; 52968ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 52978ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 52988ad4d6ddSJeff Bonwick sguid = 0; 529999653d4eSeschrock /* 530099653d4eSeschrock * If we have just finished replacing a hot spared device, then 530199653d4eSeschrock * we need to detach the parent's first child (the original hot 530299653d4eSeschrock * spare) as well. 530399653d4eSeschrock */ 5304cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5305cb04b873SMark J Musante ppvd->vdev_children == 2) { 530699653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 53078ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 530899653d4eSeschrock } 53098ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 53108ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5311ea8dc4b6Seschrock return; 53128ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 531399653d4eSeschrock return; 53148ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5315fa9e4066Sahrens } 5316fa9e4066Sahrens 53178ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5318fa9e4066Sahrens } 5319fa9e4066Sahrens 5320c67d9675Seschrock /* 5321b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5322c67d9675Seschrock */ 5323c67d9675Seschrock int 53246809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 53256809eb4eSEric Schrock boolean_t ispath) 5326c67d9675Seschrock { 5327c5904d13Seschrock vdev_t *vd; 5328208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5329c67d9675Seschrock 5330f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5331f9af39baSGeorge Wilson 5332b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5333c67d9675Seschrock 53346809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5335b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5336c67d9675Seschrock 53370e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5338b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 53390e34b6a7Sbonwick 53406809eb4eSEric Schrock if (ispath) { 5341208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5342208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5343208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5344208044b8SGeorge Wilson sync = B_TRUE; 5345208044b8SGeorge Wilson } 53466809eb4eSEric Schrock } else { 5347208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5348208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5349208044b8SGeorge Wilson sync = B_TRUE; 5350208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 53516809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5352208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5353208044b8SGeorge Wilson sync = B_TRUE; 5354208044b8SGeorge Wilson } 53556809eb4eSEric Schrock } 5356c67d9675Seschrock 5357208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5358c67d9675Seschrock } 5359c67d9675Seschrock 53606809eb4eSEric Schrock int 53616809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 53626809eb4eSEric Schrock { 53636809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 53646809eb4eSEric Schrock } 53656809eb4eSEric Schrock 53666809eb4eSEric Schrock int 53676809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 53686809eb4eSEric Schrock { 53696809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 53706809eb4eSEric Schrock } 53716809eb4eSEric Schrock 5372fa9e4066Sahrens /* 5373fa9e4066Sahrens * ========================================================================== 53743f9d6ad7SLin Ling * SPA Scanning 5375fa9e4066Sahrens * ========================================================================== 5376fa9e4066Sahrens */ 5377fa9e4066Sahrens 5378ea8dc4b6Seschrock int 53793f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5380fa9e4066Sahrens { 5381e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 53823f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 53833f9d6ad7SLin Ling return (EBUSY); 53843f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 53853f9d6ad7SLin Ling } 5386bb8b5132Sek 53873f9d6ad7SLin Ling int 53883f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 53893f9d6ad7SLin Ling { 53903f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 53913f9d6ad7SLin Ling 53923f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5393fa9e4066Sahrens return (ENOTSUP); 5394fa9e4066Sahrens 5395fa9e4066Sahrens /* 5396088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5397088f3894Sahrens * writeable leaf device, we have nothing to do. 5398fa9e4066Sahrens */ 53993f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5400088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5401088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5402ea8dc4b6Seschrock return (0); 5403ea8dc4b6Seschrock } 5404fa9e4066Sahrens 54053f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5406fa9e4066Sahrens } 5407fa9e4066Sahrens 5408ea8dc4b6Seschrock /* 5409ea8dc4b6Seschrock * ========================================================================== 5410ea8dc4b6Seschrock * SPA async task processing 5411ea8dc4b6Seschrock * ========================================================================== 5412ea8dc4b6Seschrock */ 5413ea8dc4b6Seschrock 5414ea8dc4b6Seschrock static void 54153d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5416fa9e4066Sahrens { 541749cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 541898d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 541998d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 542049cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 54211d713200SEric Schrock 54221d713200SEric Schrock /* 54231d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 54241d713200SEric Schrock * vdev_clear() as that will cause us to throw away 54251d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 54261d713200SEric Schrock * device, all of which is a waste. 54271d713200SEric Schrock */ 54281d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 54291d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 54301d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 54311d713200SEric Schrock 5432e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5433ea8dc4b6Seschrock } 543449cf58c0SBrendan Gregg - Sun Microsystems 5435e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 543649cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5437ea8dc4b6Seschrock } 5438fa9e4066Sahrens 5439e14bb325SJeff Bonwick static void 5440e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5441e14bb325SJeff Bonwick { 5442e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 544398d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5444e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5445e14bb325SJeff Bonwick } 5446e14bb325SJeff Bonwick 5447e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5448e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5449e14bb325SJeff Bonwick } 5450e14bb325SJeff Bonwick 5451573ca77eSGeorge Wilson static void 5452573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5453573ca77eSGeorge Wilson { 5454573ca77eSGeorge Wilson sysevent_id_t eid; 5455573ca77eSGeorge Wilson nvlist_t *attr; 5456573ca77eSGeorge Wilson char *physpath; 5457573ca77eSGeorge Wilson 5458573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5459573ca77eSGeorge Wilson return; 5460573ca77eSGeorge Wilson 5461573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5462573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5463573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5464573ca77eSGeorge Wilson } 5465573ca77eSGeorge Wilson 5466573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5467573ca77eSGeorge Wilson return; 5468573ca77eSGeorge Wilson 5469573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5470573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5471573ca77eSGeorge Wilson 5472573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5473573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5474573ca77eSGeorge Wilson 5475573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5476573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5477573ca77eSGeorge Wilson 5478573ca77eSGeorge Wilson nvlist_free(attr); 5479573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5480573ca77eSGeorge Wilson } 5481573ca77eSGeorge Wilson 5482ea8dc4b6Seschrock static void 5483ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5484ea8dc4b6Seschrock { 5485e14bb325SJeff Bonwick int tasks; 5486ea8dc4b6Seschrock 5487ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5488ea8dc4b6Seschrock 5489ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5490ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5491ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5492ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5493ea8dc4b6Seschrock 54940373e76bSbonwick /* 54950373e76bSbonwick * See if the config needs to be updated. 54960373e76bSbonwick */ 54970373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5498b24ab676SJeff Bonwick uint64_t old_space, new_space; 5499573ca77eSGeorge Wilson 55000373e76bSbonwick mutex_enter(&spa_namespace_lock); 5501b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 55020373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5503b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 55040373e76bSbonwick mutex_exit(&spa_namespace_lock); 5505573ca77eSGeorge Wilson 5506573ca77eSGeorge Wilson /* 5507573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5508573ca77eSGeorge Wilson * then log an internal history event. 5509573ca77eSGeorge Wilson */ 5510b24ab676SJeff Bonwick if (new_space != old_space) { 55114445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5512c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5513b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5514573ca77eSGeorge Wilson } 55150373e76bSbonwick } 55160373e76bSbonwick 5517ea8dc4b6Seschrock /* 55183d7072f8Seschrock * See if any devices need to be marked REMOVED. 5519ea8dc4b6Seschrock */ 5520e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 55218f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 55223d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5523e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 552449cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5525e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 552649cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5527e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5528e14bb325SJeff Bonwick } 5529e14bb325SJeff Bonwick 5530573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5531573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5532573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5533573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5534573ca77eSGeorge Wilson } 5535573ca77eSGeorge Wilson 5536e14bb325SJeff Bonwick /* 5537e14bb325SJeff Bonwick * See if any devices need to be probed. 5538e14bb325SJeff Bonwick */ 5539e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 55408f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5541e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5542e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 55433d7072f8Seschrock } 5544ea8dc4b6Seschrock 5545ea8dc4b6Seschrock /* 5546ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5547ea8dc4b6Seschrock */ 55483d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 55493d7072f8Seschrock spa_vdev_resilver_done(spa); 5550fa9e4066Sahrens 5551ea8dc4b6Seschrock /* 5552ea8dc4b6Seschrock * Kick off a resilver. 5553ea8dc4b6Seschrock */ 5554088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 55553f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5556ea8dc4b6Seschrock 5557ea8dc4b6Seschrock /* 5558ea8dc4b6Seschrock * Let the world know that we're done. 5559ea8dc4b6Seschrock */ 5560ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5561ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5562ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5563ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5564ea8dc4b6Seschrock thread_exit(); 5565ea8dc4b6Seschrock } 5566ea8dc4b6Seschrock 5567ea8dc4b6Seschrock void 5568ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5569ea8dc4b6Seschrock { 5570ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5571ea8dc4b6Seschrock spa->spa_async_suspended++; 5572ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5573ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5574ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5575ea8dc4b6Seschrock } 5576ea8dc4b6Seschrock 5577ea8dc4b6Seschrock void 5578ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5579ea8dc4b6Seschrock { 5580ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5581ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5582ea8dc4b6Seschrock spa->spa_async_suspended--; 5583ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5584ea8dc4b6Seschrock } 5585ea8dc4b6Seschrock 5586ea8dc4b6Seschrock static void 5587ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5588ea8dc4b6Seschrock { 5589ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5590ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 55910373e76bSbonwick spa->spa_async_thread == NULL && 55920373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 5593ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5594ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5595ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5596ea8dc4b6Seschrock } 5597ea8dc4b6Seschrock 5598ea8dc4b6Seschrock void 5599ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5600ea8dc4b6Seschrock { 56013f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5602ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5603ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5604ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5605fa9e4066Sahrens } 5606fa9e4066Sahrens 5607fa9e4066Sahrens /* 5608fa9e4066Sahrens * ========================================================================== 5609fa9e4066Sahrens * SPA syncing routines 5610fa9e4066Sahrens * ========================================================================== 5611fa9e4066Sahrens */ 5612fa9e4066Sahrens 5613cde58dbcSMatthew Ahrens static int 5614cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5615cde58dbcSMatthew Ahrens { 5616cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5617cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5618cde58dbcSMatthew Ahrens return (0); 5619b24ab676SJeff Bonwick } 5620b24ab676SJeff Bonwick 5621cde58dbcSMatthew Ahrens static int 5622cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5623b24ab676SJeff Bonwick { 5624b24ab676SJeff Bonwick zio_t *zio = arg; 5625b24ab676SJeff Bonwick 5626b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5627b24ab676SJeff Bonwick zio->io_flags)); 5628cde58dbcSMatthew Ahrens return (0); 5629fa9e4066Sahrens } 5630fa9e4066Sahrens 5631fa9e4066Sahrens static void 563299653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5633fa9e4066Sahrens { 5634fa9e4066Sahrens char *packed = NULL; 5635f7991ba4STim Haley size_t bufsize; 5636fa9e4066Sahrens size_t nvsize = 0; 5637fa9e4066Sahrens dmu_buf_t *db; 5638fa9e4066Sahrens 563999653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5640fa9e4066Sahrens 5641f7991ba4STim Haley /* 5642f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 5643f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 5644f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5645f7991ba4STim Haley */ 5646ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 5647f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5648fa9e4066Sahrens 564999653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5650ea8dc4b6Seschrock KM_SLEEP) == 0); 5651f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5652fa9e4066Sahrens 5653f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5654fa9e4066Sahrens 5655f7991ba4STim Haley kmem_free(packed, bufsize); 5656fa9e4066Sahrens 565799653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5658fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5659fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5660ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5661fa9e4066Sahrens } 5662fa9e4066Sahrens 566399653d4eSeschrock static void 5664fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5665fa94a07fSbrendan const char *config, const char *entry) 566699653d4eSeschrock { 566799653d4eSeschrock nvlist_t *nvroot; 5668fa94a07fSbrendan nvlist_t **list; 566999653d4eSeschrock int i; 567099653d4eSeschrock 5671fa94a07fSbrendan if (!sav->sav_sync) 567299653d4eSeschrock return; 567399653d4eSeschrock 567499653d4eSeschrock /* 5675fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5676fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 56773d7072f8Seschrock * valid and the vdevs are labeled appropriately. 567899653d4eSeschrock */ 5679fa94a07fSbrendan if (sav->sav_object == 0) { 5680fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5681fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5682fa94a07fSbrendan sizeof (uint64_t), tx); 568399653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5684fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5685fa94a07fSbrendan &sav->sav_object, tx) == 0); 568699653d4eSeschrock } 568799653d4eSeschrock 568899653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5689fa94a07fSbrendan if (sav->sav_count == 0) { 5690fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 569199653d4eSeschrock } else { 5692fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5693fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5694fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 56953f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5696fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5697fa94a07fSbrendan sav->sav_count) == 0); 5698fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5699fa94a07fSbrendan nvlist_free(list[i]); 5700fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 570199653d4eSeschrock } 570299653d4eSeschrock 5703fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 570406eeb2adSek nvlist_free(nvroot); 570599653d4eSeschrock 5706fa94a07fSbrendan sav->sav_sync = B_FALSE; 570799653d4eSeschrock } 570899653d4eSeschrock 570999653d4eSeschrock static void 571099653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 571199653d4eSeschrock { 571299653d4eSeschrock nvlist_t *config; 571399653d4eSeschrock 5714e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 571599653d4eSeschrock return; 571699653d4eSeschrock 5717e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5718e14bb325SJeff Bonwick 5719e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5720e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5721e14bb325SJeff Bonwick 572225345e46SGeorge Wilson /* 572325345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 572425345e46SGeorge Wilson * the config object gets updated with the correct version. 572525345e46SGeorge Wilson */ 572625345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 572725345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 572825345e46SGeorge Wilson spa->spa_uberblock.ub_version); 572925345e46SGeorge Wilson 5730e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 573199653d4eSeschrock 573299653d4eSeschrock if (spa->spa_config_syncing) 573399653d4eSeschrock nvlist_free(spa->spa_config_syncing); 573499653d4eSeschrock spa->spa_config_syncing = config; 573599653d4eSeschrock 573699653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 573799653d4eSeschrock } 573899653d4eSeschrock 5739ad135b5dSChristopher Siden static void 5740ad135b5dSChristopher Siden spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx) 5741ad135b5dSChristopher Siden { 5742ad135b5dSChristopher Siden spa_t *spa = arg1; 5743ad135b5dSChristopher Siden uint64_t version = *(uint64_t *)arg2; 5744ad135b5dSChristopher Siden 5745ad135b5dSChristopher Siden /* 5746ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 5747ad135b5dSChristopher Siden */ 5748ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 5749ad135b5dSChristopher Siden 5750ad135b5dSChristopher Siden ASSERT(version <= SPA_VERSION); 5751ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 5752ad135b5dSChristopher Siden 5753ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 5754ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 57554445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 5756ad135b5dSChristopher Siden } 5757ad135b5dSChristopher Siden 5758990b4856Slling /* 5759990b4856Slling * Set zpool properties. 5760990b4856Slling */ 5761b1b8ab34Slling static void 57623f9d6ad7SLin Ling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 5763b1b8ab34Slling { 5764b1b8ab34Slling spa_t *spa = arg1; 5765b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5766990b4856Slling nvlist_t *nvp = arg2; 5767ad135b5dSChristopher Siden nvpair_t *elem = NULL; 5768b1b8ab34Slling 5769e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5770e14bb325SJeff Bonwick 5771990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5772ad135b5dSChristopher Siden uint64_t intval; 5773ad135b5dSChristopher Siden char *strval, *fname; 5774ad135b5dSChristopher Siden zpool_prop_t prop; 5775ad135b5dSChristopher Siden const char *propname; 5776ad135b5dSChristopher Siden zprop_type_t proptype; 5777ad135b5dSChristopher Siden zfeature_info_t *feature; 5778ad135b5dSChristopher Siden 5779990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 5780ad135b5dSChristopher Siden case ZPROP_INVAL: 5781ad135b5dSChristopher Siden /* 5782ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 5783ad135b5dSChristopher Siden */ 5784ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 5785ad135b5dSChristopher Siden 5786ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 5787b420f3adSRichard Lowe VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature)); 5788ad135b5dSChristopher Siden 5789ad135b5dSChristopher Siden spa_feature_enable(spa, feature, tx); 57904445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 57914445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 5792ad135b5dSChristopher Siden break; 5793ad135b5dSChristopher Siden 5794990b4856Slling case ZPOOL_PROP_VERSION: 5795ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5796990b4856Slling /* 5797ad135b5dSChristopher Siden * The version is synced seperatly before other 5798ad135b5dSChristopher Siden * properties and should be correct by now. 5799990b4856Slling */ 5800ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 5801ecd6cf80Smarks break; 5802990b4856Slling 5803990b4856Slling case ZPOOL_PROP_ALTROOT: 5804990b4856Slling /* 5805990b4856Slling * 'altroot' is a non-persistent property. It should 5806990b4856Slling * have been set temporarily at creation or import time. 5807990b4856Slling */ 5808990b4856Slling ASSERT(spa->spa_root != NULL); 5809b1b8ab34Slling break; 58103d7072f8Seschrock 5811f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 58122f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5813990b4856Slling /* 5814f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 5815f9af39baSGeorge Wilson * properties. 5816990b4856Slling */ 58173d7072f8Seschrock break; 58188704186eSDan McDonald case ZPOOL_PROP_COMMENT: 58198704186eSDan McDonald VERIFY(nvpair_value_string(elem, &strval) == 0); 58208704186eSDan McDonald if (spa->spa_comment != NULL) 58218704186eSDan McDonald spa_strfree(spa->spa_comment); 58228704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 58238704186eSDan McDonald /* 58248704186eSDan McDonald * We need to dirty the configuration on all the vdevs 58258704186eSDan McDonald * so that their labels get updated. It's unnecessary 58268704186eSDan McDonald * to do this for pool creation since the vdev's 58278704186eSDan McDonald * configuratoin has already been dirtied. 58288704186eSDan McDonald */ 58298704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 58308704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 58314445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 58324445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 58338704186eSDan McDonald break; 5834990b4856Slling default: 5835990b4856Slling /* 5836990b4856Slling * Set pool property values in the poolprops mos object. 5837990b4856Slling */ 5838990b4856Slling if (spa->spa_pool_props_object == 0) { 5839ad135b5dSChristopher Siden spa->spa_pool_props_object = 5840ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 5841990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 5842ad135b5dSChristopher Siden tx); 5843990b4856Slling } 5844990b4856Slling 5845990b4856Slling /* normalize the property name */ 5846990b4856Slling propname = zpool_prop_to_name(prop); 5847990b4856Slling proptype = zpool_prop_get_type(prop); 5848990b4856Slling 5849990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 5850990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 5851990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 5852990b4856Slling VERIFY(zap_update(mos, 5853990b4856Slling spa->spa_pool_props_object, propname, 5854990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 58554445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 58564445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 5857990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 5858990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5859990b4856Slling 5860990b4856Slling if (proptype == PROP_TYPE_INDEX) { 5861990b4856Slling const char *unused; 5862990b4856Slling VERIFY(zpool_prop_index_to_string( 5863990b4856Slling prop, intval, &unused) == 0); 5864990b4856Slling } 5865990b4856Slling VERIFY(zap_update(mos, 5866990b4856Slling spa->spa_pool_props_object, propname, 5867990b4856Slling 8, 1, &intval, tx) == 0); 58684445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 58694445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 5870990b4856Slling } else { 5871990b4856Slling ASSERT(0); /* not allowed */ 5872990b4856Slling } 5873990b4856Slling 58740a4e9518Sgw switch (prop) { 58750a4e9518Sgw case ZPOOL_PROP_DELEGATION: 5876990b4856Slling spa->spa_delegation = intval; 58770a4e9518Sgw break; 58780a4e9518Sgw case ZPOOL_PROP_BOOTFS: 5879990b4856Slling spa->spa_bootfs = intval; 58800a4e9518Sgw break; 58810a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 58820a4e9518Sgw spa->spa_failmode = intval; 58830a4e9518Sgw break; 5884573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 5885573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 5886b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 5887b98131cfSEric Taylor spa_async_request(spa, 5888b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 5889573ca77eSGeorge Wilson break; 5890b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 5891b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 5892b24ab676SJeff Bonwick break; 58930a4e9518Sgw default: 58940a4e9518Sgw break; 58950a4e9518Sgw } 5896990b4856Slling } 5897990b4856Slling 5898b1b8ab34Slling } 5899e14bb325SJeff Bonwick 5900e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 5901b1b8ab34Slling } 5902b1b8ab34Slling 5903cde58dbcSMatthew Ahrens /* 5904cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 5905cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 5906cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 5907cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 5908cde58dbcSMatthew Ahrens * tasks. 5909cde58dbcSMatthew Ahrens */ 5910cde58dbcSMatthew Ahrens static void 5911cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 5912cde58dbcSMatthew Ahrens { 5913cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5914cde58dbcSMatthew Ahrens 5915cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 5916cde58dbcSMatthew Ahrens 5917cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 5918cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 5919cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 5920cde58dbcSMatthew Ahrens 5921cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 5922cde58dbcSMatthew Ahrens spa->spa_minref += 3; 5923cde58dbcSMatthew Ahrens } 5924cde58dbcSMatthew Ahrens 5925cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 5926cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 5927cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 5928cde58dbcSMatthew Ahrens } 5929cde58dbcSMatthew Ahrens 5930cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 5931cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 5932cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 5933cde58dbcSMatthew Ahrens 5934cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 5935cde58dbcSMatthew Ahrens spa->spa_minref += 3; 5936cde58dbcSMatthew Ahrens } 5937ad135b5dSChristopher Siden 5938ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 5939ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 5940ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 5941ad135b5dSChristopher Siden } 5942cde58dbcSMatthew Ahrens } 5943cde58dbcSMatthew Ahrens 5944fa9e4066Sahrens /* 5945fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 5946fa9e4066Sahrens * part of the process, so we iterate until it converges. 5947fa9e4066Sahrens */ 5948fa9e4066Sahrens void 5949fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 5950fa9e4066Sahrens { 5951fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5952fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 5953cde58dbcSMatthew Ahrens bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 5954b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 59550373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 5956fa9e4066Sahrens vdev_t *vd; 5957fa9e4066Sahrens dmu_tx_t *tx; 5958e14bb325SJeff Bonwick int error; 5959fa9e4066Sahrens 5960f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 5961f9af39baSGeorge Wilson 5962fa9e4066Sahrens /* 5963fa9e4066Sahrens * Lock out configuration changes. 5964fa9e4066Sahrens */ 5965e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5966fa9e4066Sahrens 5967fa9e4066Sahrens spa->spa_syncing_txg = txg; 5968fa9e4066Sahrens spa->spa_sync_pass = 0; 5969fa9e4066Sahrens 5970e14bb325SJeff Bonwick /* 5971e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 5972e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 5973e14bb325SJeff Bonwick */ 5974e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 59758ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 59768ad4d6ddSJeff Bonwick /* 59778ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 59788ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 59798ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 59808ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 59818ad4d6ddSJeff Bonwick * into the root vdev tree. 59828ad4d6ddSJeff Bonwick */ 59838ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 59848ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 59858ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 59868ad4d6ddSJeff Bonwick vdev_state_clean(vd); 59878ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 59888ad4d6ddSJeff Bonwick } 59898ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 59908ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 5991e14bb325SJeff Bonwick } 5992e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 5993e14bb325SJeff Bonwick 599499653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 599599653d4eSeschrock 5996283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 5997283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 5998283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 5999283b8460SGeorge.Wilson 600099653d4eSeschrock /* 6001e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 600299653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 600399653d4eSeschrock */ 6004e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6005e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 600699653d4eSeschrock int i; 600799653d4eSeschrock 600899653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 600999653d4eSeschrock vd = rvd->vdev_child[i]; 601099653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 601199653d4eSeschrock break; 601299653d4eSeschrock } 601399653d4eSeschrock if (i == rvd->vdev_children) { 601499653d4eSeschrock spa->spa_deflate = TRUE; 601599653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 601699653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 601799653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 601899653d4eSeschrock } 601999653d4eSeschrock } 602099653d4eSeschrock 6021fa9e4066Sahrens /* 60223f9d6ad7SLin Ling * If anything has changed in this txg, or if someone is waiting 60233f9d6ad7SLin Ling * for this txg to sync (eg, spa_vdev_remove()), push the 60243f9d6ad7SLin Ling * deferred frees from the previous txg. If not, leave them 60253f9d6ad7SLin Ling * alone so that we don't generate work on an otherwise idle 60263f9d6ad7SLin Ling * system. 6027fa9e4066Sahrens */ 6028fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 60291615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 60303f9d6ad7SLin Ling !txg_list_empty(&dp->dp_sync_tasks, txg) || 6031cde58dbcSMatthew Ahrens ((dsl_scan_active(dp->dp_scan) || 6032cde58dbcSMatthew Ahrens txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 6033cde58dbcSMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 6034cde58dbcSMatthew Ahrens VERIFY3U(bpobj_iterate(defer_bpo, 6035cde58dbcSMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 6036fb09f5aaSMadhav Suresh VERIFY0(zio_wait(zio)); 6037cde58dbcSMatthew Ahrens } 6038fa9e4066Sahrens 6039fa9e4066Sahrens /* 6040fa9e4066Sahrens * Iterate to convergence. 6041fa9e4066Sahrens */ 6042fa9e4066Sahrens do { 6043b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6044fa9e4066Sahrens 6045fa9e4066Sahrens spa_sync_config_object(spa, tx); 6046fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6047fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6048fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6049fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6050ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6051fa9e4066Sahrens dsl_pool_sync(dp, txg); 6052fa9e4066Sahrens 605301f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 6054b24ab676SJeff Bonwick zio_t *zio = zio_root(spa, NULL, NULL, 0); 6055cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, spa_free_sync_cb, 6056cde58dbcSMatthew Ahrens zio, tx); 6057b24ab676SJeff Bonwick VERIFY(zio_wait(zio) == 0); 6058b24ab676SJeff Bonwick } else { 6059cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 6060cde58dbcSMatthew Ahrens defer_bpo, tx); 6061fa9e4066Sahrens } 6062fa9e4066Sahrens 6063b24ab676SJeff Bonwick ddt_sync(spa, txg); 60643f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6065afee20e4SGeorge Wilson 6066b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6067b24ab676SJeff Bonwick vdev_sync(vd, txg); 6068b24ab676SJeff Bonwick 6069cde58dbcSMatthew Ahrens if (pass == 1) 6070cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6071fa9e4066Sahrens 6072cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6073fa9e4066Sahrens 6074fa9e4066Sahrens /* 6075fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6076fa9e4066Sahrens * to commit the transaction group. 60770373e76bSbonwick * 607817f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 607917f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6080e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6081e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 60820373e76bSbonwick */ 6083e14bb325SJeff Bonwick for (;;) { 6084e14bb325SJeff Bonwick /* 6085e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6086e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6087e14bb325SJeff Bonwick */ 6088e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6089e14bb325SJeff Bonwick 6090e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6091e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6092e14bb325SJeff Bonwick int svdcount = 0; 6093e14bb325SJeff Bonwick int children = rvd->vdev_children; 6094e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6095e14bb325SJeff Bonwick 6096573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6097e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6098e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6099e14bb325SJeff Bonwick continue; 6100e14bb325SJeff Bonwick svd[svdcount++] = vd; 6101e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6102e14bb325SJeff Bonwick break; 6103e14bb325SJeff Bonwick } 61048956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 61058956713aSEric Schrock if (error != 0) 61068956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 61078956713aSEric Schrock B_TRUE); 6108e14bb325SJeff Bonwick } else { 6109e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 61108956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 61118956713aSEric Schrock if (error != 0) 61128956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 61138956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 61140373e76bSbonwick } 6115e14bb325SJeff Bonwick 6116dfbb9432SGeorge Wilson if (error == 0) 6117dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6118dfbb9432SGeorge Wilson 6119e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6120e14bb325SJeff Bonwick 6121e14bb325SJeff Bonwick if (error == 0) 6122e14bb325SJeff Bonwick break; 6123e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6124e14bb325SJeff Bonwick zio_resume_wait(spa); 61250373e76bSbonwick } 612699653d4eSeschrock dmu_tx_commit(tx); 612799653d4eSeschrock 6128283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6129283b8460SGeorge.Wilson 61300373e76bSbonwick /* 61310373e76bSbonwick * Clear the dirty config list. 6132fa9e4066Sahrens */ 6133e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 61340373e76bSbonwick vdev_config_clean(vd); 61350373e76bSbonwick 61360373e76bSbonwick /* 61370373e76bSbonwick * Now that the new config has synced transactionally, 61380373e76bSbonwick * let it become visible to the config cache. 61390373e76bSbonwick */ 61400373e76bSbonwick if (spa->spa_config_syncing != NULL) { 61410373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 61420373e76bSbonwick spa->spa_config_txg = txg; 61430373e76bSbonwick spa->spa_config_syncing = NULL; 61440373e76bSbonwick } 6145fa9e4066Sahrens 6146fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 6147fa9e4066Sahrens 6148b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6149fa9e4066Sahrens 6150fa9e4066Sahrens /* 6151fa9e4066Sahrens * Update usable space statistics. 6152fa9e4066Sahrens */ 6153fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6154fa9e4066Sahrens vdev_sync_done(vd, txg); 6155fa9e4066Sahrens 6156485bbbf5SGeorge Wilson spa_update_dspace(spa); 6157485bbbf5SGeorge Wilson 6158fa9e4066Sahrens /* 6159fa9e4066Sahrens * It had better be the case that we didn't dirty anything 616099653d4eSeschrock * since vdev_config_sync(). 6161fa9e4066Sahrens */ 6162fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6163fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6164fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6165b24ab676SJeff Bonwick 6166b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6167fa9e4066Sahrens 6168e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6169ea8dc4b6Seschrock 6170468c413aSTim Haley spa_handle_ignored_writes(spa); 6171468c413aSTim Haley 6172ea8dc4b6Seschrock /* 6173ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6174ea8dc4b6Seschrock */ 6175ea8dc4b6Seschrock spa_async_dispatch(spa); 6176fa9e4066Sahrens } 6177fa9e4066Sahrens 6178fa9e4066Sahrens /* 6179fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6180fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6181fa9e4066Sahrens * sync. 6182fa9e4066Sahrens */ 6183fa9e4066Sahrens void 6184fa9e4066Sahrens spa_sync_allpools(void) 6185fa9e4066Sahrens { 6186fa9e4066Sahrens spa_t *spa = NULL; 6187fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6188fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6189f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6190f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6191fa9e4066Sahrens continue; 6192fa9e4066Sahrens spa_open_ref(spa, FTAG); 6193fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6194fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6195fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6196fa9e4066Sahrens spa_close(spa, FTAG); 6197fa9e4066Sahrens } 6198fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6199fa9e4066Sahrens } 6200fa9e4066Sahrens 6201fa9e4066Sahrens /* 6202fa9e4066Sahrens * ========================================================================== 6203fa9e4066Sahrens * Miscellaneous routines 6204fa9e4066Sahrens * ========================================================================== 6205fa9e4066Sahrens */ 6206fa9e4066Sahrens 6207fa9e4066Sahrens /* 6208fa9e4066Sahrens * Remove all pools in the system. 6209fa9e4066Sahrens */ 6210fa9e4066Sahrens void 6211fa9e4066Sahrens spa_evict_all(void) 6212fa9e4066Sahrens { 6213fa9e4066Sahrens spa_t *spa; 6214fa9e4066Sahrens 6215fa9e4066Sahrens /* 6216fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6217fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6218fa9e4066Sahrens */ 6219fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6220fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6221fa9e4066Sahrens /* 6222ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6223ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6224ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6225fa9e4066Sahrens */ 6226fa9e4066Sahrens spa_open_ref(spa, FTAG); 6227fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6228ea8dc4b6Seschrock spa_async_suspend(spa); 6229fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6230fa9e4066Sahrens spa_close(spa, FTAG); 6231fa9e4066Sahrens 6232fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6233fa9e4066Sahrens spa_unload(spa); 6234fa9e4066Sahrens spa_deactivate(spa); 6235fa9e4066Sahrens } 6236fa9e4066Sahrens spa_remove(spa); 6237fa9e4066Sahrens } 6238fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6239fa9e4066Sahrens } 6240ea8dc4b6Seschrock 6241ea8dc4b6Seschrock vdev_t * 62426809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6243ea8dc4b6Seschrock { 6244c5904d13Seschrock vdev_t *vd; 6245c5904d13Seschrock int i; 6246c5904d13Seschrock 6247c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6248c5904d13Seschrock return (vd); 6249c5904d13Seschrock 62506809eb4eSEric Schrock if (aux) { 6251c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6252c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 62536809eb4eSEric Schrock if (vd->vdev_guid == guid) 62546809eb4eSEric Schrock return (vd); 62556809eb4eSEric Schrock } 62566809eb4eSEric Schrock 62576809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 62586809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6259c5904d13Seschrock if (vd->vdev_guid == guid) 6260c5904d13Seschrock return (vd); 6261c5904d13Seschrock } 6262c5904d13Seschrock } 6263c5904d13Seschrock 6264c5904d13Seschrock return (NULL); 6265ea8dc4b6Seschrock } 6266eaca9bbdSeschrock 6267eaca9bbdSeschrock void 6268990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6269eaca9bbdSeschrock { 6270f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6271f9af39baSGeorge Wilson 6272e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6273eaca9bbdSeschrock 6274eaca9bbdSeschrock /* 6275eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6276eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6277eaca9bbdSeschrock * possible. 6278eaca9bbdSeschrock */ 6279e7437265Sahrens ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 6280990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 6281eaca9bbdSeschrock 6282990b4856Slling spa->spa_uberblock.ub_version = version; 6283eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6284eaca9bbdSeschrock 6285e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 628699653d4eSeschrock 628799653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 628899653d4eSeschrock } 628999653d4eSeschrock 629099653d4eSeschrock boolean_t 629199653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 629299653d4eSeschrock { 629399653d4eSeschrock int i; 629439c23413Seschrock uint64_t spareguid; 6295fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 629699653d4eSeschrock 6297fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6298fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 629999653d4eSeschrock return (B_TRUE); 630099653d4eSeschrock 6301fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6302fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6303fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 630439c23413Seschrock return (B_TRUE); 630539c23413Seschrock } 630639c23413Seschrock 630799653d4eSeschrock return (B_FALSE); 6308eaca9bbdSeschrock } 6309b1b8ab34Slling 631089a89ebfSlling /* 631189a89ebfSlling * Check if a pool has an active shared spare device. 631289a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 631389a89ebfSlling */ 631489a89ebfSlling static boolean_t 631589a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 631689a89ebfSlling { 631789a89ebfSlling int i, refcnt; 631889a89ebfSlling uint64_t pool; 631989a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 632089a89ebfSlling 632189a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 632289a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 632389a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 632489a89ebfSlling refcnt > 2) 632589a89ebfSlling return (B_TRUE); 632689a89ebfSlling } 632789a89ebfSlling 632889a89ebfSlling return (B_FALSE); 632989a89ebfSlling } 633089a89ebfSlling 63313d7072f8Seschrock /* 63323d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 63333d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 63343d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 63353d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 63363d7072f8Seschrock * or zdb as real changes. 63373d7072f8Seschrock */ 63383d7072f8Seschrock void 63393d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 63403d7072f8Seschrock { 63413d7072f8Seschrock #ifdef _KERNEL 63423d7072f8Seschrock sysevent_t *ev; 63433d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 63443d7072f8Seschrock sysevent_value_t value; 63453d7072f8Seschrock sysevent_id_t eid; 63463d7072f8Seschrock 63473d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 63483d7072f8Seschrock SE_SLEEP); 63493d7072f8Seschrock 63503d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 63513d7072f8Seschrock value.value.sv_string = spa_name(spa); 63523d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 63533d7072f8Seschrock goto done; 63543d7072f8Seschrock 63553d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 63563d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 63573d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 63583d7072f8Seschrock goto done; 63593d7072f8Seschrock 63603d7072f8Seschrock if (vd) { 63613d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 63623d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 63633d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 63643d7072f8Seschrock SE_SLEEP) != 0) 63653d7072f8Seschrock goto done; 63663d7072f8Seschrock 63673d7072f8Seschrock if (vd->vdev_path) { 63683d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 63693d7072f8Seschrock value.value.sv_string = vd->vdev_path; 63703d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 63713d7072f8Seschrock &value, SE_SLEEP) != 0) 63723d7072f8Seschrock goto done; 63733d7072f8Seschrock } 63743d7072f8Seschrock } 63753d7072f8Seschrock 6376b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6377b01c3b58Seschrock goto done; 6378b01c3b58Seschrock attr = NULL; 6379b01c3b58Seschrock 63803d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 63813d7072f8Seschrock 63823d7072f8Seschrock done: 63833d7072f8Seschrock if (attr) 63843d7072f8Seschrock sysevent_free_attr(attr); 63853d7072f8Seschrock sysevent_free(ev); 63863d7072f8Seschrock #endif 63873d7072f8Seschrock } 6388