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> 66*3b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 67fa9e4066Sahrens 685679c89fSjv #ifdef _KERNEL 69dedec472SJack Meng #include <sys/bootprops.h> 7035a5a358SJonathan Adams #include <sys/callb.h> 7135a5a358SJonathan Adams #include <sys/cpupart.h> 7235a5a358SJonathan Adams #include <sys/pool.h> 7335a5a358SJonathan Adams #include <sys/sysdc.h> 7435a5a358SJonathan Adams #include <sys/zone.h> 755679c89fSjv #endif /* _KERNEL */ 765679c89fSjv 77990b4856Slling #include "zfs_prop.h" 78b7b97454Sperrin #include "zfs_comutil.h" 79990b4856Slling 8035a5a358SJonathan Adams typedef enum zti_modes { 81ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 82ec94d322SAdam Leventhal ZTI_MODE_ONLINE_PERCENT, /* value is % of online CPUs */ 83ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 84ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 85ec94d322SAdam Leventhal ZTI_NMODES 8635a5a358SJonathan Adams } zti_modes_t; 87416e0cd8Sek 88ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 89ec94d322SAdam Leventhal #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } 90ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 91ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 922e0c549eSJonathan Adams 93ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 94ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 952e0c549eSJonathan Adams 962e0c549eSJonathan Adams typedef struct zio_taskq_info { 97ec94d322SAdam Leventhal zti_modes_t zti_mode; 9880eb36f2SGeorge Wilson uint_t zti_value; 99ec94d322SAdam Leventhal uint_t zti_count; 1002e0c549eSJonathan Adams } zio_taskq_info_t; 1012e0c549eSJonathan Adams 1022e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 10335a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1042e0c549eSJonathan Adams }; 1052e0c549eSJonathan Adams 10680eb36f2SGeorge Wilson /* 107ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 108ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 109ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 110ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 111ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 112ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 113ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 114ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 115ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 116ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 117ec94d322SAdam Leventhal * particular taskq is chosen at random. 118ec94d322SAdam Leventhal * 119ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 120ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 121ec94d322SAdam Leventhal * need to be handled with minimum delay. 12280eb36f2SGeorge Wilson */ 12380eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 12480eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 125ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 126ec94d322SAdam Leventhal { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */ 127ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 128ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 129ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 130ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1312e0c549eSJonathan Adams }; 1322e0c549eSJonathan Adams 133*3b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx); 134*3b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx); 13589a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1361195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1371195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1381195e687SMark J Musante char **ereport); 139cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 140990b4856Slling 14135a5a358SJonathan Adams uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 14235a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 14335a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 14435a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 14535a5a358SJonathan Adams 14635a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 14701f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 14835a5a358SJonathan Adams 14935a5a358SJonathan Adams /* 15035a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 15135a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 15235a5a358SJonathan Adams */ 15335a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 15435a5a358SJonathan Adams 155990b4856Slling /* 156990b4856Slling * ========================================================================== 157990b4856Slling * SPA properties routines 158990b4856Slling * ========================================================================== 159990b4856Slling */ 160990b4856Slling 161990b4856Slling /* 162990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 163990b4856Slling */ 1649d82f4f6Slling static void 165990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 166990b4856Slling uint64_t intval, zprop_source_t src) 167990b4856Slling { 168990b4856Slling const char *propname = zpool_prop_to_name(prop); 169990b4856Slling nvlist_t *propval; 170990b4856Slling 1719d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1729d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 173990b4856Slling 1749d82f4f6Slling if (strval != NULL) 1759d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1769d82f4f6Slling else 1779d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 178990b4856Slling 1799d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 180990b4856Slling nvlist_free(propval); 181990b4856Slling } 182990b4856Slling 183990b4856Slling /* 184990b4856Slling * Get property values from the spa configuration. 185990b4856Slling */ 1869d82f4f6Slling static void 187990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 188990b4856Slling { 1894263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 190ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 191379c004dSEric Schrock uint64_t size; 192485bbbf5SGeorge Wilson uint64_t alloc; 1934263d13fSGeorge Wilson uint64_t space; 194990b4856Slling uint64_t cap, version; 195990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 196c5904d13Seschrock spa_config_dirent_t *dp; 197990b4856Slling 198e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 199e14bb325SJeff Bonwick 2004263d13fSGeorge Wilson if (rvd != NULL) { 201485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 202b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 203379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 204379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 205485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 206485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 207485bbbf5SGeorge Wilson size - alloc, src); 2084263d13fSGeorge Wilson 2094263d13fSGeorge Wilson space = 0; 2104263d13fSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2114263d13fSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 2124263d13fSGeorge Wilson space += tvd->vdev_max_asize - tvd->vdev_asize; 2134263d13fSGeorge Wilson } 2144263d13fSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space, 2154263d13fSGeorge Wilson src); 2164263d13fSGeorge Wilson 217f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 218f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 219379c004dSEric Schrock 220485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 221379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 222379c004dSEric Schrock 223b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 224b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 225b24ab676SJeff Bonwick 226379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2274263d13fSGeorge Wilson rvd->vdev_state, src); 228379c004dSEric Schrock 229379c004dSEric Schrock version = spa_version(spa); 230379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 231379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 232379c004dSEric Schrock else 233379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 234379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 235379c004dSEric Schrock } 236990b4856Slling 237ad135b5dSChristopher Siden if (pool != NULL) { 238ad135b5dSChristopher Siden dsl_dir_t *freedir = pool->dp_free_dir; 239ad135b5dSChristopher Siden 240ad135b5dSChristopher Siden /* 241ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 242ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 243ad135b5dSChristopher Siden */ 244ad135b5dSChristopher Siden if (freedir != NULL) { 245ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 246ad135b5dSChristopher Siden freedir->dd_phys->dd_used_bytes, src); 247ad135b5dSChristopher Siden } else { 248ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 249ad135b5dSChristopher Siden NULL, 0, src); 250ad135b5dSChristopher Siden } 251ad135b5dSChristopher Siden } 252ad135b5dSChristopher Siden 2539d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 254990b4856Slling 2558704186eSDan McDonald if (spa->spa_comment != NULL) { 2568704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2578704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2588704186eSDan McDonald } 2598704186eSDan McDonald 2609d82f4f6Slling if (spa->spa_root != NULL) 2619d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2629d82f4f6Slling 0, ZPROP_SRC_LOCAL); 263990b4856Slling 264c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 265c5904d13Seschrock if (dp->scd_path == NULL) { 2669d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 267c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 268c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2699d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 270c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2712f8aaab3Seschrock } 2722f8aaab3Seschrock } 273990b4856Slling } 274990b4856Slling 275990b4856Slling /* 276990b4856Slling * Get zpool property values. 277990b4856Slling */ 278990b4856Slling int 279990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 280990b4856Slling { 281b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 282990b4856Slling zap_cursor_t zc; 283990b4856Slling zap_attribute_t za; 284990b4856Slling int err; 285990b4856Slling 2869d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 287990b4856Slling 288e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 289e14bb325SJeff Bonwick 290990b4856Slling /* 291990b4856Slling * Get properties from the spa config. 292990b4856Slling */ 2939d82f4f6Slling spa_prop_get_config(spa, nvp); 294990b4856Slling 295990b4856Slling /* If no pool property object, no more prop to get. */ 296afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 297990b4856Slling mutex_exit(&spa->spa_props_lock); 298990b4856Slling return (0); 299990b4856Slling } 300990b4856Slling 301990b4856Slling /* 302990b4856Slling * Get properties from the MOS pool property object. 303990b4856Slling */ 304990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 305990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 306990b4856Slling zap_cursor_advance(&zc)) { 307990b4856Slling uint64_t intval = 0; 308990b4856Slling char *strval = NULL; 309990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 310990b4856Slling zpool_prop_t prop; 311990b4856Slling 312990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 313990b4856Slling continue; 314990b4856Slling 315990b4856Slling switch (za.za_integer_length) { 316990b4856Slling case 8: 317990b4856Slling /* integer property */ 318990b4856Slling if (za.za_first_integer != 319990b4856Slling zpool_prop_default_numeric(prop)) 320990b4856Slling src = ZPROP_SRC_LOCAL; 321990b4856Slling 322990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 323990b4856Slling dsl_pool_t *dp; 324990b4856Slling dsl_dataset_t *ds = NULL; 325990b4856Slling 326990b4856Slling dp = spa_get_dsl(spa); 327*3b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 328745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 329745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 330*3b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 331990b4856Slling break; 332990b4856Slling } 333990b4856Slling 334990b4856Slling strval = kmem_alloc( 335990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 336990b4856Slling KM_SLEEP); 337990b4856Slling dsl_dataset_name(ds, strval); 338745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 339*3b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 340990b4856Slling } else { 341990b4856Slling strval = NULL; 342990b4856Slling intval = za.za_first_integer; 343990b4856Slling } 344990b4856Slling 3459d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 346990b4856Slling 347990b4856Slling if (strval != NULL) 348990b4856Slling kmem_free(strval, 349990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 350990b4856Slling 351990b4856Slling break; 352990b4856Slling 353990b4856Slling case 1: 354990b4856Slling /* string property */ 355990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 356990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 357990b4856Slling za.za_name, 1, za.za_num_integers, strval); 358990b4856Slling if (err) { 359990b4856Slling kmem_free(strval, za.za_num_integers); 360990b4856Slling break; 361990b4856Slling } 3629d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 363990b4856Slling kmem_free(strval, za.za_num_integers); 364990b4856Slling break; 365990b4856Slling 366990b4856Slling default: 367990b4856Slling break; 368990b4856Slling } 369990b4856Slling } 370990b4856Slling zap_cursor_fini(&zc); 371990b4856Slling mutex_exit(&spa->spa_props_lock); 372990b4856Slling out: 373990b4856Slling if (err && err != ENOENT) { 374990b4856Slling nvlist_free(*nvp); 3759d82f4f6Slling *nvp = NULL; 376990b4856Slling return (err); 377990b4856Slling } 378990b4856Slling 379990b4856Slling return (0); 380990b4856Slling } 381990b4856Slling 382990b4856Slling /* 383990b4856Slling * Validate the given pool properties nvlist and modify the list 384990b4856Slling * for the property values to be set. 385990b4856Slling */ 386990b4856Slling static int 387990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 388990b4856Slling { 389990b4856Slling nvpair_t *elem; 390990b4856Slling int error = 0, reset_bootfs = 0; 391d5285caeSGeorge Wilson uint64_t objnum = 0; 392ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 393990b4856Slling 394990b4856Slling elem = NULL; 395990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 396990b4856Slling uint64_t intval; 397ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 398ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 399ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 400ad135b5dSChristopher Siden 401ad135b5dSChristopher Siden switch (prop) { 402ad135b5dSChristopher Siden case ZPROP_INVAL: 403ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 404ad135b5dSChristopher Siden error = EINVAL; 405ad135b5dSChristopher Siden break; 406ad135b5dSChristopher Siden } 407990b4856Slling 408ad135b5dSChristopher Siden /* 409ad135b5dSChristopher Siden * Sanitize the input. 410ad135b5dSChristopher Siden */ 411ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 412ad135b5dSChristopher Siden error = EINVAL; 413ad135b5dSChristopher Siden break; 414ad135b5dSChristopher Siden } 415990b4856Slling 416ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 417ad135b5dSChristopher Siden error = EINVAL; 418ad135b5dSChristopher Siden break; 419ad135b5dSChristopher Siden } 420ad135b5dSChristopher Siden 421ad135b5dSChristopher Siden if (intval != 0) { 422ad135b5dSChristopher Siden error = EINVAL; 423ad135b5dSChristopher Siden break; 424ad135b5dSChristopher Siden } 425ad135b5dSChristopher Siden 426ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 427ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 428ad135b5dSChristopher Siden error = EINVAL; 429ad135b5dSChristopher Siden break; 430ad135b5dSChristopher Siden } 431ad135b5dSChristopher Siden 432ad135b5dSChristopher Siden has_feature = B_TRUE; 433ad135b5dSChristopher Siden break; 434990b4856Slling 435990b4856Slling case ZPOOL_PROP_VERSION: 436990b4856Slling error = nvpair_value_uint64(elem, &intval); 437990b4856Slling if (!error && 438ad135b5dSChristopher Siden (intval < spa_version(spa) || 439ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 440ad135b5dSChristopher Siden has_feature)) 441990b4856Slling error = EINVAL; 442990b4856Slling break; 443990b4856Slling 444990b4856Slling case ZPOOL_PROP_DELEGATION: 445990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 446d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 447573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 448990b4856Slling error = nvpair_value_uint64(elem, &intval); 449990b4856Slling if (!error && intval > 1) 450990b4856Slling error = EINVAL; 451990b4856Slling break; 452990b4856Slling 453990b4856Slling case ZPOOL_PROP_BOOTFS: 45425f89ee2SJeff Bonwick /* 45525f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 45625f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 45725f89ee2SJeff Bonwick * the bootfs property cannot be set. 45825f89ee2SJeff Bonwick */ 459990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 460990b4856Slling error = ENOTSUP; 461990b4856Slling break; 462990b4856Slling } 463990b4856Slling 464990b4856Slling /* 46515e6edf1Sgw * Make sure the vdev config is bootable 466990b4856Slling */ 46715e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 468990b4856Slling error = ENOTSUP; 469990b4856Slling break; 470990b4856Slling } 471990b4856Slling 472990b4856Slling reset_bootfs = 1; 473990b4856Slling 474990b4856Slling error = nvpair_value_string(elem, &strval); 475990b4856Slling 476990b4856Slling if (!error) { 477ad135b5dSChristopher Siden objset_t *os; 47815e6edf1Sgw uint64_t compress; 47915e6edf1Sgw 480990b4856Slling if (strval == NULL || strval[0] == '\0') { 481990b4856Slling objnum = zpool_prop_default_numeric( 482990b4856Slling ZPOOL_PROP_BOOTFS); 483990b4856Slling break; 484990b4856Slling } 485990b4856Slling 486503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 487990b4856Slling break; 48815e6edf1Sgw 489503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 490503ad85cSMatthew Ahrens 491503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 492503ad85cSMatthew Ahrens error = ENOTSUP; 493*3b2aab18SMatthew Ahrens } else if ((error = 494*3b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os), 49515e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 496*3b2aab18SMatthew Ahrens &compress)) == 0 && 49715e6edf1Sgw !BOOTFS_COMPRESS_VALID(compress)) { 49815e6edf1Sgw error = ENOTSUP; 49915e6edf1Sgw } else { 50015e6edf1Sgw objnum = dmu_objset_id(os); 50115e6edf1Sgw } 502503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 503990b4856Slling } 504990b4856Slling break; 505e14bb325SJeff Bonwick 5060a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 5070a4e9518Sgw error = nvpair_value_uint64(elem, &intval); 5080a4e9518Sgw if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 5090a4e9518Sgw intval > ZIO_FAILURE_MODE_PANIC)) 5100a4e9518Sgw error = EINVAL; 5110a4e9518Sgw 5120a4e9518Sgw /* 5130a4e9518Sgw * This is a special case which only occurs when 5140a4e9518Sgw * the pool has completely failed. This allows 5150a4e9518Sgw * the user to change the in-core failmode property 5160a4e9518Sgw * without syncing it out to disk (I/Os might 5170a4e9518Sgw * currently be blocked). We do this by returning 5180a4e9518Sgw * EIO to the caller (spa_prop_set) to trick it 5190a4e9518Sgw * into thinking we encountered a property validation 5200a4e9518Sgw * error. 5210a4e9518Sgw */ 522e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) { 5230a4e9518Sgw spa->spa_failmode = intval; 5240a4e9518Sgw error = EIO; 5250a4e9518Sgw } 5260a4e9518Sgw break; 5272f8aaab3Seschrock 5282f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5292f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0) 5302f8aaab3Seschrock break; 5312f8aaab3Seschrock 5322f8aaab3Seschrock if (strval[0] == '\0') 5332f8aaab3Seschrock break; 5342f8aaab3Seschrock 5352f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5362f8aaab3Seschrock break; 5372f8aaab3Seschrock 5382f8aaab3Seschrock if (strval[0] != '/') { 5392f8aaab3Seschrock error = EINVAL; 5402f8aaab3Seschrock break; 5412f8aaab3Seschrock } 5422f8aaab3Seschrock 5432f8aaab3Seschrock slash = strrchr(strval, '/'); 5442f8aaab3Seschrock ASSERT(slash != NULL); 5452f8aaab3Seschrock 5462f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 5472f8aaab3Seschrock strcmp(slash, "/..") == 0) 5482f8aaab3Seschrock error = EINVAL; 5492f8aaab3Seschrock break; 550b24ab676SJeff Bonwick 5518704186eSDan McDonald case ZPOOL_PROP_COMMENT: 5528704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0) 5538704186eSDan McDonald break; 5548704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 5558704186eSDan McDonald /* 5568704186eSDan McDonald * The kernel doesn't have an easy isprint() 5578704186eSDan McDonald * check. For this kernel check, we merely 5588704186eSDan McDonald * check ASCII apart from DEL. Fix this if 5598704186eSDan McDonald * there is an easy-to-use kernel isprint(). 5608704186eSDan McDonald */ 5618704186eSDan McDonald if (*check >= 0x7f) { 5628704186eSDan McDonald error = EINVAL; 5638704186eSDan McDonald break; 5648704186eSDan McDonald } 5658704186eSDan McDonald check++; 5668704186eSDan McDonald } 5678704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) 5688704186eSDan McDonald error = E2BIG; 5698704186eSDan McDonald break; 5708704186eSDan McDonald 571b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 572b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP) 573b24ab676SJeff Bonwick error = ENOTSUP; 574b24ab676SJeff Bonwick else 575b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval); 576b24ab676SJeff Bonwick if (error == 0 && 577b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 578b24ab676SJeff Bonwick error = EINVAL; 579b24ab676SJeff Bonwick break; 580990b4856Slling } 581990b4856Slling 582990b4856Slling if (error) 583990b4856Slling break; 584990b4856Slling } 585990b4856Slling 586990b4856Slling if (!error && reset_bootfs) { 587990b4856Slling error = nvlist_remove(props, 588990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 589990b4856Slling 590990b4856Slling if (!error) { 591990b4856Slling error = nvlist_add_uint64(props, 592990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 593990b4856Slling } 594990b4856Slling } 595990b4856Slling 596990b4856Slling return (error); 597990b4856Slling } 598990b4856Slling 599379c004dSEric Schrock void 600379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 601379c004dSEric Schrock { 602379c004dSEric Schrock char *cachefile; 603379c004dSEric Schrock spa_config_dirent_t *dp; 604379c004dSEric Schrock 605379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 606379c004dSEric Schrock &cachefile) != 0) 607379c004dSEric Schrock return; 608379c004dSEric Schrock 609379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t), 610379c004dSEric Schrock KM_SLEEP); 611379c004dSEric Schrock 612379c004dSEric Schrock if (cachefile[0] == '\0') 613379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path); 614379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0) 615379c004dSEric Schrock dp->scd_path = NULL; 616379c004dSEric Schrock else 617379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile); 618379c004dSEric Schrock 619379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp); 620379c004dSEric Schrock if (need_sync) 621379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 622379c004dSEric Schrock } 623379c004dSEric Schrock 624990b4856Slling int 625990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp) 626990b4856Slling { 627990b4856Slling int error; 628ad135b5dSChristopher Siden nvpair_t *elem = NULL; 629379c004dSEric Schrock boolean_t need_sync = B_FALSE; 630990b4856Slling 631990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0) 632990b4856Slling return (error); 633990b4856Slling 634379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 635ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 636379c004dSEric Schrock 637f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE || 638f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT || 639f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY) 640379c004dSEric Schrock continue; 641379c004dSEric Schrock 642ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { 643ad135b5dSChristopher Siden uint64_t ver; 644ad135b5dSChristopher Siden 645ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) { 646ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0); 647ad135b5dSChristopher Siden } else { 648ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 649ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES; 650ad135b5dSChristopher Siden need_sync = B_TRUE; 651ad135b5dSChristopher Siden } 652ad135b5dSChristopher Siden 653ad135b5dSChristopher Siden /* Save time if the version is already set. */ 654ad135b5dSChristopher Siden if (ver == spa_version(spa)) 655ad135b5dSChristopher Siden continue; 656ad135b5dSChristopher Siden 657ad135b5dSChristopher Siden /* 658ad135b5dSChristopher Siden * In addition to the pool directory object, we might 659ad135b5dSChristopher Siden * create the pool properties object, the features for 660ad135b5dSChristopher Siden * read object, the features for write object, or the 661ad135b5dSChristopher Siden * feature descriptions object. 662ad135b5dSChristopher Siden */ 663*3b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL, 664*3b2aab18SMatthew Ahrens spa_sync_version, &ver, 6); 665ad135b5dSChristopher Siden if (error) 666ad135b5dSChristopher Siden return (error); 667ad135b5dSChristopher Siden continue; 668ad135b5dSChristopher Siden } 669ad135b5dSChristopher Siden 670379c004dSEric Schrock need_sync = B_TRUE; 671379c004dSEric Schrock break; 672379c004dSEric Schrock } 673379c004dSEric Schrock 674ad135b5dSChristopher Siden if (need_sync) { 675*3b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 676*3b2aab18SMatthew Ahrens nvp, 6)); 677ad135b5dSChristopher Siden } 678ad135b5dSChristopher Siden 679ad135b5dSChristopher Siden return (0); 680990b4856Slling } 681990b4856Slling 682990b4856Slling /* 683990b4856Slling * If the bootfs property value is dsobj, clear it. 684990b4856Slling */ 685990b4856Slling void 686990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 687990b4856Slling { 688990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 689990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset, 690990b4856Slling spa->spa_pool_props_object, 691990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 692990b4856Slling spa->spa_bootfs = 0; 693990b4856Slling } 694990b4856Slling } 695990b4856Slling 696dfbb9432SGeorge Wilson /*ARGSUSED*/ 697dfbb9432SGeorge Wilson static int 698*3b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx) 699dfbb9432SGeorge Wilson { 700*3b2aab18SMatthew Ahrens uint64_t *newguid = arg; 701*3b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 702dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 703dfbb9432SGeorge Wilson uint64_t vdev_state; 704dfbb9432SGeorge Wilson 705dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 706dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state; 707dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 708dfbb9432SGeorge Wilson 709dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY) 710dfbb9432SGeorge Wilson return (ENXIO); 711dfbb9432SGeorge Wilson 712dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid); 713dfbb9432SGeorge Wilson 714dfbb9432SGeorge Wilson return (0); 715dfbb9432SGeorge Wilson } 716dfbb9432SGeorge Wilson 717dfbb9432SGeorge Wilson static void 718*3b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx) 719dfbb9432SGeorge Wilson { 720*3b2aab18SMatthew Ahrens uint64_t *newguid = arg; 721*3b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 722dfbb9432SGeorge Wilson uint64_t oldguid; 723dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 724dfbb9432SGeorge Wilson 725dfbb9432SGeorge Wilson oldguid = spa_guid(spa); 726dfbb9432SGeorge Wilson 727dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 728dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid; 729dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid); 730dfbb9432SGeorge Wilson vdev_config_dirty(rvd); 731dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 732dfbb9432SGeorge Wilson 73320128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 734dfbb9432SGeorge Wilson oldguid, *newguid); 735dfbb9432SGeorge Wilson } 736dfbb9432SGeorge Wilson 737e9103aaeSGarrett D'Amore /* 738e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later 739e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify 740e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our 741e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are 742e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present 743e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a 744e9103aaeSGarrett D'Amore * sysevent to update any watchers. 745e9103aaeSGarrett D'Amore */ 746e9103aaeSGarrett D'Amore int 747e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa) 748e9103aaeSGarrett D'Amore { 749dfbb9432SGeorge Wilson int error; 750dfbb9432SGeorge Wilson uint64_t guid; 751e9103aaeSGarrett D'Amore 752dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock); 753dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL); 754e9103aaeSGarrett D'Amore 755*3b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 756*3b2aab18SMatthew Ahrens spa_change_guid_sync, &guid, 5); 757e9103aaeSGarrett D'Amore 758dfbb9432SGeorge Wilson if (error == 0) { 759dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 760dfbb9432SGeorge Wilson spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID); 761dfbb9432SGeorge Wilson } 762e9103aaeSGarrett D'Amore 763dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock); 764e9103aaeSGarrett D'Amore 765dfbb9432SGeorge Wilson return (error); 766e9103aaeSGarrett D'Amore } 767e9103aaeSGarrett D'Amore 768fa9e4066Sahrens /* 769fa9e4066Sahrens * ========================================================================== 770fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export) 771fa9e4066Sahrens * ========================================================================== 772fa9e4066Sahrens */ 773fa9e4066Sahrens 774ea8dc4b6Seschrock static int 775ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b) 776ea8dc4b6Seschrock { 777ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a; 778ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b; 779ea8dc4b6Seschrock int ret; 780ea8dc4b6Seschrock 781ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 782ea8dc4b6Seschrock sizeof (zbookmark_t)); 783ea8dc4b6Seschrock 784ea8dc4b6Seschrock if (ret < 0) 785ea8dc4b6Seschrock return (-1); 786ea8dc4b6Seschrock else if (ret > 0) 787ea8dc4b6Seschrock return (1); 788ea8dc4b6Seschrock else 789ea8dc4b6Seschrock return (0); 790ea8dc4b6Seschrock } 791ea8dc4b6Seschrock 792ea8dc4b6Seschrock /* 793ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and 794ea8dc4b6Seschrock * re-initializes them in the process. 795ea8dc4b6Seschrock */ 796ea8dc4b6Seschrock void 797ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 798ea8dc4b6Seschrock { 799ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 800ea8dc4b6Seschrock 801ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 802ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 803ea8dc4b6Seschrock 804ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 805ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 806ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 807ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 808ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 809ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 810ea8dc4b6Seschrock } 811ea8dc4b6Seschrock 812ec94d322SAdam Leventhal static void 813ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 814fa9e4066Sahrens { 815ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 816ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 817ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 818ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 819ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 820ec94d322SAdam Leventhal char name[32]; 8215aeb9474SGarrett D'Amore uint_t flags = 0; 82235a5a358SJonathan Adams boolean_t batch = B_FALSE; 823fa9e4066Sahrens 824ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 825ec94d322SAdam Leventhal tqs->stqs_count = 0; 826ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 827ec94d322SAdam Leventhal return; 828ec94d322SAdam Leventhal } 829fa9e4066Sahrens 830ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 831fa9e4066Sahrens 832ec94d322SAdam Leventhal tqs->stqs_count = count; 833ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 83435a5a358SJonathan Adams 835ec94d322SAdam Leventhal for (uint_t i = 0; i < count; i++) { 836ec94d322SAdam Leventhal taskq_t *tq; 83735a5a358SJonathan Adams 838ec94d322SAdam Leventhal switch (mode) { 839ec94d322SAdam Leventhal case ZTI_MODE_FIXED: 840ec94d322SAdam Leventhal ASSERT3U(value, >=, 1); 841ec94d322SAdam Leventhal value = MAX(value, 1); 842ec94d322SAdam Leventhal break; 843ec94d322SAdam Leventhal 844ec94d322SAdam Leventhal case ZTI_MODE_BATCH: 845ec94d322SAdam Leventhal batch = B_TRUE; 846ec94d322SAdam Leventhal flags |= TASKQ_THREADS_CPU_PCT; 847ec94d322SAdam Leventhal value = zio_taskq_batch_pct; 848ec94d322SAdam Leventhal break; 849ec94d322SAdam Leventhal 850ec94d322SAdam Leventhal case ZTI_MODE_ONLINE_PERCENT: 851ec94d322SAdam Leventhal flags |= TASKQ_THREADS_CPU_PCT; 852ec94d322SAdam Leventhal break; 853ec94d322SAdam Leventhal 854ec94d322SAdam Leventhal default: 855ec94d322SAdam Leventhal panic("unrecognized mode for %s_%s taskq (%u:%u) in " 856ec94d322SAdam Leventhal "spa_activate()", 857ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], mode, value); 858ec94d322SAdam Leventhal break; 859ec94d322SAdam Leventhal } 860ec94d322SAdam Leventhal 861ec94d322SAdam Leventhal if (count > 1) { 862ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 863ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 864ec94d322SAdam Leventhal } else { 865ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 866ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 867ec94d322SAdam Leventhal } 868ec94d322SAdam Leventhal 869ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 870ec94d322SAdam Leventhal if (batch) 871ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 872ec94d322SAdam Leventhal 873ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 874ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 875ec94d322SAdam Leventhal } else { 876ec94d322SAdam Leventhal tq = taskq_create_proc(name, value, maxclsyspri, 50, 877ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 878ec94d322SAdam Leventhal } 879ec94d322SAdam Leventhal 880ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 881ec94d322SAdam Leventhal } 882ec94d322SAdam Leventhal } 883ec94d322SAdam Leventhal 884ec94d322SAdam Leventhal static void 885ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 886ec94d322SAdam Leventhal { 887ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 888ec94d322SAdam Leventhal 889ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 890ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 891ec94d322SAdam Leventhal return; 892ec94d322SAdam Leventhal } 893ec94d322SAdam Leventhal 894ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 895ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 896ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 89735a5a358SJonathan Adams } 89835a5a358SJonathan Adams 899ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 900ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 901ec94d322SAdam Leventhal } 90235a5a358SJonathan Adams 903ec94d322SAdam Leventhal /* 904ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 905ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 906ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 907ec94d322SAdam Leventhal * the low bits of gethrtime(). 908ec94d322SAdam Leventhal */ 909ec94d322SAdam Leventhal void 910ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 911ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 912ec94d322SAdam Leventhal { 913ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 914ec94d322SAdam Leventhal taskq_t *tq; 915ec94d322SAdam Leventhal 916ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 917ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 918ec94d322SAdam Leventhal 919ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 920ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 921ec94d322SAdam Leventhal } else { 922ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 92335a5a358SJonathan Adams } 924ec94d322SAdam Leventhal 925ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent); 92635a5a358SJonathan Adams } 92735a5a358SJonathan Adams 92835a5a358SJonathan Adams static void 92935a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa) 93035a5a358SJonathan Adams { 931e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 932e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 933ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q); 93435a5a358SJonathan Adams } 93535a5a358SJonathan Adams } 93635a5a358SJonathan Adams } 93735a5a358SJonathan Adams 93835a5a358SJonathan Adams #ifdef _KERNEL 93935a5a358SJonathan Adams static void 94035a5a358SJonathan Adams spa_thread(void *arg) 94135a5a358SJonathan Adams { 94235a5a358SJonathan Adams callb_cpr_t cprinfo; 9432e0c549eSJonathan Adams 94435a5a358SJonathan Adams spa_t *spa = arg; 94535a5a358SJonathan Adams user_t *pu = PTOU(curproc); 9462e0c549eSJonathan Adams 94735a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 94835a5a358SJonathan Adams spa->spa_name); 9492e0c549eSJonathan Adams 95035a5a358SJonathan Adams ASSERT(curproc != &p0); 95135a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 95235a5a358SJonathan Adams "zpool-%s", spa->spa_name); 95335a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 9542e0c549eSJonathan Adams 95535a5a358SJonathan Adams /* bind this thread to the requested psrset */ 95635a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) { 95735a5a358SJonathan Adams pool_lock(); 95835a5a358SJonathan Adams mutex_enter(&cpu_lock); 95935a5a358SJonathan Adams mutex_enter(&pidlock); 96035a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 96180eb36f2SGeorge Wilson 96235a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 96335a5a358SJonathan Adams 0, NULL, NULL) == 0) { 96435a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind; 96535a5a358SJonathan Adams } else { 96635a5a358SJonathan Adams cmn_err(CE_WARN, 96735a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to " 96835a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 96935a5a358SJonathan Adams } 97035a5a358SJonathan Adams 97135a5a358SJonathan Adams mutex_exit(&curproc->p_lock); 97235a5a358SJonathan Adams mutex_exit(&pidlock); 97335a5a358SJonathan Adams mutex_exit(&cpu_lock); 97435a5a358SJonathan Adams pool_unlock(); 97535a5a358SJonathan Adams } 97635a5a358SJonathan Adams 97735a5a358SJonathan Adams if (zio_taskq_sysdc) { 97835a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0); 97935a5a358SJonathan Adams } 98035a5a358SJonathan Adams 98135a5a358SJonathan Adams spa->spa_proc = curproc; 98235a5a358SJonathan Adams spa->spa_did = curthread->t_did; 98335a5a358SJonathan Adams 98435a5a358SJonathan Adams spa_create_zio_taskqs(spa); 98535a5a358SJonathan Adams 98635a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 98735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 98835a5a358SJonathan Adams 98935a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE; 99035a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 99135a5a358SJonathan Adams 99235a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo); 99335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE) 99435a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 99535a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 99635a5a358SJonathan Adams 99735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 99835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE; 99935a5a358SJonathan Adams spa->spa_proc = &p0; 100035a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 100135a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 100235a5a358SJonathan Adams 100335a5a358SJonathan Adams mutex_enter(&curproc->p_lock); 100435a5a358SJonathan Adams lwp_exit(); 100535a5a358SJonathan Adams } 100635a5a358SJonathan Adams #endif 100735a5a358SJonathan Adams 100835a5a358SJonathan Adams /* 100935a5a358SJonathan Adams * Activate an uninitialized pool. 101035a5a358SJonathan Adams */ 101135a5a358SJonathan Adams static void 101235a5a358SJonathan Adams spa_activate(spa_t *spa, int mode) 101335a5a358SJonathan Adams { 101435a5a358SJonathan Adams ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 101535a5a358SJonathan Adams 101635a5a358SJonathan Adams spa->spa_state = POOL_STATE_ACTIVE; 101735a5a358SJonathan Adams spa->spa_mode = mode; 101835a5a358SJonathan Adams 101935a5a358SJonathan Adams spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 102035a5a358SJonathan Adams spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 102135a5a358SJonathan Adams 102235a5a358SJonathan Adams /* Try to create a covering process */ 102335a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 102435a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 102535a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 102635a5a358SJonathan Adams spa->spa_did = 0; 102735a5a358SJonathan Adams 102835a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */ 102935a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 103035a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 103135a5a358SJonathan Adams NULL, 0) == 0) { 103235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED; 103335a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) { 103435a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, 103535a5a358SJonathan Adams &spa->spa_proc_lock); 10362e0c549eSJonathan Adams } 103735a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 103835a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 103935a5a358SJonathan Adams ASSERT(spa->spa_did != 0); 104035a5a358SJonathan Adams } else { 104135a5a358SJonathan Adams #ifdef _KERNEL 104235a5a358SJonathan Adams cmn_err(CE_WARN, 104335a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n", 104435a5a358SJonathan Adams spa->spa_name); 104535a5a358SJonathan Adams #endif 1046e14bb325SJeff Bonwick } 1047fa9e4066Sahrens } 104835a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 104935a5a358SJonathan Adams 105035a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */ 105135a5a358SJonathan Adams if (spa->spa_proc == &p0) { 105235a5a358SJonathan Adams spa_create_zio_taskqs(spa); 105335a5a358SJonathan Adams } 1054fa9e4066Sahrens 1055e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1056e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node)); 1057e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1058e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node)); 1059fa9e4066Sahrens 1060fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list, 1061fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node)); 1062ea8dc4b6Seschrock 1063ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub, 1064ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1065ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1066ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last, 1067ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t), 1068ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl)); 1069fa9e4066Sahrens } 1070fa9e4066Sahrens 1071fa9e4066Sahrens /* 1072fa9e4066Sahrens * Opposite of spa_activate(). 1073fa9e4066Sahrens */ 1074fa9e4066Sahrens static void 1075fa9e4066Sahrens spa_deactivate(spa_t *spa) 1076fa9e4066Sahrens { 1077fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE); 1078fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL); 1079fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL); 108025f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL); 1081fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1082fa9e4066Sahrens 1083fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list); 1084fa9e4066Sahrens 1085e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list); 1086e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list); 1087fa9e4066Sahrens 1088e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) { 1089e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1090ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q); 1091e14bb325SJeff Bonwick } 1092fa9e4066Sahrens } 1093fa9e4066Sahrens 1094fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class); 1095fa9e4066Sahrens spa->spa_normal_class = NULL; 1096fa9e4066Sahrens 10978654d025Sperrin metaslab_class_destroy(spa->spa_log_class); 10988654d025Sperrin spa->spa_log_class = NULL; 10998654d025Sperrin 1100ea8dc4b6Seschrock /* 1101ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may 1102ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case. 1103ea8dc4b6Seschrock */ 1104ea8dc4b6Seschrock spa_errlog_drain(spa); 1105ea8dc4b6Seschrock 1106ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub); 1107ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last); 1108ea8dc4b6Seschrock 1109fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 111035a5a358SJonathan Adams 111135a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock); 111235a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) { 111335a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 111435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE; 111535a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv); 111635a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 111735a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0); 111835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 111935a5a358SJonathan Adams } 112035a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 112135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 112235a5a358SJonathan Adams } 112335a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0); 112435a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock); 112535a5a358SJonathan Adams 112635a5a358SJonathan Adams /* 112735a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS 112835a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath 112935a5a358SJonathan Adams * it. 113035a5a358SJonathan Adams */ 113135a5a358SJonathan Adams if (spa->spa_did != 0) { 113235a5a358SJonathan Adams thread_join(spa->spa_did); 113335a5a358SJonathan Adams spa->spa_did = 0; 113435a5a358SJonathan Adams } 1135fa9e4066Sahrens } 1136fa9e4066Sahrens 1137fa9e4066Sahrens /* 1138fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This 1139fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev 1140fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import. 1141fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine. 1142fa9e4066Sahrens */ 114399653d4eSeschrock static int 114499653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 114599653d4eSeschrock uint_t id, int atype) 1146fa9e4066Sahrens { 1147fa9e4066Sahrens nvlist_t **child; 1148573ca77eSGeorge Wilson uint_t children; 114999653d4eSeschrock int error; 1150fa9e4066Sahrens 115199653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 115299653d4eSeschrock return (error); 1153fa9e4066Sahrens 115499653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf) 115599653d4eSeschrock return (0); 1156fa9e4066Sahrens 1157e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1158e14bb325SJeff Bonwick &child, &children); 1159e14bb325SJeff Bonwick 1160e14bb325SJeff Bonwick if (error == ENOENT) 1161e14bb325SJeff Bonwick return (0); 1162e14bb325SJeff Bonwick 1163e14bb325SJeff Bonwick if (error) { 116499653d4eSeschrock vdev_free(*vdp); 116599653d4eSeschrock *vdp = NULL; 116699653d4eSeschrock return (EINVAL); 1167fa9e4066Sahrens } 1168fa9e4066Sahrens 1169573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 117099653d4eSeschrock vdev_t *vd; 117199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 117299653d4eSeschrock atype)) != 0) { 117399653d4eSeschrock vdev_free(*vdp); 117499653d4eSeschrock *vdp = NULL; 117599653d4eSeschrock return (error); 1176fa9e4066Sahrens } 1177fa9e4066Sahrens } 1178fa9e4066Sahrens 117999653d4eSeschrock ASSERT(*vdp != NULL); 118099653d4eSeschrock 118199653d4eSeschrock return (0); 1182fa9e4066Sahrens } 1183fa9e4066Sahrens 1184fa9e4066Sahrens /* 1185fa9e4066Sahrens * Opposite of spa_load(). 1186fa9e4066Sahrens */ 1187fa9e4066Sahrens static void 1188fa9e4066Sahrens spa_unload(spa_t *spa) 1189fa9e4066Sahrens { 119099653d4eSeschrock int i; 119199653d4eSeschrock 1192e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1193e14bb325SJeff Bonwick 1194ea8dc4b6Seschrock /* 1195ea8dc4b6Seschrock * Stop async tasks. 1196ea8dc4b6Seschrock */ 1197ea8dc4b6Seschrock spa_async_suspend(spa); 1198ea8dc4b6Seschrock 1199fa9e4066Sahrens /* 1200fa9e4066Sahrens * Stop syncing. 1201fa9e4066Sahrens */ 1202fa9e4066Sahrens if (spa->spa_sync_on) { 1203fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool); 1204fa9e4066Sahrens spa->spa_sync_on = B_FALSE; 1205fa9e4066Sahrens } 1206fa9e4066Sahrens 1207fa9e4066Sahrens /* 1208e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete. 1209fa9e4066Sahrens */ 121054d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) { 121154d692b7SGeorge Wilson (void) zio_wait(spa->spa_async_zio_root); 121254d692b7SGeorge Wilson spa->spa_async_zio_root = NULL; 121354d692b7SGeorge Wilson } 1214fa9e4066Sahrens 1215cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj); 1216cde58dbcSMatthew Ahrens 1217fa9e4066Sahrens /* 1218fa9e4066Sahrens * Close the dsl pool. 1219fa9e4066Sahrens */ 1220fa9e4066Sahrens if (spa->spa_dsl_pool) { 1221fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool); 1222fa9e4066Sahrens spa->spa_dsl_pool = NULL; 1223afee20e4SGeorge Wilson spa->spa_meta_objset = NULL; 1224fa9e4066Sahrens } 1225fa9e4066Sahrens 1226b24ab676SJeff Bonwick ddt_unload(spa); 1227b24ab676SJeff Bonwick 12288ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 12298ad4d6ddSJeff Bonwick 12308ad4d6ddSJeff Bonwick /* 12318ad4d6ddSJeff Bonwick * Drop and purge level 2 cache 12328ad4d6ddSJeff Bonwick */ 12338ad4d6ddSJeff Bonwick spa_l2cache_drop(spa); 12348ad4d6ddSJeff Bonwick 1235fa9e4066Sahrens /* 1236fa9e4066Sahrens * Close all vdevs. 1237fa9e4066Sahrens */ 12380e34b6a7Sbonwick if (spa->spa_root_vdev) 1239fa9e4066Sahrens vdev_free(spa->spa_root_vdev); 12400e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == NULL); 1241ea8dc4b6Seschrock 1242fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1243fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]); 1244fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) { 1245fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1246fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 1247fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 124899653d4eSeschrock } 1249fa94a07fSbrendan if (spa->spa_spares.sav_config) { 1250fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config); 1251fa94a07fSbrendan spa->spa_spares.sav_config = NULL; 1252fa94a07fSbrendan } 12532ce8af81SEric Schrock spa->spa_spares.sav_count = 0; 1254fa94a07fSbrendan 1255cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 1256cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1257fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1258cd0837ccSGeorge Wilson } 1259fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) { 1260fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs, 1261fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *)); 1262fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL; 1263fa94a07fSbrendan } 1264fa94a07fSbrendan if (spa->spa_l2cache.sav_config) { 1265fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config); 1266fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL; 126799653d4eSeschrock } 12682ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0; 126999653d4eSeschrock 1270ea8dc4b6Seschrock spa->spa_async_suspended = 0; 12718ad4d6ddSJeff Bonwick 12728704186eSDan McDonald if (spa->spa_comment != NULL) { 12738704186eSDan McDonald spa_strfree(spa->spa_comment); 12748704186eSDan McDonald spa->spa_comment = NULL; 12758704186eSDan McDonald } 12768704186eSDan McDonald 12778ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1278fa9e4066Sahrens } 1279fa9e4066Sahrens 128099653d4eSeschrock /* 128199653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for 128299653d4eSeschrock * this pool. When this is called, we have some form of basic information in 1283fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1284fa94a07fSbrendan * then re-generate a more complete list including status information. 128599653d4eSeschrock */ 128699653d4eSeschrock static void 128799653d4eSeschrock spa_load_spares(spa_t *spa) 128899653d4eSeschrock { 128999653d4eSeschrock nvlist_t **spares; 129099653d4eSeschrock uint_t nspares; 129199653d4eSeschrock int i; 129239c23413Seschrock vdev_t *vd, *tvd; 129399653d4eSeschrock 1294e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1295e14bb325SJeff Bonwick 129699653d4eSeschrock /* 129799653d4eSeschrock * First, close and free any existing spare vdevs. 129899653d4eSeschrock */ 1299fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 1300fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i]; 130139c23413Seschrock 130239c23413Seschrock /* Undo the call to spa_activate() below */ 1303c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1304c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare) 130539c23413Seschrock spa_spare_remove(tvd); 130639c23413Seschrock vdev_close(vd); 130739c23413Seschrock vdev_free(vd); 130899653d4eSeschrock } 130939c23413Seschrock 1310fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) 1311fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs, 1312fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *)); 131399653d4eSeschrock 1314fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL) 131599653d4eSeschrock nspares = 0; 131699653d4eSeschrock else 1317fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 131899653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 131999653d4eSeschrock 1320fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares; 1321fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL; 132299653d4eSeschrock 132399653d4eSeschrock if (nspares == 0) 132499653d4eSeschrock return; 132599653d4eSeschrock 132699653d4eSeschrock /* 132799653d4eSeschrock * Construct the array of vdevs, opening them to get status in the 132839c23413Seschrock * process. For each spare, there is potentially two different vdev_t 132939c23413Seschrock * structures associated with it: one in the list of spares (used only 133039c23413Seschrock * for basic validation purposes) and one in the active vdev 133139c23413Seschrock * configuration (if it's spared in). During this phase we open and 133239c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the 133339c23413Seschrock * active configuration, then we also mark this vdev as an active spare. 133499653d4eSeschrock */ 1335fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1336fa94a07fSbrendan KM_SLEEP); 1337fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) { 133899653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 133999653d4eSeschrock VDEV_ALLOC_SPARE) == 0); 134099653d4eSeschrock ASSERT(vd != NULL); 134199653d4eSeschrock 1342fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd; 134399653d4eSeschrock 1344c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1345c5904d13Seschrock B_FALSE)) != NULL) { 134639c23413Seschrock if (!tvd->vdev_isspare) 134739c23413Seschrock spa_spare_add(tvd); 134839c23413Seschrock 134939c23413Seschrock /* 135039c23413Seschrock * We only mark the spare active if we were successfully 135139c23413Seschrock * able to load the vdev. Otherwise, importing a pool 135239c23413Seschrock * with a bad active spare would result in strange 135339c23413Seschrock * behavior, because multiple pool would think the spare 135439c23413Seschrock * is actively in use. 135539c23413Seschrock * 135639c23413Seschrock * There is a vulnerability here to an equally bizarre 135739c23413Seschrock * circumstance, where a dead active spare is later 135839c23413Seschrock * brought back to life (onlined or otherwise). Given 135939c23413Seschrock * the rarity of this scenario, and the extra complexity 136039c23413Seschrock * it adds, we ignore the possibility. 136139c23413Seschrock */ 136239c23413Seschrock if (!vdev_is_dead(tvd)) 136339c23413Seschrock spa_spare_activate(tvd); 136439c23413Seschrock } 136539c23413Seschrock 1366e14bb325SJeff Bonwick vd->vdev_top = vd; 13676809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares; 1368e14bb325SJeff Bonwick 136999653d4eSeschrock if (vdev_open(vd) != 0) 137099653d4eSeschrock continue; 137199653d4eSeschrock 1372fa94a07fSbrendan if (vdev_validate_aux(vd) == 0) 1373fa94a07fSbrendan spa_spare_add(vd); 137499653d4eSeschrock } 137599653d4eSeschrock 137699653d4eSeschrock /* 137799653d4eSeschrock * Recompute the stashed list of spares, with status information 137899653d4eSeschrock * this time. 137999653d4eSeschrock */ 1380fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 138199653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0); 138299653d4eSeschrock 1383fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1384fa94a07fSbrendan KM_SLEEP); 1385fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 1386fa94a07fSbrendan spares[i] = vdev_config_generate(spa, 13873f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1388fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1389fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1390fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) 139199653d4eSeschrock nvlist_free(spares[i]); 1392fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1393fa94a07fSbrendan } 1394fa94a07fSbrendan 1395fa94a07fSbrendan /* 1396fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for 1397fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in 1398fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1399fa94a07fSbrendan * then re-generate a more complete list including status information. 1400fa94a07fSbrendan * Devices which are already active have their details maintained, and are 1401fa94a07fSbrendan * not re-opened. 1402fa94a07fSbrendan */ 1403fa94a07fSbrendan static void 1404fa94a07fSbrendan spa_load_l2cache(spa_t *spa) 1405fa94a07fSbrendan { 1406fa94a07fSbrendan nvlist_t **l2cache; 1407fa94a07fSbrendan uint_t nl2cache; 1408fa94a07fSbrendan int i, j, oldnvdevs; 1409573ca77eSGeorge Wilson uint64_t guid; 1410fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs; 1411fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 1412fa94a07fSbrendan 1413e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1414e14bb325SJeff Bonwick 1415fa94a07fSbrendan if (sav->sav_config != NULL) { 1416fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1417fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1418fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 1419fa94a07fSbrendan } else { 1420fa94a07fSbrendan nl2cache = 0; 1421d5285caeSGeorge Wilson newvdevs = NULL; 1422fa94a07fSbrendan } 1423fa94a07fSbrendan 1424fa94a07fSbrendan oldvdevs = sav->sav_vdevs; 1425fa94a07fSbrendan oldnvdevs = sav->sav_count; 1426fa94a07fSbrendan sav->sav_vdevs = NULL; 1427fa94a07fSbrendan sav->sav_count = 0; 1428fa94a07fSbrendan 1429fa94a07fSbrendan /* 1430fa94a07fSbrendan * Process new nvlist of vdevs. 1431fa94a07fSbrendan */ 1432fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 1433fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1434fa94a07fSbrendan &guid) == 0); 1435fa94a07fSbrendan 1436fa94a07fSbrendan newvdevs[i] = NULL; 1437fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) { 1438fa94a07fSbrendan vd = oldvdevs[j]; 1439fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) { 1440fa94a07fSbrendan /* 1441fa94a07fSbrendan * Retain previous vdev for add/remove ops. 1442fa94a07fSbrendan */ 1443fa94a07fSbrendan newvdevs[i] = vd; 1444fa94a07fSbrendan oldvdevs[j] = NULL; 1445fa94a07fSbrendan break; 1446fa94a07fSbrendan } 1447fa94a07fSbrendan } 1448fa94a07fSbrendan 1449fa94a07fSbrendan if (newvdevs[i] == NULL) { 1450fa94a07fSbrendan /* 1451fa94a07fSbrendan * Create new vdev 1452fa94a07fSbrendan */ 1453fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1454fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0); 1455fa94a07fSbrendan ASSERT(vd != NULL); 1456fa94a07fSbrendan newvdevs[i] = vd; 1457fa94a07fSbrendan 1458fa94a07fSbrendan /* 1459fa94a07fSbrendan * Commit this vdev as an l2cache device, 1460fa94a07fSbrendan * even if it fails to open. 1461fa94a07fSbrendan */ 1462fa94a07fSbrendan spa_l2cache_add(vd); 1463fa94a07fSbrendan 1464c5904d13Seschrock vd->vdev_top = vd; 1465c5904d13Seschrock vd->vdev_aux = sav; 1466c5904d13Seschrock 1467c5904d13Seschrock spa_l2cache_activate(vd); 1468c5904d13Seschrock 1469fa94a07fSbrendan if (vdev_open(vd) != 0) 1470fa94a07fSbrendan continue; 1471fa94a07fSbrendan 1472fa94a07fSbrendan (void) vdev_validate_aux(vd); 1473fa94a07fSbrendan 1474573ca77eSGeorge Wilson if (!vdev_is_dead(vd)) 1475573ca77eSGeorge Wilson l2arc_add_vdev(spa, vd); 1476fa94a07fSbrendan } 1477fa94a07fSbrendan } 1478fa94a07fSbrendan 1479fa94a07fSbrendan /* 1480fa94a07fSbrendan * Purge vdevs that were dropped 1481fa94a07fSbrendan */ 1482fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) { 1483fa94a07fSbrendan uint64_t pool; 1484fa94a07fSbrendan 1485fa94a07fSbrendan vd = oldvdevs[i]; 1486fa94a07fSbrendan if (vd != NULL) { 1487cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache); 1488cd0837ccSGeorge Wilson 14898ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 14908ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 1491fa94a07fSbrendan l2arc_remove_vdev(vd); 1492cd0837ccSGeorge Wilson vdev_clear_stats(vd); 1493cd0837ccSGeorge Wilson vdev_free(vd); 1494fa94a07fSbrendan } 1495fa94a07fSbrendan } 1496fa94a07fSbrendan 1497fa94a07fSbrendan if (oldvdevs) 1498fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1499fa94a07fSbrendan 1500fa94a07fSbrendan if (sav->sav_config == NULL) 1501fa94a07fSbrendan goto out; 1502fa94a07fSbrendan 1503fa94a07fSbrendan sav->sav_vdevs = newvdevs; 1504fa94a07fSbrendan sav->sav_count = (int)nl2cache; 1505fa94a07fSbrendan 1506fa94a07fSbrendan /* 1507fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status 1508fa94a07fSbrendan * information this time. 1509fa94a07fSbrendan */ 1510fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1511fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 1512fa94a07fSbrendan 1513fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1514fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1515fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa, 15163f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1517fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1518fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1519fa94a07fSbrendan out: 1520fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 1521fa94a07fSbrendan nvlist_free(l2cache[i]); 1522fa94a07fSbrendan if (sav->sav_count) 1523fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *)); 152499653d4eSeschrock } 152599653d4eSeschrock 152699653d4eSeschrock static int 152799653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 152899653d4eSeschrock { 152999653d4eSeschrock dmu_buf_t *db; 153099653d4eSeschrock char *packed = NULL; 153199653d4eSeschrock size_t nvsize = 0; 153299653d4eSeschrock int error; 153399653d4eSeschrock *value = NULL; 153499653d4eSeschrock 153599653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 153699653d4eSeschrock nvsize = *(uint64_t *)db->db_data; 153799653d4eSeschrock dmu_buf_rele(db, FTAG); 153899653d4eSeschrock 153999653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP); 15407bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 15417bfdf011SNeil Perrin DMU_READ_PREFETCH); 154299653d4eSeschrock if (error == 0) 154399653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0); 154499653d4eSeschrock kmem_free(packed, nvsize); 154599653d4eSeschrock 154699653d4eSeschrock return (error); 154799653d4eSeschrock } 154899653d4eSeschrock 15493d7072f8Seschrock /* 15503d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a 15513d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed. 15523d7072f8Seschrock */ 15533d7072f8Seschrock static void 15543d7072f8Seschrock spa_check_removed(vdev_t *vd) 15553d7072f8Seschrock { 1556573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 15573d7072f8Seschrock spa_check_removed(vd->vdev_child[c]); 15583d7072f8Seschrock 15593d7072f8Seschrock if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 15603d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd); 15613d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 15623d7072f8Seschrock } 15633d7072f8Seschrock } 15643d7072f8Seschrock 1565e6ca193dSGeorge Wilson /* 15664b964adaSGeorge Wilson * Validate the current config against the MOS config 1567e6ca193dSGeorge Wilson */ 15684b964adaSGeorge Wilson static boolean_t 15694b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config) 1570e6ca193dSGeorge Wilson { 15714b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev; 15724b964adaSGeorge Wilson nvlist_t *nv; 15734b964adaSGeorge Wilson 15744b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); 15754b964adaSGeorge Wilson 15764b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 15774b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 15784b964adaSGeorge Wilson 15794b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); 1580e6ca193dSGeorge Wilson 158188ecc943SGeorge Wilson /* 15824b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional 15834b964adaSGeorge Wilson * diagnostic information about missing devices in this config. 15844b964adaSGeorge Wilson * We'll pass this up to the user for further processing. 158588ecc943SGeorge Wilson */ 15864b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 15874b964adaSGeorge Wilson nvlist_t **child, *nv; 15884b964adaSGeorge Wilson uint64_t idx = 0; 15894b964adaSGeorge Wilson 15904b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 15914b964adaSGeorge Wilson KM_SLEEP); 15924b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1593e6ca193dSGeorge Wilson 15944b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 15954b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 15964b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 15974b964adaSGeorge Wilson 15984b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 15994b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops && 16004b964adaSGeorge Wilson mtvd->vdev_islog) 16014b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd, 16024b964adaSGeorge Wilson B_FALSE, 0); 16034b964adaSGeorge Wilson } 16044b964adaSGeorge Wilson 16054b964adaSGeorge Wilson if (idx) { 16064b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv, 16074b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0); 16084b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info, 16094b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); 16104b964adaSGeorge Wilson 16114b964adaSGeorge Wilson for (int i = 0; i < idx; i++) 16124b964adaSGeorge Wilson nvlist_free(child[i]); 16134b964adaSGeorge Wilson } 16144b964adaSGeorge Wilson nvlist_free(nv); 16154b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **)); 16164b964adaSGeorge Wilson } 16174b964adaSGeorge Wilson 16184b964adaSGeorge Wilson /* 16194b964adaSGeorge Wilson * Compare the root vdev tree with the information we have 16204b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev 16214b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd). 16224b964adaSGeorge Wilson */ 162388ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 16244b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 16254b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c]; 16264b964adaSGeorge Wilson 16274b964adaSGeorge Wilson /* 16284b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration. 16294b964adaSGeorge Wilson * If we find that the MOS config has more accurate information 16304b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead. 16314b964adaSGeorge Wilson */ 16324b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops && 16334b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) { 16344b964adaSGeorge Wilson 16354b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) 16364b964adaSGeorge Wilson continue; 16374b964adaSGeorge Wilson 16384b964adaSGeorge Wilson /* 16394b964adaSGeorge Wilson * Device specific actions. 16404b964adaSGeorge Wilson */ 16414b964adaSGeorge Wilson if (mtvd->vdev_islog) { 16424b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR); 16434b964adaSGeorge Wilson } else { 16444b964adaSGeorge Wilson /* 16454b964adaSGeorge Wilson * XXX - once we have 'readonly' pool 16464b964adaSGeorge Wilson * support we should be able to handle 16474b964adaSGeorge Wilson * missing data devices by transitioning 16484b964adaSGeorge Wilson * the pool to readonly. 16494b964adaSGeorge Wilson */ 16504b964adaSGeorge Wilson continue; 16514b964adaSGeorge Wilson } 16524b964adaSGeorge Wilson 16534b964adaSGeorge Wilson /* 16544b964adaSGeorge Wilson * Swap the missing vdev with the data we were 16554b964adaSGeorge Wilson * able to obtain from the MOS config. 16564b964adaSGeorge Wilson */ 16574b964adaSGeorge Wilson vdev_remove_child(rvd, tvd); 16584b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd); 16594b964adaSGeorge Wilson 16604b964adaSGeorge Wilson vdev_add_child(rvd, mtvd); 16614b964adaSGeorge Wilson vdev_add_child(mrvd, tvd); 16624b964adaSGeorge Wilson 16634b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 16644b964adaSGeorge Wilson vdev_load(mtvd); 16654b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 16664b964adaSGeorge Wilson 16674b964adaSGeorge Wilson vdev_reopen(rvd); 16684b964adaSGeorge Wilson } else if (mtvd->vdev_islog) { 16694b964adaSGeorge Wilson /* 16704b964adaSGeorge Wilson * Load the slog device's state from the MOS config 16714b964adaSGeorge Wilson * since it's possible that the label does not 16724b964adaSGeorge Wilson * contain the most up-to-date information. 16734b964adaSGeorge Wilson */ 16744b964adaSGeorge Wilson vdev_load_log_state(tvd, mtvd); 16754b964adaSGeorge Wilson vdev_reopen(tvd); 16764b964adaSGeorge Wilson } 1677e6ca193dSGeorge Wilson } 16784b964adaSGeorge Wilson vdev_free(mrvd); 167988ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG); 16804b964adaSGeorge Wilson 16814b964adaSGeorge Wilson /* 16824b964adaSGeorge Wilson * Ensure we were able to validate the config. 16834b964adaSGeorge Wilson */ 16844b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); 1685e6ca193dSGeorge Wilson } 1686e6ca193dSGeorge Wilson 1687b87f3af3Sperrin /* 1688b87f3af3Sperrin * Check for missing log devices 1689b87f3af3Sperrin */ 1690*3b2aab18SMatthew Ahrens static boolean_t 1691b87f3af3Sperrin spa_check_logs(spa_t *spa) 1692b87f3af3Sperrin { 1693*3b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 1694*3b2aab18SMatthew Ahrens 1695b87f3af3Sperrin switch (spa->spa_log_state) { 1696b87f3af3Sperrin case SPA_LOG_MISSING: 1697b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1698b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1699*3b2aab18SMatthew Ahrens rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain, 1700*3b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN) != 0); 1701*3b2aab18SMatthew Ahrens if (rv) 17021195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1703b87f3af3Sperrin break; 1704b87f3af3Sperrin } 1705*3b2aab18SMatthew Ahrens return (rv); 1706b87f3af3Sperrin } 1707b87f3af3Sperrin 17081195e687SMark J Musante static boolean_t 17091195e687SMark J Musante spa_passivate_log(spa_t *spa) 17101195e687SMark J Musante { 17111195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17121195e687SMark J Musante boolean_t slog_found = B_FALSE; 17131195e687SMark J Musante 17141195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17151195e687SMark J Musante 17161195e687SMark J Musante if (!spa_has_slogs(spa)) 17171195e687SMark J Musante return (B_FALSE); 17181195e687SMark J Musante 17191195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17201195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17211195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17221195e687SMark J Musante 17231195e687SMark J Musante if (tvd->vdev_islog) { 17241195e687SMark J Musante metaslab_group_passivate(mg); 17251195e687SMark J Musante slog_found = B_TRUE; 17261195e687SMark J Musante } 17271195e687SMark J Musante } 17281195e687SMark J Musante 17291195e687SMark J Musante return (slog_found); 17301195e687SMark J Musante } 17311195e687SMark J Musante 17321195e687SMark J Musante static void 17331195e687SMark J Musante spa_activate_log(spa_t *spa) 17341195e687SMark J Musante { 17351195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17361195e687SMark J Musante 17371195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17381195e687SMark J Musante 17391195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17401195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17411195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17421195e687SMark J Musante 17431195e687SMark J Musante if (tvd->vdev_islog) 17441195e687SMark J Musante metaslab_group_activate(mg); 17451195e687SMark J Musante } 17461195e687SMark J Musante } 17471195e687SMark J Musante 17481195e687SMark J Musante int 17491195e687SMark J Musante spa_offline_log(spa_t *spa) 17501195e687SMark J Musante { 1751*3b2aab18SMatthew Ahrens int error; 17521195e687SMark J Musante 1753*3b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 1754*3b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 1755*3b2aab18SMatthew Ahrens if (error == 0) { 17561195e687SMark J Musante /* 17571195e687SMark J Musante * We successfully offlined the log device, sync out the 17581195e687SMark J Musante * current txg so that the "stubby" block can be removed 17591195e687SMark J Musante * by zil_sync(). 17601195e687SMark J Musante */ 17611195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 17621195e687SMark J Musante } 17631195e687SMark J Musante return (error); 17641195e687SMark J Musante } 17651195e687SMark J Musante 1766b693757aSEric Schrock static void 1767b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1768b693757aSEric Schrock { 1769b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1770b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1771b693757aSEric Schrock } 1772b693757aSEric Schrock 1773b24ab676SJeff Bonwick void 1774b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1775b24ab676SJeff Bonwick { 1776b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1777b24ab676SJeff Bonwick 1778b24ab676SJeff Bonwick if (zio->io_error) 1779b24ab676SJeff Bonwick return; 1780b24ab676SJeff Bonwick 1781b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1782b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1783b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1784b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1785b24ab676SJeff Bonwick } 1786b24ab676SJeff Bonwick 1787468c413aSTim Haley typedef struct spa_load_error { 1788c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1789468c413aSTim Haley uint64_t sle_data_count; 1790468c413aSTim Haley } spa_load_error_t; 1791468c413aSTim Haley 1792468c413aSTim Haley static void 1793468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1794468c413aSTim Haley { 1795468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1796468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1797468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1798468c413aSTim Haley int error = zio->io_error; 1799468c413aSTim Haley 1800468c413aSTim Haley if (error) { 1801ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1802468c413aSTim Haley type != DMU_OT_INTENT_LOG) 1803c8ee1847SVictor Latushkin atomic_add_64(&sle->sle_meta_count, 1); 1804468c413aSTim Haley else 1805468c413aSTim Haley atomic_add_64(&sle->sle_data_count, 1); 1806468c413aSTim Haley } 1807468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1808468c413aSTim Haley } 1809468c413aSTim Haley 1810468c413aSTim Haley /*ARGSUSED*/ 1811468c413aSTim Haley static int 1812b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 18131b912ec7SGeorge Wilson const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1814468c413aSTim Haley { 1815468c413aSTim Haley if (bp != NULL) { 1816468c413aSTim Haley zio_t *rio = arg; 1817468c413aSTim Haley size_t size = BP_GET_PSIZE(bp); 1818468c413aSTim Haley void *data = zio_data_buf_alloc(size); 1819468c413aSTim Haley 1820468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size, 1821468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1822468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1823468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1824468c413aSTim Haley } 1825468c413aSTim Haley return (0); 1826468c413aSTim Haley } 1827468c413aSTim Haley 1828468c413aSTim Haley static int 1829468c413aSTim Haley spa_load_verify(spa_t *spa) 1830468c413aSTim Haley { 1831468c413aSTim Haley zio_t *rio; 1832468c413aSTim Haley spa_load_error_t sle = { 0 }; 1833468c413aSTim Haley zpool_rewind_policy_t policy; 1834468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1835468c413aSTim Haley int error; 1836468c413aSTim Haley 1837c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1838c8ee1847SVictor Latushkin 1839c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1840c8ee1847SVictor Latushkin return (0); 1841c8ee1847SVictor Latushkin 1842468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1843468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1844468c413aSTim Haley 1845bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg, 1846bbfd46c4SJeff Bonwick TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 1847468c413aSTim Haley 1848468c413aSTim Haley (void) zio_wait(rio); 1849468c413aSTim Haley 1850c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1851468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1852468c413aSTim Haley 1853c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1854468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 18554b964adaSGeorge Wilson int64_t loss = 0; 18564b964adaSGeorge Wilson 1857468c413aSTim Haley verify_ok = B_TRUE; 1858468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1859468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 18604b964adaSGeorge Wilson 18614b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 18624b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 18634b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 18644b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 18654b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 18664b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 18674b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1868a33cae98STim Haley } else { 1869a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1870468c413aSTim Haley } 1871468c413aSTim Haley 1872468c413aSTim Haley if (error) { 1873468c413aSTim Haley if (error != ENXIO && error != EIO) 1874468c413aSTim Haley error = EIO; 1875468c413aSTim Haley return (error); 1876468c413aSTim Haley } 1877468c413aSTim Haley 1878468c413aSTim Haley return (verify_ok ? 0 : EIO); 1879468c413aSTim Haley } 1880468c413aSTim Haley 18811195e687SMark J Musante /* 18821195e687SMark J Musante * Find a value in the pool props object. 18831195e687SMark J Musante */ 18841195e687SMark J Musante static void 18851195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 18861195e687SMark J Musante { 18871195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 18881195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 18891195e687SMark J Musante } 18901195e687SMark J Musante 18911195e687SMark J Musante /* 18921195e687SMark J Musante * Find a value in the pool directory object. 18931195e687SMark J Musante */ 18941195e687SMark J Musante static int 18951195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 18961195e687SMark J Musante { 18971195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 18981195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 18991195e687SMark J Musante } 19001195e687SMark J Musante 19011195e687SMark J Musante static int 19021195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 19031195e687SMark J Musante { 19041195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 19051195e687SMark J Musante return (err); 19061195e687SMark J Musante } 19071195e687SMark J Musante 19081195e687SMark J Musante /* 19091195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 19101195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 19111195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 19121195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 19131195e687SMark J Musante * 19141195e687SMark J Musante * This function determines what to do with that list: either rejoin 19151195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 19161195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 19171195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 19181195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 19191195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 19201195e687SMark J Musante * 1921d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1922d41c4376SMark J Musante * the original pool. 19231195e687SMark J Musante */ 19241195e687SMark J Musante static void 19251195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 19261195e687SMark J Musante { 19271195e687SMark J Musante uint_t extracted; 19281195e687SMark J Musante uint64_t *glist; 19291195e687SMark J Musante uint_t i, gcount; 19301195e687SMark J Musante nvlist_t *nvl; 19311195e687SMark J Musante vdev_t **vd; 19321195e687SMark J Musante boolean_t attempt_reopen; 19331195e687SMark J Musante 19341195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 19351195e687SMark J Musante return; 19361195e687SMark J Musante 19371195e687SMark J Musante /* check that the config is complete */ 19381195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 19391195e687SMark J Musante &glist, &gcount) != 0) 19401195e687SMark J Musante return; 19411195e687SMark J Musante 19421195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 19431195e687SMark J Musante 19441195e687SMark J Musante /* attempt to online all the vdevs & validate */ 19451195e687SMark J Musante attempt_reopen = B_TRUE; 19461195e687SMark J Musante for (i = 0; i < gcount; i++) { 19471195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 19481195e687SMark J Musante continue; 19491195e687SMark J Musante 19501195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 19511195e687SMark J Musante if (vd[i] == NULL) { 19521195e687SMark J Musante /* 19531195e687SMark J Musante * Don't bother attempting to reopen the disks; 19541195e687SMark J Musante * just do the split. 19551195e687SMark J Musante */ 19561195e687SMark J Musante attempt_reopen = B_FALSE; 19571195e687SMark J Musante } else { 19581195e687SMark J Musante /* attempt to re-online it */ 19591195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 19601195e687SMark J Musante } 19611195e687SMark J Musante } 19621195e687SMark J Musante 19631195e687SMark J Musante if (attempt_reopen) { 19641195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 19651195e687SMark J Musante 19661195e687SMark J Musante /* check each device to see what state it's in */ 19671195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 19681195e687SMark J Musante if (vd[i] != NULL && 19691195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 19701195e687SMark J Musante break; 19711195e687SMark J Musante ++extracted; 19721195e687SMark J Musante } 19731195e687SMark J Musante } 19741195e687SMark J Musante 19751195e687SMark J Musante /* 19761195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 19771195e687SMark J Musante * even attempted to look at them, then we split them off for 19781195e687SMark J Musante * good. 19791195e687SMark J Musante */ 19801195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 19811195e687SMark J Musante for (i = 0; i < gcount; i++) 19821195e687SMark J Musante if (vd[i] != NULL) 19831195e687SMark J Musante vdev_split(vd[i]); 19841195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 19851195e687SMark J Musante } 19861195e687SMark J Musante 19871195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 19881195e687SMark J Musante } 19891195e687SMark J Musante 19901195e687SMark J Musante static int 19911195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 19921195e687SMark J Musante boolean_t mosconfig) 19931195e687SMark J Musante { 19941195e687SMark J Musante nvlist_t *config = spa->spa_config; 19951195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 19968704186eSDan McDonald char *comment; 19971195e687SMark J Musante int error; 19981195e687SMark J Musante uint64_t pool_guid; 19991195e687SMark J Musante nvlist_t *nvl; 20001195e687SMark J Musante 20011195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 20021195e687SMark J Musante return (EINVAL); 20031195e687SMark J Musante 20048704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 20058704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 20068704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 20078704186eSDan McDonald 20081195e687SMark J Musante /* 20091195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 20101195e687SMark J Musante * it's not present treat it as the initial version. 20111195e687SMark J Musante */ 20121195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 20131195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 20141195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 20151195e687SMark J Musante 20161195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 20171195e687SMark J Musante &spa->spa_config_txg); 20181195e687SMark J Musante 20191195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 20201195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 20211195e687SMark J Musante error = EEXIST; 20221195e687SMark J Musante } else { 2023e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 20241195e687SMark J Musante 20251195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 20261195e687SMark J Musante &nvl) == 0) { 20271195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 20281195e687SMark J Musante KM_SLEEP) == 0); 20291195e687SMark J Musante } 20301195e687SMark J Musante 2031ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2032ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2033ad135b5dSChristopher Siden 203411027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 20351195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 20361195e687SMark J Musante mosconfig, &ereport); 20371195e687SMark J Musante } 20381195e687SMark J Musante 20391195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 204011027bc7STim Haley if (error) { 204111027bc7STim Haley if (error != EEXIST) { 204211027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 204311027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 204411027bc7STim Haley } 204511027bc7STim Haley if (error != EBADF) { 204611027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 204711027bc7STim Haley } 204811027bc7STim Haley } 20491195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 20501195e687SMark J Musante spa->spa_ena = 0; 20511195e687SMark J Musante 20521195e687SMark J Musante return (error); 20531195e687SMark J Musante } 20541195e687SMark J Musante 2055fa9e4066Sahrens /* 2056fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2057ea8dc4b6Seschrock * source of configuration information. 2058fa9e4066Sahrens */ 2059fa9e4066Sahrens static int 20601195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 20611195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 20621195e687SMark J Musante char **ereport) 2063fa9e4066Sahrens { 2064fa9e4066Sahrens int error = 0; 2065871a9500SMark J Musante nvlist_t *nvroot = NULL; 2066ad135b5dSChristopher Siden nvlist_t *label; 2067fa9e4066Sahrens vdev_t *rvd; 2068fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 20694b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 20708ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 20711195e687SMark J Musante int parse; 2072cde58dbcSMatthew Ahrens uint64_t obj; 2073ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2074fa9e4066Sahrens 20758ad4d6ddSJeff Bonwick /* 20768ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 20778ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 20788ad4d6ddSJeff Bonwick */ 20798ad4d6ddSJeff Bonwick if (!mosconfig) 20808ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 20818ad4d6ddSJeff Bonwick 2082e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2083e14bb325SJeff Bonwick 2084ea8dc4b6Seschrock spa->spa_load_state = state; 20850373e76bSbonwick 20861195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 20871195e687SMark J Musante return (EINVAL); 2088fa9e4066Sahrens 20891195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 20901195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2091b5989ec7Seschrock 209254d692b7SGeorge Wilson /* 209354d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 209454d692b7SGeorge Wilson */ 209525f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 209625f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 209754d692b7SGeorge Wilson 2098fa9e4066Sahrens /* 209999653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 210099653d4eSeschrock * value that will be returned by spa_version() since parsing the 210199653d4eSeschrock * configuration requires knowing the version number. 2102fa9e4066Sahrens */ 2103e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21041195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2105e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2106fa9e4066Sahrens 210799653d4eSeschrock if (error != 0) 21081195e687SMark J Musante return (error); 2109fa9e4066Sahrens 21100e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 21111195e687SMark J Musante 21121195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 21131195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 21141195e687SMark J Musante } 2115fa9e4066Sahrens 2116fa9e4066Sahrens /* 2117fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2118fa9e4066Sahrens */ 2119e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21200bf246f5Smc error = vdev_open(rvd); 2121e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 21220bf246f5Smc if (error != 0) 21231195e687SMark J Musante return (error); 2124fa9e4066Sahrens 2125560e6e96Seschrock /* 212677e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 212777e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 212877e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 21291195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 213077e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 213177e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 213277e3a39cSMark J Musante * the vdev config. 21331195e687SMark J Musante * 21341195e687SMark J Musante * If we're assembling a new pool that's been split off from an 21351195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 21361195e687SMark J Musante * validation for now. 2137560e6e96Seschrock */ 21381195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 21391195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2140d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 21411195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2142560e6e96Seschrock 21431195e687SMark J Musante if (error != 0) 21441195e687SMark J Musante return (error); 21451195e687SMark J Musante 21461195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 21471195e687SMark J Musante return (ENXIO); 2148560e6e96Seschrock } 2149560e6e96Seschrock 2150fa9e4066Sahrens /* 2151fa9e4066Sahrens * Find the best uberblock. 2152fa9e4066Sahrens */ 2153ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2154fa9e4066Sahrens 2155fa9e4066Sahrens /* 2156fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2157fa9e4066Sahrens */ 2158ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2159ad135b5dSChristopher Siden nvlist_free(label); 21601195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2161ad135b5dSChristopher Siden } 2162ea8dc4b6Seschrock 2163ea8dc4b6Seschrock /* 2164ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2165ea8dc4b6Seschrock */ 2166ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2167ad135b5dSChristopher Siden nvlist_free(label); 21681195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2169ad135b5dSChristopher Siden } 2170ad135b5dSChristopher Siden 2171ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2172ad135b5dSChristopher Siden nvlist_t *features; 2173ad135b5dSChristopher Siden 2174ad135b5dSChristopher Siden /* 2175ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2176ad135b5dSChristopher Siden * MOS in the label, return failure. 2177ad135b5dSChristopher Siden */ 2178ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2179ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2180ad135b5dSChristopher Siden nvlist_free(label); 2181ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2182ad135b5dSChristopher Siden ENXIO)); 2183ad135b5dSChristopher Siden } 2184ad135b5dSChristopher Siden 2185ad135b5dSChristopher Siden /* 2186ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2187ad135b5dSChristopher Siden * from the label. 2188ad135b5dSChristopher Siden */ 2189ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2190ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2191ad135b5dSChristopher Siden } 2192ad135b5dSChristopher Siden 2193ad135b5dSChristopher Siden nvlist_free(label); 2194ad135b5dSChristopher Siden 2195ad135b5dSChristopher Siden /* 2196ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2197ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2198ad135b5dSChristopher Siden * cannot open a pool. 2199ad135b5dSChristopher Siden */ 2200ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2201ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2202ad135b5dSChristopher Siden 2203ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2204ad135b5dSChristopher Siden 0); 2205ad135b5dSChristopher Siden 2206ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2207ad135b5dSChristopher Siden NULL); nvp != NULL; 2208ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2209ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2210ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2211ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2212ad135b5dSChristopher Siden } 2213ad135b5dSChristopher Siden } 2214ad135b5dSChristopher Siden 2215ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2216ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2217ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2218ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2219ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2220ad135b5dSChristopher Siden ENOTSUP)); 2221ad135b5dSChristopher Siden } 2222ad135b5dSChristopher Siden 2223ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2224ad135b5dSChristopher Siden } 2225fa9e4066Sahrens 2226fa9e4066Sahrens /* 2227fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 22284b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 22294b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 22304b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 22314b964adaSGeorge Wilson * can handle missing vdevs. 2232fa9e4066Sahrens */ 22334b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 22344b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 22351195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 22361195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 22371195e687SMark J Musante 22381195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 22391195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22401195e687SMark J Musante spa_try_repair(spa, config); 22411195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 22421195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 22431195e687SMark J Musante spa->spa_config_splitting = NULL; 2244fa9e4066Sahrens } 2245fa9e4066Sahrens 2246fa9e4066Sahrens /* 2247fa9e4066Sahrens * Initialize internal SPA structures. 2248fa9e4066Sahrens */ 2249fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2250fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2251468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2252c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2253468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2254468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2255b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 22563f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2257b24ab676SJeff Bonwick 2258ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 22591195e687SMark J Musante if (error) 22601195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2261fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2262fa9e4066Sahrens 22631195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 22641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2265fa9e4066Sahrens 2266ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2267ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 226857221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2269ad135b5dSChristopher Siden 2270ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2271ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2272ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2273ad135b5dSChristopher Siden } 2274ad135b5dSChristopher Siden 2275ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2276ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2277ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2278ad135b5dSChristopher Siden } 2279ad135b5dSChristopher Siden 2280ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2281ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2282ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2283ad135b5dSChristopher Siden } 2284ad135b5dSChristopher Siden 228557221772SChristopher Siden enabled_feat = fnvlist_alloc(); 228657221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2287ad135b5dSChristopher Siden 2288ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2289ad135b5dSChristopher Siden spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj, 229057221772SChristopher Siden unsup_feat, enabled_feat)) 2291ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2292ad135b5dSChristopher Siden 2293ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 2294ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2295ad135b5dSChristopher Siden spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj, 229657221772SChristopher Siden unsup_feat, enabled_feat)) { 2297ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 229857221772SChristopher Siden } 2299ad135b5dSChristopher Siden } 2300ad135b5dSChristopher Siden 230157221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 230257221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 230357221772SChristopher Siden 2304ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 230557221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 230657221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2307ad135b5dSChristopher Siden } 2308ad135b5dSChristopher Siden 230957221772SChristopher Siden fnvlist_free(enabled_feat); 231057221772SChristopher Siden fnvlist_free(unsup_feat); 2311ad135b5dSChristopher Siden 2312ad135b5dSChristopher Siden if (!missing_feat_read) { 2313ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2314ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2315ad135b5dSChristopher Siden } 2316ad135b5dSChristopher Siden 2317ad135b5dSChristopher Siden /* 2318ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2319ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2320ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2321ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2322ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2323ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2324ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2325ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2326ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2327ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2328ad135b5dSChristopher Siden * available for open in read-only mode. 2329ad135b5dSChristopher Siden * 2330ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2331ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2332ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2333ad135b5dSChristopher Siden * userland in order to know whether to display the 2334ad135b5dSChristopher Siden * abovementioned note. 2335ad135b5dSChristopher Siden */ 2336ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2337ad135b5dSChristopher Siden spa_writeable(spa))) { 2338ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2339ad135b5dSChristopher Siden ENOTSUP)); 2340ad135b5dSChristopher Siden } 2341ad135b5dSChristopher Siden } 2342ad135b5dSChristopher Siden 2343ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2344ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2345ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2346ad135b5dSChristopher Siden if (error != 0) 2347ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2348ad135b5dSChristopher Siden 2349fa9e4066Sahrens if (!mosconfig) { 235095173954Sek uint64_t hostid; 2351871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2352871a9500SMark J Musante 2353871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2354871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2355fa9e4066Sahrens 235688ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 235777650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 235895173954Sek char *hostname; 235995173954Sek unsigned long myhostid = 0; 236095173954Sek 236188ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 236295173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 236395173954Sek 23645679c89fSjv #ifdef _KERNEL 23655679c89fSjv myhostid = zone_get_hostid(NULL); 23665679c89fSjv #else /* _KERNEL */ 23675679c89fSjv /* 23685679c89fSjv * We're emulating the system's hostid in userland, so 23695679c89fSjv * we can't use zone_get_hostid(). 23705679c89fSjv */ 237195173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 23725679c89fSjv #endif /* _KERNEL */ 237317194a52Slling if (hostid != 0 && myhostid != 0 && 23745679c89fSjv hostid != myhostid) { 2375871a9500SMark J Musante nvlist_free(nvconfig); 237695173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 237795173954Sek "loaded as it was last accessed by " 237877650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2379654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2380e14bb325SJeff Bonwick spa_name(spa), hostname, 238195173954Sek (unsigned long)hostid); 23821195e687SMark J Musante return (EBADF); 238395173954Sek } 238495173954Sek } 2385c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2386c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2387c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2388c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 238995173954Sek 239088ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2391fa9e4066Sahrens spa_unload(spa); 2392fa9e4066Sahrens spa_deactivate(spa); 23938ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2394fa9e4066Sahrens 23951195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2396fa9e4066Sahrens } 2397fa9e4066Sahrens 2398cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2399cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2400cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2401cde58dbcSMatthew Ahrens if (error != 0) 24021195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2403fa9e4066Sahrens 240499653d4eSeschrock /* 240599653d4eSeschrock * Load the bit that tells us to use the new accounting function 240699653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 240799653d4eSeschrock * be present. 240899653d4eSeschrock */ 24091195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 24101195e687SMark J Musante if (error != 0 && error != ENOENT) 24111195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 241299653d4eSeschrock 24133f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 24143f9d6ad7SLin Ling &spa->spa_creation_version); 24153f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 24163f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24173f9d6ad7SLin Ling 2418fa9e4066Sahrens /* 2419ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2420ea8dc4b6Seschrock * not be present. 2421fa9e4066Sahrens */ 24221195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 24231195e687SMark J Musante if (error != 0 && error != ENOENT) 24241195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2425ea8dc4b6Seschrock 24261195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 24271195e687SMark J Musante &spa->spa_errlog_scrub); 24281195e687SMark J Musante if (error != 0 && error != ENOENT) 24291195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2430ea8dc4b6Seschrock 243106eeb2adSek /* 243206eeb2adSek * Load the history object. If we have an older pool, this 243306eeb2adSek * will not be present. 243406eeb2adSek */ 24351195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 24361195e687SMark J Musante if (error != 0 && error != ENOENT) 24371195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24381195e687SMark J Musante 24391195e687SMark J Musante /* 24401195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 24411195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 24421195e687SMark J Musante * devices. 24431195e687SMark J Musante */ 244406eeb2adSek 244599653d4eSeschrock /* 244699653d4eSeschrock * Load any hot spares for this pool. 244799653d4eSeschrock */ 24481195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 24491195e687SMark J Musante if (error != 0 && error != ENOENT) 24501195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24511195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2452e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2453fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 24541195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 24551195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 245699653d4eSeschrock 2457e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 245899653d4eSeschrock spa_load_spares(spa); 2459e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24601195e687SMark J Musante } else if (error == 0) { 24611195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 246299653d4eSeschrock } 246399653d4eSeschrock 2464fa94a07fSbrendan /* 2465fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2466fa94a07fSbrendan */ 24671195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2468fa94a07fSbrendan &spa->spa_l2cache.sav_object); 24691195e687SMark J Musante if (error != 0 && error != ENOENT) 24701195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24711195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2472fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2473fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 24741195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 24751195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2476fa94a07fSbrendan 2477e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2478fa94a07fSbrendan spa_load_l2cache(spa); 2479e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24801195e687SMark J Musante } else if (error == 0) { 24811195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2482fa94a07fSbrendan } 2483fa94a07fSbrendan 2484990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2485ecd6cf80Smarks 24861195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 24871195e687SMark J Musante if (error && error != ENOENT) 24881195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2489b1b8ab34Slling 2490b1b8ab34Slling if (error == 0) { 24911195e687SMark J Musante uint64_t autoreplace; 24921195e687SMark J Musante 24931195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 24941195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 24951195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 24961195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 24971195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 24981195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 24991195e687SMark J Musante &spa->spa_dedup_ditto); 25001195e687SMark J Musante 2501b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2502b1b8ab34Slling } 2503b1b8ab34Slling 25043d7072f8Seschrock /* 25053d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 25063d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 25073d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 25083d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 25093d7072f8Seschrock * over. 25103d7072f8Seschrock */ 2511b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 25123d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2513b693757aSEric Schrock /* 2514b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2515b693757aSEric Schrock * at this point we're using the spare definitions from 2516b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2517b693757aSEric Schrock */ 2518b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2519b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2520b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2521b693757aSEric Schrock } 2522b693757aSEric Schrock } 25233d7072f8Seschrock 2524ea8dc4b6Seschrock /* 2525560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2526ea8dc4b6Seschrock */ 2527560e6e96Seschrock vdev_load(rvd); 25280373e76bSbonwick 2529fa9e4066Sahrens /* 2530fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2531fa9e4066Sahrens */ 2532e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2533fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2534e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2535fa9e4066Sahrens 2536b24ab676SJeff Bonwick /* 2537b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2538b24ab676SJeff Bonwick */ 2539b24ab676SJeff Bonwick error = ddt_load(spa); 25401195e687SMark J Musante if (error != 0) 25411195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2542b24ab676SJeff Bonwick 2543485bbbf5SGeorge Wilson spa_update_dspace(spa); 2544485bbbf5SGeorge Wilson 2545b24ab676SJeff Bonwick /* 25464b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 25474b964adaSGeorge Wilson * information which might be missing. If we fail to validate 25484b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 25494b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 25504b964adaSGeorge Wilson * over. 2551b24ab676SJeff Bonwick */ 25521195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2553871a9500SMark J Musante nvlist_t *nvconfig; 2554871a9500SMark J Musante 2555871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2556871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2557871a9500SMark J Musante 25584b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 25594b964adaSGeorge Wilson nvlist_free(nvconfig); 25604b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 25614b964adaSGeorge Wilson ENXIO)); 25624b964adaSGeorge Wilson } 25631195e687SMark J Musante nvlist_free(nvconfig); 25641195e687SMark J Musante 25654b964adaSGeorge Wilson /* 2566ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 25674b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 25684b964adaSGeorge Wilson * more toplevel vdevs are faulted. 25694b964adaSGeorge Wilson */ 25704b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 25714b964adaSGeorge Wilson return (ENXIO); 25724b964adaSGeorge Wilson 25731195e687SMark J Musante if (spa_check_logs(spa)) { 25741195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 25751195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 25761195e687SMark J Musante } 2577b24ab676SJeff Bonwick } 2578b24ab676SJeff Bonwick 2579ad135b5dSChristopher Siden if (missing_feat_write) { 2580ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2581ad135b5dSChristopher Siden 2582ad135b5dSChristopher Siden /* 2583ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2584ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2585ad135b5dSChristopher Siden * information and can return to userland. 2586ad135b5dSChristopher Siden */ 2587ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2588ad135b5dSChristopher Siden } 2589ad135b5dSChristopher Siden 25904b964adaSGeorge Wilson /* 25914b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 25924b964adaSGeorge Wilson * to start pushing transactions. 25934b964adaSGeorge Wilson */ 25944b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 25954b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 25964b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 25974b964adaSGeorge Wilson error)); 25984b964adaSGeorge Wilson } 25994b964adaSGeorge Wilson 2600468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2601468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 26025dabedeeSbonwick dmu_tx_t *tx; 26030373e76bSbonwick int need_update = B_FALSE; 26048ad4d6ddSJeff Bonwick 26058ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 26065dabedeeSbonwick 26070373e76bSbonwick /* 26080373e76bSbonwick * Claim log blocks that haven't been committed yet. 26090373e76bSbonwick * This must all happen in a single txg. 2610b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2611b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2612468c413aSTim Haley * Price of rollback is that we abandon the log. 26130373e76bSbonwick */ 2614b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2615b24ab676SJeff Bonwick 26165dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2617fa9e4066Sahrens spa_first_txg(spa)); 2618e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 26190b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2620fa9e4066Sahrens dmu_tx_commit(tx); 2621fa9e4066Sahrens 2622b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2623b24ab676SJeff Bonwick 26241195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2625fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2626fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2627fa9e4066Sahrens 2628fa9e4066Sahrens /* 2629b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2630b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2631b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2632b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2633b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2634fa9e4066Sahrens */ 2635b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 26360e34b6a7Sbonwick 26370e34b6a7Sbonwick /* 26380373e76bSbonwick * If the config cache is stale, or we have uninitialized 26390373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2640bc758434SLin Ling * 26414b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2642bc758434SLin Ling * in-core spa_config and update the disk labels. 26430e34b6a7Sbonwick */ 26440373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 26454b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 26464b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 26474b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 26480373e76bSbonwick need_update = B_TRUE; 26490373e76bSbonwick 26508ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 26510373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 26520373e76bSbonwick need_update = B_TRUE; 26530e34b6a7Sbonwick 26540e34b6a7Sbonwick /* 26550373e76bSbonwick * Update the config cache asychronously in case we're the 26560373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 26570e34b6a7Sbonwick */ 26580373e76bSbonwick if (need_update) 26590373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 26608ad4d6ddSJeff Bonwick 26618ad4d6ddSJeff Bonwick /* 26628ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 26638ad4d6ddSJeff Bonwick */ 26643f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 26653f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 26668ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2667503ad85cSMatthew Ahrens 26684445fffbSMatthew Ahrens /* 26694445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 26704445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 26714445fffbSMatthew Ahrens */ 26724445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 26734445fffbSMatthew Ahrens 2674503ad85cSMatthew Ahrens /* 2675503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2676503ad85cSMatthew Ahrens */ 2677503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2678503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2679ca45db41SChris Kirby 2680ca45db41SChris Kirby /* 2681ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2682ca45db41SChris Kirby */ 2683ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2684fa9e4066Sahrens } 2685fa9e4066Sahrens 26861195e687SMark J Musante return (0); 2687fa9e4066Sahrens } 2688fa9e4066Sahrens 2689468c413aSTim Haley static int 2690468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2691468c413aSTim Haley { 2692f9af39baSGeorge Wilson int mode = spa->spa_mode; 2693f9af39baSGeorge Wilson 2694468c413aSTim Haley spa_unload(spa); 2695468c413aSTim Haley spa_deactivate(spa); 2696468c413aSTim Haley 2697468c413aSTim Haley spa->spa_load_max_txg--; 2698468c413aSTim Haley 2699f9af39baSGeorge Wilson spa_activate(spa, mode); 2700468c413aSTim Haley spa_async_suspend(spa); 2701468c413aSTim Haley 27021195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2703468c413aSTim Haley } 2704468c413aSTim Haley 2705ad135b5dSChristopher Siden /* 2706ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2707ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2708ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2709ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2710ad135b5dSChristopher Siden * spa_load(). 2711ad135b5dSChristopher Siden */ 2712468c413aSTim Haley static int 2713468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2714c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2715468c413aSTim Haley { 2716ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2717468c413aSTim Haley nvlist_t *config = NULL; 2718468c413aSTim Haley int load_error, rewind_error; 2719c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2720468c413aSTim Haley uint64_t min_txg; 2721468c413aSTim Haley 2722a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2723468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 27241195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2725a33cae98STim Haley } else { 2726468c413aSTim Haley spa->spa_load_max_txg = max_request; 2727a33cae98STim Haley } 2728468c413aSTim Haley 27291195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 27301195e687SMark J Musante mosconfig); 2731468c413aSTim Haley if (load_error == 0) 2732468c413aSTim Haley return (0); 2733468c413aSTim Haley 2734468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2735468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2736468c413aSTim Haley 2737468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2738468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2739468c413aSTim Haley 2740c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2741468c413aSTim Haley nvlist_free(config); 2742468c413aSTim Haley return (load_error); 2743468c413aSTim Haley } 2744468c413aSTim Haley 2745ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2746ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 27471195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2748ad135b5dSChristopher Siden } else { 2749ad135b5dSChristopher Siden /* 2750ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2751ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2752ad135b5dSChristopher Siden * to rewind. 2753ad135b5dSChristopher Siden */ 2754ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 2755ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2756ad135b5dSChristopher Siden } 2757468c413aSTim Haley 2758c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2759c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2760c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2761c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2762468c413aSTim Haley 2763c8ee1847SVictor Latushkin /* 2764c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2765c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2766c8ee1847SVictor Latushkin */ 2767c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2768c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2769c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2770468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2771468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2772468c413aSTim Haley } 2773468c413aSTim Haley 2774468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2775468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2776468c413aSTim Haley 2777468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2778468c413aSTim Haley spa_config_set(spa, config); 2779468c413aSTim Haley 2780ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2781ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 2782ad135b5dSChristopher Siden return (rewind_error); 2783ad135b5dSChristopher Siden } else { 2784ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 2785ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 2786ad135b5dSChristopher Siden spa->spa_load_info); 2787ad135b5dSChristopher Siden 2788ad135b5dSChristopher Siden /* Restore the initial load info */ 2789ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 2790ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 2791ad135b5dSChristopher Siden 2792ad135b5dSChristopher Siden return (load_error); 2793ad135b5dSChristopher Siden } 2794468c413aSTim Haley } 2795468c413aSTim Haley 2796fa9e4066Sahrens /* 2797fa9e4066Sahrens * Pool Open/Import 2798fa9e4066Sahrens * 2799fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2800fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2801fa9e4066Sahrens * case of an open, the pool configuration will exist in the 28023d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2803fa9e4066Sahrens * 2804fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2805fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2806fa9e4066Sahrens * ambiguous state. 2807fa9e4066Sahrens */ 2808fa9e4066Sahrens static int 2809468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2810468c413aSTim Haley nvlist_t **config) 2811fa9e4066Sahrens { 2812fa9e4066Sahrens spa_t *spa; 28134b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 2814fa9e4066Sahrens int error; 2815fa9e4066Sahrens int locked = B_FALSE; 2816fa9e4066Sahrens 2817fa9e4066Sahrens *spapp = NULL; 2818fa9e4066Sahrens 2819fa9e4066Sahrens /* 2820fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2821fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2822fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2823fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2824fa9e4066Sahrens */ 2825fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2826fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2827fa9e4066Sahrens locked = B_TRUE; 2828fa9e4066Sahrens } 2829fa9e4066Sahrens 2830fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2831fa9e4066Sahrens if (locked) 2832fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2833fa9e4066Sahrens return (ENOENT); 2834fa9e4066Sahrens } 2835468c413aSTim Haley 2836fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 28374b44c88cSTim Haley zpool_rewind_policy_t policy; 28384b44c88cSTim Haley 28394b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 28404b44c88cSTim Haley &policy); 28414b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 28424b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2843fa9e4066Sahrens 28448ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2845fa9e4066Sahrens 2846468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2847468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2848468c413aSTim Haley 2849468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2850c8ee1847SVictor Latushkin policy.zrp_request); 2851fa9e4066Sahrens 2852fa9e4066Sahrens if (error == EBADF) { 2853fa9e4066Sahrens /* 2854560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2855560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2856560e6e96Seschrock * that the pool has been exported or destroyed. If 2857560e6e96Seschrock * this is the case, the config cache is out of sync and 2858560e6e96Seschrock * we should remove the pool from the namespace. 2859fa9e4066Sahrens */ 2860fa9e4066Sahrens spa_unload(spa); 2861fa9e4066Sahrens spa_deactivate(spa); 2862c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2863fa9e4066Sahrens spa_remove(spa); 2864fa9e4066Sahrens if (locked) 2865fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2866fa9e4066Sahrens return (ENOENT); 2867ea8dc4b6Seschrock } 2868ea8dc4b6Seschrock 2869ea8dc4b6Seschrock if (error) { 2870fa9e4066Sahrens /* 2871fa9e4066Sahrens * We can't open the pool, but we still have useful 2872fa9e4066Sahrens * information: the state of each vdev after the 2873fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2874fa9e4066Sahrens */ 28754b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 2876468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2877468c413aSTim Haley KM_SLEEP) == 0); 28784b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 28794b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 28804b964adaSGeorge Wilson spa->spa_load_info) == 0); 28814b964adaSGeorge Wilson } 2882fa9e4066Sahrens spa_unload(spa); 2883fa9e4066Sahrens spa_deactivate(spa); 2884468c413aSTim Haley spa->spa_last_open_failed = error; 2885fa9e4066Sahrens if (locked) 2886fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2887fa9e4066Sahrens *spapp = NULL; 2888fa9e4066Sahrens return (error); 2889fa9e4066Sahrens } 2890fa9e4066Sahrens } 2891fa9e4066Sahrens 2892fa9e4066Sahrens spa_open_ref(spa, tag); 28933d7072f8Seschrock 2894468c413aSTim Haley if (config != NULL) 2895468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2896468c413aSTim Haley 28974b964adaSGeorge Wilson /* 28984b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 28994b964adaSGeorge Wilson * gathered while doing the load. 29004b964adaSGeorge Wilson */ 29014b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 29024b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 29034b964adaSGeorge Wilson spa->spa_load_info) == 0); 29044b964adaSGeorge Wilson } 29054b964adaSGeorge Wilson 2906a33cae98STim Haley if (locked) { 2907a33cae98STim Haley spa->spa_last_open_failed = 0; 2908a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 2909a33cae98STim Haley spa->spa_load_txg = 0; 2910fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2911a33cae98STim Haley } 2912fa9e4066Sahrens 2913fa9e4066Sahrens *spapp = spa; 2914fa9e4066Sahrens 2915fa9e4066Sahrens return (0); 2916fa9e4066Sahrens } 2917fa9e4066Sahrens 2918468c413aSTim Haley int 2919468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 2920468c413aSTim Haley nvlist_t **config) 2921468c413aSTim Haley { 2922468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 2923468c413aSTim Haley } 2924468c413aSTim Haley 2925fa9e4066Sahrens int 2926fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2927fa9e4066Sahrens { 2928468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 2929fa9e4066Sahrens } 2930fa9e4066Sahrens 2931ea8dc4b6Seschrock /* 2932ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 2933ea8dc4b6Seschrock * preventing it from being exported or destroyed. 2934ea8dc4b6Seschrock */ 2935ea8dc4b6Seschrock spa_t * 2936ea8dc4b6Seschrock spa_inject_addref(char *name) 2937ea8dc4b6Seschrock { 2938ea8dc4b6Seschrock spa_t *spa; 2939ea8dc4b6Seschrock 2940ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2941ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 2942ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2943ea8dc4b6Seschrock return (NULL); 2944ea8dc4b6Seschrock } 2945ea8dc4b6Seschrock spa->spa_inject_ref++; 2946ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2947ea8dc4b6Seschrock 2948ea8dc4b6Seschrock return (spa); 2949ea8dc4b6Seschrock } 2950ea8dc4b6Seschrock 2951ea8dc4b6Seschrock void 2952ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 2953ea8dc4b6Seschrock { 2954ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2955ea8dc4b6Seschrock spa->spa_inject_ref--; 2956ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2957ea8dc4b6Seschrock } 2958ea8dc4b6Seschrock 2959fa94a07fSbrendan /* 2960fa94a07fSbrendan * Add spares device information to the nvlist. 2961fa94a07fSbrendan */ 296299653d4eSeschrock static void 296399653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 296499653d4eSeschrock { 296599653d4eSeschrock nvlist_t **spares; 296699653d4eSeschrock uint_t i, nspares; 296799653d4eSeschrock nvlist_t *nvroot; 296899653d4eSeschrock uint64_t guid; 296999653d4eSeschrock vdev_stat_t *vs; 297099653d4eSeschrock uint_t vsc; 297139c23413Seschrock uint64_t pool; 297299653d4eSeschrock 29736809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 29746809eb4eSEric Schrock 2975fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 297699653d4eSeschrock return; 297799653d4eSeschrock 297899653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 297999653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2980fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 298199653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 298299653d4eSeschrock if (nspares != 0) { 298399653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 298499653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 298599653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 298699653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 298799653d4eSeschrock 298899653d4eSeschrock /* 298999653d4eSeschrock * Go through and find any spares which have since been 299099653d4eSeschrock * repurposed as an active spare. If this is the case, update 299199653d4eSeschrock * their status appropriately. 299299653d4eSeschrock */ 299399653d4eSeschrock for (i = 0; i < nspares; i++) { 299499653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 299599653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 299689a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 299789a89ebfSlling pool != 0ULL) { 299899653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 29993f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 300099653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 300199653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 300299653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 300399653d4eSeschrock } 300499653d4eSeschrock } 300599653d4eSeschrock } 300699653d4eSeschrock } 300799653d4eSeschrock 3008fa94a07fSbrendan /* 3009fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3010fa94a07fSbrendan */ 3011fa94a07fSbrendan static void 3012fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3013fa94a07fSbrendan { 3014fa94a07fSbrendan nvlist_t **l2cache; 3015fa94a07fSbrendan uint_t i, j, nl2cache; 3016fa94a07fSbrendan nvlist_t *nvroot; 3017fa94a07fSbrendan uint64_t guid; 3018fa94a07fSbrendan vdev_t *vd; 3019fa94a07fSbrendan vdev_stat_t *vs; 3020fa94a07fSbrendan uint_t vsc; 3021fa94a07fSbrendan 30226809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 30236809eb4eSEric Schrock 3024fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3025fa94a07fSbrendan return; 3026fa94a07fSbrendan 3027fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3028fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3029fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3030fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3031fa94a07fSbrendan if (nl2cache != 0) { 3032fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3033fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3034fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3035fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3036fa94a07fSbrendan 3037fa94a07fSbrendan /* 3038fa94a07fSbrendan * Update level 2 cache device stats. 3039fa94a07fSbrendan */ 3040fa94a07fSbrendan 3041fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3042fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3043fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3044fa94a07fSbrendan 3045fa94a07fSbrendan vd = NULL; 3046fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3047fa94a07fSbrendan if (guid == 3048fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3049fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3050fa94a07fSbrendan break; 3051fa94a07fSbrendan } 3052fa94a07fSbrendan } 3053fa94a07fSbrendan ASSERT(vd != NULL); 3054fa94a07fSbrendan 3055fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 30563f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 30573f9d6ad7SLin Ling == 0); 3058fa94a07fSbrendan vdev_get_stats(vd, vs); 3059fa94a07fSbrendan } 3060fa94a07fSbrendan } 3061fa94a07fSbrendan } 3062fa94a07fSbrendan 3063ad135b5dSChristopher Siden static void 3064ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3065ad135b5dSChristopher Siden { 3066ad135b5dSChristopher Siden nvlist_t *features; 3067ad135b5dSChristopher Siden zap_cursor_t zc; 3068ad135b5dSChristopher Siden zap_attribute_t za; 3069ad135b5dSChristopher Siden 3070ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3071ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3072ad135b5dSChristopher Siden 3073ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3074ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3075ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3076ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3077ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3078ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3079ad135b5dSChristopher Siden za.za_num_integers == 1); 3080b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3081ad135b5dSChristopher Siden za.za_first_integer)); 3082ad135b5dSChristopher Siden } 3083ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3084ad135b5dSChristopher Siden } 3085ad135b5dSChristopher Siden 3086ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3087ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3088ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3089ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3090ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3091ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3092ad135b5dSChristopher Siden za.za_num_integers == 1); 3093b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3094ad135b5dSChristopher Siden za.za_first_integer)); 3095ad135b5dSChristopher Siden } 3096ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3097ad135b5dSChristopher Siden } 3098ad135b5dSChristopher Siden 3099ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3100ad135b5dSChristopher Siden features) == 0); 3101ad135b5dSChristopher Siden nvlist_free(features); 3102ad135b5dSChristopher Siden } 3103ad135b5dSChristopher Siden 3104fa9e4066Sahrens int 3105ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3106ad135b5dSChristopher Siden char *altroot, size_t buflen) 3107fa9e4066Sahrens { 3108fa9e4066Sahrens int error; 3109fa9e4066Sahrens spa_t *spa; 3110fa9e4066Sahrens 3111fa9e4066Sahrens *config = NULL; 3112468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3113fa9e4066Sahrens 31146809eb4eSEric Schrock if (spa != NULL) { 31156809eb4eSEric Schrock /* 31166809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 31176809eb4eSEric Schrock * or l2cache devices could change and the config would be 31186809eb4eSEric Schrock * self-inconsistent. 31196809eb4eSEric Schrock */ 31206809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3121ea8dc4b6Seschrock 31226809eb4eSEric Schrock if (*config != NULL) { 312311027bc7STim Haley uint64_t loadtimes[2]; 312411027bc7STim Haley 312511027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 312611027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 312711027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 312811027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 312911027bc7STim Haley 3130e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 31316809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 31326809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3133e14bb325SJeff Bonwick 31346809eb4eSEric Schrock if (spa_suspended(spa)) 31356809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 31366809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 31376809eb4eSEric Schrock spa->spa_failmode) == 0); 31386809eb4eSEric Schrock 31396809eb4eSEric Schrock spa_add_spares(spa, *config); 31406809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3141ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 31426809eb4eSEric Schrock } 314399653d4eSeschrock } 314499653d4eSeschrock 3145ea8dc4b6Seschrock /* 3146ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3147ea8dc4b6Seschrock * and call spa_lookup() directly. 3148ea8dc4b6Seschrock */ 3149ea8dc4b6Seschrock if (altroot) { 3150ea8dc4b6Seschrock if (spa == NULL) { 3151ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3152ea8dc4b6Seschrock spa = spa_lookup(name); 3153ea8dc4b6Seschrock if (spa) 3154ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3155ea8dc4b6Seschrock else 3156ea8dc4b6Seschrock altroot[0] = '\0'; 3157ea8dc4b6Seschrock spa = NULL; 3158ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3159ea8dc4b6Seschrock } else { 3160ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3161ea8dc4b6Seschrock } 3162ea8dc4b6Seschrock } 3163ea8dc4b6Seschrock 31646809eb4eSEric Schrock if (spa != NULL) { 31656809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3166fa9e4066Sahrens spa_close(spa, FTAG); 31676809eb4eSEric Schrock } 3168fa9e4066Sahrens 3169fa9e4066Sahrens return (error); 3170fa9e4066Sahrens } 3171fa9e4066Sahrens 317299653d4eSeschrock /* 3173fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3174fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3175fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3176fa94a07fSbrendan * specified, as long as they are well-formed. 317799653d4eSeschrock */ 317899653d4eSeschrock static int 3179fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3180fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3181fa94a07fSbrendan vdev_labeltype_t label) 318299653d4eSeschrock { 3183fa94a07fSbrendan nvlist_t **dev; 3184fa94a07fSbrendan uint_t i, ndev; 318599653d4eSeschrock vdev_t *vd; 318699653d4eSeschrock int error; 318799653d4eSeschrock 3188e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3189e14bb325SJeff Bonwick 319099653d4eSeschrock /* 3191fa94a07fSbrendan * It's acceptable to have no devs specified. 319299653d4eSeschrock */ 3193fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 319499653d4eSeschrock return (0); 319599653d4eSeschrock 3196fa94a07fSbrendan if (ndev == 0) 319799653d4eSeschrock return (EINVAL); 319899653d4eSeschrock 319999653d4eSeschrock /* 3200fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3201fa94a07fSbrendan * device type. 320299653d4eSeschrock */ 3203fa94a07fSbrendan if (spa_version(spa) < version) 320499653d4eSeschrock return (ENOTSUP); 320599653d4eSeschrock 320639c23413Seschrock /* 3207fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 320839c23413Seschrock * checking. 320939c23413Seschrock */ 3210fa94a07fSbrendan sav->sav_pending = dev; 3211fa94a07fSbrendan sav->sav_npending = ndev; 321239c23413Seschrock 3213fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3214fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 321599653d4eSeschrock mode)) != 0) 321639c23413Seschrock goto out; 321799653d4eSeschrock 321899653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 321999653d4eSeschrock vdev_free(vd); 322039c23413Seschrock error = EINVAL; 322139c23413Seschrock goto out; 322299653d4eSeschrock } 322399653d4eSeschrock 3224fa94a07fSbrendan /* 3225e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3226e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3227fa94a07fSbrendan */ 3228e14bb325SJeff Bonwick #ifdef _KERNEL 3229fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3230fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3231fa94a07fSbrendan error = ENOTBLK; 3232cd0837ccSGeorge Wilson vdev_free(vd); 3233fa94a07fSbrendan goto out; 3234fa94a07fSbrendan } 3235e14bb325SJeff Bonwick #endif 323699653d4eSeschrock vd->vdev_top = vd; 323799653d4eSeschrock 323839c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3239fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3240fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 324139c23413Seschrock vd->vdev_guid) == 0); 324239c23413Seschrock } 324399653d4eSeschrock 324499653d4eSeschrock vdev_free(vd); 324539c23413Seschrock 3246fa94a07fSbrendan if (error && 3247fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 324839c23413Seschrock goto out; 324939c23413Seschrock else 325039c23413Seschrock error = 0; 325199653d4eSeschrock } 325299653d4eSeschrock 325339c23413Seschrock out: 3254fa94a07fSbrendan sav->sav_pending = NULL; 3255fa94a07fSbrendan sav->sav_npending = 0; 325639c23413Seschrock return (error); 325799653d4eSeschrock } 325899653d4eSeschrock 3259fa94a07fSbrendan static int 3260fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3261fa94a07fSbrendan { 3262fa94a07fSbrendan int error; 3263fa94a07fSbrendan 3264e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3265e14bb325SJeff Bonwick 3266fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3267fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3268fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3269fa94a07fSbrendan return (error); 3270fa94a07fSbrendan } 3271fa94a07fSbrendan 3272fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3273fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3274fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3275fa94a07fSbrendan } 3276fa94a07fSbrendan 3277fa94a07fSbrendan static void 3278fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3279fa94a07fSbrendan const char *config) 3280fa94a07fSbrendan { 3281fa94a07fSbrendan int i; 3282fa94a07fSbrendan 3283fa94a07fSbrendan if (sav->sav_config != NULL) { 3284fa94a07fSbrendan nvlist_t **olddevs; 3285fa94a07fSbrendan uint_t oldndevs; 3286fa94a07fSbrendan nvlist_t **newdevs; 3287fa94a07fSbrendan 3288fa94a07fSbrendan /* 3289fa94a07fSbrendan * Generate new dev list by concatentating with the 3290fa94a07fSbrendan * current dev list. 3291fa94a07fSbrendan */ 3292fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3293fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3294fa94a07fSbrendan 3295fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3296fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3297fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3298fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3299fa94a07fSbrendan KM_SLEEP) == 0); 3300fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3301fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3302fa94a07fSbrendan KM_SLEEP) == 0); 3303fa94a07fSbrendan 3304fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3305fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3306fa94a07fSbrendan 3307fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3308fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3309fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3310fa94a07fSbrendan nvlist_free(newdevs[i]); 3311fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3312fa94a07fSbrendan } else { 3313fa94a07fSbrendan /* 3314fa94a07fSbrendan * Generate a new dev list. 3315fa94a07fSbrendan */ 3316fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3317fa94a07fSbrendan KM_SLEEP) == 0); 3318fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3319fa94a07fSbrendan devs, ndevs) == 0); 3320fa94a07fSbrendan } 3321fa94a07fSbrendan } 3322fa94a07fSbrendan 3323fa94a07fSbrendan /* 3324fa94a07fSbrendan * Stop and drop level 2 ARC devices 3325fa94a07fSbrendan */ 3326fa94a07fSbrendan void 3327fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3328fa94a07fSbrendan { 3329fa94a07fSbrendan vdev_t *vd; 3330fa94a07fSbrendan int i; 3331fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3332fa94a07fSbrendan 3333fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3334fa94a07fSbrendan uint64_t pool; 3335fa94a07fSbrendan 3336fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3337fa94a07fSbrendan ASSERT(vd != NULL); 3338fa94a07fSbrendan 33398ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 33408ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3341fa94a07fSbrendan l2arc_remove_vdev(vd); 3342fa94a07fSbrendan } 3343fa94a07fSbrendan } 3344fa94a07fSbrendan 3345fa9e4066Sahrens /* 3346fa9e4066Sahrens * Pool Creation 3347fa9e4066Sahrens */ 3348fa9e4066Sahrens int 3349990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 33504445fffbSMatthew Ahrens nvlist_t *zplprops) 3351fa9e4066Sahrens { 3352fa9e4066Sahrens spa_t *spa; 3353990b4856Slling char *altroot = NULL; 33540373e76bSbonwick vdev_t *rvd; 3355fa9e4066Sahrens dsl_pool_t *dp; 3356fa9e4066Sahrens dmu_tx_t *tx; 3357573ca77eSGeorge Wilson int error = 0; 3358fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3359fa94a07fSbrendan nvlist_t **spares, **l2cache; 3360fa94a07fSbrendan uint_t nspares, nl2cache; 3361cde58dbcSMatthew Ahrens uint64_t version, obj; 3362ad135b5dSChristopher Siden boolean_t has_features; 3363fa9e4066Sahrens 3364fa9e4066Sahrens /* 3365fa9e4066Sahrens * If this pool already exists, return failure. 3366fa9e4066Sahrens */ 3367fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3368fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3369fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3370fa9e4066Sahrens return (EEXIST); 3371fa9e4066Sahrens } 3372fa9e4066Sahrens 3373fa9e4066Sahrens /* 3374fa9e4066Sahrens * Allocate a new spa_t structure. 3375fa9e4066Sahrens */ 3376990b4856Slling (void) nvlist_lookup_string(props, 3377990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3378468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 33798ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3380fa9e4066Sahrens 3381990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3382990b4856Slling spa_deactivate(spa); 3383990b4856Slling spa_remove(spa); 3384c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3385990b4856Slling return (error); 3386990b4856Slling } 3387990b4856Slling 3388ad135b5dSChristopher Siden has_features = B_FALSE; 3389ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3390ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3391ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3392ad135b5dSChristopher Siden has_features = B_TRUE; 3393ad135b5dSChristopher Siden } 3394ad135b5dSChristopher Siden 3395ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3396ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3397990b4856Slling version = SPA_VERSION; 3398ad135b5dSChristopher Siden } 3399ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3400b24ab676SJeff Bonwick 3401b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3402b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3403990b4856Slling spa->spa_uberblock.ub_version = version; 3404fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3405fa9e4066Sahrens 340654d692b7SGeorge Wilson /* 340754d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 340854d692b7SGeorge Wilson */ 340925f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 341025f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 341154d692b7SGeorge Wilson 34120373e76bSbonwick /* 34130373e76bSbonwick * Create the root vdev. 34140373e76bSbonwick */ 3415e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 34160373e76bSbonwick 341799653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 34180373e76bSbonwick 341999653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 342099653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 34210373e76bSbonwick 3422b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 34230373e76bSbonwick error = EINVAL; 342499653d4eSeschrock 342599653d4eSeschrock if (error == 0 && 342699653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3427fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 342899653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3429573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3430573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3431573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3432573ca77eSGeorge Wilson } 34330373e76bSbonwick } 34340373e76bSbonwick 3435e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3436fa9e4066Sahrens 343799653d4eSeschrock if (error != 0) { 3438fa9e4066Sahrens spa_unload(spa); 3439fa9e4066Sahrens spa_deactivate(spa); 3440fa9e4066Sahrens spa_remove(spa); 3441fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3442fa9e4066Sahrens return (error); 3443fa9e4066Sahrens } 3444fa9e4066Sahrens 344599653d4eSeschrock /* 344699653d4eSeschrock * Get the list of spares, if specified. 344799653d4eSeschrock */ 344899653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 344999653d4eSeschrock &spares, &nspares) == 0) { 3450fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 345199653d4eSeschrock KM_SLEEP) == 0); 3452fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 345399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3454e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 345599653d4eSeschrock spa_load_spares(spa); 3456e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3457fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3458fa94a07fSbrendan } 3459fa94a07fSbrendan 3460fa94a07fSbrendan /* 3461fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3462fa94a07fSbrendan */ 3463fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3464fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3465fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3466fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3467fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3468fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3469e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3470fa94a07fSbrendan spa_load_l2cache(spa); 3471e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3472fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 347399653d4eSeschrock } 347499653d4eSeschrock 3475ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 34760a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3477fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3478ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3479fa9e4066Sahrens 3480485bbbf5SGeorge Wilson /* 3481485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3482485bbbf5SGeorge Wilson */ 3483485bbbf5SGeorge Wilson ddt_create(spa); 3484485bbbf5SGeorge Wilson 3485485bbbf5SGeorge Wilson spa_update_dspace(spa); 3486485bbbf5SGeorge Wilson 3487fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3488fa9e4066Sahrens 3489fa9e4066Sahrens /* 3490fa9e4066Sahrens * Create the pool config object. 3491fa9e4066Sahrens */ 3492fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3493f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3494fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3495fa9e4066Sahrens 3496ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3497fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3498ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3499ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3500ea8dc4b6Seschrock } 3501fa9e4066Sahrens 3502ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3503ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3504ad135b5dSChristopher Siden 35053f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 35063f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 35073f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 35083f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 35093f9d6ad7SLin Ling } 35103f9d6ad7SLin Ling 3511990b4856Slling /* Newly created pools with the right version are always deflated. */ 3512990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3513990b4856Slling spa->spa_deflate = TRUE; 3514990b4856Slling if (zap_add(spa->spa_meta_objset, 3515990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3516990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3517990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3518990b4856Slling } 351999653d4eSeschrock } 352099653d4eSeschrock 3521fa9e4066Sahrens /* 3522cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3523fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3524fa9e4066Sahrens * keeps changing. 3525fa9e4066Sahrens */ 3526cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3527cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3528cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3529ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3530cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3531cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3532cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3533ea8dc4b6Seschrock } 3534b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3535cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3536fa9e4066Sahrens 353706eeb2adSek /* 353806eeb2adSek * Create the pool's history object. 353906eeb2adSek */ 3540990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3541990b4856Slling spa_history_create_obj(spa, tx); 3542990b4856Slling 3543990b4856Slling /* 3544990b4856Slling * Set pool properties. 3545990b4856Slling */ 3546990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3547990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 35480a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3549573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3550b24ab676SJeff Bonwick 3551379c004dSEric Schrock if (props != NULL) { 3552379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 3553*3b2aab18SMatthew Ahrens spa_sync_props(props, tx); 3554379c004dSEric Schrock } 355506eeb2adSek 3556fa9e4066Sahrens dmu_tx_commit(tx); 3557fa9e4066Sahrens 3558fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3559fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3560fa9e4066Sahrens 3561fa9e4066Sahrens /* 3562fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3563fa9e4066Sahrens * bean counters are appropriately updated. 3564fa9e4066Sahrens */ 3565fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3566fa9e4066Sahrens 3567c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3568fa9e4066Sahrens 35694445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3570228975ccSek 3571088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 3572088f3894Sahrens 3573daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3574daaa36a7SGeorge Wilson 3575fa9e4066Sahrens return (0); 3576fa9e4066Sahrens } 3577fa9e4066Sahrens 3578e7cbe64fSgw #ifdef _KERNEL 3579e7cbe64fSgw /* 358021ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 358121ecdf64SLin Ling * during the system boot up time. 3582e7cbe64fSgw */ 358321ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 358421ecdf64SLin Ling 358521ecdf64SLin Ling static nvlist_t * 358621ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3587e7cbe64fSgw { 358821ecdf64SLin Ling nvlist_t *config; 3589e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3590e7cbe64fSgw uint64_t pgid; 3591e7cbe64fSgw 359221ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 359321ecdf64SLin Ling return (NULL); 359421ecdf64SLin Ling 3595e7cbe64fSgw /* 3596e7cbe64fSgw * Add this top-level vdev to the child array. 3597e7cbe64fSgw */ 359821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 359921ecdf64SLin Ling &nvtop) == 0); 360021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 360121ecdf64SLin Ling &pgid) == 0); 360221ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3603e7cbe64fSgw 3604e7cbe64fSgw /* 3605e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3606e7cbe64fSgw */ 3607e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 360821ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 360921ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3610e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3611e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3612e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3613e7cbe64fSgw &nvtop, 1) == 0); 3614e7cbe64fSgw 3615e7cbe64fSgw /* 3616e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3617e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3618e7cbe64fSgw */ 3619e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3620e7cbe64fSgw nvlist_free(nvroot); 362121ecdf64SLin Ling return (config); 3622e7cbe64fSgw } 3623e7cbe64fSgw 3624e7cbe64fSgw /* 362521ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 362621ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 362721ecdf64SLin Ling * device has a more recent txg. 3628051aabe6Staylor */ 362921ecdf64SLin Ling static void 363021ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3631051aabe6Staylor { 3632573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 363321ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3634051aabe6Staylor 363521ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 363621ecdf64SLin Ling nvlist_t *label; 363721ecdf64SLin Ling uint64_t label_txg; 3638051aabe6Staylor 363921ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 364021ecdf64SLin Ling &label) != 0) 364121ecdf64SLin Ling return; 3642051aabe6Staylor 364321ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 364421ecdf64SLin Ling &label_txg) == 0); 3645051aabe6Staylor 364621ecdf64SLin Ling /* 364721ecdf64SLin Ling * Do we have a better boot device? 364821ecdf64SLin Ling */ 364921ecdf64SLin Ling if (label_txg > *txg) { 365021ecdf64SLin Ling *txg = label_txg; 365121ecdf64SLin Ling *avd = vd; 3652051aabe6Staylor } 365321ecdf64SLin Ling nvlist_free(label); 3654051aabe6Staylor } 3655051aabe6Staylor } 3656051aabe6Staylor 3657e7cbe64fSgw /* 3658e7cbe64fSgw * Import a root pool. 3659e7cbe64fSgw * 3660051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3661051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3662051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3663e7cbe64fSgw * 3664e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3665e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3666e7cbe64fSgw * e.g. 3667e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3668e7cbe64fSgw */ 3669e7cbe64fSgw int 3670051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3671e7cbe64fSgw { 367221ecdf64SLin Ling spa_t *spa; 367321ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 367421ecdf64SLin Ling nvlist_t *config, *nvtop; 367521ecdf64SLin Ling uint64_t guid, txg; 3676e7cbe64fSgw char *pname; 3677e7cbe64fSgw int error; 3678e7cbe64fSgw 3679e7cbe64fSgw /* 368021ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3681e7cbe64fSgw */ 3682dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3683dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3684dedec472SJack Meng if (config == NULL) { 3685dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3686dedec472SJack Meng /* iscsi boot */ 3687dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3688dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3689dedec472SJack Meng } 3690dedec472SJack Meng } 3691dedec472SJack Meng #endif 3692dedec472SJack Meng if (config == NULL) { 3693ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 369421ecdf64SLin Ling devpath); 369521ecdf64SLin Ling return (EIO); 369621ecdf64SLin Ling } 3697e7cbe64fSgw 369821ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 369921ecdf64SLin Ling &pname) == 0); 370021ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3701e7cbe64fSgw 37026809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 37036809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 37046809eb4eSEric Schrock /* 37056809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 37066809eb4eSEric Schrock * can replace it with the correct config we just read in. 37076809eb4eSEric Schrock */ 37086809eb4eSEric Schrock spa_remove(spa); 37096809eb4eSEric Schrock } 37106809eb4eSEric Schrock 3711468c413aSTim Haley spa = spa_add(pname, config, NULL); 37126809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 37134b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3714e7cbe64fSgw 371521ecdf64SLin Ling /* 371621ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 371721ecdf64SLin Ling */ 371821ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 371921ecdf64SLin Ling &nvtop) == 0); 372021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 372121ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 372221ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 372321ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 372421ecdf64SLin Ling if (error) { 372521ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 372621ecdf64SLin Ling nvlist_free(config); 372721ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 372821ecdf64SLin Ling pname); 372921ecdf64SLin Ling return (error); 373021ecdf64SLin Ling } 373121ecdf64SLin Ling 373221ecdf64SLin Ling /* 373321ecdf64SLin Ling * Get the boot vdev. 373421ecdf64SLin Ling */ 373521ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 373621ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 373721ecdf64SLin Ling (u_longlong_t)guid); 373821ecdf64SLin Ling error = ENOENT; 373921ecdf64SLin Ling goto out; 374021ecdf64SLin Ling } 3741e7cbe64fSgw 374221ecdf64SLin Ling /* 374321ecdf64SLin Ling * Determine if there is a better boot device. 374421ecdf64SLin Ling */ 374521ecdf64SLin Ling avd = bvd; 374621ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 374721ecdf64SLin Ling if (avd != bvd) { 374821ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 374921ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 375021ecdf64SLin Ling error = EINVAL; 375121ecdf64SLin Ling goto out; 375221ecdf64SLin Ling } 3753e7cbe64fSgw 375421ecdf64SLin Ling /* 375521ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 375621ecdf64SLin Ling * we're booting off the active spare. 375721ecdf64SLin Ling */ 375821ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 375921ecdf64SLin Ling !bvd->vdev_isspare) { 376021ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 376121ecdf64SLin Ling "try booting from '%s'", 3762cb04b873SMark J Musante bvd->vdev_parent-> 3763cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 376421ecdf64SLin Ling error = EINVAL; 376521ecdf64SLin Ling goto out; 376621ecdf64SLin Ling } 376721ecdf64SLin Ling 376821ecdf64SLin Ling error = 0; 376921ecdf64SLin Ling out: 377021ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 377121ecdf64SLin Ling vdev_free(rvd); 377221ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 377321ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 377421ecdf64SLin Ling 377521ecdf64SLin Ling nvlist_free(config); 3776e7cbe64fSgw return (error); 3777e7cbe64fSgw } 377821ecdf64SLin Ling 3779e7cbe64fSgw #endif 3780e7cbe64fSgw 37816809eb4eSEric Schrock /* 37826809eb4eSEric Schrock * Import a non-root pool into the system. 37836809eb4eSEric Schrock */ 3784c5904d13Seschrock int 37854b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 3786c5904d13Seschrock { 37876809eb4eSEric Schrock spa_t *spa; 37886809eb4eSEric Schrock char *altroot = NULL; 3789468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3790468c413aSTim Haley zpool_rewind_policy_t policy; 3791f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 3792f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 37936809eb4eSEric Schrock int error; 37946809eb4eSEric Schrock nvlist_t *nvroot; 37956809eb4eSEric Schrock nvlist_t **spares, **l2cache; 37966809eb4eSEric Schrock uint_t nspares, nl2cache; 37976809eb4eSEric Schrock 37986809eb4eSEric Schrock /* 37996809eb4eSEric Schrock * If a pool with this name exists, return failure. 38006809eb4eSEric Schrock */ 38016809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 38021195e687SMark J Musante if (spa_lookup(pool) != NULL) { 38036809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38046809eb4eSEric Schrock return (EEXIST); 38056809eb4eSEric Schrock } 38066809eb4eSEric Schrock 38076809eb4eSEric Schrock /* 38086809eb4eSEric Schrock * Create and initialize the spa structure. 38096809eb4eSEric Schrock */ 38106809eb4eSEric Schrock (void) nvlist_lookup_string(props, 38116809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3812f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 3813f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3814f9af39baSGeorge Wilson if (readonly) 3815f9af39baSGeorge Wilson mode = FREAD; 3816468c413aSTim Haley spa = spa_add(pool, config, altroot); 38174b964adaSGeorge Wilson spa->spa_import_flags = flags; 38184b964adaSGeorge Wilson 38194b964adaSGeorge Wilson /* 38204b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 38214b964adaSGeorge Wilson * as if it had been loaded at boot. 38224b964adaSGeorge Wilson */ 38234b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 38244b964adaSGeorge Wilson if (props != NULL) 38254b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 38264b964adaSGeorge Wilson 38274b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 38284b964adaSGeorge Wilson 38294b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 38304445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 38314b964adaSGeorge Wilson 38324b964adaSGeorge Wilson return (0); 38334b964adaSGeorge Wilson } 38344b964adaSGeorge Wilson 3835f9af39baSGeorge Wilson spa_activate(spa, mode); 38366809eb4eSEric Schrock 383725f89ee2SJeff Bonwick /* 383825f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 383925f89ee2SJeff Bonwick */ 384025f89ee2SJeff Bonwick spa_async_suspend(spa); 384125f89ee2SJeff Bonwick 38424b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 38434b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 38444b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 38454b964adaSGeorge Wilson 38466809eb4eSEric Schrock /* 38476809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 38486809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 38496809eb4eSEric Schrock * doing an import. 38506809eb4eSEric Schrock */ 3851468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3852468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 38534b964adaSGeorge Wilson 3854468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3855c8ee1847SVictor Latushkin policy.zrp_request); 3856468c413aSTim Haley 3857468c413aSTim Haley /* 38584b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 38594b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 3860468c413aSTim Haley */ 38614b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 38624b964adaSGeorge Wilson spa->spa_load_info) == 0); 38636809eb4eSEric Schrock 38646809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 38656809eb4eSEric Schrock /* 38666809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 38676809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 38686809eb4eSEric Schrock */ 38696809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 38706809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 38716809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 38726809eb4eSEric Schrock spa_load_spares(spa); 38736809eb4eSEric Schrock } 38746809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 38756809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 38766809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 38776809eb4eSEric Schrock spa_load_l2cache(spa); 38786809eb4eSEric Schrock } 38796809eb4eSEric Schrock 38806809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 38816809eb4eSEric Schrock &nvroot) == 0); 38826809eb4eSEric Schrock if (error == 0) 38836809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38846809eb4eSEric Schrock VDEV_ALLOC_SPARE); 38856809eb4eSEric Schrock if (error == 0) 38866809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38876809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 38886809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 38896809eb4eSEric Schrock 38906809eb4eSEric Schrock if (props != NULL) 38916809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38926809eb4eSEric Schrock 38936809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 38946809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 38956809eb4eSEric Schrock spa_unload(spa); 38966809eb4eSEric Schrock spa_deactivate(spa); 38976809eb4eSEric Schrock spa_remove(spa); 38986809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38996809eb4eSEric Schrock return (error); 39006809eb4eSEric Schrock } 39016809eb4eSEric Schrock 3902955ef359SLin Ling spa_async_resume(spa); 3903955ef359SLin Ling 39046809eb4eSEric Schrock /* 39056809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 39066809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 39076809eb4eSEric Schrock */ 39086809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 39096809eb4eSEric Schrock &spares, &nspares) == 0) { 39106809eb4eSEric Schrock if (spa->spa_spares.sav_config) 39116809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 39126809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 39136809eb4eSEric Schrock else 39146809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 39156809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 39166809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 39176809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 39186809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 39196809eb4eSEric Schrock spa_load_spares(spa); 39206809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 39216809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 39226809eb4eSEric Schrock } 39236809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 39246809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 39256809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 39266809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 39276809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 39286809eb4eSEric Schrock else 39296809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 39306809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 39316809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 39326809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 39336809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 39346809eb4eSEric Schrock spa_load_l2cache(spa); 39356809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 39366809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 39376809eb4eSEric Schrock } 39386809eb4eSEric Schrock 3939b693757aSEric Schrock /* 3940b693757aSEric Schrock * Check for any removed devices. 3941b693757aSEric Schrock */ 3942b693757aSEric Schrock if (spa->spa_autoreplace) { 3943b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3944b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3945b693757aSEric Schrock } 3946b693757aSEric Schrock 39476809eb4eSEric Schrock if (spa_writeable(spa)) { 39486809eb4eSEric Schrock /* 39496809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 39506809eb4eSEric Schrock */ 3951bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 39526809eb4eSEric Schrock } 39536809eb4eSEric Schrock 3954573ca77eSGeorge Wilson /* 3955573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 3956573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 3957573ca77eSGeorge Wilson */ 3958573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 3959573ca77eSGeorge Wilson 39606809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 39614445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 39626809eb4eSEric Schrock 39636809eb4eSEric Schrock return (0); 3964c5904d13Seschrock } 3965c5904d13Seschrock 3966fa9e4066Sahrens nvlist_t * 3967fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 3968fa9e4066Sahrens { 3969fa9e4066Sahrens nvlist_t *config = NULL; 3970fa9e4066Sahrens char *poolname; 3971fa9e4066Sahrens spa_t *spa; 3972fa9e4066Sahrens uint64_t state; 39737b7154beSLin Ling int error; 3974fa9e4066Sahrens 3975fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3976fa9e4066Sahrens return (NULL); 3977fa9e4066Sahrens 3978fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3979fa9e4066Sahrens return (NULL); 3980fa9e4066Sahrens 3981fa9e4066Sahrens /* 39820373e76bSbonwick * Create and initialize the spa structure. 3983fa9e4066Sahrens */ 39840373e76bSbonwick mutex_enter(&spa_namespace_lock); 3985468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 39868ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 3987fa9e4066Sahrens 3988fa9e4066Sahrens /* 39890373e76bSbonwick * Pass off the heavy lifting to spa_load(). 3990ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 3991ecc2d604Sbonwick * is actually the one to trust when doing an import. 3992fa9e4066Sahrens */ 39931195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3994fa9e4066Sahrens 3995fa9e4066Sahrens /* 3996fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 3997fa9e4066Sahrens */ 3998fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 3999fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4000fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4001fa9e4066Sahrens poolname) == 0); 4002fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4003fa9e4066Sahrens state) == 0); 400495173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 400595173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4006ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4007ad135b5dSChristopher Siden spa->spa_load_info) == 0); 400899653d4eSeschrock 4009e7cbe64fSgw /* 4010e7cbe64fSgw * If the bootfs property exists on this pool then we 4011e7cbe64fSgw * copy it out so that external consumers can tell which 4012e7cbe64fSgw * pools are bootable. 4013e7cbe64fSgw */ 40147b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4015e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4016e7cbe64fSgw 4017e7cbe64fSgw /* 4018e7cbe64fSgw * We have to play games with the name since the 4019e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4020e7cbe64fSgw */ 4021e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4022e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4023e7cbe64fSgw char *cp; 4024e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4025e7cbe64fSgw 4026e7cbe64fSgw cp = strchr(tmpname, '/'); 4027e7cbe64fSgw if (cp == NULL) { 4028e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4029e7cbe64fSgw MAXPATHLEN); 4030e7cbe64fSgw } else { 4031e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4032e7cbe64fSgw "%s/%s", poolname, ++cp); 4033e7cbe64fSgw } 4034e7cbe64fSgw VERIFY(nvlist_add_string(config, 4035e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4036e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4037e7cbe64fSgw } 4038e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4039e7cbe64fSgw } 4040e7cbe64fSgw 404199653d4eSeschrock /* 4042fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 404399653d4eSeschrock */ 40446809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 404599653d4eSeschrock spa_add_spares(spa, config); 4046fa94a07fSbrendan spa_add_l2cache(spa, config); 40476809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4048fa9e4066Sahrens } 4049fa9e4066Sahrens 4050fa9e4066Sahrens spa_unload(spa); 4051fa9e4066Sahrens spa_deactivate(spa); 4052fa9e4066Sahrens spa_remove(spa); 4053fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4054fa9e4066Sahrens 4055fa9e4066Sahrens return (config); 4056fa9e4066Sahrens } 4057fa9e4066Sahrens 4058fa9e4066Sahrens /* 4059fa9e4066Sahrens * Pool export/destroy 4060fa9e4066Sahrens * 4061fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4062fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4063fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4064394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4065394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4066fa9e4066Sahrens */ 4067fa9e4066Sahrens static int 406889a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4069394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4070fa9e4066Sahrens { 4071fa9e4066Sahrens spa_t *spa; 4072fa9e4066Sahrens 407344cd46caSbillm if (oldconfig) 407444cd46caSbillm *oldconfig = NULL; 407544cd46caSbillm 40768ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4077fa9e4066Sahrens return (EROFS); 4078fa9e4066Sahrens 4079fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4080fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4081fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4082fa9e4066Sahrens return (ENOENT); 4083fa9e4066Sahrens } 4084fa9e4066Sahrens 4085ea8dc4b6Seschrock /* 4086ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4087ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4088ea8dc4b6Seschrock */ 4089ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4090ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4091ea8dc4b6Seschrock spa_async_suspend(spa); 4092ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4093ea8dc4b6Seschrock spa_close(spa, FTAG); 4094ea8dc4b6Seschrock 4095fa9e4066Sahrens /* 4096fa9e4066Sahrens * The pool will be in core if it's openable, 4097fa9e4066Sahrens * in which case we can modify its state. 4098fa9e4066Sahrens */ 4099fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4100fa9e4066Sahrens /* 4101fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4102fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4103fa9e4066Sahrens */ 4104fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4105fa9e4066Sahrens 4106ea8dc4b6Seschrock /* 4107ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4108ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4109ea8dc4b6Seschrock * fault injection handlers. 4110ea8dc4b6Seschrock */ 4111ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4112ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4113ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4114ea8dc4b6Seschrock spa_async_resume(spa); 4115fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4116fa9e4066Sahrens return (EBUSY); 4117fa9e4066Sahrens } 4118fa9e4066Sahrens 411989a89ebfSlling /* 412089a89ebfSlling * A pool cannot be exported if it has an active shared spare. 412189a89ebfSlling * This is to prevent other pools stealing the active spare 412289a89ebfSlling * from an exported pool. At user's own will, such pool can 412389a89ebfSlling * be forcedly exported. 412489a89ebfSlling */ 412589a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 412689a89ebfSlling spa_has_active_shared_spare(spa)) { 412789a89ebfSlling spa_async_resume(spa); 412889a89ebfSlling mutex_exit(&spa_namespace_lock); 412989a89ebfSlling return (EXDEV); 413089a89ebfSlling } 413189a89ebfSlling 4132fa9e4066Sahrens /* 4133fa9e4066Sahrens * We want this to be reflected on every label, 4134fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4135fa9e4066Sahrens * final sync that pushes these changes out. 4136fa9e4066Sahrens */ 4137394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4138e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4139ea8dc4b6Seschrock spa->spa_state = new_state; 41403f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 41413f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4142ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4143e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4144ea8dc4b6Seschrock } 4145fa9e4066Sahrens } 4146fa9e4066Sahrens 41473d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 41483d7072f8Seschrock 4149fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4150fa9e4066Sahrens spa_unload(spa); 4151fa9e4066Sahrens spa_deactivate(spa); 4152fa9e4066Sahrens } 4153fa9e4066Sahrens 415444cd46caSbillm if (oldconfig && spa->spa_config) 415544cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 415644cd46caSbillm 4157ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4158394ab0cbSGeorge Wilson if (!hardforce) 4159394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4160ea8dc4b6Seschrock spa_remove(spa); 4161ea8dc4b6Seschrock } 4162fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4163fa9e4066Sahrens 4164fa9e4066Sahrens return (0); 4165fa9e4066Sahrens } 4166fa9e4066Sahrens 4167fa9e4066Sahrens /* 4168fa9e4066Sahrens * Destroy a storage pool. 4169fa9e4066Sahrens */ 4170fa9e4066Sahrens int 4171fa9e4066Sahrens spa_destroy(char *pool) 4172fa9e4066Sahrens { 4173394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4174394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4175fa9e4066Sahrens } 4176fa9e4066Sahrens 4177fa9e4066Sahrens /* 4178fa9e4066Sahrens * Export a storage pool. 4179fa9e4066Sahrens */ 4180fa9e4066Sahrens int 4181394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4182394ab0cbSGeorge Wilson boolean_t hardforce) 4183fa9e4066Sahrens { 4184394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4185394ab0cbSGeorge Wilson force, hardforce)); 4186fa9e4066Sahrens } 4187fa9e4066Sahrens 4188ea8dc4b6Seschrock /* 4189ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4190ea8dc4b6Seschrock * from the namespace in any way. 4191ea8dc4b6Seschrock */ 4192ea8dc4b6Seschrock int 4193ea8dc4b6Seschrock spa_reset(char *pool) 4194ea8dc4b6Seschrock { 419589a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4196394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4197ea8dc4b6Seschrock } 4198ea8dc4b6Seschrock 4199fa9e4066Sahrens /* 4200fa9e4066Sahrens * ========================================================================== 4201fa9e4066Sahrens * Device manipulation 4202fa9e4066Sahrens * ========================================================================== 4203fa9e4066Sahrens */ 4204fa9e4066Sahrens 4205fa9e4066Sahrens /* 42068654d025Sperrin * Add a device to a storage pool. 4207fa9e4066Sahrens */ 4208fa9e4066Sahrens int 4209fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4210fa9e4066Sahrens { 421188ecc943SGeorge Wilson uint64_t txg, id; 42128ad4d6ddSJeff Bonwick int error; 4213fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 42140e34b6a7Sbonwick vdev_t *vd, *tvd; 4215fa94a07fSbrendan nvlist_t **spares, **l2cache; 4216fa94a07fSbrendan uint_t nspares, nl2cache; 4217fa9e4066Sahrens 4218f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4219f9af39baSGeorge Wilson 4220fa9e4066Sahrens txg = spa_vdev_enter(spa); 4221fa9e4066Sahrens 422299653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 422399653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 422499653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4225fa9e4066Sahrens 4226e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 422799653d4eSeschrock 4228fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4229fa94a07fSbrendan &nspares) != 0) 423099653d4eSeschrock nspares = 0; 423199653d4eSeschrock 4232fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4233fa94a07fSbrendan &nl2cache) != 0) 4234fa94a07fSbrendan nl2cache = 0; 4235fa94a07fSbrendan 4236e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4237fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4238fa9e4066Sahrens 4239e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4240e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4241e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 424299653d4eSeschrock 424339c23413Seschrock /* 4244fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4245fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 424639c23413Seschrock */ 4247e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 424839c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 424939c23413Seschrock 425039c23413Seschrock /* 425139c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 425239c23413Seschrock */ 42538ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 425488ecc943SGeorge Wilson 425588ecc943SGeorge Wilson /* 425688ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 425788ecc943SGeorge Wilson */ 425888ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 425988ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 426088ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 426188ecc943SGeorge Wilson break; 426288ecc943SGeorge Wilson } 426388ecc943SGeorge Wilson } 426439c23413Seschrock tvd = vd->vdev_child[c]; 426539c23413Seschrock vdev_remove_child(vd, tvd); 426688ecc943SGeorge Wilson tvd->vdev_id = id; 426739c23413Seschrock vdev_add_child(rvd, tvd); 426839c23413Seschrock vdev_config_dirty(tvd); 426939c23413Seschrock } 427039c23413Seschrock 427199653d4eSeschrock if (nspares != 0) { 4272fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4273fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 427499653d4eSeschrock spa_load_spares(spa); 4275fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4276fa94a07fSbrendan } 4277fa94a07fSbrendan 4278fa94a07fSbrendan if (nl2cache != 0) { 4279fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4280fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4281fa94a07fSbrendan spa_load_l2cache(spa); 4282fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4283fa9e4066Sahrens } 4284fa9e4066Sahrens 4285fa9e4066Sahrens /* 42860e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 42870e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 42880e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 42890e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 42900e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 42910e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 42920373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 42930e34b6a7Sbonwick * 42940e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 42950e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 42960e34b6a7Sbonwick * steps will be completed the next time we load the pool. 42970e34b6a7Sbonwick */ 42980373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 42990e34b6a7Sbonwick 43000373e76bSbonwick mutex_enter(&spa_namespace_lock); 43010373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 43020373e76bSbonwick mutex_exit(&spa_namespace_lock); 4303fa9e4066Sahrens 43040373e76bSbonwick return (0); 4305fa9e4066Sahrens } 4306fa9e4066Sahrens 4307fa9e4066Sahrens /* 4308fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4309fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4310fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4311fa9e4066Sahrens * 4312fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4313fa9e4066Sahrens * existing device; in this case the two devices are made into their own 43143d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4315fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4316fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4317fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4318fa9e4066Sahrens * is automatically detached. 4319fa9e4066Sahrens */ 4320fa9e4066Sahrens int 4321ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4322fa9e4066Sahrens { 43233f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4324fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4325fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 432699653d4eSeschrock vdev_ops_t *pvops; 43279b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 43289b3f6b42SEric Kustarz int newvd_isspare; 43299b3f6b42SEric Kustarz int error; 4330fa9e4066Sahrens 4331f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4332f9af39baSGeorge Wilson 4333fa9e4066Sahrens txg = spa_vdev_enter(spa); 4334fa9e4066Sahrens 4335c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4336fa9e4066Sahrens 4337fa9e4066Sahrens if (oldvd == NULL) 4338fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4339fa9e4066Sahrens 43400e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 43410e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 43420e34b6a7Sbonwick 4343fa9e4066Sahrens pvd = oldvd->vdev_parent; 4344fa9e4066Sahrens 434599653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4346cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 43473d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 43483d7072f8Seschrock 43493d7072f8Seschrock if (newrootvd->vdev_children != 1) 4350fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4351fa9e4066Sahrens 4352fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4353fa9e4066Sahrens 4354fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4355fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4356fa9e4066Sahrens 435799653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4358fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4359fa9e4066Sahrens 43608654d025Sperrin /* 43618654d025Sperrin * Spares can't replace logs 43628654d025Sperrin */ 4363ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 43648654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 43658654d025Sperrin 436699653d4eSeschrock if (!replacing) { 436799653d4eSeschrock /* 436899653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 436999653d4eSeschrock * vdev. 437099653d4eSeschrock */ 437199653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 437299653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 437399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 437499653d4eSeschrock 437599653d4eSeschrock pvops = &vdev_mirror_ops; 437699653d4eSeschrock } else { 437799653d4eSeschrock /* 437899653d4eSeschrock * Active hot spares can only be replaced by inactive hot 437999653d4eSeschrock * spares. 438099653d4eSeschrock */ 438199653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4382cb04b873SMark J Musante oldvd->vdev_isspare && 438399653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 438499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 438599653d4eSeschrock 438699653d4eSeschrock /* 438799653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 438899653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 438939c23413Seschrock * want to create a replacing vdev. The user is not allowed to 439039c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 439139c23413Seschrock * the same (spare replaces spare, non-spare replaces 439239c23413Seschrock * non-spare). 439399653d4eSeschrock */ 4394cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4395cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 439699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4397cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4398cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 439939c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4400cb04b873SMark J Musante } 4401cb04b873SMark J Musante 4402cb04b873SMark J Musante if (newvd->vdev_isspare) 440399653d4eSeschrock pvops = &vdev_spare_ops; 440499653d4eSeschrock else 440599653d4eSeschrock pvops = &vdev_replacing_ops; 440699653d4eSeschrock } 440799653d4eSeschrock 44082a79c5feSlling /* 4409573ca77eSGeorge Wilson * Make sure the new device is big enough. 44102a79c5feSlling */ 4411573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4412fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4413fa9e4066Sahrens 4414ecc2d604Sbonwick /* 4415ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4416ecc2d604Sbonwick * than the top-level vdev. 4417ecc2d604Sbonwick */ 4418ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4419fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4420fa9e4066Sahrens 4421fa9e4066Sahrens /* 4422fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4423fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4424fa9e4066Sahrens */ 4425fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4426fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4427fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4428fa9e4066Sahrens KM_SLEEP); 4429fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4430fa9e4066Sahrens newvd->vdev_path, "old"); 4431fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4432fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4433fa9e4066Sahrens oldvd->vdev_devid = NULL; 4434fa9e4066Sahrens } 4435fa9e4066Sahrens } 4436fa9e4066Sahrens 4437cb04b873SMark J Musante /* mark the device being resilvered */ 4438cb04b873SMark J Musante newvd->vdev_resilvering = B_TRUE; 4439cb04b873SMark J Musante 4440fa9e4066Sahrens /* 444199653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 444299653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4443fa9e4066Sahrens */ 4444fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4445fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4446fa9e4066Sahrens 4447fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4448fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4449fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4450fa9e4066Sahrens 4451fa9e4066Sahrens /* 4452fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4453fa9e4066Sahrens */ 4454fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4455fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 445688ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4457fa9e4066Sahrens vdev_add_child(pvd, newvd); 4458fa9e4066Sahrens 4459fa9e4066Sahrens tvd = newvd->vdev_top; 4460fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4461fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4462fa9e4066Sahrens 4463fa9e4066Sahrens vdev_config_dirty(tvd); 4464fa9e4066Sahrens 4465fa9e4066Sahrens /* 44663f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 44673f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 44683f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4469fa9e4066Sahrens */ 44703f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4471fa9e4066Sahrens 44723f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 44733f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4474fa9e4066Sahrens 44756809eb4eSEric Schrock if (newvd->vdev_isspare) { 447639c23413Seschrock spa_spare_activate(newvd); 44776809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 44786809eb4eSEric Schrock } 44796809eb4eSEric Schrock 4480e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4481e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 44829b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4483ea8dc4b6Seschrock 4484fa9e4066Sahrens /* 4485fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4486fa9e4066Sahrens */ 4487ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4488fa9e4066Sahrens 44893f9d6ad7SLin Ling /* 44903f9d6ad7SLin Ling * Restart the resilver 44913f9d6ad7SLin Ling */ 44923f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 44933f9d6ad7SLin Ling 44943f9d6ad7SLin Ling /* 44953f9d6ad7SLin Ling * Commit the config 44963f9d6ad7SLin Ling */ 44973f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4498fa9e4066Sahrens 44994445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 45003f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4501c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4502c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4503c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 45049b3f6b42SEric Kustarz 45059b3f6b42SEric Kustarz spa_strfree(oldvdpath); 45069b3f6b42SEric Kustarz spa_strfree(newvdpath); 45079b3f6b42SEric Kustarz 4508943e9869SLori Alt if (spa->spa_bootfs) 4509943e9869SLori Alt spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 4510943e9869SLori Alt 4511fa9e4066Sahrens return (0); 4512fa9e4066Sahrens } 4513fa9e4066Sahrens 4514fa9e4066Sahrens /* 4515fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4516fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4517fa9e4066Sahrens * is a replacing vdev. 4518fa9e4066Sahrens */ 4519fa9e4066Sahrens int 45208ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4521fa9e4066Sahrens { 4522fa9e4066Sahrens uint64_t txg; 45238ad4d6ddSJeff Bonwick int error; 4524fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4525fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 452699653d4eSeschrock boolean_t unspare = B_FALSE; 4527d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 45281195e687SMark J Musante char *vdpath; 4529fa9e4066Sahrens 4530f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4531f9af39baSGeorge Wilson 4532fa9e4066Sahrens txg = spa_vdev_enter(spa); 4533fa9e4066Sahrens 4534c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4535fa9e4066Sahrens 4536fa9e4066Sahrens if (vd == NULL) 4537fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4538fa9e4066Sahrens 45390e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 45400e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 45410e34b6a7Sbonwick 4542fa9e4066Sahrens pvd = vd->vdev_parent; 4543fa9e4066Sahrens 45448ad4d6ddSJeff Bonwick /* 45458ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 45468ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 45478ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 45488ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 45498ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 45508ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 45518ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 45528ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 45538ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 45548ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 45558ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 45568ad4d6ddSJeff Bonwick */ 45578ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 45588ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 45598ad4d6ddSJeff Bonwick 4560fa9e4066Sahrens /* 4561cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 456299653d4eSeschrock */ 4563cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4564cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4565cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 456699653d4eSeschrock 456799653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4568e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4569fa9e4066Sahrens 4570fa9e4066Sahrens /* 457199653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4572fa9e4066Sahrens */ 4573fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 457499653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 457599653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4576fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4577fa9e4066Sahrens 4578fa9e4066Sahrens /* 45798ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 45808ad4d6ddSJeff Bonwick * we cannot safely detach it. 4581fa9e4066Sahrens */ 45828ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4583fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4584fa9e4066Sahrens 45858ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4586fa9e4066Sahrens 4587bf82a41bSeschrock /* 4588bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4589bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4590bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4591bf82a41bSeschrock */ 4592cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4593cb04b873SMark J Musante vd->vdev_path != NULL) { 4594cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4595cb04b873SMark J Musante 4596cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4597cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4598cb04b873SMark J Musante 4599cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4600cb04b873SMark J Musante continue; 4601cb04b873SMark J Musante 4602cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4603cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4604cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4605cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4606cb04b873SMark J Musante break; 4607cb04b873SMark J Musante } 4608bf82a41bSeschrock } 4609bf82a41bSeschrock } 4610bf82a41bSeschrock 461199653d4eSeschrock /* 461299653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 461399653d4eSeschrock * that the spare should become a real disk, and be removed from the 461499653d4eSeschrock * active spare list for the pool. 461599653d4eSeschrock */ 461699653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4617cb04b873SMark J Musante vd->vdev_id == 0 && 4618cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 461999653d4eSeschrock unspare = B_TRUE; 462099653d4eSeschrock 4621fa9e4066Sahrens /* 4622fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4623fa9e4066Sahrens * This must be done after all other error cases are handled, 4624fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4625fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4626fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4627fa9e4066Sahrens * it's being detached! 4628fa9e4066Sahrens */ 462939c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4630fa9e4066Sahrens 4631fa9e4066Sahrens /* 4632fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4633fa9e4066Sahrens */ 4634fa9e4066Sahrens vdev_remove_child(pvd, vd); 4635fa9e4066Sahrens vdev_compact_children(pvd); 4636fa9e4066Sahrens 4637fa9e4066Sahrens /* 4638fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4639fa9e4066Sahrens */ 4640cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4641fa9e4066Sahrens 464299653d4eSeschrock /* 464399653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 46448ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 46458ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 46468ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 46478ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 46488ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 46498ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 465099653d4eSeschrock */ 465199653d4eSeschrock if (unspare) { 465299653d4eSeschrock ASSERT(cvd->vdev_isspare); 465339c23413Seschrock spa_spare_remove(cvd); 465499653d4eSeschrock unspare_guid = cvd->vdev_guid; 46558ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4656cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 465799653d4eSeschrock } 465899653d4eSeschrock 4659fa9e4066Sahrens /* 4660fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4661fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4662fa9e4066Sahrens */ 4663cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4664cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4665cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4666fa9e4066Sahrens vdev_remove_parent(cvd); 4667cb04b873SMark J Musante cvd->vdev_resilvering = B_FALSE; 4668cb04b873SMark J Musante } 4669cb04b873SMark J Musante 4670fa9e4066Sahrens 4671fa9e4066Sahrens /* 4672fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4673fa9e4066Sahrens * may have been the previous top-level vdev. 4674fa9e4066Sahrens */ 4675fa9e4066Sahrens tvd = cvd->vdev_top; 4676fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4677fa9e4066Sahrens 4678fa9e4066Sahrens /* 467939c23413Seschrock * Reevaluate the parent vdev state. 4680fa9e4066Sahrens */ 46813d7072f8Seschrock vdev_propagate_state(cvd); 4682fa9e4066Sahrens 4683fa9e4066Sahrens /* 4684573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4685573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4686573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4687573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4688573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4689fa9e4066Sahrens */ 4690573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4691573ca77eSGeorge Wilson vdev_reopen(tvd); 4692573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4693573ca77eSGeorge Wilson } 4694fa9e4066Sahrens 4695fa9e4066Sahrens vdev_config_dirty(tvd); 4696fa9e4066Sahrens 4697fa9e4066Sahrens /* 469839c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 469939c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 470039c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 470139c23413Seschrock * prevent vd from being accessed after it's freed. 4702fa9e4066Sahrens */ 47031195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 47048ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4705fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4706ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4707ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4708fa9e4066Sahrens 47093d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 47103d7072f8Seschrock 4711cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4712cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4713cb04b873SMark J Musante 471499653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 471599653d4eSeschrock 47164445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 47171195e687SMark J Musante "vdev=%s", vdpath); 47181195e687SMark J Musante spa_strfree(vdpath); 47191195e687SMark J Musante 472099653d4eSeschrock /* 472139c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 472239c23413Seschrock * then we want to go through and remove the device from the hot spare 472339c23413Seschrock * list of every other pool. 472499653d4eSeschrock */ 472599653d4eSeschrock if (unspare) { 4726cb04b873SMark J Musante spa_t *altspa = NULL; 4727cb04b873SMark J Musante 472899653d4eSeschrock mutex_enter(&spa_namespace_lock); 4729cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4730cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4731cb04b873SMark J Musante altspa == spa) 473299653d4eSeschrock continue; 4733cb04b873SMark J Musante 4734cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 47359af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4736cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 47379af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4738cb04b873SMark J Musante spa_close(altspa, FTAG); 473999653d4eSeschrock } 474099653d4eSeschrock mutex_exit(&spa_namespace_lock); 4741cb04b873SMark J Musante 4742cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4743cb04b873SMark J Musante spa_vdev_resilver_done(spa); 474499653d4eSeschrock } 474599653d4eSeschrock 4746cb04b873SMark J Musante /* all done with the spa; OK to release */ 4747cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 4748cb04b873SMark J Musante spa_close(spa, FTAG); 4749cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 4750cb04b873SMark J Musante 475199653d4eSeschrock return (error); 475299653d4eSeschrock } 475399653d4eSeschrock 47541195e687SMark J Musante /* 47551195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 47561195e687SMark J Musante */ 47571195e687SMark J Musante int 47581195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 47591195e687SMark J Musante nvlist_t *props, boolean_t exp) 47601195e687SMark J Musante { 47611195e687SMark J Musante int error = 0; 47621195e687SMark J Musante uint64_t txg, *glist; 47631195e687SMark J Musante spa_t *newspa; 47641195e687SMark J Musante uint_t c, children, lastlog; 47651195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 47661195e687SMark J Musante dmu_tx_t *tx; 47671195e687SMark J Musante char *altroot = NULL; 47681195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 47691195e687SMark J Musante boolean_t activate_slog; 47701195e687SMark J Musante 4771f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 47721195e687SMark J Musante 47731195e687SMark J Musante txg = spa_vdev_enter(spa); 47741195e687SMark J Musante 47751195e687SMark J Musante /* clear the log and flush everything up to now */ 47761195e687SMark J Musante activate_slog = spa_passivate_log(spa); 47771195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 47781195e687SMark J Musante error = spa_offline_log(spa); 47791195e687SMark J Musante txg = spa_vdev_config_enter(spa); 47801195e687SMark J Musante 47811195e687SMark J Musante if (activate_slog) 47821195e687SMark J Musante spa_activate_log(spa); 47831195e687SMark J Musante 47841195e687SMark J Musante if (error != 0) 47851195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 47861195e687SMark J Musante 47871195e687SMark J Musante /* check new spa name before going any further */ 47881195e687SMark J Musante if (spa_lookup(newname) != NULL) 47891195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 47901195e687SMark J Musante 47911195e687SMark J Musante /* 47921195e687SMark J Musante * scan through all the children to ensure they're all mirrors 47931195e687SMark J Musante */ 47941195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 47951195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 47961195e687SMark J Musante &children) != 0) 47971195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47981195e687SMark J Musante 47991195e687SMark J Musante /* first, check to ensure we've got the right child count */ 48001195e687SMark J Musante rvd = spa->spa_root_vdev; 48011195e687SMark J Musante lastlog = 0; 48021195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 48031195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 48041195e687SMark J Musante 48051195e687SMark J Musante /* don't count the holes & logs as children */ 48061195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 48071195e687SMark J Musante if (lastlog == 0) 48081195e687SMark J Musante lastlog = c; 48091195e687SMark J Musante continue; 48101195e687SMark J Musante } 48111195e687SMark J Musante 48121195e687SMark J Musante lastlog = 0; 48131195e687SMark J Musante } 48141195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 48151195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 48161195e687SMark J Musante 48171195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 48181195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 48191195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 48201195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 48211195e687SMark J Musante 48221195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 48231195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 48241195e687SMark J Musante 48251195e687SMark J Musante /* then, loop over each vdev and validate it */ 48261195e687SMark J Musante for (c = 0; c < children; c++) { 48271195e687SMark J Musante uint64_t is_hole = 0; 48281195e687SMark J Musante 48291195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 48301195e687SMark J Musante &is_hole); 48311195e687SMark J Musante 48321195e687SMark J Musante if (is_hole != 0) { 48331195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 48341195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 48351195e687SMark J Musante continue; 48361195e687SMark J Musante } else { 48371195e687SMark J Musante error = EINVAL; 48381195e687SMark J Musante break; 48391195e687SMark J Musante } 48401195e687SMark J Musante } 48411195e687SMark J Musante 48421195e687SMark J Musante /* which disk is going to be split? */ 48431195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 48441195e687SMark J Musante &glist[c]) != 0) { 48451195e687SMark J Musante error = EINVAL; 48461195e687SMark J Musante break; 48471195e687SMark J Musante } 48481195e687SMark J Musante 48491195e687SMark J Musante /* look it up in the spa */ 48501195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 48511195e687SMark J Musante if (vml[c] == NULL) { 48521195e687SMark J Musante error = ENODEV; 48531195e687SMark J Musante break; 48541195e687SMark J Musante } 48551195e687SMark J Musante 48561195e687SMark J Musante /* make sure there's nothing stopping the split */ 48571195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 48581195e687SMark J Musante vml[c]->vdev_islog || 48591195e687SMark J Musante vml[c]->vdev_ishole || 48601195e687SMark J Musante vml[c]->vdev_isspare || 48611195e687SMark J Musante vml[c]->vdev_isl2cache || 48621195e687SMark J Musante !vdev_writeable(vml[c]) || 4863d41c4376SMark J Musante vml[c]->vdev_children != 0 || 48641195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 48651195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 48661195e687SMark J Musante error = EINVAL; 48671195e687SMark J Musante break; 48681195e687SMark J Musante } 48691195e687SMark J Musante 48701195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 48711195e687SMark J Musante error = EBUSY; 48721195e687SMark J Musante break; 48731195e687SMark J Musante } 48741195e687SMark J Musante 48751195e687SMark J Musante /* we need certain info from the top level */ 48761195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 48771195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 48781195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 48791195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 48801195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 48811195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 48821195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 48831195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 48841195e687SMark J Musante } 48851195e687SMark J Musante 48861195e687SMark J Musante if (error != 0) { 48871195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 48881195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 48891195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 48901195e687SMark J Musante } 48911195e687SMark J Musante 48921195e687SMark J Musante /* stop writers from using the disks */ 48931195e687SMark J Musante for (c = 0; c < children; c++) { 48941195e687SMark J Musante if (vml[c] != NULL) 48951195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 48961195e687SMark J Musante } 48971195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 48981195e687SMark J Musante 48991195e687SMark J Musante /* 49001195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 49011195e687SMark J Musante * will disappear once the config is regenerated. 49021195e687SMark J Musante */ 49031195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 49041195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 49051195e687SMark J Musante glist, children) == 0); 49061195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 49071195e687SMark J Musante 490898295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 49091195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 49101195e687SMark J Musante nvl) == 0); 491198295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 49121195e687SMark J Musante spa->spa_config_splitting = nvl; 49131195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 49141195e687SMark J Musante 49151195e687SMark J Musante /* configure and create the new pool */ 49161195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 49171195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 49181195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 49191195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 49201195e687SMark J Musante spa_version(spa)) == 0); 49211195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 49221195e687SMark J Musante spa->spa_config_txg) == 0); 49231195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 49241195e687SMark J Musante spa_generate_guid(NULL)) == 0); 49251195e687SMark J Musante (void) nvlist_lookup_string(props, 49261195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 49271195e687SMark J Musante 4928d41c4376SMark J Musante /* add the new pool to the namespace */ 49291195e687SMark J Musante newspa = spa_add(newname, config, altroot); 49301195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 49311195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 49321195e687SMark J Musante 49331195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 49341195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 49351195e687SMark J Musante 49361195e687SMark J Musante if (zio_injection_enabled) 49371195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 49381195e687SMark J Musante 49391195e687SMark J Musante spa_activate(newspa, spa_mode_global); 49401195e687SMark J Musante spa_async_suspend(newspa); 49411195e687SMark J Musante 49421195e687SMark J Musante /* create the new pool from the disks of the original pool */ 49431195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 49441195e687SMark J Musante if (error) 49451195e687SMark J Musante goto out; 49461195e687SMark J Musante 49471195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 49481195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 49491195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 49501195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 49511195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 49521195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 49531195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 49541195e687SMark J Musante B_TRUE)); 49551195e687SMark J Musante } 49561195e687SMark J Musante 49571195e687SMark J Musante /* set the props */ 49581195e687SMark J Musante if (props != NULL) { 49591195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 49601195e687SMark J Musante error = spa_prop_set(newspa, props); 49611195e687SMark J Musante if (error) 49621195e687SMark J Musante goto out; 49631195e687SMark J Musante } 49641195e687SMark J Musante 49651195e687SMark J Musante /* flush everything */ 49661195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 49671195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 49681195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 49691195e687SMark J Musante 49701195e687SMark J Musante if (zio_injection_enabled) 49711195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 49721195e687SMark J Musante 49731195e687SMark J Musante spa_async_resume(newspa); 49741195e687SMark J Musante 49751195e687SMark J Musante /* finally, update the original pool's config */ 49761195e687SMark J Musante txg = spa_vdev_config_enter(spa); 49771195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 49781195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 49791195e687SMark J Musante if (error != 0) 49801195e687SMark J Musante dmu_tx_abort(tx); 49811195e687SMark J Musante for (c = 0; c < children; c++) { 49821195e687SMark J Musante if (vml[c] != NULL) { 49831195e687SMark J Musante vdev_split(vml[c]); 49841195e687SMark J Musante if (error == 0) 49854445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 49864445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 49871195e687SMark J Musante vdev_free(vml[c]); 49881195e687SMark J Musante } 49891195e687SMark J Musante } 49901195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 49911195e687SMark J Musante spa->spa_config_splitting = NULL; 49921195e687SMark J Musante nvlist_free(nvl); 49931195e687SMark J Musante if (error == 0) 49941195e687SMark J Musante dmu_tx_commit(tx); 49951195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 49961195e687SMark J Musante 49971195e687SMark J Musante if (zio_injection_enabled) 49981195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 49991195e687SMark J Musante 50001195e687SMark J Musante /* split is complete; log a history record */ 50014445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 50024445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 50031195e687SMark J Musante 50041195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 50051195e687SMark J Musante 50061195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 50071195e687SMark J Musante if (exp) 50081195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 50091195e687SMark J Musante B_FALSE, B_FALSE); 50101195e687SMark J Musante 50111195e687SMark J Musante return (error); 50121195e687SMark J Musante 50131195e687SMark J Musante out: 50141195e687SMark J Musante spa_unload(newspa); 50151195e687SMark J Musante spa_deactivate(newspa); 50161195e687SMark J Musante spa_remove(newspa); 50171195e687SMark J Musante 50181195e687SMark J Musante txg = spa_vdev_config_enter(spa); 501998295d61SMark J Musante 502098295d61SMark J Musante /* re-online all offlined disks */ 502198295d61SMark J Musante for (c = 0; c < children; c++) { 502298295d61SMark J Musante if (vml[c] != NULL) 502398295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 502498295d61SMark J Musante } 502598295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 502698295d61SMark J Musante 50271195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 50281195e687SMark J Musante spa->spa_config_splitting = NULL; 5029d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 50301195e687SMark J Musante 50311195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 50321195e687SMark J Musante return (error); 50331195e687SMark J Musante } 50341195e687SMark J Musante 5035e14bb325SJeff Bonwick static nvlist_t * 5036e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 503799653d4eSeschrock { 5038e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5039e14bb325SJeff Bonwick uint64_t guid; 504099653d4eSeschrock 5041e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5042e14bb325SJeff Bonwick &guid) == 0); 504399653d4eSeschrock 5044e14bb325SJeff Bonwick if (guid == target_guid) 5045e14bb325SJeff Bonwick return (nvpp[i]); 504699653d4eSeschrock } 504799653d4eSeschrock 5048e14bb325SJeff Bonwick return (NULL); 5049fa94a07fSbrendan } 5050fa94a07fSbrendan 5051e14bb325SJeff Bonwick static void 5052e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 5053e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 5054fa94a07fSbrendan { 5055e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5056fa94a07fSbrendan 5057e14bb325SJeff Bonwick if (count > 1) 5058e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5059fa94a07fSbrendan 5060e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5061e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5062e14bb325SJeff Bonwick continue; 5063e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5064fa94a07fSbrendan } 5065fa94a07fSbrendan 5066e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5067e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5068fa94a07fSbrendan 5069e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5070e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5071fa94a07fSbrendan 5072e14bb325SJeff Bonwick if (count > 1) 5073e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5074fa94a07fSbrendan } 5075fa94a07fSbrendan 507688ecc943SGeorge Wilson /* 507788ecc943SGeorge Wilson * Evacuate the device. 507888ecc943SGeorge Wilson */ 50793f9d6ad7SLin Ling static int 508088ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 508188ecc943SGeorge Wilson { 508288ecc943SGeorge Wilson uint64_t txg; 50833f9d6ad7SLin Ling int error = 0; 508488ecc943SGeorge Wilson 508588ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 508688ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5087b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 508888ecc943SGeorge Wilson 508988ecc943SGeorge Wilson /* 509088ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 509188ecc943SGeorge Wilson * since we need to do I/O but we do keep the 509288ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 509388ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 509488ecc943SGeorge Wilson */ 509588ecc943SGeorge Wilson if (vd->vdev_islog) { 50963f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 50973f9d6ad7SLin Ling error = spa_offline_log(spa); 5098a1521560SJeff Bonwick } else { 50993f9d6ad7SLin Ling error = ENOTSUP; 510088ecc943SGeorge Wilson } 510188ecc943SGeorge Wilson 5102a1521560SJeff Bonwick if (error) 5103a1521560SJeff Bonwick return (error); 5104a1521560SJeff Bonwick 510588ecc943SGeorge Wilson /* 5106a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5107a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 510888ecc943SGeorge Wilson */ 5109fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 511088ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 511188ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 511288ecc943SGeorge Wilson vdev_dirty(vd, 0, NULL, txg); 511388ecc943SGeorge Wilson vdev_config_dirty(vd); 511488ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 511588ecc943SGeorge Wilson 511688ecc943SGeorge Wilson return (0); 511788ecc943SGeorge Wilson } 511888ecc943SGeorge Wilson 511988ecc943SGeorge Wilson /* 512088ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 512188ecc943SGeorge Wilson */ 51223f9d6ad7SLin Ling static void 5123a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 512488ecc943SGeorge Wilson { 512588ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 512688ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 512788ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 512888ecc943SGeorge Wilson 512988ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 513088ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5131b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 513288ecc943SGeorge Wilson 51333f9d6ad7SLin Ling /* 51343f9d6ad7SLin Ling * Only remove any devices which are empty. 51353f9d6ad7SLin Ling */ 51363f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 51373f9d6ad7SLin Ling return; 51383f9d6ad7SLin Ling 513988ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5140b24ab676SJeff Bonwick 5141b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5142b24ab676SJeff Bonwick vdev_state_clean(vd); 5143b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5144b24ab676SJeff Bonwick vdev_config_clean(vd); 5145b24ab676SJeff Bonwick 514688ecc943SGeorge Wilson vdev_free(vd); 514788ecc943SGeorge Wilson 514888ecc943SGeorge Wilson if (last_vdev) { 514988ecc943SGeorge Wilson vdev_compact_children(rvd); 515088ecc943SGeorge Wilson } else { 515188ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 515288ecc943SGeorge Wilson vdev_add_child(rvd, vd); 515388ecc943SGeorge Wilson } 5154fcbfa62bSLin Ling vdev_config_dirty(rvd); 5155fcbfa62bSLin Ling 5156fcbfa62bSLin Ling /* 5157fcbfa62bSLin Ling * Reassess the health of our root vdev. 5158fcbfa62bSLin Ling */ 5159fcbfa62bSLin Ling vdev_reopen(rvd); 516088ecc943SGeorge Wilson } 516188ecc943SGeorge Wilson 51623f9d6ad7SLin Ling /* 51633f9d6ad7SLin Ling * Remove a device from the pool - 51643f9d6ad7SLin Ling * 51653f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 51663f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 51673f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 51683f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 51693f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 51703f9d6ad7SLin Ling */ 51713f9d6ad7SLin Ling 5172fa94a07fSbrendan /* 5173fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 517488ecc943SGeorge Wilson * spares, slogs, and level 2 ARC devices. 5175fa94a07fSbrendan */ 5176fa94a07fSbrendan int 5177fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5178fa94a07fSbrendan { 5179fa94a07fSbrendan vdev_t *vd; 5180a1521560SJeff Bonwick metaslab_group_t *mg; 5181e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 51828ad4d6ddSJeff Bonwick uint64_t txg = 0; 518388ecc943SGeorge Wilson uint_t nspares, nl2cache; 5184fa94a07fSbrendan int error = 0; 51858ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5186fa94a07fSbrendan 5187f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5188f9af39baSGeorge Wilson 51898ad4d6ddSJeff Bonwick if (!locked) 51908ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5191fa94a07fSbrendan 5192c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5193fa94a07fSbrendan 5194fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5195fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5196e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5197e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5198e14bb325SJeff Bonwick /* 5199e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5200e14bb325SJeff Bonwick * in this pool. 5201e14bb325SJeff Bonwick */ 5202e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5203e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5204e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5205e14bb325SJeff Bonwick spa_load_spares(spa); 5206e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5207e14bb325SJeff Bonwick } else { 5208e14bb325SJeff Bonwick error = EBUSY; 5209e14bb325SJeff Bonwick } 5210e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5211fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5212e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5213e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5214e14bb325SJeff Bonwick /* 5215e14bb325SJeff Bonwick * Cache devices can always be removed. 5216e14bb325SJeff Bonwick */ 5217e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5218e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5219fa94a07fSbrendan spa_load_l2cache(spa); 5220fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 522188ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 522288ecc943SGeorge Wilson ASSERT(!locked); 5223b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 522488ecc943SGeorge Wilson 522588ecc943SGeorge Wilson /* 522688ecc943SGeorge Wilson * XXX - Once we have bp-rewrite this should 522788ecc943SGeorge Wilson * become the common case. 522888ecc943SGeorge Wilson */ 522988ecc943SGeorge Wilson 5230a1521560SJeff Bonwick mg = vd->vdev_mg; 5231a1521560SJeff Bonwick 523288ecc943SGeorge Wilson /* 5233a1521560SJeff Bonwick * Stop allocating from this vdev. 523488ecc943SGeorge Wilson */ 5235a1521560SJeff Bonwick metaslab_group_passivate(mg); 523688ecc943SGeorge Wilson 5237b24ab676SJeff Bonwick /* 5238b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5239b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5240b24ab676SJeff Bonwick */ 5241b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5242b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5243b24ab676SJeff Bonwick 5244a1521560SJeff Bonwick /* 5245a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5246a1521560SJeff Bonwick */ 5247a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5248a1521560SJeff Bonwick 524988ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 525088ecc943SGeorge Wilson 5251a1521560SJeff Bonwick /* 5252a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5253a1521560SJeff Bonwick */ 5254a1521560SJeff Bonwick if (error) { 5255a1521560SJeff Bonwick metaslab_group_activate(mg); 5256a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5257a1521560SJeff Bonwick } 5258a1521560SJeff Bonwick 5259a1521560SJeff Bonwick /* 5260a1521560SJeff Bonwick * Clean up the vdev namespace. 5261a1521560SJeff Bonwick */ 5262a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 526388ecc943SGeorge Wilson 5264e14bb325SJeff Bonwick } else if (vd != NULL) { 5265e14bb325SJeff Bonwick /* 5266e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5267e14bb325SJeff Bonwick */ 5268e14bb325SJeff Bonwick error = ENOTSUP; 5269e14bb325SJeff Bonwick } else { 5270e14bb325SJeff Bonwick /* 5271e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5272e14bb325SJeff Bonwick */ 5273e14bb325SJeff Bonwick error = ENOENT; 5274fa94a07fSbrendan } 527599653d4eSeschrock 52768ad4d6ddSJeff Bonwick if (!locked) 52778ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 52788ad4d6ddSJeff Bonwick 52798ad4d6ddSJeff Bonwick return (error); 5280fa9e4066Sahrens } 5281fa9e4066Sahrens 5282fa9e4066Sahrens /* 52833d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 52843d7072f8Seschrock * current spared, so we can detach it. 5285fa9e4066Sahrens */ 5286ea8dc4b6Seschrock static vdev_t * 52873d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5288fa9e4066Sahrens { 5289ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5290fa9e4066Sahrens 5291573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 52923d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5293ea8dc4b6Seschrock if (oldvd != NULL) 5294ea8dc4b6Seschrock return (oldvd); 5295ea8dc4b6Seschrock } 5296fa9e4066Sahrens 52973d7072f8Seschrock /* 5298cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5299cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5300cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5301cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5302cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5303cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 53043d7072f8Seschrock */ 5305cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5306cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5307cb04b873SMark J Musante 5308cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5309ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5310ea8dc4b6Seschrock 53118ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5312e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 53138ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5314ea8dc4b6Seschrock return (oldvd); 5315fa9e4066Sahrens } 5316ea8dc4b6Seschrock 53173d7072f8Seschrock /* 53183d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 53193d7072f8Seschrock */ 5320cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5321cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5322cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5323cb04b873SMark J Musante 5324cb04b873SMark J Musante if (last->vdev_unspare) { 5325cb04b873SMark J Musante oldvd = first; 5326cb04b873SMark J Musante newvd = last; 5327cb04b873SMark J Musante } else if (first->vdev_unspare) { 5328cb04b873SMark J Musante oldvd = last; 5329cb04b873SMark J Musante newvd = first; 5330cb04b873SMark J Musante } else { 5331cb04b873SMark J Musante oldvd = NULL; 5332cb04b873SMark J Musante } 53333d7072f8Seschrock 5334cb04b873SMark J Musante if (oldvd != NULL && 53358ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5336e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5337cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 53383d7072f8Seschrock return (oldvd); 5339cb04b873SMark J Musante 5340cb04b873SMark J Musante /* 5341cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5342cb04b873SMark J Musante * and those spares are not required, then we want to 5343cb04b873SMark J Musante * attempt to free them up now so that they can be used 5344cb04b873SMark J Musante * by other pools. Once we're back down to a single 5345cb04b873SMark J Musante * disk+spare, we stop removing them. 5346cb04b873SMark J Musante */ 5347cb04b873SMark J Musante if (vd->vdev_children > 2) { 5348cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5349cb04b873SMark J Musante 5350cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5351cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5352cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5353cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5354cb04b873SMark J Musante return (newvd); 53553d7072f8Seschrock } 53563d7072f8Seschrock } 53573d7072f8Seschrock 5358ea8dc4b6Seschrock return (NULL); 5359fa9e4066Sahrens } 5360fa9e4066Sahrens 5361ea8dc4b6Seschrock static void 53623d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5363fa9e4066Sahrens { 53648ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 53658ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5366ea8dc4b6Seschrock 53678ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5368ea8dc4b6Seschrock 53693d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 53708ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 53718ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5372ea8dc4b6Seschrock guid = vd->vdev_guid; 53738ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 53748ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 53758ad4d6ddSJeff Bonwick sguid = 0; 537699653d4eSeschrock /* 537799653d4eSeschrock * If we have just finished replacing a hot spared device, then 537899653d4eSeschrock * we need to detach the parent's first child (the original hot 537999653d4eSeschrock * spare) as well. 538099653d4eSeschrock */ 5381cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5382cb04b873SMark J Musante ppvd->vdev_children == 2) { 538399653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 53848ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 538599653d4eSeschrock } 53868ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 53878ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5388ea8dc4b6Seschrock return; 53898ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 539099653d4eSeschrock return; 53918ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5392fa9e4066Sahrens } 5393fa9e4066Sahrens 53948ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5395fa9e4066Sahrens } 5396fa9e4066Sahrens 5397c67d9675Seschrock /* 5398b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5399c67d9675Seschrock */ 5400c67d9675Seschrock int 54016809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 54026809eb4eSEric Schrock boolean_t ispath) 5403c67d9675Seschrock { 5404c5904d13Seschrock vdev_t *vd; 5405208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5406c67d9675Seschrock 5407f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5408f9af39baSGeorge Wilson 5409b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5410c67d9675Seschrock 54116809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5412b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5413c67d9675Seschrock 54140e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5415b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 54160e34b6a7Sbonwick 54176809eb4eSEric Schrock if (ispath) { 5418208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5419208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5420208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5421208044b8SGeorge Wilson sync = B_TRUE; 5422208044b8SGeorge Wilson } 54236809eb4eSEric Schrock } else { 5424208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5425208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5426208044b8SGeorge Wilson sync = B_TRUE; 5427208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 54286809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5429208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5430208044b8SGeorge Wilson sync = B_TRUE; 5431208044b8SGeorge Wilson } 54326809eb4eSEric Schrock } 5433c67d9675Seschrock 5434208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5435c67d9675Seschrock } 5436c67d9675Seschrock 54376809eb4eSEric Schrock int 54386809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 54396809eb4eSEric Schrock { 54406809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 54416809eb4eSEric Schrock } 54426809eb4eSEric Schrock 54436809eb4eSEric Schrock int 54446809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 54456809eb4eSEric Schrock { 54466809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 54476809eb4eSEric Schrock } 54486809eb4eSEric Schrock 5449fa9e4066Sahrens /* 5450fa9e4066Sahrens * ========================================================================== 54513f9d6ad7SLin Ling * SPA Scanning 5452fa9e4066Sahrens * ========================================================================== 5453fa9e4066Sahrens */ 5454fa9e4066Sahrens 5455ea8dc4b6Seschrock int 54563f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5457fa9e4066Sahrens { 5458e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 54593f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 54603f9d6ad7SLin Ling return (EBUSY); 54613f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 54623f9d6ad7SLin Ling } 5463bb8b5132Sek 54643f9d6ad7SLin Ling int 54653f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 54663f9d6ad7SLin Ling { 54673f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 54683f9d6ad7SLin Ling 54693f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5470fa9e4066Sahrens return (ENOTSUP); 5471fa9e4066Sahrens 5472fa9e4066Sahrens /* 5473088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5474088f3894Sahrens * writeable leaf device, we have nothing to do. 5475fa9e4066Sahrens */ 54763f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5477088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5478088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5479ea8dc4b6Seschrock return (0); 5480ea8dc4b6Seschrock } 5481fa9e4066Sahrens 54823f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5483fa9e4066Sahrens } 5484fa9e4066Sahrens 5485ea8dc4b6Seschrock /* 5486ea8dc4b6Seschrock * ========================================================================== 5487ea8dc4b6Seschrock * SPA async task processing 5488ea8dc4b6Seschrock * ========================================================================== 5489ea8dc4b6Seschrock */ 5490ea8dc4b6Seschrock 5491ea8dc4b6Seschrock static void 54923d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5493fa9e4066Sahrens { 549449cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 549598d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 549698d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 549749cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 54981d713200SEric Schrock 54991d713200SEric Schrock /* 55001d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 55011d713200SEric Schrock * vdev_clear() as that will cause us to throw away 55021d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 55031d713200SEric Schrock * device, all of which is a waste. 55041d713200SEric Schrock */ 55051d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 55061d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 55071d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 55081d713200SEric Schrock 5509e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5510ea8dc4b6Seschrock } 551149cf58c0SBrendan Gregg - Sun Microsystems 5512e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 551349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5514ea8dc4b6Seschrock } 5515fa9e4066Sahrens 5516e14bb325SJeff Bonwick static void 5517e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5518e14bb325SJeff Bonwick { 5519e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 552098d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5521e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5522e14bb325SJeff Bonwick } 5523e14bb325SJeff Bonwick 5524e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5525e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5526e14bb325SJeff Bonwick } 5527e14bb325SJeff Bonwick 5528573ca77eSGeorge Wilson static void 5529573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5530573ca77eSGeorge Wilson { 5531573ca77eSGeorge Wilson sysevent_id_t eid; 5532573ca77eSGeorge Wilson nvlist_t *attr; 5533573ca77eSGeorge Wilson char *physpath; 5534573ca77eSGeorge Wilson 5535573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5536573ca77eSGeorge Wilson return; 5537573ca77eSGeorge Wilson 5538573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5539573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5540573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5541573ca77eSGeorge Wilson } 5542573ca77eSGeorge Wilson 5543573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5544573ca77eSGeorge Wilson return; 5545573ca77eSGeorge Wilson 5546573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5547573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5548573ca77eSGeorge Wilson 5549573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5550573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5551573ca77eSGeorge Wilson 5552573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5553573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5554573ca77eSGeorge Wilson 5555573ca77eSGeorge Wilson nvlist_free(attr); 5556573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5557573ca77eSGeorge Wilson } 5558573ca77eSGeorge Wilson 5559ea8dc4b6Seschrock static void 5560ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5561ea8dc4b6Seschrock { 5562e14bb325SJeff Bonwick int tasks; 5563ea8dc4b6Seschrock 5564ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5565ea8dc4b6Seschrock 5566ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5567ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5568ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5569ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5570ea8dc4b6Seschrock 55710373e76bSbonwick /* 55720373e76bSbonwick * See if the config needs to be updated. 55730373e76bSbonwick */ 55740373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5575b24ab676SJeff Bonwick uint64_t old_space, new_space; 5576573ca77eSGeorge Wilson 55770373e76bSbonwick mutex_enter(&spa_namespace_lock); 5578b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 55790373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5580b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 55810373e76bSbonwick mutex_exit(&spa_namespace_lock); 5582573ca77eSGeorge Wilson 5583573ca77eSGeorge Wilson /* 5584573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5585573ca77eSGeorge Wilson * then log an internal history event. 5586573ca77eSGeorge Wilson */ 5587b24ab676SJeff Bonwick if (new_space != old_space) { 55884445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5589c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5590b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5591573ca77eSGeorge Wilson } 55920373e76bSbonwick } 55930373e76bSbonwick 5594ea8dc4b6Seschrock /* 55953d7072f8Seschrock * See if any devices need to be marked REMOVED. 5596ea8dc4b6Seschrock */ 5597e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 55988f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 55993d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5600e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 560149cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5602e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 560349cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5604e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5605e14bb325SJeff Bonwick } 5606e14bb325SJeff Bonwick 5607573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5608573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5609573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5610573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5611573ca77eSGeorge Wilson } 5612573ca77eSGeorge Wilson 5613e14bb325SJeff Bonwick /* 5614e14bb325SJeff Bonwick * See if any devices need to be probed. 5615e14bb325SJeff Bonwick */ 5616e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 56178f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5618e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5619e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 56203d7072f8Seschrock } 5621ea8dc4b6Seschrock 5622ea8dc4b6Seschrock /* 5623ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5624ea8dc4b6Seschrock */ 56253d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 56263d7072f8Seschrock spa_vdev_resilver_done(spa); 5627fa9e4066Sahrens 5628ea8dc4b6Seschrock /* 5629ea8dc4b6Seschrock * Kick off a resilver. 5630ea8dc4b6Seschrock */ 5631088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 56323f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5633ea8dc4b6Seschrock 5634ea8dc4b6Seschrock /* 5635ea8dc4b6Seschrock * Let the world know that we're done. 5636ea8dc4b6Seschrock */ 5637ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5638ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5639ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5640ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5641ea8dc4b6Seschrock thread_exit(); 5642ea8dc4b6Seschrock } 5643ea8dc4b6Seschrock 5644ea8dc4b6Seschrock void 5645ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5646ea8dc4b6Seschrock { 5647ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5648ea8dc4b6Seschrock spa->spa_async_suspended++; 5649ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5650ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5651ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5652ea8dc4b6Seschrock } 5653ea8dc4b6Seschrock 5654ea8dc4b6Seschrock void 5655ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5656ea8dc4b6Seschrock { 5657ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5658ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5659ea8dc4b6Seschrock spa->spa_async_suspended--; 5660ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5661ea8dc4b6Seschrock } 5662ea8dc4b6Seschrock 5663ea8dc4b6Seschrock static void 5664ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5665ea8dc4b6Seschrock { 5666ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5667ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 56680373e76bSbonwick spa->spa_async_thread == NULL && 56690373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 5670ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5671ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5672ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5673ea8dc4b6Seschrock } 5674ea8dc4b6Seschrock 5675ea8dc4b6Seschrock void 5676ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5677ea8dc4b6Seschrock { 56783f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5679ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5680ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5681ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5682fa9e4066Sahrens } 5683fa9e4066Sahrens 5684fa9e4066Sahrens /* 5685fa9e4066Sahrens * ========================================================================== 5686fa9e4066Sahrens * SPA syncing routines 5687fa9e4066Sahrens * ========================================================================== 5688fa9e4066Sahrens */ 5689fa9e4066Sahrens 5690cde58dbcSMatthew Ahrens static int 5691cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5692cde58dbcSMatthew Ahrens { 5693cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5694cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5695cde58dbcSMatthew Ahrens return (0); 5696b24ab676SJeff Bonwick } 5697b24ab676SJeff Bonwick 5698cde58dbcSMatthew Ahrens static int 5699cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5700b24ab676SJeff Bonwick { 5701b24ab676SJeff Bonwick zio_t *zio = arg; 5702b24ab676SJeff Bonwick 5703b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5704b24ab676SJeff Bonwick zio->io_flags)); 5705cde58dbcSMatthew Ahrens return (0); 5706fa9e4066Sahrens } 5707fa9e4066Sahrens 5708fa9e4066Sahrens static void 570999653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5710fa9e4066Sahrens { 5711fa9e4066Sahrens char *packed = NULL; 5712f7991ba4STim Haley size_t bufsize; 5713fa9e4066Sahrens size_t nvsize = 0; 5714fa9e4066Sahrens dmu_buf_t *db; 5715fa9e4066Sahrens 571699653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5717fa9e4066Sahrens 5718f7991ba4STim Haley /* 5719f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 5720f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 5721f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5722f7991ba4STim Haley */ 5723ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 5724f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5725fa9e4066Sahrens 572699653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5727ea8dc4b6Seschrock KM_SLEEP) == 0); 5728f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5729fa9e4066Sahrens 5730f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5731fa9e4066Sahrens 5732f7991ba4STim Haley kmem_free(packed, bufsize); 5733fa9e4066Sahrens 573499653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5735fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5736fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5737ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5738fa9e4066Sahrens } 5739fa9e4066Sahrens 574099653d4eSeschrock static void 5741fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5742fa94a07fSbrendan const char *config, const char *entry) 574399653d4eSeschrock { 574499653d4eSeschrock nvlist_t *nvroot; 5745fa94a07fSbrendan nvlist_t **list; 574699653d4eSeschrock int i; 574799653d4eSeschrock 5748fa94a07fSbrendan if (!sav->sav_sync) 574999653d4eSeschrock return; 575099653d4eSeschrock 575199653d4eSeschrock /* 5752fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5753fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 57543d7072f8Seschrock * valid and the vdevs are labeled appropriately. 575599653d4eSeschrock */ 5756fa94a07fSbrendan if (sav->sav_object == 0) { 5757fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5758fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5759fa94a07fSbrendan sizeof (uint64_t), tx); 576099653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5761fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5762fa94a07fSbrendan &sav->sav_object, tx) == 0); 576399653d4eSeschrock } 576499653d4eSeschrock 576599653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5766fa94a07fSbrendan if (sav->sav_count == 0) { 5767fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 576899653d4eSeschrock } else { 5769fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5770fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5771fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 57723f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5773fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5774fa94a07fSbrendan sav->sav_count) == 0); 5775fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5776fa94a07fSbrendan nvlist_free(list[i]); 5777fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 577899653d4eSeschrock } 577999653d4eSeschrock 5780fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 578106eeb2adSek nvlist_free(nvroot); 578299653d4eSeschrock 5783fa94a07fSbrendan sav->sav_sync = B_FALSE; 578499653d4eSeschrock } 578599653d4eSeschrock 578699653d4eSeschrock static void 578799653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 578899653d4eSeschrock { 578999653d4eSeschrock nvlist_t *config; 579099653d4eSeschrock 5791e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 579299653d4eSeschrock return; 579399653d4eSeschrock 5794e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5795e14bb325SJeff Bonwick 5796e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5797e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5798e14bb325SJeff Bonwick 579925345e46SGeorge Wilson /* 580025345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 580125345e46SGeorge Wilson * the config object gets updated with the correct version. 580225345e46SGeorge Wilson */ 580325345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 580425345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 580525345e46SGeorge Wilson spa->spa_uberblock.ub_version); 580625345e46SGeorge Wilson 5807e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 580899653d4eSeschrock 580999653d4eSeschrock if (spa->spa_config_syncing) 581099653d4eSeschrock nvlist_free(spa->spa_config_syncing); 581199653d4eSeschrock spa->spa_config_syncing = config; 581299653d4eSeschrock 581399653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 581499653d4eSeschrock } 581599653d4eSeschrock 5816ad135b5dSChristopher Siden static void 5817*3b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx) 5818ad135b5dSChristopher Siden { 5819*3b2aab18SMatthew Ahrens uint64_t *versionp = arg; 5820*3b2aab18SMatthew Ahrens uint64_t version = *versionp; 5821*3b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 5822ad135b5dSChristopher Siden 5823ad135b5dSChristopher Siden /* 5824ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 5825ad135b5dSChristopher Siden */ 5826ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 5827ad135b5dSChristopher Siden 582862eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 5829ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 5830ad135b5dSChristopher Siden 5831ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 5832ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 58334445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 5834ad135b5dSChristopher Siden } 5835ad135b5dSChristopher Siden 5836990b4856Slling /* 5837990b4856Slling * Set zpool properties. 5838990b4856Slling */ 5839b1b8ab34Slling static void 5840*3b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx) 5841b1b8ab34Slling { 5842*3b2aab18SMatthew Ahrens nvlist_t *nvp = arg; 5843*3b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 5844b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5845ad135b5dSChristopher Siden nvpair_t *elem = NULL; 5846b1b8ab34Slling 5847e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5848e14bb325SJeff Bonwick 5849990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5850ad135b5dSChristopher Siden uint64_t intval; 5851ad135b5dSChristopher Siden char *strval, *fname; 5852ad135b5dSChristopher Siden zpool_prop_t prop; 5853ad135b5dSChristopher Siden const char *propname; 5854ad135b5dSChristopher Siden zprop_type_t proptype; 5855ad135b5dSChristopher Siden zfeature_info_t *feature; 5856ad135b5dSChristopher Siden 5857990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 5858ad135b5dSChristopher Siden case ZPROP_INVAL: 5859ad135b5dSChristopher Siden /* 5860ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 5861ad135b5dSChristopher Siden */ 5862ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 5863ad135b5dSChristopher Siden 5864ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 5865b420f3adSRichard Lowe VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature)); 5866ad135b5dSChristopher Siden 5867ad135b5dSChristopher Siden spa_feature_enable(spa, feature, tx); 58684445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 58694445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 5870ad135b5dSChristopher Siden break; 5871ad135b5dSChristopher Siden 5872990b4856Slling case ZPOOL_PROP_VERSION: 5873ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5874990b4856Slling /* 5875ad135b5dSChristopher Siden * The version is synced seperatly before other 5876ad135b5dSChristopher Siden * properties and should be correct by now. 5877990b4856Slling */ 5878ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 5879ecd6cf80Smarks break; 5880990b4856Slling 5881990b4856Slling case ZPOOL_PROP_ALTROOT: 5882990b4856Slling /* 5883990b4856Slling * 'altroot' is a non-persistent property. It should 5884990b4856Slling * have been set temporarily at creation or import time. 5885990b4856Slling */ 5886990b4856Slling ASSERT(spa->spa_root != NULL); 5887b1b8ab34Slling break; 58883d7072f8Seschrock 5889f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 58902f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5891990b4856Slling /* 5892f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 5893f9af39baSGeorge Wilson * properties. 5894990b4856Slling */ 58953d7072f8Seschrock break; 58968704186eSDan McDonald case ZPOOL_PROP_COMMENT: 58978704186eSDan McDonald VERIFY(nvpair_value_string(elem, &strval) == 0); 58988704186eSDan McDonald if (spa->spa_comment != NULL) 58998704186eSDan McDonald spa_strfree(spa->spa_comment); 59008704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 59018704186eSDan McDonald /* 59028704186eSDan McDonald * We need to dirty the configuration on all the vdevs 59038704186eSDan McDonald * so that their labels get updated. It's unnecessary 59048704186eSDan McDonald * to do this for pool creation since the vdev's 59058704186eSDan McDonald * configuratoin has already been dirtied. 59068704186eSDan McDonald */ 59078704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 59088704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 59094445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59104445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 59118704186eSDan McDonald break; 5912990b4856Slling default: 5913990b4856Slling /* 5914990b4856Slling * Set pool property values in the poolprops mos object. 5915990b4856Slling */ 5916990b4856Slling if (spa->spa_pool_props_object == 0) { 5917ad135b5dSChristopher Siden spa->spa_pool_props_object = 5918ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 5919990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 5920ad135b5dSChristopher Siden tx); 5921990b4856Slling } 5922990b4856Slling 5923990b4856Slling /* normalize the property name */ 5924990b4856Slling propname = zpool_prop_to_name(prop); 5925990b4856Slling proptype = zpool_prop_get_type(prop); 5926990b4856Slling 5927990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 5928990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 5929990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 5930990b4856Slling VERIFY(zap_update(mos, 5931990b4856Slling spa->spa_pool_props_object, propname, 5932990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 59334445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59344445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 5935990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 5936990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5937990b4856Slling 5938990b4856Slling if (proptype == PROP_TYPE_INDEX) { 5939990b4856Slling const char *unused; 5940990b4856Slling VERIFY(zpool_prop_index_to_string( 5941990b4856Slling prop, intval, &unused) == 0); 5942990b4856Slling } 5943990b4856Slling VERIFY(zap_update(mos, 5944990b4856Slling spa->spa_pool_props_object, propname, 5945990b4856Slling 8, 1, &intval, tx) == 0); 59464445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59474445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 5948990b4856Slling } else { 5949990b4856Slling ASSERT(0); /* not allowed */ 5950990b4856Slling } 5951990b4856Slling 59520a4e9518Sgw switch (prop) { 59530a4e9518Sgw case ZPOOL_PROP_DELEGATION: 5954990b4856Slling spa->spa_delegation = intval; 59550a4e9518Sgw break; 59560a4e9518Sgw case ZPOOL_PROP_BOOTFS: 5957990b4856Slling spa->spa_bootfs = intval; 59580a4e9518Sgw break; 59590a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 59600a4e9518Sgw spa->spa_failmode = intval; 59610a4e9518Sgw break; 5962573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 5963573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 5964b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 5965b98131cfSEric Taylor spa_async_request(spa, 5966b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 5967573ca77eSGeorge Wilson break; 5968b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 5969b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 5970b24ab676SJeff Bonwick break; 59710a4e9518Sgw default: 59720a4e9518Sgw break; 59730a4e9518Sgw } 5974990b4856Slling } 5975990b4856Slling 5976b1b8ab34Slling } 5977e14bb325SJeff Bonwick 5978e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 5979b1b8ab34Slling } 5980b1b8ab34Slling 5981cde58dbcSMatthew Ahrens /* 5982cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 5983cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 5984cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 5985cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 5986cde58dbcSMatthew Ahrens * tasks. 5987cde58dbcSMatthew Ahrens */ 5988cde58dbcSMatthew Ahrens static void 5989cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 5990cde58dbcSMatthew Ahrens { 5991cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5992cde58dbcSMatthew Ahrens 5993cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 5994cde58dbcSMatthew Ahrens 5995*3b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 5996*3b2aab18SMatthew Ahrens 5997cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 5998cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 5999cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 6000cde58dbcSMatthew Ahrens 6001cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 6002cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6003cde58dbcSMatthew Ahrens } 6004cde58dbcSMatthew Ahrens 6005cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6006cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6007cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6008cde58dbcSMatthew Ahrens } 6009cde58dbcSMatthew Ahrens 6010cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6011cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6012cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6013cde58dbcSMatthew Ahrens 6014cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6015cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6016cde58dbcSMatthew Ahrens } 6017ad135b5dSChristopher Siden 6018ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6019ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6020ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6021ad135b5dSChristopher Siden } 6022*3b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 6023cde58dbcSMatthew Ahrens } 6024cde58dbcSMatthew Ahrens 6025fa9e4066Sahrens /* 6026fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6027fa9e4066Sahrens * part of the process, so we iterate until it converges. 6028fa9e4066Sahrens */ 6029fa9e4066Sahrens void 6030fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6031fa9e4066Sahrens { 6032fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6033fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6034cde58dbcSMatthew Ahrens bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 6035b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 60360373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6037fa9e4066Sahrens vdev_t *vd; 6038fa9e4066Sahrens dmu_tx_t *tx; 6039e14bb325SJeff Bonwick int error; 6040fa9e4066Sahrens 6041f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6042f9af39baSGeorge Wilson 6043fa9e4066Sahrens /* 6044fa9e4066Sahrens * Lock out configuration changes. 6045fa9e4066Sahrens */ 6046e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6047fa9e4066Sahrens 6048fa9e4066Sahrens spa->spa_syncing_txg = txg; 6049fa9e4066Sahrens spa->spa_sync_pass = 0; 6050fa9e4066Sahrens 6051e14bb325SJeff Bonwick /* 6052e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6053e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6054e14bb325SJeff Bonwick */ 6055e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 60568ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 60578ad4d6ddSJeff Bonwick /* 60588ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 60598ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 60608ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 60618ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 60628ad4d6ddSJeff Bonwick * into the root vdev tree. 60638ad4d6ddSJeff Bonwick */ 60648ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 60658ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 60668ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 60678ad4d6ddSJeff Bonwick vdev_state_clean(vd); 60688ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 60698ad4d6ddSJeff Bonwick } 60708ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 60718ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6072e14bb325SJeff Bonwick } 6073e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6074e14bb325SJeff Bonwick 607599653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 607699653d4eSeschrock 6077283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6078283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6079283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6080283b8460SGeorge.Wilson 608199653d4eSeschrock /* 6082e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 608399653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 608499653d4eSeschrock */ 6085e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6086e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 608799653d4eSeschrock int i; 608899653d4eSeschrock 608999653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 609099653d4eSeschrock vd = rvd->vdev_child[i]; 609199653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 609299653d4eSeschrock break; 609399653d4eSeschrock } 609499653d4eSeschrock if (i == rvd->vdev_children) { 609599653d4eSeschrock spa->spa_deflate = TRUE; 609699653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 609799653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 609899653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 609999653d4eSeschrock } 610099653d4eSeschrock } 610199653d4eSeschrock 6102fa9e4066Sahrens /* 61033f9d6ad7SLin Ling * If anything has changed in this txg, or if someone is waiting 61043f9d6ad7SLin Ling * for this txg to sync (eg, spa_vdev_remove()), push the 61053f9d6ad7SLin Ling * deferred frees from the previous txg. If not, leave them 61063f9d6ad7SLin Ling * alone so that we don't generate work on an otherwise idle 61073f9d6ad7SLin Ling * system. 6108fa9e4066Sahrens */ 6109fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 61101615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 61113f9d6ad7SLin Ling !txg_list_empty(&dp->dp_sync_tasks, txg) || 6112cde58dbcSMatthew Ahrens ((dsl_scan_active(dp->dp_scan) || 6113cde58dbcSMatthew Ahrens txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 6114cde58dbcSMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 6115cde58dbcSMatthew Ahrens VERIFY3U(bpobj_iterate(defer_bpo, 6116cde58dbcSMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 6117fb09f5aaSMadhav Suresh VERIFY0(zio_wait(zio)); 6118cde58dbcSMatthew Ahrens } 6119fa9e4066Sahrens 6120fa9e4066Sahrens /* 6121fa9e4066Sahrens * Iterate to convergence. 6122fa9e4066Sahrens */ 6123fa9e4066Sahrens do { 6124b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6125fa9e4066Sahrens 6126fa9e4066Sahrens spa_sync_config_object(spa, tx); 6127fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6128fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6129fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6130fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6131ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6132fa9e4066Sahrens dsl_pool_sync(dp, txg); 6133fa9e4066Sahrens 613401f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 6135b24ab676SJeff Bonwick zio_t *zio = zio_root(spa, NULL, NULL, 0); 6136cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, spa_free_sync_cb, 6137cde58dbcSMatthew Ahrens zio, tx); 6138b24ab676SJeff Bonwick VERIFY(zio_wait(zio) == 0); 6139b24ab676SJeff Bonwick } else { 6140cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 6141cde58dbcSMatthew Ahrens defer_bpo, tx); 6142fa9e4066Sahrens } 6143fa9e4066Sahrens 6144b24ab676SJeff Bonwick ddt_sync(spa, txg); 61453f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6146afee20e4SGeorge Wilson 6147b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6148b24ab676SJeff Bonwick vdev_sync(vd, txg); 6149b24ab676SJeff Bonwick 6150cde58dbcSMatthew Ahrens if (pass == 1) 6151cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6152fa9e4066Sahrens 6153cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6154fa9e4066Sahrens 6155fa9e4066Sahrens /* 6156fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6157fa9e4066Sahrens * to commit the transaction group. 61580373e76bSbonwick * 615917f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 616017f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6161e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6162e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 61630373e76bSbonwick */ 6164e14bb325SJeff Bonwick for (;;) { 6165e14bb325SJeff Bonwick /* 6166e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6167e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6168e14bb325SJeff Bonwick */ 6169e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6170e14bb325SJeff Bonwick 6171e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6172e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6173e14bb325SJeff Bonwick int svdcount = 0; 6174e14bb325SJeff Bonwick int children = rvd->vdev_children; 6175e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6176e14bb325SJeff Bonwick 6177573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6178e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6179e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6180e14bb325SJeff Bonwick continue; 6181e14bb325SJeff Bonwick svd[svdcount++] = vd; 6182e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6183e14bb325SJeff Bonwick break; 6184e14bb325SJeff Bonwick } 61858956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 61868956713aSEric Schrock if (error != 0) 61878956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 61888956713aSEric Schrock B_TRUE); 6189e14bb325SJeff Bonwick } else { 6190e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 61918956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 61928956713aSEric Schrock if (error != 0) 61938956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 61948956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 61950373e76bSbonwick } 6196e14bb325SJeff Bonwick 6197dfbb9432SGeorge Wilson if (error == 0) 6198dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6199dfbb9432SGeorge Wilson 6200e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6201e14bb325SJeff Bonwick 6202e14bb325SJeff Bonwick if (error == 0) 6203e14bb325SJeff Bonwick break; 6204e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6205e14bb325SJeff Bonwick zio_resume_wait(spa); 62060373e76bSbonwick } 620799653d4eSeschrock dmu_tx_commit(tx); 620899653d4eSeschrock 6209283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6210283b8460SGeorge.Wilson 62110373e76bSbonwick /* 62120373e76bSbonwick * Clear the dirty config list. 6213fa9e4066Sahrens */ 6214e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 62150373e76bSbonwick vdev_config_clean(vd); 62160373e76bSbonwick 62170373e76bSbonwick /* 62180373e76bSbonwick * Now that the new config has synced transactionally, 62190373e76bSbonwick * let it become visible to the config cache. 62200373e76bSbonwick */ 62210373e76bSbonwick if (spa->spa_config_syncing != NULL) { 62220373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 62230373e76bSbonwick spa->spa_config_txg = txg; 62240373e76bSbonwick spa->spa_config_syncing = NULL; 62250373e76bSbonwick } 6226fa9e4066Sahrens 6227fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 6228fa9e4066Sahrens 6229b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6230fa9e4066Sahrens 6231fa9e4066Sahrens /* 6232fa9e4066Sahrens * Update usable space statistics. 6233fa9e4066Sahrens */ 6234fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6235fa9e4066Sahrens vdev_sync_done(vd, txg); 6236fa9e4066Sahrens 6237485bbbf5SGeorge Wilson spa_update_dspace(spa); 6238485bbbf5SGeorge Wilson 6239fa9e4066Sahrens /* 6240fa9e4066Sahrens * It had better be the case that we didn't dirty anything 624199653d4eSeschrock * since vdev_config_sync(). 6242fa9e4066Sahrens */ 6243fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6244fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6245fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6246b24ab676SJeff Bonwick 6247b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6248fa9e4066Sahrens 6249e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6250ea8dc4b6Seschrock 6251468c413aSTim Haley spa_handle_ignored_writes(spa); 6252468c413aSTim Haley 6253ea8dc4b6Seschrock /* 6254ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6255ea8dc4b6Seschrock */ 6256ea8dc4b6Seschrock spa_async_dispatch(spa); 6257fa9e4066Sahrens } 6258fa9e4066Sahrens 6259fa9e4066Sahrens /* 6260fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6261fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6262fa9e4066Sahrens * sync. 6263fa9e4066Sahrens */ 6264fa9e4066Sahrens void 6265fa9e4066Sahrens spa_sync_allpools(void) 6266fa9e4066Sahrens { 6267fa9e4066Sahrens spa_t *spa = NULL; 6268fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6269fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6270f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6271f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6272fa9e4066Sahrens continue; 6273fa9e4066Sahrens spa_open_ref(spa, FTAG); 6274fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6275fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6276fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6277fa9e4066Sahrens spa_close(spa, FTAG); 6278fa9e4066Sahrens } 6279fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6280fa9e4066Sahrens } 6281fa9e4066Sahrens 6282fa9e4066Sahrens /* 6283fa9e4066Sahrens * ========================================================================== 6284fa9e4066Sahrens * Miscellaneous routines 6285fa9e4066Sahrens * ========================================================================== 6286fa9e4066Sahrens */ 6287fa9e4066Sahrens 6288fa9e4066Sahrens /* 6289fa9e4066Sahrens * Remove all pools in the system. 6290fa9e4066Sahrens */ 6291fa9e4066Sahrens void 6292fa9e4066Sahrens spa_evict_all(void) 6293fa9e4066Sahrens { 6294fa9e4066Sahrens spa_t *spa; 6295fa9e4066Sahrens 6296fa9e4066Sahrens /* 6297fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6298fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6299fa9e4066Sahrens */ 6300fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6301fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6302fa9e4066Sahrens /* 6303ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6304ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6305ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6306fa9e4066Sahrens */ 6307fa9e4066Sahrens spa_open_ref(spa, FTAG); 6308fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6309ea8dc4b6Seschrock spa_async_suspend(spa); 6310fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6311fa9e4066Sahrens spa_close(spa, FTAG); 6312fa9e4066Sahrens 6313fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6314fa9e4066Sahrens spa_unload(spa); 6315fa9e4066Sahrens spa_deactivate(spa); 6316fa9e4066Sahrens } 6317fa9e4066Sahrens spa_remove(spa); 6318fa9e4066Sahrens } 6319fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6320fa9e4066Sahrens } 6321ea8dc4b6Seschrock 6322ea8dc4b6Seschrock vdev_t * 63236809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6324ea8dc4b6Seschrock { 6325c5904d13Seschrock vdev_t *vd; 6326c5904d13Seschrock int i; 6327c5904d13Seschrock 6328c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6329c5904d13Seschrock return (vd); 6330c5904d13Seschrock 63316809eb4eSEric Schrock if (aux) { 6332c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6333c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 63346809eb4eSEric Schrock if (vd->vdev_guid == guid) 63356809eb4eSEric Schrock return (vd); 63366809eb4eSEric Schrock } 63376809eb4eSEric Schrock 63386809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 63396809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6340c5904d13Seschrock if (vd->vdev_guid == guid) 6341c5904d13Seschrock return (vd); 6342c5904d13Seschrock } 6343c5904d13Seschrock } 6344c5904d13Seschrock 6345c5904d13Seschrock return (NULL); 6346ea8dc4b6Seschrock } 6347eaca9bbdSeschrock 6348eaca9bbdSeschrock void 6349990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6350eaca9bbdSeschrock { 6351f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6352f9af39baSGeorge Wilson 6353e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6354eaca9bbdSeschrock 6355eaca9bbdSeschrock /* 6356eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6357eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6358eaca9bbdSeschrock * possible. 6359eaca9bbdSeschrock */ 636062eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 6361990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 6362eaca9bbdSeschrock 6363990b4856Slling spa->spa_uberblock.ub_version = version; 6364eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6365eaca9bbdSeschrock 6366e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 636799653d4eSeschrock 636899653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 636999653d4eSeschrock } 637099653d4eSeschrock 637199653d4eSeschrock boolean_t 637299653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 637399653d4eSeschrock { 637499653d4eSeschrock int i; 637539c23413Seschrock uint64_t spareguid; 6376fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 637799653d4eSeschrock 6378fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6379fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 638099653d4eSeschrock return (B_TRUE); 638199653d4eSeschrock 6382fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6383fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6384fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 638539c23413Seschrock return (B_TRUE); 638639c23413Seschrock } 638739c23413Seschrock 638899653d4eSeschrock return (B_FALSE); 6389eaca9bbdSeschrock } 6390b1b8ab34Slling 639189a89ebfSlling /* 639289a89ebfSlling * Check if a pool has an active shared spare device. 639389a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 639489a89ebfSlling */ 639589a89ebfSlling static boolean_t 639689a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 639789a89ebfSlling { 639889a89ebfSlling int i, refcnt; 639989a89ebfSlling uint64_t pool; 640089a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 640189a89ebfSlling 640289a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 640389a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 640489a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 640589a89ebfSlling refcnt > 2) 640689a89ebfSlling return (B_TRUE); 640789a89ebfSlling } 640889a89ebfSlling 640989a89ebfSlling return (B_FALSE); 641089a89ebfSlling } 641189a89ebfSlling 64123d7072f8Seschrock /* 64133d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 64143d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 64153d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 64163d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 64173d7072f8Seschrock * or zdb as real changes. 64183d7072f8Seschrock */ 64193d7072f8Seschrock void 64203d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 64213d7072f8Seschrock { 64223d7072f8Seschrock #ifdef _KERNEL 64233d7072f8Seschrock sysevent_t *ev; 64243d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 64253d7072f8Seschrock sysevent_value_t value; 64263d7072f8Seschrock sysevent_id_t eid; 64273d7072f8Seschrock 64283d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 64293d7072f8Seschrock SE_SLEEP); 64303d7072f8Seschrock 64313d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 64323d7072f8Seschrock value.value.sv_string = spa_name(spa); 64333d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 64343d7072f8Seschrock goto done; 64353d7072f8Seschrock 64363d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 64373d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 64383d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 64393d7072f8Seschrock goto done; 64403d7072f8Seschrock 64413d7072f8Seschrock if (vd) { 64423d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 64433d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 64443d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 64453d7072f8Seschrock SE_SLEEP) != 0) 64463d7072f8Seschrock goto done; 64473d7072f8Seschrock 64483d7072f8Seschrock if (vd->vdev_path) { 64493d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 64503d7072f8Seschrock value.value.sv_string = vd->vdev_path; 64513d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 64523d7072f8Seschrock &value, SE_SLEEP) != 0) 64533d7072f8Seschrock goto done; 64543d7072f8Seschrock } 64553d7072f8Seschrock } 64563d7072f8Seschrock 6457b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6458b01c3b58Seschrock goto done; 6459b01c3b58Seschrock attr = NULL; 6460b01c3b58Seschrock 64613d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 64623d7072f8Seschrock 64633d7072f8Seschrock done: 64643d7072f8Seschrock if (attr) 64653d7072f8Seschrock sysevent_free_attr(attr); 64663d7072f8Seschrock sysevent_free(ev); 64673d7072f8Seschrock #endif 64683d7072f8Seschrock } 6469