1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 2398d1cbfeSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 245aeb9474SGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 254263d13fSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 265aeb9474SGarrett D'Amore */ 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state. 30fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a 31fa9e4066Sahrens * pool. 32fa9e4066Sahrens */ 33fa9e4066Sahrens 34fa9e4066Sahrens #include <sys/zfs_context.h> 35ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 36fa9e4066Sahrens #include <sys/spa_impl.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zio_checksum.h> 39fa9e4066Sahrens #include <sys/dmu.h> 40fa9e4066Sahrens #include <sys/dmu_tx.h> 41fa9e4066Sahrens #include <sys/zap.h> 42fa9e4066Sahrens #include <sys/zil.h> 43b24ab676SJeff Bonwick #include <sys/ddt.h> 44fa9e4066Sahrens #include <sys/vdev_impl.h> 45fa9e4066Sahrens #include <sys/metaslab.h> 4688ecc943SGeorge Wilson #include <sys/metaslab_impl.h> 47fa9e4066Sahrens #include <sys/uberblock_impl.h> 48fa9e4066Sahrens #include <sys/txg.h> 49fa9e4066Sahrens #include <sys/avl.h> 50fa9e4066Sahrens #include <sys/dmu_traverse.h> 51b1b8ab34Slling #include <sys/dmu_objset.h> 52fa9e4066Sahrens #include <sys/unique.h> 53fa9e4066Sahrens #include <sys/dsl_pool.h> 54b1b8ab34Slling #include <sys/dsl_dataset.h> 55fa9e4066Sahrens #include <sys/dsl_dir.h> 56fa9e4066Sahrens #include <sys/dsl_prop.h> 57b1b8ab34Slling #include <sys/dsl_synctask.h> 58fa9e4066Sahrens #include <sys/fs/zfs.h> 59fa94a07fSbrendan #include <sys/arc.h> 60fa9e4066Sahrens #include <sys/callb.h> 6195173954Sek #include <sys/systeminfo.h> 62e7cbe64fSgw #include <sys/spa_boot.h> 63573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h> 643f9d6ad7SLin Ling #include <sys/dsl_scan.h> 65ad135b5dSChristopher Siden #include <sys/zfeature.h> 66fa9e4066Sahrens 675679c89fSjv #ifdef _KERNEL 68dedec472SJack Meng #include <sys/bootprops.h> 6935a5a358SJonathan Adams #include <sys/callb.h> 7035a5a358SJonathan Adams #include <sys/cpupart.h> 7135a5a358SJonathan Adams #include <sys/pool.h> 7235a5a358SJonathan Adams #include <sys/sysdc.h> 7335a5a358SJonathan Adams #include <sys/zone.h> 745679c89fSjv #endif /* _KERNEL */ 755679c89fSjv 76990b4856Slling #include "zfs_prop.h" 77b7b97454Sperrin #include "zfs_comutil.h" 78990b4856Slling 7935a5a358SJonathan Adams typedef enum zti_modes { 80*ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 81*ec94d322SAdam Leventhal ZTI_MODE_ONLINE_PERCENT, /* value is % of online CPUs */ 82*ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 83*ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */ 84*ec94d322SAdam Leventhal ZTI_NMODES 8535a5a358SJonathan Adams } zti_modes_t; 86416e0cd8Sek 87*ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 88*ec94d322SAdam Leventhal #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } 89*ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 90*ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 912e0c549eSJonathan Adams 92*ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1) 93*ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1) 942e0c549eSJonathan Adams 952e0c549eSJonathan Adams typedef struct zio_taskq_info { 96*ec94d322SAdam Leventhal zti_modes_t zti_mode; 9780eb36f2SGeorge Wilson uint_t zti_value; 98*ec94d322SAdam Leventhal uint_t zti_count; 992e0c549eSJonathan Adams } zio_taskq_info_t; 1002e0c549eSJonathan Adams 1012e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 10235a5a358SJonathan Adams "issue", "issue_high", "intr", "intr_high" 1032e0c549eSJonathan Adams }; 1042e0c549eSJonathan Adams 10580eb36f2SGeorge Wilson /* 106*ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When 107*ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized 108*ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static 109*ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 110*ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH 111*ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations 112*ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a 113*ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 114*ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per- 115*ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the 116*ec94d322SAdam Leventhal * particular taskq is chosen at random. 117*ec94d322SAdam Leventhal * 118*ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue 119*ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 120*ec94d322SAdam Leventhal * need to be handled with minimum delay. 12180eb36f2SGeorge Wilson */ 12280eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 12380eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 124*ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 125*ec94d322SAdam Leventhal { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */ 126*ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 127*ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 128*ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 129*ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 1302e0c549eSJonathan Adams }; 1312e0c549eSJonathan Adams 132ad135b5dSChristopher Siden static dsl_syncfunc_t spa_sync_version; 1333f9d6ad7SLin Ling static dsl_syncfunc_t spa_sync_props; 134dfbb9432SGeorge Wilson static dsl_checkfunc_t spa_change_guid_check; 135dfbb9432SGeorge Wilson static dsl_syncfunc_t spa_change_guid_sync; 13689a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa); 1371195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, 1381195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 1391195e687SMark J Musante char **ereport); 140cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa); 141990b4856Slling 14235a5a358SJonathan Adams uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ 14335a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE; 14435a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 14535a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */ 14635a5a358SJonathan Adams 14735a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 14801f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free; 14935a5a358SJonathan Adams 15035a5a358SJonathan Adams /* 15135a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order 15235a5a358SJonathan Adams * to get the vdev stats associated with the imported devices. 15335a5a358SJonathan Adams */ 15435a5a358SJonathan Adams #define TRYIMPORT_NAME "$import" 15535a5a358SJonathan Adams 156990b4856Slling /* 157990b4856Slling * ========================================================================== 158990b4856Slling * SPA properties routines 159990b4856Slling * ========================================================================== 160990b4856Slling */ 161990b4856Slling 162990b4856Slling /* 163990b4856Slling * Add a (source=src, propname=propval) list to an nvlist. 164990b4856Slling */ 1659d82f4f6Slling static void 166990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 167990b4856Slling uint64_t intval, zprop_source_t src) 168990b4856Slling { 169990b4856Slling const char *propname = zpool_prop_to_name(prop); 170990b4856Slling nvlist_t *propval; 171990b4856Slling 1729d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1739d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 174990b4856Slling 1759d82f4f6Slling if (strval != NULL) 1769d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 1779d82f4f6Slling else 1789d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 179990b4856Slling 1809d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 181990b4856Slling nvlist_free(propval); 182990b4856Slling } 183990b4856Slling 184990b4856Slling /* 185990b4856Slling * Get property values from the spa configuration. 186990b4856Slling */ 1879d82f4f6Slling static void 188990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 189990b4856Slling { 1904263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 191ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool; 192379c004dSEric Schrock uint64_t size; 193485bbbf5SGeorge Wilson uint64_t alloc; 1944263d13fSGeorge Wilson uint64_t space; 195990b4856Slling uint64_t cap, version; 196990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 197c5904d13Seschrock spa_config_dirent_t *dp; 198990b4856Slling 199e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 200e14bb325SJeff Bonwick 2014263d13fSGeorge Wilson if (rvd != NULL) { 202485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 203b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa)); 204379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 205379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 206485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 207485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 208485bbbf5SGeorge Wilson size - alloc, src); 2094263d13fSGeorge Wilson 2104263d13fSGeorge Wilson space = 0; 2114263d13fSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 2124263d13fSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c]; 2134263d13fSGeorge Wilson space += tvd->vdev_max_asize - tvd->vdev_asize; 2144263d13fSGeorge Wilson } 2154263d13fSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space, 2164263d13fSGeorge Wilson src); 2174263d13fSGeorge Wilson 218f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 219f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src); 220379c004dSEric Schrock 221485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size); 222379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 223379c004dSEric Schrock 224b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 225b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src); 226b24ab676SJeff Bonwick 227379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 2284263d13fSGeorge Wilson rvd->vdev_state, src); 229379c004dSEric Schrock 230379c004dSEric Schrock version = spa_version(spa); 231379c004dSEric Schrock if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 232379c004dSEric Schrock src = ZPROP_SRC_DEFAULT; 233379c004dSEric Schrock else 234379c004dSEric Schrock src = ZPROP_SRC_LOCAL; 235379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 236379c004dSEric Schrock } 237990b4856Slling 238ad135b5dSChristopher Siden if (pool != NULL) { 239ad135b5dSChristopher Siden dsl_dir_t *freedir = pool->dp_free_dir; 240ad135b5dSChristopher Siden 241ad135b5dSChristopher Siden /* 242ad135b5dSChristopher Siden * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 243ad135b5dSChristopher Siden * when opening pools before this version freedir will be NULL. 244ad135b5dSChristopher Siden */ 245ad135b5dSChristopher Siden if (freedir != NULL) { 246ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 247ad135b5dSChristopher Siden freedir->dd_phys->dd_used_bytes, src); 248ad135b5dSChristopher Siden } else { 249ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 250ad135b5dSChristopher Siden NULL, 0, src); 251ad135b5dSChristopher Siden } 252ad135b5dSChristopher Siden } 253ad135b5dSChristopher Siden 2549d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 255990b4856Slling 2568704186eSDan McDonald if (spa->spa_comment != NULL) { 2578704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 2588704186eSDan McDonald 0, ZPROP_SRC_LOCAL); 2598704186eSDan McDonald } 2608704186eSDan McDonald 2619d82f4f6Slling if (spa->spa_root != NULL) 2629d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 2639d82f4f6Slling 0, ZPROP_SRC_LOCAL); 264990b4856Slling 265c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) { 266c5904d13Seschrock if (dp->scd_path == NULL) { 2679d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 268c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL); 269c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 2709d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 271c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL); 2722f8aaab3Seschrock } 2732f8aaab3Seschrock } 274990b4856Slling } 275990b4856Slling 276990b4856Slling /* 277990b4856Slling * Get zpool property values. 278990b4856Slling */ 279990b4856Slling int 280990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp) 281990b4856Slling { 282b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset; 283990b4856Slling zap_cursor_t zc; 284990b4856Slling zap_attribute_t za; 285990b4856Slling int err; 286990b4856Slling 2879d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 288990b4856Slling 289e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 290e14bb325SJeff Bonwick 291990b4856Slling /* 292990b4856Slling * Get properties from the spa config. 293990b4856Slling */ 2949d82f4f6Slling spa_prop_get_config(spa, nvp); 295990b4856Slling 296990b4856Slling /* If no pool property object, no more prop to get. */ 297afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) { 298990b4856Slling mutex_exit(&spa->spa_props_lock); 299990b4856Slling return (0); 300990b4856Slling } 301990b4856Slling 302990b4856Slling /* 303990b4856Slling * Get properties from the MOS pool property object. 304990b4856Slling */ 305990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 306990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0; 307990b4856Slling zap_cursor_advance(&zc)) { 308990b4856Slling uint64_t intval = 0; 309990b4856Slling char *strval = NULL; 310990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT; 311990b4856Slling zpool_prop_t prop; 312990b4856Slling 313990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 314990b4856Slling continue; 315990b4856Slling 316990b4856Slling switch (za.za_integer_length) { 317990b4856Slling case 8: 318990b4856Slling /* integer property */ 319990b4856Slling if (za.za_first_integer != 320990b4856Slling zpool_prop_default_numeric(prop)) 321990b4856Slling src = ZPROP_SRC_LOCAL; 322990b4856Slling 323990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) { 324990b4856Slling dsl_pool_t *dp; 325990b4856Slling dsl_dataset_t *ds = NULL; 326990b4856Slling 327990b4856Slling dp = spa_get_dsl(spa); 328990b4856Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 329745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp, 330745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) { 331990b4856Slling rw_exit(&dp->dp_config_rwlock); 332990b4856Slling break; 333990b4856Slling } 334990b4856Slling 335990b4856Slling strval = kmem_alloc( 336990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 337990b4856Slling KM_SLEEP); 338990b4856Slling dsl_dataset_name(ds, strval); 339745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 340990b4856Slling rw_exit(&dp->dp_config_rwlock); 341990b4856Slling } else { 342990b4856Slling strval = NULL; 343990b4856Slling intval = za.za_first_integer; 344990b4856Slling } 345990b4856Slling 3469d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src); 347990b4856Slling 348990b4856Slling if (strval != NULL) 349990b4856Slling kmem_free(strval, 350990b4856Slling MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 351990b4856Slling 352990b4856Slling break; 353990b4856Slling 354990b4856Slling case 1: 355990b4856Slling /* string property */ 356990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 357990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object, 358990b4856Slling za.za_name, 1, za.za_num_integers, strval); 359990b4856Slling if (err) { 360990b4856Slling kmem_free(strval, za.za_num_integers); 361990b4856Slling break; 362990b4856Slling } 3639d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src); 364990b4856Slling kmem_free(strval, za.za_num_integers); 365990b4856Slling break; 366990b4856Slling 367990b4856Slling default: 368990b4856Slling break; 369990b4856Slling } 370990b4856Slling } 371990b4856Slling zap_cursor_fini(&zc); 372990b4856Slling mutex_exit(&spa->spa_props_lock); 373990b4856Slling out: 374990b4856Slling if (err && err != ENOENT) { 375990b4856Slling nvlist_free(*nvp); 3769d82f4f6Slling *nvp = NULL; 377990b4856Slling return (err); 378990b4856Slling } 379990b4856Slling 380990b4856Slling return (0); 381990b4856Slling } 382990b4856Slling 383990b4856Slling /* 384990b4856Slling * Validate the given pool properties nvlist and modify the list 385990b4856Slling * for the property values to be set. 386990b4856Slling */ 387990b4856Slling static int 388990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props) 389990b4856Slling { 390990b4856Slling nvpair_t *elem; 391990b4856Slling int error = 0, reset_bootfs = 0; 392d5285caeSGeorge Wilson uint64_t objnum = 0; 393ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE; 394990b4856Slling 395990b4856Slling elem = NULL; 396990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 397990b4856Slling uint64_t intval; 398ad135b5dSChristopher Siden char *strval, *slash, *check, *fname; 399ad135b5dSChristopher Siden const char *propname = nvpair_name(elem); 400ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname); 401ad135b5dSChristopher Siden 402ad135b5dSChristopher Siden switch (prop) { 403ad135b5dSChristopher Siden case ZPROP_INVAL: 404ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) { 405ad135b5dSChristopher Siden error = EINVAL; 406ad135b5dSChristopher Siden break; 407ad135b5dSChristopher Siden } 408990b4856Slling 409ad135b5dSChristopher Siden /* 410ad135b5dSChristopher Siden * Sanitize the input. 411ad135b5dSChristopher Siden */ 412ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) { 413ad135b5dSChristopher Siden error = EINVAL; 414ad135b5dSChristopher Siden break; 415ad135b5dSChristopher Siden } 416990b4856Slling 417ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) { 418ad135b5dSChristopher Siden error = EINVAL; 419ad135b5dSChristopher Siden break; 420ad135b5dSChristopher Siden } 421ad135b5dSChristopher Siden 422ad135b5dSChristopher Siden if (intval != 0) { 423ad135b5dSChristopher Siden error = EINVAL; 424ad135b5dSChristopher Siden break; 425ad135b5dSChristopher Siden } 426ad135b5dSChristopher Siden 427ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1; 428ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) { 429ad135b5dSChristopher Siden error = EINVAL; 430ad135b5dSChristopher Siden break; 431ad135b5dSChristopher Siden } 432ad135b5dSChristopher Siden 433ad135b5dSChristopher Siden has_feature = B_TRUE; 434ad135b5dSChristopher Siden break; 435990b4856Slling 436990b4856Slling case ZPOOL_PROP_VERSION: 437990b4856Slling error = nvpair_value_uint64(elem, &intval); 438990b4856Slling if (!error && 439ad135b5dSChristopher Siden (intval < spa_version(spa) || 440ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES || 441ad135b5dSChristopher Siden has_feature)) 442990b4856Slling error = EINVAL; 443990b4856Slling break; 444990b4856Slling 445990b4856Slling case ZPOOL_PROP_DELEGATION: 446990b4856Slling case ZPOOL_PROP_AUTOREPLACE: 447d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS: 448573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 449990b4856Slling error = nvpair_value_uint64(elem, &intval); 450990b4856Slling if (!error && intval > 1) 451990b4856Slling error = EINVAL; 452990b4856Slling break; 453990b4856Slling 454990b4856Slling case ZPOOL_PROP_BOOTFS: 45525f89ee2SJeff Bonwick /* 45625f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS, 45725f89ee2SJeff Bonwick * or the pool is still being created (version == 0), 45825f89ee2SJeff Bonwick * the bootfs property cannot be set. 45925f89ee2SJeff Bonwick */ 460990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) { 461990b4856Slling error = ENOTSUP; 462990b4856Slling break; 463990b4856Slling } 464990b4856Slling 465990b4856Slling /* 46615e6edf1Sgw * Make sure the vdev config is bootable 467990b4856Slling */ 46815e6edf1Sgw if (!vdev_is_bootable(spa->spa_root_vdev)) { 469990b4856Slling error = ENOTSUP; 470990b4856Slling break; 471990b4856Slling } 472990b4856Slling 473990b4856Slling reset_bootfs = 1; 474990b4856Slling 475990b4856Slling error = nvpair_value_string(elem, &strval); 476990b4856Slling 477990b4856Slling if (!error) { 478ad135b5dSChristopher Siden objset_t *os; 47915e6edf1Sgw uint64_t compress; 48015e6edf1Sgw 481990b4856Slling if (strval == NULL || strval[0] == '\0') { 482990b4856Slling objnum = zpool_prop_default_numeric( 483990b4856Slling ZPOOL_PROP_BOOTFS); 484990b4856Slling break; 485990b4856Slling } 486990b4856Slling 487503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os)) 488990b4856Slling break; 48915e6edf1Sgw 490503ad85cSMatthew Ahrens /* Must be ZPL and not gzip compressed. */ 491503ad85cSMatthew Ahrens 492503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) { 493503ad85cSMatthew Ahrens error = ENOTSUP; 494503ad85cSMatthew Ahrens } else if ((error = dsl_prop_get_integer(strval, 49515e6edf1Sgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 49615e6edf1Sgw &compress, NULL)) == 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 */ 663ad135b5dSChristopher Siden error = dsl_sync_task_do(spa_get_dsl(spa), NULL, 664ad135b5dSChristopher Siden spa_sync_version, spa, &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) { 675379c004dSEric Schrock return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 676ad135b5dSChristopher Siden spa, 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 698dfbb9432SGeorge Wilson spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx) 699dfbb9432SGeorge Wilson { 700dfbb9432SGeorge Wilson spa_t *spa = arg1; 701dfbb9432SGeorge Wilson uint64_t *newguid = arg2; 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 718dfbb9432SGeorge Wilson spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx) 719dfbb9432SGeorge Wilson { 720dfbb9432SGeorge Wilson spa_t *spa = arg1; 721dfbb9432SGeorge Wilson uint64_t *newguid = arg2; 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 755dfbb9432SGeorge Wilson error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check, 756dfbb9432SGeorge Wilson spa_change_guid_sync, spa, &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 812*ec94d322SAdam Leventhal static void 813*ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 814fa9e4066Sahrens { 815*ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 816*ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode; 817*ec94d322SAdam Leventhal uint_t value = ztip->zti_value; 818*ec94d322SAdam Leventhal uint_t count = ztip->zti_count; 819*ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 820*ec94d322SAdam Leventhal char name[32]; 8215aeb9474SGarrett D'Amore uint_t flags = 0; 82235a5a358SJonathan Adams boolean_t batch = B_FALSE; 823fa9e4066Sahrens 824*ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) { 825*ec94d322SAdam Leventhal tqs->stqs_count = 0; 826*ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 827*ec94d322SAdam Leventhal return; 828*ec94d322SAdam Leventhal } 829fa9e4066Sahrens 830*ec94d322SAdam Leventhal ASSERT3U(count, >, 0); 831fa9e4066Sahrens 832*ec94d322SAdam Leventhal tqs->stqs_count = count; 833*ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 83435a5a358SJonathan Adams 835*ec94d322SAdam Leventhal for (uint_t i = 0; i < count; i++) { 836*ec94d322SAdam Leventhal taskq_t *tq; 83735a5a358SJonathan Adams 838*ec94d322SAdam Leventhal switch (mode) { 839*ec94d322SAdam Leventhal case ZTI_MODE_FIXED: 840*ec94d322SAdam Leventhal ASSERT3U(value, >=, 1); 841*ec94d322SAdam Leventhal value = MAX(value, 1); 842*ec94d322SAdam Leventhal break; 843*ec94d322SAdam Leventhal 844*ec94d322SAdam Leventhal case ZTI_MODE_BATCH: 845*ec94d322SAdam Leventhal batch = B_TRUE; 846*ec94d322SAdam Leventhal flags |= TASKQ_THREADS_CPU_PCT; 847*ec94d322SAdam Leventhal value = zio_taskq_batch_pct; 848*ec94d322SAdam Leventhal break; 849*ec94d322SAdam Leventhal 850*ec94d322SAdam Leventhal case ZTI_MODE_ONLINE_PERCENT: 851*ec94d322SAdam Leventhal flags |= TASKQ_THREADS_CPU_PCT; 852*ec94d322SAdam Leventhal break; 853*ec94d322SAdam Leventhal 854*ec94d322SAdam Leventhal default: 855*ec94d322SAdam Leventhal panic("unrecognized mode for %s_%s taskq (%u:%u) in " 856*ec94d322SAdam Leventhal "spa_activate()", 857*ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], mode, value); 858*ec94d322SAdam Leventhal break; 859*ec94d322SAdam Leventhal } 860*ec94d322SAdam Leventhal 861*ec94d322SAdam Leventhal if (count > 1) { 862*ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u", 863*ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i); 864*ec94d322SAdam Leventhal } else { 865*ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s", 866*ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]); 867*ec94d322SAdam Leventhal } 868*ec94d322SAdam Leventhal 869*ec94d322SAdam Leventhal if (zio_taskq_sysdc && spa->spa_proc != &p0) { 870*ec94d322SAdam Leventhal if (batch) 871*ec94d322SAdam Leventhal flags |= TASKQ_DC_BATCH; 872*ec94d322SAdam Leventhal 873*ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX, 874*ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags); 875*ec94d322SAdam Leventhal } else { 876*ec94d322SAdam Leventhal tq = taskq_create_proc(name, value, maxclsyspri, 50, 877*ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags); 878*ec94d322SAdam Leventhal } 879*ec94d322SAdam Leventhal 880*ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq; 881*ec94d322SAdam Leventhal } 882*ec94d322SAdam Leventhal } 883*ec94d322SAdam Leventhal 884*ec94d322SAdam Leventhal static void 885*ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 886*ec94d322SAdam Leventhal { 887*ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 888*ec94d322SAdam Leventhal 889*ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) { 890*ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count); 891*ec94d322SAdam Leventhal return; 892*ec94d322SAdam Leventhal } 893*ec94d322SAdam Leventhal 894*ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) { 895*ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 896*ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]); 89735a5a358SJonathan Adams } 89835a5a358SJonathan Adams 899*ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 900*ec94d322SAdam Leventhal tqs->stqs_taskq = NULL; 901*ec94d322SAdam Leventhal } 90235a5a358SJonathan Adams 903*ec94d322SAdam Leventhal /* 904*ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 905*ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention 906*ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using 907*ec94d322SAdam Leventhal * the low bits of gethrtime(). 908*ec94d322SAdam Leventhal */ 909*ec94d322SAdam Leventhal void 910*ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 911*ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 912*ec94d322SAdam Leventhal { 913*ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 914*ec94d322SAdam Leventhal taskq_t *tq; 915*ec94d322SAdam Leventhal 916*ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL); 917*ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0); 918*ec94d322SAdam Leventhal 919*ec94d322SAdam Leventhal if (tqs->stqs_count == 1) { 920*ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0]; 921*ec94d322SAdam Leventhal } else { 922*ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 92335a5a358SJonathan Adams } 924*ec94d322SAdam Leventhal 925*ec94d322SAdam 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++) { 933*ec94d322SAdam 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++) { 1090*ec94d322SAdam 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 */ 16904b964adaSGeorge Wilson static int 1691b87f3af3Sperrin spa_check_logs(spa_t *spa) 1692b87f3af3Sperrin { 1693b87f3af3Sperrin switch (spa->spa_log_state) { 1694b87f3af3Sperrin case SPA_LOG_MISSING: 1695b87f3af3Sperrin /* need to recheck in case slog has been restored */ 1696b87f3af3Sperrin case SPA_LOG_UNKNOWN: 1697b87f3af3Sperrin if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1698b87f3af3Sperrin DS_FIND_CHILDREN)) { 16991195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING); 1700b87f3af3Sperrin return (1); 1701b87f3af3Sperrin } 1702b87f3af3Sperrin break; 1703b87f3af3Sperrin } 1704b87f3af3Sperrin return (0); 1705b87f3af3Sperrin } 1706b87f3af3Sperrin 17071195e687SMark J Musante static boolean_t 17081195e687SMark J Musante spa_passivate_log(spa_t *spa) 17091195e687SMark J Musante { 17101195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17111195e687SMark J Musante boolean_t slog_found = B_FALSE; 17121195e687SMark J Musante 17131195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17141195e687SMark J Musante 17151195e687SMark J Musante if (!spa_has_slogs(spa)) 17161195e687SMark J Musante return (B_FALSE); 17171195e687SMark J Musante 17181195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17191195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17201195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17211195e687SMark J Musante 17221195e687SMark J Musante if (tvd->vdev_islog) { 17231195e687SMark J Musante metaslab_group_passivate(mg); 17241195e687SMark J Musante slog_found = B_TRUE; 17251195e687SMark J Musante } 17261195e687SMark J Musante } 17271195e687SMark J Musante 17281195e687SMark J Musante return (slog_found); 17291195e687SMark J Musante } 17301195e687SMark J Musante 17311195e687SMark J Musante static void 17321195e687SMark J Musante spa_activate_log(spa_t *spa) 17331195e687SMark J Musante { 17341195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev; 17351195e687SMark J Musante 17361195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 17371195e687SMark J Musante 17381195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) { 17391195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c]; 17401195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg; 17411195e687SMark J Musante 17421195e687SMark J Musante if (tvd->vdev_islog) 17431195e687SMark J Musante metaslab_group_activate(mg); 17441195e687SMark J Musante } 17451195e687SMark J Musante } 17461195e687SMark J Musante 17471195e687SMark J Musante int 17481195e687SMark J Musante spa_offline_log(spa_t *spa) 17491195e687SMark J Musante { 17501195e687SMark J Musante int error = 0; 17511195e687SMark J Musante 17521195e687SMark J Musante if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, 17531195e687SMark J Musante NULL, DS_FIND_CHILDREN)) == 0) { 17541195e687SMark J Musante 17551195e687SMark J Musante /* 17561195e687SMark J Musante * We successfully offlined the log device, sync out the 17571195e687SMark J Musante * current txg so that the "stubby" block can be removed 17581195e687SMark J Musante * by zil_sync(). 17591195e687SMark J Musante */ 17601195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0); 17611195e687SMark J Musante } 17621195e687SMark J Musante return (error); 17631195e687SMark J Musante } 17641195e687SMark J Musante 1765b693757aSEric Schrock static void 1766b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav) 1767b693757aSEric Schrock { 1768b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++) 1769b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]); 1770b693757aSEric Schrock } 1771b693757aSEric Schrock 1772b24ab676SJeff Bonwick void 1773b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio) 1774b24ab676SJeff Bonwick { 1775b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 1776b24ab676SJeff Bonwick 1777b24ab676SJeff Bonwick if (zio->io_error) 1778b24ab676SJeff Bonwick return; 1779b24ab676SJeff Bonwick 1780b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1781b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1782b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1783b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock); 1784b24ab676SJeff Bonwick } 1785b24ab676SJeff Bonwick 1786468c413aSTim Haley typedef struct spa_load_error { 1787c8ee1847SVictor Latushkin uint64_t sle_meta_count; 1788468c413aSTim Haley uint64_t sle_data_count; 1789468c413aSTim Haley } spa_load_error_t; 1790468c413aSTim Haley 1791468c413aSTim Haley static void 1792468c413aSTim Haley spa_load_verify_done(zio_t *zio) 1793468c413aSTim Haley { 1794468c413aSTim Haley blkptr_t *bp = zio->io_bp; 1795468c413aSTim Haley spa_load_error_t *sle = zio->io_private; 1796468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp); 1797468c413aSTim Haley int error = zio->io_error; 1798468c413aSTim Haley 1799468c413aSTim Haley if (error) { 1800ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 1801468c413aSTim Haley type != DMU_OT_INTENT_LOG) 1802c8ee1847SVictor Latushkin atomic_add_64(&sle->sle_meta_count, 1); 1803468c413aSTim Haley else 1804468c413aSTim Haley atomic_add_64(&sle->sle_data_count, 1); 1805468c413aSTim Haley } 1806468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size); 1807468c413aSTim Haley } 1808468c413aSTim Haley 1809468c413aSTim Haley /*ARGSUSED*/ 1810468c413aSTim Haley static int 1811b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 18121b912ec7SGeorge Wilson const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1813468c413aSTim Haley { 1814468c413aSTim Haley if (bp != NULL) { 1815468c413aSTim Haley zio_t *rio = arg; 1816468c413aSTim Haley size_t size = BP_GET_PSIZE(bp); 1817468c413aSTim Haley void *data = zio_data_buf_alloc(size); 1818468c413aSTim Haley 1819468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size, 1820468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1821468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1822468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1823468c413aSTim Haley } 1824468c413aSTim Haley return (0); 1825468c413aSTim Haley } 1826468c413aSTim Haley 1827468c413aSTim Haley static int 1828468c413aSTim Haley spa_load_verify(spa_t *spa) 1829468c413aSTim Haley { 1830468c413aSTim Haley zio_t *rio; 1831468c413aSTim Haley spa_load_error_t sle = { 0 }; 1832468c413aSTim Haley zpool_rewind_policy_t policy; 1833468c413aSTim Haley boolean_t verify_ok = B_FALSE; 1834468c413aSTim Haley int error; 1835468c413aSTim Haley 1836c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy); 1837c8ee1847SVictor Latushkin 1838c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND) 1839c8ee1847SVictor Latushkin return (0); 1840c8ee1847SVictor Latushkin 1841468c413aSTim Haley rio = zio_root(spa, NULL, &sle, 1842468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1843468c413aSTim Haley 1844bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg, 1845bbfd46c4SJeff Bonwick TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); 1846468c413aSTim Haley 1847468c413aSTim Haley (void) zio_wait(rio); 1848468c413aSTim Haley 1849c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count; 1850468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count; 1851468c413aSTim Haley 1852c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && 1853468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) { 18544b964adaSGeorge Wilson int64_t loss = 0; 18554b964adaSGeorge Wilson 1856468c413aSTim Haley verify_ok = B_TRUE; 1857468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1858468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 18594b964adaSGeorge Wilson 18604b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 18614b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 18624b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 18634b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info, 18644b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 18654b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info, 18664b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 1867a33cae98STim Haley } else { 1868a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1869468c413aSTim Haley } 1870468c413aSTim Haley 1871468c413aSTim Haley if (error) { 1872468c413aSTim Haley if (error != ENXIO && error != EIO) 1873468c413aSTim Haley error = EIO; 1874468c413aSTim Haley return (error); 1875468c413aSTim Haley } 1876468c413aSTim Haley 1877468c413aSTim Haley return (verify_ok ? 0 : EIO); 1878468c413aSTim Haley } 1879468c413aSTim Haley 18801195e687SMark J Musante /* 18811195e687SMark J Musante * Find a value in the pool props object. 18821195e687SMark J Musante */ 18831195e687SMark J Musante static void 18841195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 18851195e687SMark J Musante { 18861195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 18871195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 18881195e687SMark J Musante } 18891195e687SMark J Musante 18901195e687SMark J Musante /* 18911195e687SMark J Musante * Find a value in the pool directory object. 18921195e687SMark J Musante */ 18931195e687SMark J Musante static int 18941195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) 18951195e687SMark J Musante { 18961195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 18971195e687SMark J Musante name, sizeof (uint64_t), 1, val)); 18981195e687SMark J Musante } 18991195e687SMark J Musante 19001195e687SMark J Musante static int 19011195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 19021195e687SMark J Musante { 19031195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 19041195e687SMark J Musante return (err); 19051195e687SMark J Musante } 19061195e687SMark J Musante 19071195e687SMark J Musante /* 19081195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the 19091195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 19101195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains 19111195e687SMark J Musante * a list of all the guids of the vdevs that are being split off. 19121195e687SMark J Musante * 19131195e687SMark J Musante * This function determines what to do with that list: either rejoin 19141195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt 19151195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and 19161195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was 19171195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 19181195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split. 19191195e687SMark J Musante * 1920d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in 1921d41c4376SMark J Musante * the original pool. 19221195e687SMark J Musante */ 19231195e687SMark J Musante static void 19241195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config) 19251195e687SMark J Musante { 19261195e687SMark J Musante uint_t extracted; 19271195e687SMark J Musante uint64_t *glist; 19281195e687SMark J Musante uint_t i, gcount; 19291195e687SMark J Musante nvlist_t *nvl; 19301195e687SMark J Musante vdev_t **vd; 19311195e687SMark J Musante boolean_t attempt_reopen; 19321195e687SMark J Musante 19331195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 19341195e687SMark J Musante return; 19351195e687SMark J Musante 19361195e687SMark J Musante /* check that the config is complete */ 19371195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 19381195e687SMark J Musante &glist, &gcount) != 0) 19391195e687SMark J Musante return; 19401195e687SMark J Musante 19411195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 19421195e687SMark J Musante 19431195e687SMark J Musante /* attempt to online all the vdevs & validate */ 19441195e687SMark J Musante attempt_reopen = B_TRUE; 19451195e687SMark J Musante for (i = 0; i < gcount; i++) { 19461195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */ 19471195e687SMark J Musante continue; 19481195e687SMark J Musante 19491195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 19501195e687SMark J Musante if (vd[i] == NULL) { 19511195e687SMark J Musante /* 19521195e687SMark J Musante * Don't bother attempting to reopen the disks; 19531195e687SMark J Musante * just do the split. 19541195e687SMark J Musante */ 19551195e687SMark J Musante attempt_reopen = B_FALSE; 19561195e687SMark J Musante } else { 19571195e687SMark J Musante /* attempt to re-online it */ 19581195e687SMark J Musante vd[i]->vdev_offline = B_FALSE; 19591195e687SMark J Musante } 19601195e687SMark J Musante } 19611195e687SMark J Musante 19621195e687SMark J Musante if (attempt_reopen) { 19631195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 19641195e687SMark J Musante 19651195e687SMark J Musante /* check each device to see what state it's in */ 19661195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) { 19671195e687SMark J Musante if (vd[i] != NULL && 19681195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 19691195e687SMark J Musante break; 19701195e687SMark J Musante ++extracted; 19711195e687SMark J Musante } 19721195e687SMark J Musante } 19731195e687SMark J Musante 19741195e687SMark J Musante /* 19751195e687SMark J Musante * If every disk has been moved to the new pool, or if we never 19761195e687SMark J Musante * even attempted to look at them, then we split them off for 19771195e687SMark J Musante * good. 19781195e687SMark J Musante */ 19791195e687SMark J Musante if (!attempt_reopen || gcount == extracted) { 19801195e687SMark J Musante for (i = 0; i < gcount; i++) 19811195e687SMark J Musante if (vd[i] != NULL) 19821195e687SMark J Musante vdev_split(vd[i]); 19831195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 19841195e687SMark J Musante } 19851195e687SMark J Musante 19861195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *)); 19871195e687SMark J Musante } 19881195e687SMark J Musante 19891195e687SMark J Musante static int 19901195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, 19911195e687SMark J Musante boolean_t mosconfig) 19921195e687SMark J Musante { 19931195e687SMark J Musante nvlist_t *config = spa->spa_config; 19941195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL; 19958704186eSDan McDonald char *comment; 19961195e687SMark J Musante int error; 19971195e687SMark J Musante uint64_t pool_guid; 19981195e687SMark J Musante nvlist_t *nvl; 19991195e687SMark J Musante 20001195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) 20011195e687SMark J Musante return (EINVAL); 20021195e687SMark J Musante 20038704186eSDan McDonald ASSERT(spa->spa_comment == NULL); 20048704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 20058704186eSDan McDonald spa->spa_comment = spa_strdup(comment); 20068704186eSDan McDonald 20071195e687SMark J Musante /* 20081195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if 20091195e687SMark J Musante * it's not present treat it as the initial version. 20101195e687SMark J Musante */ 20111195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 20121195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0) 20131195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 20141195e687SMark J Musante 20151195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 20161195e687SMark J Musante &spa->spa_config_txg); 20171195e687SMark J Musante 20181195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 20191195e687SMark J Musante spa_guid_exists(pool_guid, 0)) { 20201195e687SMark J Musante error = EEXIST; 20211195e687SMark J Musante } else { 2022e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid; 20231195e687SMark J Musante 20241195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, 20251195e687SMark J Musante &nvl) == 0) { 20261195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, 20271195e687SMark J Musante KM_SLEEP) == 0); 20281195e687SMark J Musante } 20291195e687SMark J Musante 2030ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info); 2031ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2032ad135b5dSChristopher Siden 203311027bc7STim Haley gethrestime(&spa->spa_loaded_ts); 20341195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type, 20351195e687SMark J Musante mosconfig, &ereport); 20361195e687SMark J Musante } 20371195e687SMark J Musante 20381195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount); 203911027bc7STim Haley if (error) { 204011027bc7STim Haley if (error != EEXIST) { 204111027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0; 204211027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0; 204311027bc7STim Haley } 204411027bc7STim Haley if (error != EBADF) { 204511027bc7STim Haley zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 204611027bc7STim Haley } 204711027bc7STim Haley } 20481195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 20491195e687SMark J Musante spa->spa_ena = 0; 20501195e687SMark J Musante 20511195e687SMark J Musante return (error); 20521195e687SMark J Musante } 20531195e687SMark J Musante 2054fa9e4066Sahrens /* 2055fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a 2056ea8dc4b6Seschrock * source of configuration information. 2057fa9e4066Sahrens */ 2058fa9e4066Sahrens static int 20591195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, 20601195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, 20611195e687SMark J Musante char **ereport) 2062fa9e4066Sahrens { 2063fa9e4066Sahrens int error = 0; 2064871a9500SMark J Musante nvlist_t *nvroot = NULL; 2065ad135b5dSChristopher Siden nvlist_t *label; 2066fa9e4066Sahrens vdev_t *rvd; 2067fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock; 20684b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg; 20698ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode; 20701195e687SMark J Musante int parse; 2071cde58dbcSMatthew Ahrens uint64_t obj; 2072ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE; 2073fa9e4066Sahrens 20748ad4d6ddSJeff Bonwick /* 20758ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode. 20768ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices. 20778ad4d6ddSJeff Bonwick */ 20788ad4d6ddSJeff Bonwick if (!mosconfig) 20798ad4d6ddSJeff Bonwick spa->spa_mode = FREAD; 20808ad4d6ddSJeff Bonwick 2081e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2082e14bb325SJeff Bonwick 2083ea8dc4b6Seschrock spa->spa_load_state = state; 20840373e76bSbonwick 20851195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) 20861195e687SMark J Musante return (EINVAL); 2087fa9e4066Sahrens 20881195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ? 20891195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2090b5989ec7Seschrock 209154d692b7SGeorge Wilson /* 209254d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 209354d692b7SGeorge Wilson */ 209425f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 209525f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 209654d692b7SGeorge Wilson 2097fa9e4066Sahrens /* 209899653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the 209999653d4eSeschrock * value that will be returned by spa_version() since parsing the 210099653d4eSeschrock * configuration requires knowing the version number. 2101fa9e4066Sahrens */ 2102e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21031195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); 2104e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2105fa9e4066Sahrens 210699653d4eSeschrock if (error != 0) 21071195e687SMark J Musante return (error); 2108fa9e4066Sahrens 21090e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd); 21101195e687SMark J Musante 21111195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 21121195e687SMark J Musante ASSERT(spa_guid(spa) == pool_guid); 21131195e687SMark J Musante } 2114fa9e4066Sahrens 2115fa9e4066Sahrens /* 2116fa9e4066Sahrens * Try to open all vdevs, loading each label in the process. 2117fa9e4066Sahrens */ 2118e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 21190bf246f5Smc error = vdev_open(rvd); 2120e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 21210bf246f5Smc if (error != 0) 21221195e687SMark J Musante return (error); 2123fa9e4066Sahrens 2124560e6e96Seschrock /* 212577e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that 212677e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If 212777e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on 21281195e687SMark J Musante * that config. Otherwise, we're validating against the cached config 212977e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then 213077e3a39cSMark J Musante * later we will recursively call spa_load() and validate against 213177e3a39cSMark J Musante * the vdev config. 21321195e687SMark J Musante * 21331195e687SMark J Musante * If we're assembling a new pool that's been split off from an 21341195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip 21351195e687SMark J Musante * validation for now. 2136560e6e96Seschrock */ 21371195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 21381195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2139d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig); 21401195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 2141560e6e96Seschrock 21421195e687SMark J Musante if (error != 0) 21431195e687SMark J Musante return (error); 21441195e687SMark J Musante 21451195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 21461195e687SMark J Musante return (ENXIO); 2147560e6e96Seschrock } 2148560e6e96Seschrock 2149fa9e4066Sahrens /* 2150fa9e4066Sahrens * Find the best uberblock. 2151fa9e4066Sahrens */ 2152ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label); 2153fa9e4066Sahrens 2154fa9e4066Sahrens /* 2155fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure. 2156fa9e4066Sahrens */ 2157ad135b5dSChristopher Siden if (ub->ub_txg == 0) { 2158ad135b5dSChristopher Siden nvlist_free(label); 21591195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 2160ad135b5dSChristopher Siden } 2161ea8dc4b6Seschrock 2162ea8dc4b6Seschrock /* 2163ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it. 2164ea8dc4b6Seschrock */ 2165ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 2166ad135b5dSChristopher Siden nvlist_free(label); 21671195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 2168ad135b5dSChristopher Siden } 2169ad135b5dSChristopher Siden 2170ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2171ad135b5dSChristopher Siden nvlist_t *features; 2172ad135b5dSChristopher Siden 2173ad135b5dSChristopher Siden /* 2174ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the 2175ad135b5dSChristopher Siden * MOS in the label, return failure. 2176ad135b5dSChristopher Siden */ 2177ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label, 2178ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { 2179ad135b5dSChristopher Siden nvlist_free(label); 2180ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 2181ad135b5dSChristopher Siden ENXIO)); 2182ad135b5dSChristopher Siden } 2183ad135b5dSChristopher Siden 2184ad135b5dSChristopher Siden /* 2185ad135b5dSChristopher Siden * Update our in-core representation with the definitive values 2186ad135b5dSChristopher Siden * from the label. 2187ad135b5dSChristopher Siden */ 2188ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 2189ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 2190ad135b5dSChristopher Siden } 2191ad135b5dSChristopher Siden 2192ad135b5dSChristopher Siden nvlist_free(label); 2193ad135b5dSChristopher Siden 2194ad135b5dSChristopher Siden /* 2195ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If 2196ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we 2197ad135b5dSChristopher Siden * cannot open a pool. 2198ad135b5dSChristopher Siden */ 2199ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) { 2200ad135b5dSChristopher Siden nvlist_t *unsup_feat; 2201ad135b5dSChristopher Siden 2202ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 2203ad135b5dSChristopher Siden 0); 2204ad135b5dSChristopher Siden 2205ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 2206ad135b5dSChristopher Siden NULL); nvp != NULL; 2207ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 2208ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) { 2209ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat, 2210ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0); 2211ad135b5dSChristopher Siden } 2212ad135b5dSChristopher Siden } 2213ad135b5dSChristopher Siden 2214ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 2215ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info, 2216ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 2217ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2218ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2219ad135b5dSChristopher Siden ENOTSUP)); 2220ad135b5dSChristopher Siden } 2221ad135b5dSChristopher Siden 2222ad135b5dSChristopher Siden nvlist_free(unsup_feat); 2223ad135b5dSChristopher Siden } 2224fa9e4066Sahrens 2225fa9e4066Sahrens /* 2226fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an 22274b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool 22284b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). 22294b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we 22304b964adaSGeorge Wilson * can handle missing vdevs. 2231fa9e4066Sahrens */ 22324b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, 22334b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && 22341195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum) 22351195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 22361195e687SMark J Musante 22371195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 22381195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 22391195e687SMark J Musante spa_try_repair(spa, config); 22401195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG); 22411195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 22421195e687SMark J Musante spa->spa_config_splitting = NULL; 2243fa9e4066Sahrens } 2244fa9e4066Sahrens 2245fa9e4066Sahrens /* 2246fa9e4066Sahrens * Initialize internal SPA structures. 2247fa9e4066Sahrens */ 2248fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE; 2249fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 2250468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2251c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2252468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2253468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2254b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg; 22553f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version; 2256b24ab676SJeff Bonwick 2257ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 22581195e687SMark J Musante if (error) 22591195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2260fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 2261fa9e4066Sahrens 22621195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) 22631195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2264fa9e4066Sahrens 2265ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) { 2266ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE; 226757221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat; 2268ad135b5dSChristopher Siden 2269ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 2270ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) { 2271ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2272ad135b5dSChristopher Siden } 2273ad135b5dSChristopher Siden 2274ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 2275ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) { 2276ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2277ad135b5dSChristopher Siden } 2278ad135b5dSChristopher Siden 2279ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 2280ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) { 2281ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2282ad135b5dSChristopher Siden } 2283ad135b5dSChristopher Siden 228457221772SChristopher Siden enabled_feat = fnvlist_alloc(); 228557221772SChristopher Siden unsup_feat = fnvlist_alloc(); 2286ad135b5dSChristopher Siden 2287ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2288ad135b5dSChristopher Siden spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj, 228957221772SChristopher Siden unsup_feat, enabled_feat)) 2290ad135b5dSChristopher Siden missing_feat_read = B_TRUE; 2291ad135b5dSChristopher Siden 2292ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { 2293ad135b5dSChristopher Siden if (!feature_is_supported(spa->spa_meta_objset, 2294ad135b5dSChristopher Siden spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj, 229557221772SChristopher Siden unsup_feat, enabled_feat)) { 2296ad135b5dSChristopher Siden missing_feat_write = B_TRUE; 229757221772SChristopher Siden } 2298ad135b5dSChristopher Siden } 2299ad135b5dSChristopher Siden 230057221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 230157221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 230257221772SChristopher Siden 2303ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) { 230457221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info, 230557221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 2306ad135b5dSChristopher Siden } 2307ad135b5dSChristopher Siden 230857221772SChristopher Siden fnvlist_free(enabled_feat); 230957221772SChristopher Siden fnvlist_free(unsup_feat); 2310ad135b5dSChristopher Siden 2311ad135b5dSChristopher Siden if (!missing_feat_read) { 2312ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info, 2313ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY); 2314ad135b5dSChristopher Siden } 2315ad135b5dSChristopher Siden 2316ad135b5dSChristopher Siden /* 2317ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is 2318ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for 2319ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the 2320ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool 2321ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed 2322ad135b5dSChristopher Siden * as available in userland; if it is not available for import 2323ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in 2324ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only 2325ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable 2326ad135b5dSChristopher Siden * in userland with a special note that the pool is actually 2327ad135b5dSChristopher Siden * available for open in read-only mode. 2328ad135b5dSChristopher Siden * 2329ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 2330ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether 2331ad135b5dSChristopher Siden * the pool can be opened read-only before returning to 2332ad135b5dSChristopher Siden * userland in order to know whether to display the 2333ad135b5dSChristopher Siden * abovementioned note. 2334ad135b5dSChristopher Siden */ 2335ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write && 2336ad135b5dSChristopher Siden spa_writeable(spa))) { 2337ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 2338ad135b5dSChristopher Siden ENOTSUP)); 2339ad135b5dSChristopher Siden } 2340ad135b5dSChristopher Siden } 2341ad135b5dSChristopher Siden 2342ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 2343ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool); 2344ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 2345ad135b5dSChristopher Siden if (error != 0) 2346ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2347ad135b5dSChristopher Siden 2348fa9e4066Sahrens if (!mosconfig) { 234995173954Sek uint64_t hostid; 2350871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig; 2351871a9500SMark J Musante 2352871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2353871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2354fa9e4066Sahrens 235588ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 235677650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 235795173954Sek char *hostname; 235895173954Sek unsigned long myhostid = 0; 235995173954Sek 236088ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig, 236195173954Sek ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 236295173954Sek 23635679c89fSjv #ifdef _KERNEL 23645679c89fSjv myhostid = zone_get_hostid(NULL); 23655679c89fSjv #else /* _KERNEL */ 23665679c89fSjv /* 23675679c89fSjv * We're emulating the system's hostid in userland, so 23685679c89fSjv * we can't use zone_get_hostid(). 23695679c89fSjv */ 237095173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 23715679c89fSjv #endif /* _KERNEL */ 237217194a52Slling if (hostid != 0 && myhostid != 0 && 23735679c89fSjv hostid != myhostid) { 2374871a9500SMark J Musante nvlist_free(nvconfig); 237595173954Sek cmn_err(CE_WARN, "pool '%s' could not be " 237695173954Sek "loaded as it was last accessed by " 237777650510SLin Ling "another system (host: %s hostid: 0x%lx). " 2378654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY", 2379e14bb325SJeff Bonwick spa_name(spa), hostname, 238095173954Sek (unsigned long)hostid); 23811195e687SMark J Musante return (EBADF); 238295173954Sek } 238395173954Sek } 2384c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config, 2385c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0) 2386c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig, 2387c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0); 238895173954Sek 238988ecc943SGeorge Wilson spa_config_set(spa, nvconfig); 2390fa9e4066Sahrens spa_unload(spa); 2391fa9e4066Sahrens spa_deactivate(spa); 23928ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode); 2393fa9e4066Sahrens 23941195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); 2395fa9e4066Sahrens } 2396fa9e4066Sahrens 2397cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) 2398cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2399cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 2400cde58dbcSMatthew Ahrens if (error != 0) 24011195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2402fa9e4066Sahrens 240399653d4eSeschrock /* 240499653d4eSeschrock * Load the bit that tells us to use the new accounting function 240599653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not 240699653d4eSeschrock * be present. 240799653d4eSeschrock */ 24081195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); 24091195e687SMark J Musante if (error != 0 && error != ENOENT) 24101195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 241199653d4eSeschrock 24123f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 24133f9d6ad7SLin Ling &spa->spa_creation_version); 24143f9d6ad7SLin Ling if (error != 0 && error != ENOENT) 24153f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24163f9d6ad7SLin Ling 2417fa9e4066Sahrens /* 2418ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will 2419ea8dc4b6Seschrock * not be present. 2420fa9e4066Sahrens */ 24211195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); 24221195e687SMark J Musante if (error != 0 && error != ENOENT) 24231195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2424ea8dc4b6Seschrock 24251195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 24261195e687SMark J Musante &spa->spa_errlog_scrub); 24271195e687SMark J Musante if (error != 0 && error != ENOENT) 24281195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2429ea8dc4b6Seschrock 243006eeb2adSek /* 243106eeb2adSek * Load the history object. If we have an older pool, this 243206eeb2adSek * will not be present. 243306eeb2adSek */ 24341195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); 24351195e687SMark J Musante if (error != 0 && error != ENOENT) 24361195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24371195e687SMark J Musante 24381195e687SMark J Musante /* 24391195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of 24401195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache 24411195e687SMark J Musante * devices. 24421195e687SMark J Musante */ 244306eeb2adSek 244499653d4eSeschrock /* 244599653d4eSeschrock * Load any hot spares for this pool. 244699653d4eSeschrock */ 24471195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); 24481195e687SMark J Musante if (error != 0 && error != ENOENT) 24491195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24501195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2451e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 2452fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object, 24531195e687SMark J Musante &spa->spa_spares.sav_config) != 0) 24541195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 245599653d4eSeschrock 2456e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 245799653d4eSeschrock spa_load_spares(spa); 2458e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24591195e687SMark J Musante } else if (error == 0) { 24601195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE; 246199653d4eSeschrock } 246299653d4eSeschrock 2463fa94a07fSbrendan /* 2464fa94a07fSbrendan * Load any level 2 ARC devices for this pool. 2465fa94a07fSbrendan */ 24661195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 2467fa94a07fSbrendan &spa->spa_l2cache.sav_object); 24681195e687SMark J Musante if (error != 0 && error != ENOENT) 24691195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 24701195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 2471fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 2472fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object, 24731195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0) 24741195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2475fa94a07fSbrendan 2476e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2477fa94a07fSbrendan spa_load_l2cache(spa); 2478e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 24791195e687SMark J Musante } else if (error == 0) { 24801195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE; 2481fa94a07fSbrendan } 2482fa94a07fSbrendan 2483990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2484ecd6cf80Smarks 24851195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); 24861195e687SMark J Musante if (error && error != ENOENT) 24871195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2488b1b8ab34Slling 2489b1b8ab34Slling if (error == 0) { 24901195e687SMark J Musante uint64_t autoreplace; 24911195e687SMark J Musante 24921195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 24931195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 24941195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 24951195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 24961195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 24971195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 24981195e687SMark J Musante &spa->spa_dedup_ditto); 24991195e687SMark J Musante 2500b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0); 2501b1b8ab34Slling } 2502b1b8ab34Slling 25033d7072f8Seschrock /* 25043d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying 25053d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable 25063d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any 25073d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take 25083d7072f8Seschrock * over. 25093d7072f8Seschrock */ 2510b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 25113d7072f8Seschrock spa_check_removed(spa->spa_root_vdev); 2512b693757aSEric Schrock /* 2513b693757aSEric Schrock * For the import case, this is done in spa_import(), because 2514b693757aSEric Schrock * at this point we're using the spare definitions from 2515b693757aSEric Schrock * the MOS config, not necessarily from the userland config. 2516b693757aSEric Schrock */ 2517b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) { 2518b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 2519b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 2520b693757aSEric Schrock } 2521b693757aSEric Schrock } 25223d7072f8Seschrock 2523ea8dc4b6Seschrock /* 2524560e6e96Seschrock * Load the vdev state for all toplevel vdevs. 2525ea8dc4b6Seschrock */ 2526560e6e96Seschrock vdev_load(rvd); 25270373e76bSbonwick 2528fa9e4066Sahrens /* 2529fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree. 2530fa9e4066Sahrens */ 2531e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2532fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 2533e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 2534fa9e4066Sahrens 2535b24ab676SJeff Bonwick /* 2536b24ab676SJeff Bonwick * Load the DDTs (dedup tables). 2537b24ab676SJeff Bonwick */ 2538b24ab676SJeff Bonwick error = ddt_load(spa); 25391195e687SMark J Musante if (error != 0) 25401195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2541b24ab676SJeff Bonwick 2542485bbbf5SGeorge Wilson spa_update_dspace(spa); 2543485bbbf5SGeorge Wilson 2544b24ab676SJeff Bonwick /* 25454b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any 25464b964adaSGeorge Wilson * information which might be missing. If we fail to validate 25474b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're 25484b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred 25494b964adaSGeorge Wilson * over. 2550b24ab676SJeff Bonwick */ 25511195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) { 2552871a9500SMark J Musante nvlist_t *nvconfig; 2553871a9500SMark J Musante 2554871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) 2555871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 2556871a9500SMark J Musante 25574b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) { 25584b964adaSGeorge Wilson nvlist_free(nvconfig); 25594b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 25604b964adaSGeorge Wilson ENXIO)); 25614b964adaSGeorge Wilson } 25621195e687SMark J Musante nvlist_free(nvconfig); 25631195e687SMark J Musante 25644b964adaSGeorge Wilson /* 2565ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the 25664b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or 25674b964adaSGeorge Wilson * more toplevel vdevs are faulted. 25684b964adaSGeorge Wilson */ 25694b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) 25704b964adaSGeorge Wilson return (ENXIO); 25714b964adaSGeorge Wilson 25721195e687SMark J Musante if (spa_check_logs(spa)) { 25731195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 25741195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); 25751195e687SMark J Musante } 2576b24ab676SJeff Bonwick } 2577b24ab676SJeff Bonwick 2578ad135b5dSChristopher Siden if (missing_feat_write) { 2579ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT); 2580ad135b5dSChristopher Siden 2581ad135b5dSChristopher Siden /* 2582ad135b5dSChristopher Siden * At this point, we know that we can open the pool in 2583ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough 2584ad135b5dSChristopher Siden * information and can return to userland. 2585ad135b5dSChristopher Siden */ 2586ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); 2587ad135b5dSChristopher Siden } 2588ad135b5dSChristopher Siden 25894b964adaSGeorge Wilson /* 25904b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready 25914b964adaSGeorge Wilson * to start pushing transactions. 25924b964adaSGeorge Wilson */ 25934b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) { 25944b964adaSGeorge Wilson if (error = spa_load_verify(spa)) 25954b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 25964b964adaSGeorge Wilson error)); 25974b964adaSGeorge Wilson } 25984b964adaSGeorge Wilson 2599468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 2600468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) { 26015dabedeeSbonwick dmu_tx_t *tx; 26020373e76bSbonwick int need_update = B_FALSE; 26038ad4d6ddSJeff Bonwick 26048ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT); 26055dabedeeSbonwick 26060373e76bSbonwick /* 26070373e76bSbonwick * Claim log blocks that haven't been committed yet. 26080373e76bSbonwick * This must all happen in a single txg. 2609b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(), 2610b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback. 2611468c413aSTim Haley * Price of rollback is that we abandon the log. 26120373e76bSbonwick */ 2613b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE; 2614b24ab676SJeff Bonwick 26155dabedeeSbonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), 2616fa9e4066Sahrens spa_first_txg(spa)); 2617e14bb325SJeff Bonwick (void) dmu_objset_find(spa_name(spa), 26180b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN); 2619fa9e4066Sahrens dmu_tx_commit(tx); 2620fa9e4066Sahrens 2621b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE; 2622b24ab676SJeff Bonwick 26231195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD); 2624fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 2625fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 2626fa9e4066Sahrens 2627fa9e4066Sahrens /* 2628b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest 2629b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks 2630b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg 2631b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain() 2632b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above. 2633fa9e4066Sahrens */ 2634b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 26350e34b6a7Sbonwick 26360e34b6a7Sbonwick /* 26370373e76bSbonwick * If the config cache is stale, or we have uninitialized 26380373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config. 2639bc758434SLin Ling * 26404b964adaSGeorge Wilson * If this is a verbatim import, trust the current 2641bc758434SLin Ling * in-core spa_config and update the disk labels. 26420e34b6a7Sbonwick */ 26430373e76bSbonwick if (config_cache_txg != spa->spa_config_txg || 26444b964adaSGeorge Wilson state == SPA_LOAD_IMPORT || 26454b964adaSGeorge Wilson state == SPA_LOAD_RECOVER || 26464b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 26470373e76bSbonwick need_update = B_TRUE; 26480373e76bSbonwick 26498ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) 26500373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0) 26510373e76bSbonwick need_update = B_TRUE; 26520e34b6a7Sbonwick 26530e34b6a7Sbonwick /* 26540373e76bSbonwick * Update the config cache asychronously in case we're the 26550373e76bSbonwick * root pool, in which case the config cache isn't writable yet. 26560e34b6a7Sbonwick */ 26570373e76bSbonwick if (need_update) 26580373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 26598ad4d6ddSJeff Bonwick 26608ad4d6ddSJeff Bonwick /* 26618ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering. 26628ad4d6ddSJeff Bonwick */ 26633f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 26643f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL)) 26658ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER); 2666503ad85cSMatthew Ahrens 26674445fffbSMatthew Ahrens /* 26684445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if 26694445fffbSMatthew Ahrens * we rebooted in the middle of an operation). 26704445fffbSMatthew Ahrens */ 26714445fffbSMatthew Ahrens spa_history_log_version(spa, "open"); 26724445fffbSMatthew Ahrens 2673503ad85cSMatthew Ahrens /* 2674503ad85cSMatthew Ahrens * Delete any inconsistent datasets. 2675503ad85cSMatthew Ahrens */ 2676503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa), 2677503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 2678ca45db41SChris Kirby 2679ca45db41SChris Kirby /* 2680ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs. 2681ca45db41SChris Kirby */ 2682ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 2683fa9e4066Sahrens } 2684fa9e4066Sahrens 26851195e687SMark J Musante return (0); 2686fa9e4066Sahrens } 2687fa9e4066Sahrens 2688468c413aSTim Haley static int 2689468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 2690468c413aSTim Haley { 2691f9af39baSGeorge Wilson int mode = spa->spa_mode; 2692f9af39baSGeorge Wilson 2693468c413aSTim Haley spa_unload(spa); 2694468c413aSTim Haley spa_deactivate(spa); 2695468c413aSTim Haley 2696468c413aSTim Haley spa->spa_load_max_txg--; 2697468c413aSTim Haley 2698f9af39baSGeorge Wilson spa_activate(spa, mode); 2699468c413aSTim Haley spa_async_suspend(spa); 2700468c413aSTim Haley 27011195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); 2702468c413aSTim Haley } 2703468c413aSTim Haley 2704ad135b5dSChristopher Siden /* 2705ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If 2706ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 2707ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 2708ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as 2709ad135b5dSChristopher Siden * spa_load(). 2710ad135b5dSChristopher Siden */ 2711468c413aSTim Haley static int 2712468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 2713c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags) 2714468c413aSTim Haley { 2715ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL; 2716468c413aSTim Haley nvlist_t *config = NULL; 2717468c413aSTim Haley int load_error, rewind_error; 2718c8ee1847SVictor Latushkin uint64_t safe_rewind_txg; 2719468c413aSTim Haley uint64_t min_txg; 2720468c413aSTim Haley 2721a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 2722468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg; 27231195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2724a33cae98STim Haley } else { 2725468c413aSTim Haley spa->spa_load_max_txg = max_request; 2726a33cae98STim Haley } 2727468c413aSTim Haley 27281195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, 27291195e687SMark J Musante mosconfig); 2730468c413aSTim Haley if (load_error == 0) 2731468c413aSTim Haley return (0); 2732468c413aSTim Haley 2733468c413aSTim Haley if (spa->spa_root_vdev != NULL) 2734468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2735468c413aSTim Haley 2736468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 2737468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 2738468c413aSTim Haley 2739c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) { 2740468c413aSTim Haley nvlist_free(config); 2741468c413aSTim Haley return (load_error); 2742468c413aSTim Haley } 2743468c413aSTim Haley 2744ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2745ad135b5dSChristopher Siden /* Price of rolling back is discarding txgs, including log */ 27461195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR); 2747ad135b5dSChristopher Siden } else { 2748ad135b5dSChristopher Siden /* 2749ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first 2750ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting 2751ad135b5dSChristopher Siden * to rewind. 2752ad135b5dSChristopher Siden */ 2753ad135b5dSChristopher Siden loadinfo = spa->spa_load_info; 2754ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc(); 2755ad135b5dSChristopher Siden } 2756468c413aSTim Haley 2757c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 2758c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 2759c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 2760c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg; 2761468c413aSTim Haley 2762c8ee1847SVictor Latushkin /* 2763c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within 2764c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks 2765c8ee1847SVictor Latushkin */ 2766c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 2767c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 2768c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg) 2769468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE; 2770468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig); 2771468c413aSTim Haley } 2772468c413aSTim Haley 2773468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE; 2774468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 2775468c413aSTim Haley 2776468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 2777468c413aSTim Haley spa_config_set(spa, config); 2778468c413aSTim Haley 2779ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) { 2780ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL); 2781ad135b5dSChristopher Siden return (rewind_error); 2782ad135b5dSChristopher Siden } else { 2783ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */ 2784ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 2785ad135b5dSChristopher Siden spa->spa_load_info); 2786ad135b5dSChristopher Siden 2787ad135b5dSChristopher Siden /* Restore the initial load info */ 2788ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info); 2789ad135b5dSChristopher Siden spa->spa_load_info = loadinfo; 2790ad135b5dSChristopher Siden 2791ad135b5dSChristopher Siden return (load_error); 2792ad135b5dSChristopher Siden } 2793468c413aSTim Haley } 2794468c413aSTim Haley 2795fa9e4066Sahrens /* 2796fa9e4066Sahrens * Pool Open/Import 2797fa9e4066Sahrens * 2798fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent 2799fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the 2800fa9e4066Sahrens * case of an open, the pool configuration will exist in the 28013d7072f8Seschrock * POOL_STATE_UNINITIALIZED state. 2802fa9e4066Sahrens * 2803fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at 2804fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some 2805fa9e4066Sahrens * ambiguous state. 2806fa9e4066Sahrens */ 2807fa9e4066Sahrens static int 2808468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 2809468c413aSTim Haley nvlist_t **config) 2810fa9e4066Sahrens { 2811fa9e4066Sahrens spa_t *spa; 28124b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN; 2813fa9e4066Sahrens int error; 2814fa9e4066Sahrens int locked = B_FALSE; 2815fa9e4066Sahrens 2816fa9e4066Sahrens *spapp = NULL; 2817fa9e4066Sahrens 2818fa9e4066Sahrens /* 2819fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this 2820fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends 2821fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to 2822fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place. 2823fa9e4066Sahrens */ 2824fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) { 2825fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 2826fa9e4066Sahrens locked = B_TRUE; 2827fa9e4066Sahrens } 2828fa9e4066Sahrens 2829fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 2830fa9e4066Sahrens if (locked) 2831fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2832fa9e4066Sahrens return (ENOENT); 2833fa9e4066Sahrens } 2834468c413aSTim Haley 2835fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 28364b44c88cSTim Haley zpool_rewind_policy_t policy; 28374b44c88cSTim Haley 28384b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, 28394b44c88cSTim Haley &policy); 28404b44c88cSTim Haley if (policy.zrp_request & ZPOOL_DO_REWIND) 28414b44c88cSTim Haley state = SPA_LOAD_RECOVER; 2842fa9e4066Sahrens 28438ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 2844fa9e4066Sahrens 2845468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 2846468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2847468c413aSTim Haley 2848468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 2849c8ee1847SVictor Latushkin policy.zrp_request); 2850fa9e4066Sahrens 2851fa9e4066Sahrens if (error == EBADF) { 2852fa9e4066Sahrens /* 2853560e6e96Seschrock * If vdev_validate() returns failure (indicated by 2854560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates 2855560e6e96Seschrock * that the pool has been exported or destroyed. If 2856560e6e96Seschrock * this is the case, the config cache is out of sync and 2857560e6e96Seschrock * we should remove the pool from the namespace. 2858fa9e4066Sahrens */ 2859fa9e4066Sahrens spa_unload(spa); 2860fa9e4066Sahrens spa_deactivate(spa); 2861c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE); 2862fa9e4066Sahrens spa_remove(spa); 2863fa9e4066Sahrens if (locked) 2864fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2865fa9e4066Sahrens return (ENOENT); 2866ea8dc4b6Seschrock } 2867ea8dc4b6Seschrock 2868ea8dc4b6Seschrock if (error) { 2869fa9e4066Sahrens /* 2870fa9e4066Sahrens * We can't open the pool, but we still have useful 2871fa9e4066Sahrens * information: the state of each vdev after the 2872fa9e4066Sahrens * attempted vdev_open(). Return this to the user. 2873fa9e4066Sahrens */ 28744b964adaSGeorge Wilson if (config != NULL && spa->spa_config) { 2875468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config, 2876468c413aSTim Haley KM_SLEEP) == 0); 28774b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, 28784b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, 28794b964adaSGeorge Wilson spa->spa_load_info) == 0); 28804b964adaSGeorge Wilson } 2881fa9e4066Sahrens spa_unload(spa); 2882fa9e4066Sahrens spa_deactivate(spa); 2883468c413aSTim Haley spa->spa_last_open_failed = error; 2884fa9e4066Sahrens if (locked) 2885fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2886fa9e4066Sahrens *spapp = NULL; 2887fa9e4066Sahrens return (error); 2888fa9e4066Sahrens } 2889fa9e4066Sahrens } 2890fa9e4066Sahrens 2891fa9e4066Sahrens spa_open_ref(spa, tag); 28923d7072f8Seschrock 2893468c413aSTim Haley if (config != NULL) 2894468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 2895468c413aSTim Haley 28964b964adaSGeorge Wilson /* 28974b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we 28984b964adaSGeorge Wilson * gathered while doing the load. 28994b964adaSGeorge Wilson */ 29004b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) { 29014b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 29024b964adaSGeorge Wilson spa->spa_load_info) == 0); 29034b964adaSGeorge Wilson } 29044b964adaSGeorge Wilson 2905a33cae98STim Haley if (locked) { 2906a33cae98STim Haley spa->spa_last_open_failed = 0; 2907a33cae98STim Haley spa->spa_last_ubsync_txg = 0; 2908a33cae98STim Haley spa->spa_load_txg = 0; 2909fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 2910a33cae98STim Haley } 2911fa9e4066Sahrens 2912fa9e4066Sahrens *spapp = spa; 2913fa9e4066Sahrens 2914fa9e4066Sahrens return (0); 2915fa9e4066Sahrens } 2916fa9e4066Sahrens 2917468c413aSTim Haley int 2918468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 2919468c413aSTim Haley nvlist_t **config) 2920468c413aSTim Haley { 2921468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config)); 2922468c413aSTim Haley } 2923468c413aSTim Haley 2924fa9e4066Sahrens int 2925fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag) 2926fa9e4066Sahrens { 2927468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL)); 2928fa9e4066Sahrens } 2929fa9e4066Sahrens 2930ea8dc4b6Seschrock /* 2931ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process, 2932ea8dc4b6Seschrock * preventing it from being exported or destroyed. 2933ea8dc4b6Seschrock */ 2934ea8dc4b6Seschrock spa_t * 2935ea8dc4b6Seschrock spa_inject_addref(char *name) 2936ea8dc4b6Seschrock { 2937ea8dc4b6Seschrock spa_t *spa; 2938ea8dc4b6Seschrock 2939ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2940ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) { 2941ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2942ea8dc4b6Seschrock return (NULL); 2943ea8dc4b6Seschrock } 2944ea8dc4b6Seschrock spa->spa_inject_ref++; 2945ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2946ea8dc4b6Seschrock 2947ea8dc4b6Seschrock return (spa); 2948ea8dc4b6Seschrock } 2949ea8dc4b6Seschrock 2950ea8dc4b6Seschrock void 2951ea8dc4b6Seschrock spa_inject_delref(spa_t *spa) 2952ea8dc4b6Seschrock { 2953ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 2954ea8dc4b6Seschrock spa->spa_inject_ref--; 2955ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 2956ea8dc4b6Seschrock } 2957ea8dc4b6Seschrock 2958fa94a07fSbrendan /* 2959fa94a07fSbrendan * Add spares device information to the nvlist. 2960fa94a07fSbrendan */ 296199653d4eSeschrock static void 296299653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config) 296399653d4eSeschrock { 296499653d4eSeschrock nvlist_t **spares; 296599653d4eSeschrock uint_t i, nspares; 296699653d4eSeschrock nvlist_t *nvroot; 296799653d4eSeschrock uint64_t guid; 296899653d4eSeschrock vdev_stat_t *vs; 296999653d4eSeschrock uint_t vsc; 297039c23413Seschrock uint64_t pool; 297199653d4eSeschrock 29726809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 29736809eb4eSEric Schrock 2974fa94a07fSbrendan if (spa->spa_spares.sav_count == 0) 297599653d4eSeschrock return; 297699653d4eSeschrock 297799653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config, 297899653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2979fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 298099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 298199653d4eSeschrock if (nspares != 0) { 298299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot, 298399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 298499653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot, 298599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 298699653d4eSeschrock 298799653d4eSeschrock /* 298899653d4eSeschrock * Go through and find any spares which have since been 298999653d4eSeschrock * repurposed as an active spare. If this is the case, update 299099653d4eSeschrock * their status appropriately. 299199653d4eSeschrock */ 299299653d4eSeschrock for (i = 0; i < nspares; i++) { 299399653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i], 299499653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0); 299589a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) && 299689a89ebfSlling pool != 0ULL) { 299799653d4eSeschrock VERIFY(nvlist_lookup_uint64_array( 29983f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS, 299999653d4eSeschrock (uint64_t **)&vs, &vsc) == 0); 300099653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN; 300199653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED; 300299653d4eSeschrock } 300399653d4eSeschrock } 300499653d4eSeschrock } 300599653d4eSeschrock } 300699653d4eSeschrock 3007fa94a07fSbrendan /* 3008fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats. 3009fa94a07fSbrendan */ 3010fa94a07fSbrendan static void 3011fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config) 3012fa94a07fSbrendan { 3013fa94a07fSbrendan nvlist_t **l2cache; 3014fa94a07fSbrendan uint_t i, j, nl2cache; 3015fa94a07fSbrendan nvlist_t *nvroot; 3016fa94a07fSbrendan uint64_t guid; 3017fa94a07fSbrendan vdev_t *vd; 3018fa94a07fSbrendan vdev_stat_t *vs; 3019fa94a07fSbrendan uint_t vsc; 3020fa94a07fSbrendan 30216809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 30226809eb4eSEric Schrock 3023fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0) 3024fa94a07fSbrendan return; 3025fa94a07fSbrendan 3026fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config, 3027fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 3028fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3029fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3030fa94a07fSbrendan if (nl2cache != 0) { 3031fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, 3032fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3033fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot, 3034fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 3035fa94a07fSbrendan 3036fa94a07fSbrendan /* 3037fa94a07fSbrendan * Update level 2 cache device stats. 3038fa94a07fSbrendan */ 3039fa94a07fSbrendan 3040fa94a07fSbrendan for (i = 0; i < nl2cache; i++) { 3041fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], 3042fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0); 3043fa94a07fSbrendan 3044fa94a07fSbrendan vd = NULL; 3045fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 3046fa94a07fSbrendan if (guid == 3047fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 3048fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j]; 3049fa94a07fSbrendan break; 3050fa94a07fSbrendan } 3051fa94a07fSbrendan } 3052fa94a07fSbrendan ASSERT(vd != NULL); 3053fa94a07fSbrendan 3054fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i], 30553f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 30563f9d6ad7SLin Ling == 0); 3057fa94a07fSbrendan vdev_get_stats(vd, vs); 3058fa94a07fSbrendan } 3059fa94a07fSbrendan } 3060fa94a07fSbrendan } 3061fa94a07fSbrendan 3062ad135b5dSChristopher Siden static void 3063ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config) 3064ad135b5dSChristopher Siden { 3065ad135b5dSChristopher Siden nvlist_t *features; 3066ad135b5dSChristopher Siden zap_cursor_t zc; 3067ad135b5dSChristopher Siden zap_attribute_t za; 3068ad135b5dSChristopher Siden 3069ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 3070ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3071ad135b5dSChristopher Siden 3072ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) { 3073ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3074ad135b5dSChristopher Siden spa->spa_feat_for_read_obj); 3075ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3076ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3077ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3078ad135b5dSChristopher Siden za.za_num_integers == 1); 3079b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3080ad135b5dSChristopher Siden za.za_first_integer)); 3081ad135b5dSChristopher Siden } 3082ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3083ad135b5dSChristopher Siden } 3084ad135b5dSChristopher Siden 3085ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) { 3086ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset, 3087ad135b5dSChristopher Siden spa->spa_feat_for_write_obj); 3088ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0; 3089ad135b5dSChristopher Siden zap_cursor_advance(&zc)) { 3090ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) && 3091ad135b5dSChristopher Siden za.za_num_integers == 1); 3092b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 3093ad135b5dSChristopher Siden za.za_first_integer)); 3094ad135b5dSChristopher Siden } 3095ad135b5dSChristopher Siden zap_cursor_fini(&zc); 3096ad135b5dSChristopher Siden } 3097ad135b5dSChristopher Siden 3098ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 3099ad135b5dSChristopher Siden features) == 0); 3100ad135b5dSChristopher Siden nvlist_free(features); 3101ad135b5dSChristopher Siden } 3102ad135b5dSChristopher Siden 3103fa9e4066Sahrens int 3104ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config, 3105ad135b5dSChristopher Siden char *altroot, size_t buflen) 3106fa9e4066Sahrens { 3107fa9e4066Sahrens int error; 3108fa9e4066Sahrens spa_t *spa; 3109fa9e4066Sahrens 3110fa9e4066Sahrens *config = NULL; 3111468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config); 3112fa9e4066Sahrens 31136809eb4eSEric Schrock if (spa != NULL) { 31146809eb4eSEric Schrock /* 31156809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares 31166809eb4eSEric Schrock * or l2cache devices could change and the config would be 31176809eb4eSEric Schrock * self-inconsistent. 31186809eb4eSEric Schrock */ 31196809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3120ea8dc4b6Seschrock 31216809eb4eSEric Schrock if (*config != NULL) { 312211027bc7STim Haley uint64_t loadtimes[2]; 312311027bc7STim Haley 312411027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec; 312511027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 312611027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config, 312711027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 312811027bc7STim Haley 3129e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config, 31306809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT, 31316809eb4eSEric Schrock spa_get_errlog_size(spa)) == 0); 3132e14bb325SJeff Bonwick 31336809eb4eSEric Schrock if (spa_suspended(spa)) 31346809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config, 31356809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED, 31366809eb4eSEric Schrock spa->spa_failmode) == 0); 31376809eb4eSEric Schrock 31386809eb4eSEric Schrock spa_add_spares(spa, *config); 31396809eb4eSEric Schrock spa_add_l2cache(spa, *config); 3140ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config); 31416809eb4eSEric Schrock } 314299653d4eSeschrock } 314399653d4eSeschrock 3144ea8dc4b6Seschrock /* 3145ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat 3146ea8dc4b6Seschrock * and call spa_lookup() directly. 3147ea8dc4b6Seschrock */ 3148ea8dc4b6Seschrock if (altroot) { 3149ea8dc4b6Seschrock if (spa == NULL) { 3150ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 3151ea8dc4b6Seschrock spa = spa_lookup(name); 3152ea8dc4b6Seschrock if (spa) 3153ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3154ea8dc4b6Seschrock else 3155ea8dc4b6Seschrock altroot[0] = '\0'; 3156ea8dc4b6Seschrock spa = NULL; 3157ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 3158ea8dc4b6Seschrock } else { 3159ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen); 3160ea8dc4b6Seschrock } 3161ea8dc4b6Seschrock } 3162ea8dc4b6Seschrock 31636809eb4eSEric Schrock if (spa != NULL) { 31646809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 3165fa9e4066Sahrens spa_close(spa, FTAG); 31666809eb4eSEric Schrock } 3167fa9e4066Sahrens 3168fa9e4066Sahrens return (error); 3169fa9e4066Sahrens } 3170fa9e4066Sahrens 317199653d4eSeschrock /* 3172fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an 3173fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an 3174fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 3175fa94a07fSbrendan * specified, as long as they are well-formed. 317699653d4eSeschrock */ 317799653d4eSeschrock static int 3178fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 3179fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version, 3180fa94a07fSbrendan vdev_labeltype_t label) 318199653d4eSeschrock { 3182fa94a07fSbrendan nvlist_t **dev; 3183fa94a07fSbrendan uint_t i, ndev; 318499653d4eSeschrock vdev_t *vd; 318599653d4eSeschrock int error; 318699653d4eSeschrock 3187e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3188e14bb325SJeff Bonwick 318999653d4eSeschrock /* 3190fa94a07fSbrendan * It's acceptable to have no devs specified. 319199653d4eSeschrock */ 3192fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 319399653d4eSeschrock return (0); 319499653d4eSeschrock 3195fa94a07fSbrendan if (ndev == 0) 319699653d4eSeschrock return (EINVAL); 319799653d4eSeschrock 319899653d4eSeschrock /* 3199fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this 3200fa94a07fSbrendan * device type. 320199653d4eSeschrock */ 3202fa94a07fSbrendan if (spa_version(spa) < version) 320399653d4eSeschrock return (ENOTSUP); 320499653d4eSeschrock 320539c23413Seschrock /* 3206fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use 320739c23413Seschrock * checking. 320839c23413Seschrock */ 3209fa94a07fSbrendan sav->sav_pending = dev; 3210fa94a07fSbrendan sav->sav_npending = ndev; 321139c23413Seschrock 3212fa94a07fSbrendan for (i = 0; i < ndev; i++) { 3213fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 321499653d4eSeschrock mode)) != 0) 321539c23413Seschrock goto out; 321699653d4eSeschrock 321799653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) { 321899653d4eSeschrock vdev_free(vd); 321939c23413Seschrock error = EINVAL; 322039c23413Seschrock goto out; 322199653d4eSeschrock } 322299653d4eSeschrock 3223fa94a07fSbrendan /* 3224e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in 3225e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it. 3226fa94a07fSbrendan */ 3227e14bb325SJeff Bonwick #ifdef _KERNEL 3228fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 3229fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 3230fa94a07fSbrendan error = ENOTBLK; 3231cd0837ccSGeorge Wilson vdev_free(vd); 3232fa94a07fSbrendan goto out; 3233fa94a07fSbrendan } 3234e14bb325SJeff Bonwick #endif 323599653d4eSeschrock vd->vdev_top = vd; 323699653d4eSeschrock 323739c23413Seschrock if ((error = vdev_open(vd)) == 0 && 3238fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) { 3239fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 324039c23413Seschrock vd->vdev_guid) == 0); 324139c23413Seschrock } 324299653d4eSeschrock 324399653d4eSeschrock vdev_free(vd); 324439c23413Seschrock 3245fa94a07fSbrendan if (error && 3246fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 324739c23413Seschrock goto out; 324839c23413Seschrock else 324939c23413Seschrock error = 0; 325099653d4eSeschrock } 325199653d4eSeschrock 325239c23413Seschrock out: 3253fa94a07fSbrendan sav->sav_pending = NULL; 3254fa94a07fSbrendan sav->sav_npending = 0; 325539c23413Seschrock return (error); 325699653d4eSeschrock } 325799653d4eSeschrock 3258fa94a07fSbrendan static int 3259fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 3260fa94a07fSbrendan { 3261fa94a07fSbrendan int error; 3262fa94a07fSbrendan 3263e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3264e14bb325SJeff Bonwick 3265fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3266fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 3267fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) { 3268fa94a07fSbrendan return (error); 3269fa94a07fSbrendan } 3270fa94a07fSbrendan 3271fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 3272fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 3273fa94a07fSbrendan VDEV_LABEL_L2CACHE)); 3274fa94a07fSbrendan } 3275fa94a07fSbrendan 3276fa94a07fSbrendan static void 3277fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 3278fa94a07fSbrendan const char *config) 3279fa94a07fSbrendan { 3280fa94a07fSbrendan int i; 3281fa94a07fSbrendan 3282fa94a07fSbrendan if (sav->sav_config != NULL) { 3283fa94a07fSbrendan nvlist_t **olddevs; 3284fa94a07fSbrendan uint_t oldndevs; 3285fa94a07fSbrendan nvlist_t **newdevs; 3286fa94a07fSbrendan 3287fa94a07fSbrendan /* 3288fa94a07fSbrendan * Generate new dev list by concatentating with the 3289fa94a07fSbrendan * current dev list. 3290fa94a07fSbrendan */ 3291fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 3292fa94a07fSbrendan &olddevs, &oldndevs) == 0); 3293fa94a07fSbrendan 3294fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) * 3295fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP); 3296fa94a07fSbrendan for (i = 0; i < oldndevs; i++) 3297fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 3298fa94a07fSbrendan KM_SLEEP) == 0); 3299fa94a07fSbrendan for (i = 0; i < ndevs; i++) 3300fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 3301fa94a07fSbrendan KM_SLEEP) == 0); 3302fa94a07fSbrendan 3303fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config, 3304fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0); 3305fa94a07fSbrendan 3306fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, 3307fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0); 3308fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++) 3309fa94a07fSbrendan nvlist_free(newdevs[i]); 3310fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 3311fa94a07fSbrendan } else { 3312fa94a07fSbrendan /* 3313fa94a07fSbrendan * Generate a new dev list. 3314fa94a07fSbrendan */ 3315fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 3316fa94a07fSbrendan KM_SLEEP) == 0); 3317fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 3318fa94a07fSbrendan devs, ndevs) == 0); 3319fa94a07fSbrendan } 3320fa94a07fSbrendan } 3321fa94a07fSbrendan 3322fa94a07fSbrendan /* 3323fa94a07fSbrendan * Stop and drop level 2 ARC devices 3324fa94a07fSbrendan */ 3325fa94a07fSbrendan void 3326fa94a07fSbrendan spa_l2cache_drop(spa_t *spa) 3327fa94a07fSbrendan { 3328fa94a07fSbrendan vdev_t *vd; 3329fa94a07fSbrendan int i; 3330fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache; 3331fa94a07fSbrendan 3332fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) { 3333fa94a07fSbrendan uint64_t pool; 3334fa94a07fSbrendan 3335fa94a07fSbrendan vd = sav->sav_vdevs[i]; 3336fa94a07fSbrendan ASSERT(vd != NULL); 3337fa94a07fSbrendan 33388ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) && 33398ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd)) 3340fa94a07fSbrendan l2arc_remove_vdev(vd); 3341fa94a07fSbrendan } 3342fa94a07fSbrendan } 3343fa94a07fSbrendan 3344fa9e4066Sahrens /* 3345fa9e4066Sahrens * Pool Creation 3346fa9e4066Sahrens */ 3347fa9e4066Sahrens int 3348990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 33494445fffbSMatthew Ahrens nvlist_t *zplprops) 3350fa9e4066Sahrens { 3351fa9e4066Sahrens spa_t *spa; 3352990b4856Slling char *altroot = NULL; 33530373e76bSbonwick vdev_t *rvd; 3354fa9e4066Sahrens dsl_pool_t *dp; 3355fa9e4066Sahrens dmu_tx_t *tx; 3356573ca77eSGeorge Wilson int error = 0; 3357fa9e4066Sahrens uint64_t txg = TXG_INITIAL; 3358fa94a07fSbrendan nvlist_t **spares, **l2cache; 3359fa94a07fSbrendan uint_t nspares, nl2cache; 3360cde58dbcSMatthew Ahrens uint64_t version, obj; 3361ad135b5dSChristopher Siden boolean_t has_features; 3362fa9e4066Sahrens 3363fa9e4066Sahrens /* 3364fa9e4066Sahrens * If this pool already exists, return failure. 3365fa9e4066Sahrens */ 3366fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 3367fa9e4066Sahrens if (spa_lookup(pool) != NULL) { 3368fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3369fa9e4066Sahrens return (EEXIST); 3370fa9e4066Sahrens } 3371fa9e4066Sahrens 3372fa9e4066Sahrens /* 3373fa9e4066Sahrens * Allocate a new spa_t structure. 3374fa9e4066Sahrens */ 3375990b4856Slling (void) nvlist_lookup_string(props, 3376990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3377468c413aSTim Haley spa = spa_add(pool, NULL, altroot); 33788ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global); 3379fa9e4066Sahrens 3380990b4856Slling if (props && (error = spa_prop_validate(spa, props))) { 3381990b4856Slling spa_deactivate(spa); 3382990b4856Slling spa_remove(spa); 3383c5904d13Seschrock mutex_exit(&spa_namespace_lock); 3384990b4856Slling return (error); 3385990b4856Slling } 3386990b4856Slling 3387ad135b5dSChristopher Siden has_features = B_FALSE; 3388ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 3389ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 3390ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem))) 3391ad135b5dSChristopher Siden has_features = B_TRUE; 3392ad135b5dSChristopher Siden } 3393ad135b5dSChristopher Siden 3394ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props, 3395ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 3396990b4856Slling version = SPA_VERSION; 3397ad135b5dSChristopher Siden } 3398ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 3399b24ab676SJeff Bonwick 3400b24ab676SJeff Bonwick spa->spa_first_txg = txg; 3401b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1; 3402990b4856Slling spa->spa_uberblock.ub_version = version; 3403fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 3404fa9e4066Sahrens 340554d692b7SGeorge Wilson /* 340654d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs 340754d692b7SGeorge Wilson */ 340825f89ee2SJeff Bonwick spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 340925f89ee2SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 341054d692b7SGeorge Wilson 34110373e76bSbonwick /* 34120373e76bSbonwick * Create the root vdev. 34130373e76bSbonwick */ 3414e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 34150373e76bSbonwick 341699653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 34170373e76bSbonwick 341899653d4eSeschrock ASSERT(error != 0 || rvd != NULL); 341999653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd); 34200373e76bSbonwick 3421b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot)) 34220373e76bSbonwick error = EINVAL; 342399653d4eSeschrock 342499653d4eSeschrock if (error == 0 && 342599653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 3426fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg, 342799653d4eSeschrock VDEV_ALLOC_ADD)) == 0) { 3428573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) { 3429573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]); 3430573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg); 3431573ca77eSGeorge Wilson } 34320373e76bSbonwick } 34330373e76bSbonwick 3434e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3435fa9e4066Sahrens 343699653d4eSeschrock if (error != 0) { 3437fa9e4066Sahrens spa_unload(spa); 3438fa9e4066Sahrens spa_deactivate(spa); 3439fa9e4066Sahrens spa_remove(spa); 3440fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 3441fa9e4066Sahrens return (error); 3442fa9e4066Sahrens } 3443fa9e4066Sahrens 344499653d4eSeschrock /* 344599653d4eSeschrock * Get the list of spares, if specified. 344699653d4eSeschrock */ 344799653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 344899653d4eSeschrock &spares, &nspares) == 0) { 3449fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 345099653d4eSeschrock KM_SLEEP) == 0); 3451fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 345299653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 3453e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 345499653d4eSeschrock spa_load_spares(spa); 3455e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3456fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 3457fa94a07fSbrendan } 3458fa94a07fSbrendan 3459fa94a07fSbrendan /* 3460fa94a07fSbrendan * Get the list of level 2 cache devices, if specified. 3461fa94a07fSbrendan */ 3462fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 3463fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 3464fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 3465fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0); 3466fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 3467fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 3468e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3469fa94a07fSbrendan spa_load_l2cache(spa); 3470e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 3471fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 347299653d4eSeschrock } 347399653d4eSeschrock 3474ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE; 34750a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 3476fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset; 3477ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE; 3478fa9e4066Sahrens 3479485bbbf5SGeorge Wilson /* 3480485bbbf5SGeorge Wilson * Create DDTs (dedup tables). 3481485bbbf5SGeorge Wilson */ 3482485bbbf5SGeorge Wilson ddt_create(spa); 3483485bbbf5SGeorge Wilson 3484485bbbf5SGeorge Wilson spa_update_dspace(spa); 3485485bbbf5SGeorge Wilson 3486fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg); 3487fa9e4066Sahrens 3488fa9e4066Sahrens /* 3489fa9e4066Sahrens * Create the pool config object. 3490fa9e4066Sahrens */ 3491fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 3492f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 3493fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 3494fa9e4066Sahrens 3495ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3496fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 3497ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 3498ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config"); 3499ea8dc4b6Seschrock } 3500fa9e4066Sahrens 3501ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) 3502ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 3503ad135b5dSChristopher Siden 35043f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset, 35053f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 35063f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) { 35073f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version"); 35083f9d6ad7SLin Ling } 35093f9d6ad7SLin Ling 3510990b4856Slling /* Newly created pools with the right version are always deflated. */ 3511990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 3512990b4856Slling spa->spa_deflate = TRUE; 3513990b4856Slling if (zap_add(spa->spa_meta_objset, 3514990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3515990b4856Slling sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 3516990b4856Slling cmn_err(CE_PANIC, "failed to add deflate"); 3517990b4856Slling } 351899653d4eSeschrock } 351999653d4eSeschrock 3520fa9e4066Sahrens /* 3521cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression 3522fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize 3523fa9e4066Sahrens * keeps changing. 3524fa9e4066Sahrens */ 3525cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 3526cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj, 3527cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx); 3528ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset, 3529cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 3530cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) { 3531cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj"); 3532ea8dc4b6Seschrock } 3533b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 3534cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj)); 3535fa9e4066Sahrens 353606eeb2adSek /* 353706eeb2adSek * Create the pool's history object. 353806eeb2adSek */ 3539990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY) 3540990b4856Slling spa_history_create_obj(spa, tx); 3541990b4856Slling 3542990b4856Slling /* 3543990b4856Slling * Set pool properties. 3544990b4856Slling */ 3545990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 3546990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 35470a4e9518Sgw spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 3548573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 3549b24ab676SJeff Bonwick 3550379c004dSEric Schrock if (props != NULL) { 3551379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE); 35523f9d6ad7SLin Ling spa_sync_props(spa, props, tx); 3553379c004dSEric Schrock } 355406eeb2adSek 3555fa9e4066Sahrens dmu_tx_commit(tx); 3556fa9e4066Sahrens 3557fa9e4066Sahrens spa->spa_sync_on = B_TRUE; 3558fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool); 3559fa9e4066Sahrens 3560fa9e4066Sahrens /* 3561fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our 3562fa9e4066Sahrens * bean counters are appropriately updated. 3563fa9e4066Sahrens */ 3564fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 3565fa9e4066Sahrens 3566c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 3567fa9e4066Sahrens 35684445fffbSMatthew Ahrens spa_history_log_version(spa, "create"); 3569228975ccSek 3570088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount); 3571088f3894Sahrens 3572daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock); 3573daaa36a7SGeorge Wilson 3574fa9e4066Sahrens return (0); 3575fa9e4066Sahrens } 3576fa9e4066Sahrens 3577e7cbe64fSgw #ifdef _KERNEL 3578e7cbe64fSgw /* 357921ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool 358021ecdf64SLin Ling * during the system boot up time. 3581e7cbe64fSgw */ 358221ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 358321ecdf64SLin Ling 358421ecdf64SLin Ling static nvlist_t * 358521ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 3586e7cbe64fSgw { 358721ecdf64SLin Ling nvlist_t *config; 3588e7cbe64fSgw nvlist_t *nvtop, *nvroot; 3589e7cbe64fSgw uint64_t pgid; 3590e7cbe64fSgw 359121ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 359221ecdf64SLin Ling return (NULL); 359321ecdf64SLin Ling 3594e7cbe64fSgw /* 3595e7cbe64fSgw * Add this top-level vdev to the child array. 3596e7cbe64fSgw */ 359721ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 359821ecdf64SLin Ling &nvtop) == 0); 359921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 360021ecdf64SLin Ling &pgid) == 0); 360121ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 3602e7cbe64fSgw 3603e7cbe64fSgw /* 3604e7cbe64fSgw * Put this pool's top-level vdevs into a root vdev. 3605e7cbe64fSgw */ 3606e7cbe64fSgw VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 360721ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 360821ecdf64SLin Ling VDEV_TYPE_ROOT) == 0); 3609e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 3610e7cbe64fSgw VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 3611e7cbe64fSgw VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3612e7cbe64fSgw &nvtop, 1) == 0); 3613e7cbe64fSgw 3614e7cbe64fSgw /* 3615e7cbe64fSgw * Replace the existing vdev_tree with the new root vdev in 3616e7cbe64fSgw * this pool's configuration (remove the old, add the new). 3617e7cbe64fSgw */ 3618e7cbe64fSgw VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 3619e7cbe64fSgw nvlist_free(nvroot); 362021ecdf64SLin Ling return (config); 3621e7cbe64fSgw } 3622e7cbe64fSgw 3623e7cbe64fSgw /* 362421ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better" 362521ecdf64SLin Ling * configuration. A configuration is "better" if the label on that 362621ecdf64SLin Ling * device has a more recent txg. 3627051aabe6Staylor */ 362821ecdf64SLin Ling static void 362921ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 3630051aabe6Staylor { 3631573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) 363221ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 3633051aabe6Staylor 363421ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) { 363521ecdf64SLin Ling nvlist_t *label; 363621ecdf64SLin Ling uint64_t label_txg; 3637051aabe6Staylor 363821ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 363921ecdf64SLin Ling &label) != 0) 364021ecdf64SLin Ling return; 3641051aabe6Staylor 364221ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 364321ecdf64SLin Ling &label_txg) == 0); 3644051aabe6Staylor 364521ecdf64SLin Ling /* 364621ecdf64SLin Ling * Do we have a better boot device? 364721ecdf64SLin Ling */ 364821ecdf64SLin Ling if (label_txg > *txg) { 364921ecdf64SLin Ling *txg = label_txg; 365021ecdf64SLin Ling *avd = vd; 3651051aabe6Staylor } 365221ecdf64SLin Ling nvlist_free(label); 3653051aabe6Staylor } 3654051aabe6Staylor } 3655051aabe6Staylor 3656e7cbe64fSgw /* 3657e7cbe64fSgw * Import a root pool. 3658e7cbe64fSgw * 3659051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of 3660051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 3661051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot. 3662e7cbe64fSgw * 3663e7cbe64fSgw * For Sparc, devpath_list consists the physpath name of the booting device 3664e7cbe64fSgw * no matter the rootpool is a single device pool or a mirrored pool. 3665e7cbe64fSgw * e.g. 3666e7cbe64fSgw * "/pci@1f,0/ide@d/disk@0,0:a" 3667e7cbe64fSgw */ 3668e7cbe64fSgw int 3669051aabe6Staylor spa_import_rootpool(char *devpath, char *devid) 3670e7cbe64fSgw { 367121ecdf64SLin Ling spa_t *spa; 367221ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL; 367321ecdf64SLin Ling nvlist_t *config, *nvtop; 367421ecdf64SLin Ling uint64_t guid, txg; 3675e7cbe64fSgw char *pname; 3676e7cbe64fSgw int error; 3677e7cbe64fSgw 3678e7cbe64fSgw /* 367921ecdf64SLin Ling * Read the label from the boot device and generate a configuration. 3680e7cbe64fSgw */ 3681dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3682dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL) 3683dedec472SJack Meng if (config == NULL) { 3684dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) { 3685dedec472SJack Meng /* iscsi boot */ 3686dedec472SJack Meng get_iscsi_bootpath_phy(devpath); 3687dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid); 3688dedec472SJack Meng } 3689dedec472SJack Meng } 3690dedec472SJack Meng #endif 3691dedec472SJack Meng if (config == NULL) { 3692ad135b5dSChristopher Siden cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 369321ecdf64SLin Ling devpath); 369421ecdf64SLin Ling return (EIO); 369521ecdf64SLin Ling } 3696e7cbe64fSgw 369721ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 369821ecdf64SLin Ling &pname) == 0); 369921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 3700e7cbe64fSgw 37016809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 37026809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) { 37036809eb4eSEric Schrock /* 37046809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we 37056809eb4eSEric Schrock * can replace it with the correct config we just read in. 37066809eb4eSEric Schrock */ 37076809eb4eSEric Schrock spa_remove(spa); 37086809eb4eSEric Schrock } 37096809eb4eSEric Schrock 3710468c413aSTim Haley spa = spa_add(pname, config, NULL); 37116809eb4eSEric Schrock spa->spa_is_root = B_TRUE; 37124b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 3713e7cbe64fSgw 371421ecdf64SLin Ling /* 371521ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config. 371621ecdf64SLin Ling */ 371721ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 371821ecdf64SLin Ling &nvtop) == 0); 371921ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 372021ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 372121ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL); 372221ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 372321ecdf64SLin Ling if (error) { 372421ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 372521ecdf64SLin Ling nvlist_free(config); 372621ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 372721ecdf64SLin Ling pname); 372821ecdf64SLin Ling return (error); 372921ecdf64SLin Ling } 373021ecdf64SLin Ling 373121ecdf64SLin Ling /* 373221ecdf64SLin Ling * Get the boot vdev. 373321ecdf64SLin Ling */ 373421ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 373521ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 373621ecdf64SLin Ling (u_longlong_t)guid); 373721ecdf64SLin Ling error = ENOENT; 373821ecdf64SLin Ling goto out; 373921ecdf64SLin Ling } 3740e7cbe64fSgw 374121ecdf64SLin Ling /* 374221ecdf64SLin Ling * Determine if there is a better boot device. 374321ecdf64SLin Ling */ 374421ecdf64SLin Ling avd = bvd; 374521ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg); 374621ecdf64SLin Ling if (avd != bvd) { 374721ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 374821ecdf64SLin Ling "try booting from '%s'", avd->vdev_path); 374921ecdf64SLin Ling error = EINVAL; 375021ecdf64SLin Ling goto out; 375121ecdf64SLin Ling } 3752e7cbe64fSgw 375321ecdf64SLin Ling /* 375421ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that 375521ecdf64SLin Ling * we're booting off the active spare. 375621ecdf64SLin Ling */ 375721ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 375821ecdf64SLin Ling !bvd->vdev_isspare) { 375921ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please " 376021ecdf64SLin Ling "try booting from '%s'", 3761cb04b873SMark J Musante bvd->vdev_parent-> 3762cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 376321ecdf64SLin Ling error = EINVAL; 376421ecdf64SLin Ling goto out; 376521ecdf64SLin Ling } 376621ecdf64SLin Ling 376721ecdf64SLin Ling error = 0; 376821ecdf64SLin Ling out: 376921ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 377021ecdf64SLin Ling vdev_free(rvd); 377121ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG); 377221ecdf64SLin Ling mutex_exit(&spa_namespace_lock); 377321ecdf64SLin Ling 377421ecdf64SLin Ling nvlist_free(config); 3775e7cbe64fSgw return (error); 3776e7cbe64fSgw } 377721ecdf64SLin Ling 3778e7cbe64fSgw #endif 3779e7cbe64fSgw 37806809eb4eSEric Schrock /* 37816809eb4eSEric Schrock * Import a non-root pool into the system. 37826809eb4eSEric Schrock */ 3783c5904d13Seschrock int 37844b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 3785c5904d13Seschrock { 37866809eb4eSEric Schrock spa_t *spa; 37876809eb4eSEric Schrock char *altroot = NULL; 3788468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT; 3789468c413aSTim Haley zpool_rewind_policy_t policy; 3790f9af39baSGeorge Wilson uint64_t mode = spa_mode_global; 3791f9af39baSGeorge Wilson uint64_t readonly = B_FALSE; 37926809eb4eSEric Schrock int error; 37936809eb4eSEric Schrock nvlist_t *nvroot; 37946809eb4eSEric Schrock nvlist_t **spares, **l2cache; 37956809eb4eSEric Schrock uint_t nspares, nl2cache; 37966809eb4eSEric Schrock 37976809eb4eSEric Schrock /* 37986809eb4eSEric Schrock * If a pool with this name exists, return failure. 37996809eb4eSEric Schrock */ 38006809eb4eSEric Schrock mutex_enter(&spa_namespace_lock); 38011195e687SMark J Musante if (spa_lookup(pool) != NULL) { 38026809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38036809eb4eSEric Schrock return (EEXIST); 38046809eb4eSEric Schrock } 38056809eb4eSEric Schrock 38066809eb4eSEric Schrock /* 38076809eb4eSEric Schrock * Create and initialize the spa structure. 38086809eb4eSEric Schrock */ 38096809eb4eSEric Schrock (void) nvlist_lookup_string(props, 38106809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 3811f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props, 3812f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3813f9af39baSGeorge Wilson if (readonly) 3814f9af39baSGeorge Wilson mode = FREAD; 3815468c413aSTim Haley spa = spa_add(pool, config, altroot); 38164b964adaSGeorge Wilson spa->spa_import_flags = flags; 38174b964adaSGeorge Wilson 38184b964adaSGeorge Wilson /* 38194b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace 38204b964adaSGeorge Wilson * as if it had been loaded at boot. 38214b964adaSGeorge Wilson */ 38224b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 38234b964adaSGeorge Wilson if (props != NULL) 38244b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE); 38254b964adaSGeorge Wilson 38264b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 38274b964adaSGeorge Wilson 38284b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock); 38294445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 38304b964adaSGeorge Wilson 38314b964adaSGeorge Wilson return (0); 38324b964adaSGeorge Wilson } 38334b964adaSGeorge Wilson 3834f9af39baSGeorge Wilson spa_activate(spa, mode); 38356809eb4eSEric Schrock 383625f89ee2SJeff Bonwick /* 383725f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy. 383825f89ee2SJeff Bonwick */ 383925f89ee2SJeff Bonwick spa_async_suspend(spa); 384025f89ee2SJeff Bonwick 38414b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 38424b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND) 38434b964adaSGeorge Wilson state = SPA_LOAD_RECOVER; 38444b964adaSGeorge Wilson 38456809eb4eSEric Schrock /* 38466809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 38476809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when 38486809eb4eSEric Schrock * doing an import. 38496809eb4eSEric Schrock */ 3850468c413aSTim Haley if (state != SPA_LOAD_RECOVER) 3851468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 38524b964adaSGeorge Wilson 3853468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 3854c8ee1847SVictor Latushkin policy.zrp_request); 3855468c413aSTim Haley 3856468c413aSTim Haley /* 38574b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it 38584b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc). 3859468c413aSTim Haley */ 38604b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 38614b964adaSGeorge Wilson spa->spa_load_info) == 0); 38626809eb4eSEric Schrock 38636809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 38646809eb4eSEric Schrock /* 38656809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity 38666809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare(). 38676809eb4eSEric Schrock */ 38686809eb4eSEric Schrock if (spa->spa_spares.sav_config) { 38696809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config); 38706809eb4eSEric Schrock spa->spa_spares.sav_config = NULL; 38716809eb4eSEric Schrock spa_load_spares(spa); 38726809eb4eSEric Schrock } 38736809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) { 38746809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config); 38756809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL; 38766809eb4eSEric Schrock spa_load_l2cache(spa); 38776809eb4eSEric Schrock } 38786809eb4eSEric Schrock 38796809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 38806809eb4eSEric Schrock &nvroot) == 0); 38816809eb4eSEric Schrock if (error == 0) 38826809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38836809eb4eSEric Schrock VDEV_ALLOC_SPARE); 38846809eb4eSEric Schrock if (error == 0) 38856809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL, 38866809eb4eSEric Schrock VDEV_ALLOC_L2CACHE); 38876809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 38886809eb4eSEric Schrock 38896809eb4eSEric Schrock if (props != NULL) 38906809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE); 38916809eb4eSEric Schrock 38926809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) && 38936809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) { 38946809eb4eSEric Schrock spa_unload(spa); 38956809eb4eSEric Schrock spa_deactivate(spa); 38966809eb4eSEric Schrock spa_remove(spa); 38976809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 38986809eb4eSEric Schrock return (error); 38996809eb4eSEric Schrock } 39006809eb4eSEric Schrock 3901955ef359SLin Ling spa_async_resume(spa); 3902955ef359SLin Ling 39036809eb4eSEric Schrock /* 39046809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by 39056809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc. 39066809eb4eSEric Schrock */ 39076809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 39086809eb4eSEric Schrock &spares, &nspares) == 0) { 39096809eb4eSEric Schrock if (spa->spa_spares.sav_config) 39106809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config, 39116809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 39126809eb4eSEric Schrock else 39136809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 39146809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 39156809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 39166809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 39176809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 39186809eb4eSEric Schrock spa_load_spares(spa); 39196809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 39206809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE; 39216809eb4eSEric Schrock } 39226809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 39236809eb4eSEric Schrock &l2cache, &nl2cache) == 0) { 39246809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) 39256809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 39266809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 39276809eb4eSEric Schrock else 39286809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 39296809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0); 39306809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 39316809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 39326809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 39336809eb4eSEric Schrock spa_load_l2cache(spa); 39346809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG); 39356809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE; 39366809eb4eSEric Schrock } 39376809eb4eSEric Schrock 3938b693757aSEric Schrock /* 3939b693757aSEric Schrock * Check for any removed devices. 3940b693757aSEric Schrock */ 3941b693757aSEric Schrock if (spa->spa_autoreplace) { 3942b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares); 3943b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache); 3944b693757aSEric Schrock } 3945b693757aSEric Schrock 39466809eb4eSEric Schrock if (spa_writeable(spa)) { 39476809eb4eSEric Schrock /* 39486809eb4eSEric Schrock * Update the config cache to include the newly-imported pool. 39496809eb4eSEric Schrock */ 3950bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 39516809eb4eSEric Schrock } 39526809eb4eSEric Schrock 3953573ca77eSGeorge Wilson /* 3954573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported. 3955573ca77eSGeorge Wilson * We kick off an async task to handle this for us. 3956573ca77eSGeorge Wilson */ 3957573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 3958573ca77eSGeorge Wilson 39596809eb4eSEric Schrock mutex_exit(&spa_namespace_lock); 39604445fffbSMatthew Ahrens spa_history_log_version(spa, "import"); 39616809eb4eSEric Schrock 39626809eb4eSEric Schrock return (0); 3963c5904d13Seschrock } 3964c5904d13Seschrock 3965fa9e4066Sahrens nvlist_t * 3966fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig) 3967fa9e4066Sahrens { 3968fa9e4066Sahrens nvlist_t *config = NULL; 3969fa9e4066Sahrens char *poolname; 3970fa9e4066Sahrens spa_t *spa; 3971fa9e4066Sahrens uint64_t state; 39727b7154beSLin Ling int error; 3973fa9e4066Sahrens 3974fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3975fa9e4066Sahrens return (NULL); 3976fa9e4066Sahrens 3977fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3978fa9e4066Sahrens return (NULL); 3979fa9e4066Sahrens 3980fa9e4066Sahrens /* 39810373e76bSbonwick * Create and initialize the spa structure. 3982fa9e4066Sahrens */ 39830373e76bSbonwick mutex_enter(&spa_namespace_lock); 3984468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 39858ad4d6ddSJeff Bonwick spa_activate(spa, FREAD); 3986fa9e4066Sahrens 3987fa9e4066Sahrens /* 39880373e76bSbonwick * Pass off the heavy lifting to spa_load(). 3989ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config 3990ecc2d604Sbonwick * is actually the one to trust when doing an import. 3991fa9e4066Sahrens */ 39921195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); 3993fa9e4066Sahrens 3994fa9e4066Sahrens /* 3995fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config. 3996fa9e4066Sahrens */ 3997fa9e4066Sahrens if (spa->spa_root_vdev != NULL) { 3998fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3999fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 4000fa9e4066Sahrens poolname) == 0); 4001fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 4002fa9e4066Sahrens state) == 0); 400395173954Sek VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 400495173954Sek spa->spa_uberblock.ub_timestamp) == 0); 4005ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 4006ad135b5dSChristopher Siden spa->spa_load_info) == 0); 400799653d4eSeschrock 4008e7cbe64fSgw /* 4009e7cbe64fSgw * If the bootfs property exists on this pool then we 4010e7cbe64fSgw * copy it out so that external consumers can tell which 4011e7cbe64fSgw * pools are bootable. 4012e7cbe64fSgw */ 40137b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) { 4014e7cbe64fSgw char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4015e7cbe64fSgw 4016e7cbe64fSgw /* 4017e7cbe64fSgw * We have to play games with the name since the 4018e7cbe64fSgw * pool was opened as TRYIMPORT_NAME. 4019e7cbe64fSgw */ 4020e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa), 4021e7cbe64fSgw spa->spa_bootfs, tmpname) == 0) { 4022e7cbe64fSgw char *cp; 4023e7cbe64fSgw char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4024e7cbe64fSgw 4025e7cbe64fSgw cp = strchr(tmpname, '/'); 4026e7cbe64fSgw if (cp == NULL) { 4027e7cbe64fSgw (void) strlcpy(dsname, tmpname, 4028e7cbe64fSgw MAXPATHLEN); 4029e7cbe64fSgw } else { 4030e7cbe64fSgw (void) snprintf(dsname, MAXPATHLEN, 4031e7cbe64fSgw "%s/%s", poolname, ++cp); 4032e7cbe64fSgw } 4033e7cbe64fSgw VERIFY(nvlist_add_string(config, 4034e7cbe64fSgw ZPOOL_CONFIG_BOOTFS, dsname) == 0); 4035e7cbe64fSgw kmem_free(dsname, MAXPATHLEN); 4036e7cbe64fSgw } 4037e7cbe64fSgw kmem_free(tmpname, MAXPATHLEN); 4038e7cbe64fSgw } 4039e7cbe64fSgw 404099653d4eSeschrock /* 4041fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices. 404299653d4eSeschrock */ 40436809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 404499653d4eSeschrock spa_add_spares(spa, config); 4045fa94a07fSbrendan spa_add_l2cache(spa, config); 40466809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG); 4047fa9e4066Sahrens } 4048fa9e4066Sahrens 4049fa9e4066Sahrens spa_unload(spa); 4050fa9e4066Sahrens spa_deactivate(spa); 4051fa9e4066Sahrens spa_remove(spa); 4052fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4053fa9e4066Sahrens 4054fa9e4066Sahrens return (config); 4055fa9e4066Sahrens } 4056fa9e4066Sahrens 4057fa9e4066Sahrens /* 4058fa9e4066Sahrens * Pool export/destroy 4059fa9e4066Sahrens * 4060fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there 4061fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we 4062fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the 4063394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then 4064394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache. 4065fa9e4066Sahrens */ 4066fa9e4066Sahrens static int 406789a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 4068394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce) 4069fa9e4066Sahrens { 4070fa9e4066Sahrens spa_t *spa; 4071fa9e4066Sahrens 407244cd46caSbillm if (oldconfig) 407344cd46caSbillm *oldconfig = NULL; 407444cd46caSbillm 40758ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE)) 4076fa9e4066Sahrens return (EROFS); 4077fa9e4066Sahrens 4078fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 4079fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) { 4080fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4081fa9e4066Sahrens return (ENOENT); 4082fa9e4066Sahrens } 4083fa9e4066Sahrens 4084ea8dc4b6Seschrock /* 4085ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks, 4086ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export. 4087ea8dc4b6Seschrock */ 4088ea8dc4b6Seschrock spa_open_ref(spa, FTAG); 4089ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 4090ea8dc4b6Seschrock spa_async_suspend(spa); 4091ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock); 4092ea8dc4b6Seschrock spa_close(spa, FTAG); 4093ea8dc4b6Seschrock 4094fa9e4066Sahrens /* 4095fa9e4066Sahrens * The pool will be in core if it's openable, 4096fa9e4066Sahrens * in which case we can modify its state. 4097fa9e4066Sahrens */ 4098fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 4099fa9e4066Sahrens /* 4100fa9e4066Sahrens * Objsets may be open only because they're dirty, so we 4101fa9e4066Sahrens * have to force it to sync before checking spa_refcnt. 4102fa9e4066Sahrens */ 4103fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0); 4104fa9e4066Sahrens 4105ea8dc4b6Seschrock /* 4106ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active 4107ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by 4108ea8dc4b6Seschrock * fault injection handlers. 4109ea8dc4b6Seschrock */ 4110ea8dc4b6Seschrock if (!spa_refcount_zero(spa) || 4111ea8dc4b6Seschrock (spa->spa_inject_ref != 0 && 4112ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) { 4113ea8dc4b6Seschrock spa_async_resume(spa); 4114fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4115fa9e4066Sahrens return (EBUSY); 4116fa9e4066Sahrens } 4117fa9e4066Sahrens 411889a89ebfSlling /* 411989a89ebfSlling * A pool cannot be exported if it has an active shared spare. 412089a89ebfSlling * This is to prevent other pools stealing the active spare 412189a89ebfSlling * from an exported pool. At user's own will, such pool can 412289a89ebfSlling * be forcedly exported. 412389a89ebfSlling */ 412489a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED && 412589a89ebfSlling spa_has_active_shared_spare(spa)) { 412689a89ebfSlling spa_async_resume(spa); 412789a89ebfSlling mutex_exit(&spa_namespace_lock); 412889a89ebfSlling return (EXDEV); 412989a89ebfSlling } 413089a89ebfSlling 4131fa9e4066Sahrens /* 4132fa9e4066Sahrens * We want this to be reflected on every label, 4133fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the 4134fa9e4066Sahrens * final sync that pushes these changes out. 4135fa9e4066Sahrens */ 4136394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 4137e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4138ea8dc4b6Seschrock spa->spa_state = new_state; 41393f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) + 41403f9d6ad7SLin Ling TXG_DEFER_SIZE + 1; 4141ea8dc4b6Seschrock vdev_config_dirty(spa->spa_root_vdev); 4142e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4143ea8dc4b6Seschrock } 4144fa9e4066Sahrens } 4145fa9e4066Sahrens 41463d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 41473d7072f8Seschrock 4148fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4149fa9e4066Sahrens spa_unload(spa); 4150fa9e4066Sahrens spa_deactivate(spa); 4151fa9e4066Sahrens } 4152fa9e4066Sahrens 415344cd46caSbillm if (oldconfig && spa->spa_config) 415444cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 415544cd46caSbillm 4156ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) { 4157394ab0cbSGeorge Wilson if (!hardforce) 4158394ab0cbSGeorge Wilson spa_config_sync(spa, B_TRUE, B_TRUE); 4159ea8dc4b6Seschrock spa_remove(spa); 4160ea8dc4b6Seschrock } 4161fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 4162fa9e4066Sahrens 4163fa9e4066Sahrens return (0); 4164fa9e4066Sahrens } 4165fa9e4066Sahrens 4166fa9e4066Sahrens /* 4167fa9e4066Sahrens * Destroy a storage pool. 4168fa9e4066Sahrens */ 4169fa9e4066Sahrens int 4170fa9e4066Sahrens spa_destroy(char *pool) 4171fa9e4066Sahrens { 4172394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 4173394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4174fa9e4066Sahrens } 4175fa9e4066Sahrens 4176fa9e4066Sahrens /* 4177fa9e4066Sahrens * Export a storage pool. 4178fa9e4066Sahrens */ 4179fa9e4066Sahrens int 4180394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 4181394ab0cbSGeorge Wilson boolean_t hardforce) 4182fa9e4066Sahrens { 4183394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 4184394ab0cbSGeorge Wilson force, hardforce)); 4185fa9e4066Sahrens } 4186fa9e4066Sahrens 4187ea8dc4b6Seschrock /* 4188ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it 4189ea8dc4b6Seschrock * from the namespace in any way. 4190ea8dc4b6Seschrock */ 4191ea8dc4b6Seschrock int 4192ea8dc4b6Seschrock spa_reset(char *pool) 4193ea8dc4b6Seschrock { 419489a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 4195394ab0cbSGeorge Wilson B_FALSE, B_FALSE)); 4196ea8dc4b6Seschrock } 4197ea8dc4b6Seschrock 4198fa9e4066Sahrens /* 4199fa9e4066Sahrens * ========================================================================== 4200fa9e4066Sahrens * Device manipulation 4201fa9e4066Sahrens * ========================================================================== 4202fa9e4066Sahrens */ 4203fa9e4066Sahrens 4204fa9e4066Sahrens /* 42058654d025Sperrin * Add a device to a storage pool. 4206fa9e4066Sahrens */ 4207fa9e4066Sahrens int 4208fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 4209fa9e4066Sahrens { 421088ecc943SGeorge Wilson uint64_t txg, id; 42118ad4d6ddSJeff Bonwick int error; 4212fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 42130e34b6a7Sbonwick vdev_t *vd, *tvd; 4214fa94a07fSbrendan nvlist_t **spares, **l2cache; 4215fa94a07fSbrendan uint_t nspares, nl2cache; 4216fa9e4066Sahrens 4217f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4218f9af39baSGeorge Wilson 4219fa9e4066Sahrens txg = spa_vdev_enter(spa); 4220fa9e4066Sahrens 422199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 422299653d4eSeschrock VDEV_ALLOC_ADD)) != 0) 422399653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error)); 4224fa9e4066Sahrens 4225e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 422699653d4eSeschrock 4227fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 4228fa94a07fSbrendan &nspares) != 0) 422999653d4eSeschrock nspares = 0; 423099653d4eSeschrock 4231fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 4232fa94a07fSbrendan &nl2cache) != 0) 4233fa94a07fSbrendan nl2cache = 0; 4234fa94a07fSbrendan 4235e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 4236fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL)); 4237fa9e4066Sahrens 4238e14bb325SJeff Bonwick if (vd->vdev_children != 0 && 4239e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0) 4240e14bb325SJeff Bonwick return (spa_vdev_exit(spa, vd, txg, error)); 424199653d4eSeschrock 424239c23413Seschrock /* 4243fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the 4244fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 424539c23413Seschrock */ 4246e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 424739c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error)); 424839c23413Seschrock 424939c23413Seschrock /* 425039c23413Seschrock * Transfer each new top-level vdev from vd to rvd. 425139c23413Seschrock */ 42528ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) { 425388ecc943SGeorge Wilson 425488ecc943SGeorge Wilson /* 425588ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists. 425688ecc943SGeorge Wilson */ 425788ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) { 425888ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) { 425988ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]); 426088ecc943SGeorge Wilson break; 426188ecc943SGeorge Wilson } 426288ecc943SGeorge Wilson } 426339c23413Seschrock tvd = vd->vdev_child[c]; 426439c23413Seschrock vdev_remove_child(vd, tvd); 426588ecc943SGeorge Wilson tvd->vdev_id = id; 426639c23413Seschrock vdev_add_child(rvd, tvd); 426739c23413Seschrock vdev_config_dirty(tvd); 426839c23413Seschrock } 426939c23413Seschrock 427099653d4eSeschrock if (nspares != 0) { 4271fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 4272fa94a07fSbrendan ZPOOL_CONFIG_SPARES); 427399653d4eSeschrock spa_load_spares(spa); 4274fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE; 4275fa94a07fSbrendan } 4276fa94a07fSbrendan 4277fa94a07fSbrendan if (nl2cache != 0) { 4278fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 4279fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE); 4280fa94a07fSbrendan spa_load_l2cache(spa); 4281fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 4282fa9e4066Sahrens } 4283fa9e4066Sahrens 4284fa9e4066Sahrens /* 42850e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool. 42860e34b6a7Sbonwick * If other threads start allocating from these vdevs before we 42870e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may 42880e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache 42890e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without 42900e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 42910373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs. 42920e34b6a7Sbonwick * 42930e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that 42940e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining 42950e34b6a7Sbonwick * steps will be completed the next time we load the pool. 42960e34b6a7Sbonwick */ 42970373e76bSbonwick (void) spa_vdev_exit(spa, vd, txg, 0); 42980e34b6a7Sbonwick 42990373e76bSbonwick mutex_enter(&spa_namespace_lock); 43000373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 43010373e76bSbonwick mutex_exit(&spa_namespace_lock); 4302fa9e4066Sahrens 43030373e76bSbonwick return (0); 4304fa9e4066Sahrens } 4305fa9e4066Sahrens 4306fa9e4066Sahrens /* 4307fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device 4308fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies 4309fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev. 4310fa9e4066Sahrens * 4311fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the 4312fa9e4066Sahrens * existing device; in this case the two devices are made into their own 43133d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to 4314fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few 4315fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon 4316fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced) 4317fa9e4066Sahrens * is automatically detached. 4318fa9e4066Sahrens */ 4319fa9e4066Sahrens int 4320ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 4321fa9e4066Sahrens { 43223f9d6ad7SLin Ling uint64_t txg, dtl_max_txg; 4323fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4324fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 432599653d4eSeschrock vdev_ops_t *pvops; 43269b3f6b42SEric Kustarz char *oldvdpath, *newvdpath; 43279b3f6b42SEric Kustarz int newvd_isspare; 43289b3f6b42SEric Kustarz int error; 4329fa9e4066Sahrens 4330f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4331f9af39baSGeorge Wilson 4332fa9e4066Sahrens txg = spa_vdev_enter(spa); 4333fa9e4066Sahrens 4334c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 4335fa9e4066Sahrens 4336fa9e4066Sahrens if (oldvd == NULL) 4337fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4338fa9e4066Sahrens 43390e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf) 43400e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 43410e34b6a7Sbonwick 4342fa9e4066Sahrens pvd = oldvd->vdev_parent; 4343fa9e4066Sahrens 434499653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 4345cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0) 43463d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 43473d7072f8Seschrock 43483d7072f8Seschrock if (newrootvd->vdev_children != 1) 4349fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4350fa9e4066Sahrens 4351fa9e4066Sahrens newvd = newrootvd->vdev_child[0]; 4352fa9e4066Sahrens 4353fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf) 4354fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 4355fa9e4066Sahrens 435699653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 4357fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error)); 4358fa9e4066Sahrens 43598654d025Sperrin /* 43608654d025Sperrin * Spares can't replace logs 43618654d025Sperrin */ 4362ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 43638654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 43648654d025Sperrin 436599653d4eSeschrock if (!replacing) { 436699653d4eSeschrock /* 436799653d4eSeschrock * For attach, the only allowable parent is a mirror or the root 436899653d4eSeschrock * vdev. 436999653d4eSeschrock */ 437099653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops && 437199653d4eSeschrock pvd->vdev_ops != &vdev_root_ops) 437299653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 437399653d4eSeschrock 437499653d4eSeschrock pvops = &vdev_mirror_ops; 437599653d4eSeschrock } else { 437699653d4eSeschrock /* 437799653d4eSeschrock * Active hot spares can only be replaced by inactive hot 437899653d4eSeschrock * spares. 437999653d4eSeschrock */ 438099653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4381cb04b873SMark J Musante oldvd->vdev_isspare && 438299653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid)) 438399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 438499653d4eSeschrock 438599653d4eSeschrock /* 438699653d4eSeschrock * If the source is a hot spare, and the parent isn't already a 438799653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we 438839c23413Seschrock * want to create a replacing vdev. The user is not allowed to 438939c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is 439039c23413Seschrock * the same (spare replaces spare, non-spare replaces 439139c23413Seschrock * non-spare). 439299653d4eSeschrock */ 4393cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && 4394cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 439599653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4396cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops && 4397cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) { 439839c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 4399cb04b873SMark J Musante } 4400cb04b873SMark J Musante 4401cb04b873SMark J Musante if (newvd->vdev_isspare) 440299653d4eSeschrock pvops = &vdev_spare_ops; 440399653d4eSeschrock else 440499653d4eSeschrock pvops = &vdev_replacing_ops; 440599653d4eSeschrock } 440699653d4eSeschrock 44072a79c5feSlling /* 4408573ca77eSGeorge Wilson * Make sure the new device is big enough. 44092a79c5feSlling */ 4410573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 4411fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 4412fa9e4066Sahrens 4413ecc2d604Sbonwick /* 4414ecc2d604Sbonwick * The new device cannot have a higher alignment requirement 4415ecc2d604Sbonwick * than the top-level vdev. 4416ecc2d604Sbonwick */ 4417ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 4418fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 4419fa9e4066Sahrens 4420fa9e4066Sahrens /* 4421fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid 4422fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on. 4423fa9e4066Sahrens */ 4424fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 4425fa9e4066Sahrens spa_strfree(oldvd->vdev_path); 4426fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 4427fa9e4066Sahrens KM_SLEEP); 4428fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s", 4429fa9e4066Sahrens newvd->vdev_path, "old"); 4430fa9e4066Sahrens if (oldvd->vdev_devid != NULL) { 4431fa9e4066Sahrens spa_strfree(oldvd->vdev_devid); 4432fa9e4066Sahrens oldvd->vdev_devid = NULL; 4433fa9e4066Sahrens } 4434fa9e4066Sahrens } 4435fa9e4066Sahrens 4436cb04b873SMark J Musante /* mark the device being resilvered */ 4437cb04b873SMark J Musante newvd->vdev_resilvering = B_TRUE; 4438cb04b873SMark J Musante 4439fa9e4066Sahrens /* 444099653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new 444199653d4eSeschrock * mirror/replacing/spare vdev above oldvd. 4442fa9e4066Sahrens */ 4443fa9e4066Sahrens if (pvd->vdev_ops != pvops) 4444fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops); 4445fa9e4066Sahrens 4446fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd); 4447fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops); 4448fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd); 4449fa9e4066Sahrens 4450fa9e4066Sahrens /* 4451fa9e4066Sahrens * Extract the new device from its root and add it to pvd. 4452fa9e4066Sahrens */ 4453fa9e4066Sahrens vdev_remove_child(newrootvd, newvd); 4454fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children; 445588ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg; 4456fa9e4066Sahrens vdev_add_child(pvd, newvd); 4457fa9e4066Sahrens 4458fa9e4066Sahrens tvd = newvd->vdev_top; 4459fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd); 4460fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4461fa9e4066Sahrens 4462fa9e4066Sahrens vdev_config_dirty(tvd); 4463fa9e4066Sahrens 4464fa9e4066Sahrens /* 44653f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 44663f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when 44673f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess(). 4468fa9e4066Sahrens */ 44693f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES; 4470fa9e4066Sahrens 44713f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 44723f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL); 4473fa9e4066Sahrens 44746809eb4eSEric Schrock if (newvd->vdev_isspare) { 447539c23413Seschrock spa_spare_activate(newvd); 44766809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 44776809eb4eSEric Schrock } 44786809eb4eSEric Schrock 4479e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path); 4480e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path); 44819b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare; 4482ea8dc4b6Seschrock 4483fa9e4066Sahrens /* 4484fa9e4066Sahrens * Mark newvd's DTL dirty in this txg. 4485fa9e4066Sahrens */ 4486ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg); 4487fa9e4066Sahrens 44883f9d6ad7SLin Ling /* 44893f9d6ad7SLin Ling * Restart the resilver 44903f9d6ad7SLin Ling */ 44913f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); 44923f9d6ad7SLin Ling 44933f9d6ad7SLin Ling /* 44943f9d6ad7SLin Ling * Commit the config 44953f9d6ad7SLin Ling */ 44963f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 4497fa9e4066Sahrens 44984445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL, 44993f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s", 4500c8e1f6d2SMark J Musante replacing && newvd_isspare ? "spare in" : 4501c8e1f6d2SMark J Musante replacing ? "replace" : "attach", newvdpath, 4502c8e1f6d2SMark J Musante replacing ? "for" : "to", oldvdpath); 45039b3f6b42SEric Kustarz 45049b3f6b42SEric Kustarz spa_strfree(oldvdpath); 45059b3f6b42SEric Kustarz spa_strfree(newvdpath); 45069b3f6b42SEric Kustarz 4507943e9869SLori Alt if (spa->spa_bootfs) 4508943e9869SLori Alt spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH); 4509943e9869SLori Alt 4510fa9e4066Sahrens return (0); 4511fa9e4066Sahrens } 4512fa9e4066Sahrens 4513fa9e4066Sahrens /* 4514fa9e4066Sahrens * Detach a device from a mirror or replacing vdev. 4515fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent 4516fa9e4066Sahrens * is a replacing vdev. 4517fa9e4066Sahrens */ 4518fa9e4066Sahrens int 45198ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 4520fa9e4066Sahrens { 4521fa9e4066Sahrens uint64_t txg; 45228ad4d6ddSJeff Bonwick int error; 4523fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 4524fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd; 452599653d4eSeschrock boolean_t unspare = B_FALSE; 4526d5285caeSGeorge Wilson uint64_t unspare_guid = 0; 45271195e687SMark J Musante char *vdpath; 4528fa9e4066Sahrens 4529f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 4530f9af39baSGeorge Wilson 4531fa9e4066Sahrens txg = spa_vdev_enter(spa); 4532fa9e4066Sahrens 4533c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 4534fa9e4066Sahrens 4535fa9e4066Sahrens if (vd == NULL) 4536fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 4537fa9e4066Sahrens 45380e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 45390e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 45400e34b6a7Sbonwick 4541fa9e4066Sahrens pvd = vd->vdev_parent; 4542fa9e4066Sahrens 45438ad4d6ddSJeff Bonwick /* 45448ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it. 45458ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 45468ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing 45478ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel 45488ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up 45498ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C, 45508ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then 45518ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what 45528ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the 45538ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example, 45548ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R. 45558ad4d6ddSJeff Bonwick */ 45568ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0) 45578ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 45588ad4d6ddSJeff Bonwick 4559fa9e4066Sahrens /* 4560cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced. 456199653d4eSeschrock */ 4562cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 4563cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops) 4564cb04b873SMark J Musante return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 456599653d4eSeschrock 456699653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops || 4567e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES); 4568fa9e4066Sahrens 4569fa9e4066Sahrens /* 457099653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach. 4571fa9e4066Sahrens */ 4572fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops && 457399653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops && 457499653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops) 4575fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4576fa9e4066Sahrens 4577fa9e4066Sahrens /* 45788ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data, 45798ad4d6ddSJeff Bonwick * we cannot safely detach it. 4580fa9e4066Sahrens */ 45818ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd)) 4582fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 4583fa9e4066Sahrens 45848ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2); 4585fa9e4066Sahrens 4586bf82a41bSeschrock /* 4587bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then 4588bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old" 4589bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now. 4590bf82a41bSeschrock */ 4591cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 4592cb04b873SMark J Musante vd->vdev_path != NULL) { 4593cb04b873SMark J Musante size_t len = strlen(vd->vdev_path); 4594cb04b873SMark J Musante 4595cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) { 4596cb04b873SMark J Musante cvd = pvd->vdev_child[c]; 4597cb04b873SMark J Musante 4598cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL) 4599cb04b873SMark J Musante continue; 4600cb04b873SMark J Musante 4601cb04b873SMark J Musante if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 4602cb04b873SMark J Musante strcmp(cvd->vdev_path + len, "/old") == 0) { 4603cb04b873SMark J Musante spa_strfree(cvd->vdev_path); 4604cb04b873SMark J Musante cvd->vdev_path = spa_strdup(vd->vdev_path); 4605cb04b873SMark J Musante break; 4606cb04b873SMark J Musante } 4607bf82a41bSeschrock } 4608bf82a41bSeschrock } 4609bf82a41bSeschrock 461099653d4eSeschrock /* 461199653d4eSeschrock * If we are detaching the original disk from a spare, then it implies 461299653d4eSeschrock * that the spare should become a real disk, and be removed from the 461399653d4eSeschrock * active spare list for the pool. 461499653d4eSeschrock */ 461599653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops && 4616cb04b873SMark J Musante vd->vdev_id == 0 && 4617cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 461899653d4eSeschrock unspare = B_TRUE; 461999653d4eSeschrock 4620fa9e4066Sahrens /* 4621fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things. 4622fa9e4066Sahrens * This must be done after all other error cases are handled, 4623fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it). 4624fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal -- 4625fa9e4066Sahrens * it may be that the unwritability of the disk is the reason 4626fa9e4066Sahrens * it's being detached! 4627fa9e4066Sahrens */ 462839c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 4629fa9e4066Sahrens 4630fa9e4066Sahrens /* 4631fa9e4066Sahrens * Remove vd from its parent and compact the parent's children. 4632fa9e4066Sahrens */ 4633fa9e4066Sahrens vdev_remove_child(pvd, vd); 4634fa9e4066Sahrens vdev_compact_children(pvd); 4635fa9e4066Sahrens 4636fa9e4066Sahrens /* 4637fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below. 4638fa9e4066Sahrens */ 4639cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1]; 4640fa9e4066Sahrens 464199653d4eSeschrock /* 464299653d4eSeschrock * If we need to remove the remaining child from the list of hot spares, 46438ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process. 46448ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can 46458ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar 46468ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach; 46478ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and 46488ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 464999653d4eSeschrock */ 465099653d4eSeschrock if (unspare) { 465199653d4eSeschrock ASSERT(cvd->vdev_isspare); 465239c23413Seschrock spa_spare_remove(cvd); 465399653d4eSeschrock unspare_guid = cvd->vdev_guid; 46548ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 4655cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE; 465699653d4eSeschrock } 465799653d4eSeschrock 4658fa9e4066Sahrens /* 4659fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child, 4660fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree. 4661fa9e4066Sahrens */ 4662cb04b873SMark J Musante if (pvd->vdev_children == 1) { 4663cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops) 4664cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE; 4665fa9e4066Sahrens vdev_remove_parent(cvd); 4666cb04b873SMark J Musante cvd->vdev_resilvering = B_FALSE; 4667cb04b873SMark J Musante } 4668cb04b873SMark J Musante 4669fa9e4066Sahrens 4670fa9e4066Sahrens /* 4671fa9e4066Sahrens * We don't set tvd until now because the parent we just removed 4672fa9e4066Sahrens * may have been the previous top-level vdev. 4673fa9e4066Sahrens */ 4674fa9e4066Sahrens tvd = cvd->vdev_top; 4675fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd); 4676fa9e4066Sahrens 4677fa9e4066Sahrens /* 467839c23413Seschrock * Reevaluate the parent vdev state. 4679fa9e4066Sahrens */ 46803d7072f8Seschrock vdev_propagate_state(cvd); 4681fa9e4066Sahrens 4682fa9e4066Sahrens /* 4683573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically 4684573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we 4685573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to 4686573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev 4687573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs. 4688fa9e4066Sahrens */ 4689573ca77eSGeorge Wilson if (spa->spa_autoexpand) { 4690573ca77eSGeorge Wilson vdev_reopen(tvd); 4691573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4692573ca77eSGeorge Wilson } 4693fa9e4066Sahrens 4694fa9e4066Sahrens vdev_config_dirty(tvd); 4695fa9e4066Sahrens 4696fa9e4066Sahrens /* 469739c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 469839c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context. 469939c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to 470039c23413Seschrock * prevent vd from being accessed after it's freed. 4701fa9e4066Sahrens */ 47021195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path); 47038ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 4704fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 4705ecc2d604Sbonwick vd->vdev_detached = B_TRUE; 4706ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg); 4707fa9e4066Sahrens 47083d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 47093d7072f8Seschrock 4710cb04b873SMark J Musante /* hang on to the spa before we release the lock */ 4711cb04b873SMark J Musante spa_open_ref(spa, FTAG); 4712cb04b873SMark J Musante 471399653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0); 471499653d4eSeschrock 47154445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL, 47161195e687SMark J Musante "vdev=%s", vdpath); 47171195e687SMark J Musante spa_strfree(vdpath); 47181195e687SMark J Musante 471999653d4eSeschrock /* 472039c23413Seschrock * If this was the removal of the original device in a hot spare vdev, 472139c23413Seschrock * then we want to go through and remove the device from the hot spare 472239c23413Seschrock * list of every other pool. 472399653d4eSeschrock */ 472499653d4eSeschrock if (unspare) { 4725cb04b873SMark J Musante spa_t *altspa = NULL; 4726cb04b873SMark J Musante 472799653d4eSeschrock mutex_enter(&spa_namespace_lock); 4728cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) { 4729cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE || 4730cb04b873SMark J Musante altspa == spa) 473199653d4eSeschrock continue; 4732cb04b873SMark J Musante 4733cb04b873SMark J Musante spa_open_ref(altspa, FTAG); 47349af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock); 4735cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 47369af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock); 4737cb04b873SMark J Musante spa_close(altspa, FTAG); 473899653d4eSeschrock } 473999653d4eSeschrock mutex_exit(&spa_namespace_lock); 4740cb04b873SMark J Musante 4741cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */ 4742cb04b873SMark J Musante spa_vdev_resilver_done(spa); 474399653d4eSeschrock } 474499653d4eSeschrock 4745cb04b873SMark J Musante /* all done with the spa; OK to release */ 4746cb04b873SMark J Musante mutex_enter(&spa_namespace_lock); 4747cb04b873SMark J Musante spa_close(spa, FTAG); 4748cb04b873SMark J Musante mutex_exit(&spa_namespace_lock); 4749cb04b873SMark J Musante 475099653d4eSeschrock return (error); 475199653d4eSeschrock } 475299653d4eSeschrock 47531195e687SMark J Musante /* 47541195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them. 47551195e687SMark J Musante */ 47561195e687SMark J Musante int 47571195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 47581195e687SMark J Musante nvlist_t *props, boolean_t exp) 47591195e687SMark J Musante { 47601195e687SMark J Musante int error = 0; 47611195e687SMark J Musante uint64_t txg, *glist; 47621195e687SMark J Musante spa_t *newspa; 47631195e687SMark J Musante uint_t c, children, lastlog; 47641195e687SMark J Musante nvlist_t **child, *nvl, *tmp; 47651195e687SMark J Musante dmu_tx_t *tx; 47661195e687SMark J Musante char *altroot = NULL; 47671195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */ 47681195e687SMark J Musante boolean_t activate_slog; 47691195e687SMark J Musante 4770f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 47711195e687SMark J Musante 47721195e687SMark J Musante txg = spa_vdev_enter(spa); 47731195e687SMark J Musante 47741195e687SMark J Musante /* clear the log and flush everything up to now */ 47751195e687SMark J Musante activate_slog = spa_passivate_log(spa); 47761195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 47771195e687SMark J Musante error = spa_offline_log(spa); 47781195e687SMark J Musante txg = spa_vdev_config_enter(spa); 47791195e687SMark J Musante 47801195e687SMark J Musante if (activate_slog) 47811195e687SMark J Musante spa_activate_log(spa); 47821195e687SMark J Musante 47831195e687SMark J Musante if (error != 0) 47841195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 47851195e687SMark J Musante 47861195e687SMark J Musante /* check new spa name before going any further */ 47871195e687SMark J Musante if (spa_lookup(newname) != NULL) 47881195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 47891195e687SMark J Musante 47901195e687SMark J Musante /* 47911195e687SMark J Musante * scan through all the children to ensure they're all mirrors 47921195e687SMark J Musante */ 47931195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 47941195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 47951195e687SMark J Musante &children) != 0) 47961195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 47971195e687SMark J Musante 47981195e687SMark J Musante /* first, check to ensure we've got the right child count */ 47991195e687SMark J Musante rvd = spa->spa_root_vdev; 48001195e687SMark J Musante lastlog = 0; 48011195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) { 48021195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c]; 48031195e687SMark J Musante 48041195e687SMark J Musante /* don't count the holes & logs as children */ 48051195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) { 48061195e687SMark J Musante if (lastlog == 0) 48071195e687SMark J Musante lastlog = c; 48081195e687SMark J Musante continue; 48091195e687SMark J Musante } 48101195e687SMark J Musante 48111195e687SMark J Musante lastlog = 0; 48121195e687SMark J Musante } 48131195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 48141195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 48151195e687SMark J Musante 48161195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */ 48171195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 48181195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 48191195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 48201195e687SMark J Musante 48211195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 48221195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 48231195e687SMark J Musante 48241195e687SMark J Musante /* then, loop over each vdev and validate it */ 48251195e687SMark J Musante for (c = 0; c < children; c++) { 48261195e687SMark J Musante uint64_t is_hole = 0; 48271195e687SMark J Musante 48281195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 48291195e687SMark J Musante &is_hole); 48301195e687SMark J Musante 48311195e687SMark J Musante if (is_hole != 0) { 48321195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 48331195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 48341195e687SMark J Musante continue; 48351195e687SMark J Musante } else { 48361195e687SMark J Musante error = EINVAL; 48371195e687SMark J Musante break; 48381195e687SMark J Musante } 48391195e687SMark J Musante } 48401195e687SMark J Musante 48411195e687SMark J Musante /* which disk is going to be split? */ 48421195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 48431195e687SMark J Musante &glist[c]) != 0) { 48441195e687SMark J Musante error = EINVAL; 48451195e687SMark J Musante break; 48461195e687SMark J Musante } 48471195e687SMark J Musante 48481195e687SMark J Musante /* look it up in the spa */ 48491195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 48501195e687SMark J Musante if (vml[c] == NULL) { 48511195e687SMark J Musante error = ENODEV; 48521195e687SMark J Musante break; 48531195e687SMark J Musante } 48541195e687SMark J Musante 48551195e687SMark J Musante /* make sure there's nothing stopping the split */ 48561195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 48571195e687SMark J Musante vml[c]->vdev_islog || 48581195e687SMark J Musante vml[c]->vdev_ishole || 48591195e687SMark J Musante vml[c]->vdev_isspare || 48601195e687SMark J Musante vml[c]->vdev_isl2cache || 48611195e687SMark J Musante !vdev_writeable(vml[c]) || 4862d41c4376SMark J Musante vml[c]->vdev_children != 0 || 48631195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY || 48641195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 48651195e687SMark J Musante error = EINVAL; 48661195e687SMark J Musante break; 48671195e687SMark J Musante } 48681195e687SMark J Musante 48691195e687SMark J Musante if (vdev_dtl_required(vml[c])) { 48701195e687SMark J Musante error = EBUSY; 48711195e687SMark J Musante break; 48721195e687SMark J Musante } 48731195e687SMark J Musante 48741195e687SMark J Musante /* we need certain info from the top level */ 48751195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 48761195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0); 48771195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 48781195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0); 48791195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 48801195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0); 48811195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 48821195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0); 48831195e687SMark J Musante } 48841195e687SMark J Musante 48851195e687SMark J Musante if (error != 0) { 48861195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 48871195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 48881195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error)); 48891195e687SMark J Musante } 48901195e687SMark J Musante 48911195e687SMark J Musante /* stop writers from using the disks */ 48921195e687SMark J Musante for (c = 0; c < children; c++) { 48931195e687SMark J Musante if (vml[c] != NULL) 48941195e687SMark J Musante vml[c]->vdev_offline = B_TRUE; 48951195e687SMark J Musante } 48961195e687SMark J Musante vdev_reopen(spa->spa_root_vdev); 48971195e687SMark J Musante 48981195e687SMark J Musante /* 48991195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This 49001195e687SMark J Musante * will disappear once the config is regenerated. 49011195e687SMark J Musante */ 49021195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 49031195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 49041195e687SMark J Musante glist, children) == 0); 49051195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t)); 49061195e687SMark J Musante 490798295d61SMark J Musante mutex_enter(&spa->spa_props_lock); 49081195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 49091195e687SMark J Musante nvl) == 0); 491098295d61SMark J Musante mutex_exit(&spa->spa_props_lock); 49111195e687SMark J Musante spa->spa_config_splitting = nvl; 49121195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 49131195e687SMark J Musante 49141195e687SMark J Musante /* configure and create the new pool */ 49151195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 49161195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 49171195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 49181195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 49191195e687SMark J Musante spa_version(spa)) == 0); 49201195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 49211195e687SMark J Musante spa->spa_config_txg) == 0); 49221195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 49231195e687SMark J Musante spa_generate_guid(NULL)) == 0); 49241195e687SMark J Musante (void) nvlist_lookup_string(props, 49251195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 49261195e687SMark J Musante 4927d41c4376SMark J Musante /* add the new pool to the namespace */ 49281195e687SMark J Musante newspa = spa_add(newname, config, altroot); 49291195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg; 49301195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR); 49311195e687SMark J Musante 49321195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */ 49331195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 49341195e687SMark J Musante 49351195e687SMark J Musante if (zio_injection_enabled) 49361195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1); 49371195e687SMark J Musante 49381195e687SMark J Musante spa_activate(newspa, spa_mode_global); 49391195e687SMark J Musante spa_async_suspend(newspa); 49401195e687SMark J Musante 49411195e687SMark J Musante /* create the new pool from the disks of the original pool */ 49421195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); 49431195e687SMark J Musante if (error) 49441195e687SMark J Musante goto out; 49451195e687SMark J Musante 49461195e687SMark J Musante /* if that worked, generate a real config for the new pool */ 49471195e687SMark J Musante if (newspa->spa_root_vdev != NULL) { 49481195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 49491195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0); 49501195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 49511195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 49521195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 49531195e687SMark J Musante B_TRUE)); 49541195e687SMark J Musante } 49551195e687SMark J Musante 49561195e687SMark J Musante /* set the props */ 49571195e687SMark J Musante if (props != NULL) { 49581195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE); 49591195e687SMark J Musante error = spa_prop_set(newspa, props); 49601195e687SMark J Musante if (error) 49611195e687SMark J Musante goto out; 49621195e687SMark J Musante } 49631195e687SMark J Musante 49641195e687SMark J Musante /* flush everything */ 49651195e687SMark J Musante txg = spa_vdev_config_enter(newspa); 49661195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev); 49671195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 49681195e687SMark J Musante 49691195e687SMark J Musante if (zio_injection_enabled) 49701195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2); 49711195e687SMark J Musante 49721195e687SMark J Musante spa_async_resume(newspa); 49731195e687SMark J Musante 49741195e687SMark J Musante /* finally, update the original pool's config */ 49751195e687SMark J Musante txg = spa_vdev_config_enter(spa); 49761195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 49771195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT); 49781195e687SMark J Musante if (error != 0) 49791195e687SMark J Musante dmu_tx_abort(tx); 49801195e687SMark J Musante for (c = 0; c < children; c++) { 49811195e687SMark J Musante if (vml[c] != NULL) { 49821195e687SMark J Musante vdev_split(vml[c]); 49831195e687SMark J Musante if (error == 0) 49844445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx, 49854445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path); 49861195e687SMark J Musante vdev_free(vml[c]); 49871195e687SMark J Musante } 49881195e687SMark J Musante } 49891195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev); 49901195e687SMark J Musante spa->spa_config_splitting = NULL; 49911195e687SMark J Musante nvlist_free(nvl); 49921195e687SMark J Musante if (error == 0) 49931195e687SMark J Musante dmu_tx_commit(tx); 49941195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0); 49951195e687SMark J Musante 49961195e687SMark J Musante if (zio_injection_enabled) 49971195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3); 49981195e687SMark J Musante 49991195e687SMark J Musante /* split is complete; log a history record */ 50004445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL, 50014445fffbSMatthew Ahrens "from pool %s", spa_name(spa)); 50021195e687SMark J Musante 50031195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 50041195e687SMark J Musante 50051195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */ 50061195e687SMark J Musante if (exp) 50071195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 50081195e687SMark J Musante B_FALSE, B_FALSE); 50091195e687SMark J Musante 50101195e687SMark J Musante return (error); 50111195e687SMark J Musante 50121195e687SMark J Musante out: 50131195e687SMark J Musante spa_unload(newspa); 50141195e687SMark J Musante spa_deactivate(newspa); 50151195e687SMark J Musante spa_remove(newspa); 50161195e687SMark J Musante 50171195e687SMark J Musante txg = spa_vdev_config_enter(spa); 501898295d61SMark J Musante 501998295d61SMark J Musante /* re-online all offlined disks */ 502098295d61SMark J Musante for (c = 0; c < children; c++) { 502198295d61SMark J Musante if (vml[c] != NULL) 502298295d61SMark J Musante vml[c]->vdev_offline = B_FALSE; 502398295d61SMark J Musante } 502498295d61SMark J Musante vdev_reopen(spa->spa_root_vdev); 502598295d61SMark J Musante 50261195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 50271195e687SMark J Musante spa->spa_config_splitting = NULL; 5028d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error); 50291195e687SMark J Musante 50301195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *)); 50311195e687SMark J Musante return (error); 50321195e687SMark J Musante } 50331195e687SMark J Musante 5034e14bb325SJeff Bonwick static nvlist_t * 5035e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 503699653d4eSeschrock { 5037e14bb325SJeff Bonwick for (int i = 0; i < count; i++) { 5038e14bb325SJeff Bonwick uint64_t guid; 503999653d4eSeschrock 5040e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 5041e14bb325SJeff Bonwick &guid) == 0); 504299653d4eSeschrock 5043e14bb325SJeff Bonwick if (guid == target_guid) 5044e14bb325SJeff Bonwick return (nvpp[i]); 504599653d4eSeschrock } 504699653d4eSeschrock 5047e14bb325SJeff Bonwick return (NULL); 5048fa94a07fSbrendan } 5049fa94a07fSbrendan 5050e14bb325SJeff Bonwick static void 5051e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 5052e14bb325SJeff Bonwick nvlist_t *dev_to_remove) 5053fa94a07fSbrendan { 5054e14bb325SJeff Bonwick nvlist_t **newdev = NULL; 5055fa94a07fSbrendan 5056e14bb325SJeff Bonwick if (count > 1) 5057e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 5058fa94a07fSbrendan 5059e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) { 5060e14bb325SJeff Bonwick if (dev[i] == dev_to_remove) 5061e14bb325SJeff Bonwick continue; 5062e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 5063fa94a07fSbrendan } 5064fa94a07fSbrendan 5065e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 5066e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 5067fa94a07fSbrendan 5068e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++) 5069e14bb325SJeff Bonwick nvlist_free(newdev[i]); 5070fa94a07fSbrendan 5071e14bb325SJeff Bonwick if (count > 1) 5072e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *)); 5073fa94a07fSbrendan } 5074fa94a07fSbrendan 507588ecc943SGeorge Wilson /* 507688ecc943SGeorge Wilson * Evacuate the device. 507788ecc943SGeorge Wilson */ 50783f9d6ad7SLin Ling static int 507988ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 508088ecc943SGeorge Wilson { 508188ecc943SGeorge Wilson uint64_t txg; 50823f9d6ad7SLin Ling int error = 0; 508388ecc943SGeorge Wilson 508488ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 508588ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 5086b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 508788ecc943SGeorge Wilson 508888ecc943SGeorge Wilson /* 508988ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer 509088ecc943SGeorge Wilson * since we need to do I/O but we do keep the 509188ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device 509288ecc943SGeorge Wilson * should no longer have any blocks allocated on it. 509388ecc943SGeorge Wilson */ 509488ecc943SGeorge Wilson if (vd->vdev_islog) { 50953f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 50963f9d6ad7SLin Ling error = spa_offline_log(spa); 5097a1521560SJeff Bonwick } else { 50983f9d6ad7SLin Ling error = ENOTSUP; 509988ecc943SGeorge Wilson } 510088ecc943SGeorge Wilson 5101a1521560SJeff Bonwick if (error) 5102a1521560SJeff Bonwick return (error); 5103a1521560SJeff Bonwick 510488ecc943SGeorge Wilson /* 5105a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata 5106a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync. 510788ecc943SGeorge Wilson */ 5108fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc); 510988ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 511088ecc943SGeorge Wilson vd->vdev_removing = B_TRUE; 511188ecc943SGeorge Wilson vdev_dirty(vd, 0, NULL, txg); 511288ecc943SGeorge Wilson vdev_config_dirty(vd); 511388ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 511488ecc943SGeorge Wilson 511588ecc943SGeorge Wilson return (0); 511688ecc943SGeorge Wilson } 511788ecc943SGeorge Wilson 511888ecc943SGeorge Wilson /* 511988ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace. 512088ecc943SGeorge Wilson */ 51213f9d6ad7SLin Ling static void 5122a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) 512388ecc943SGeorge Wilson { 512488ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev; 512588ecc943SGeorge Wilson uint64_t id = vd->vdev_id; 512688ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 512788ecc943SGeorge Wilson 512888ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 512988ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5130b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 513188ecc943SGeorge Wilson 51323f9d6ad7SLin Ling /* 51333f9d6ad7SLin Ling * Only remove any devices which are empty. 51343f9d6ad7SLin Ling */ 51353f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0) 51363f9d6ad7SLin Ling return; 51373f9d6ad7SLin Ling 513888ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 5139b24ab676SJeff Bonwick 5140b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node)) 5141b24ab676SJeff Bonwick vdev_state_clean(vd); 5142b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node)) 5143b24ab676SJeff Bonwick vdev_config_clean(vd); 5144b24ab676SJeff Bonwick 514588ecc943SGeorge Wilson vdev_free(vd); 514688ecc943SGeorge Wilson 514788ecc943SGeorge Wilson if (last_vdev) { 514888ecc943SGeorge Wilson vdev_compact_children(rvd); 514988ecc943SGeorge Wilson } else { 515088ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 515188ecc943SGeorge Wilson vdev_add_child(rvd, vd); 515288ecc943SGeorge Wilson } 5153fcbfa62bSLin Ling vdev_config_dirty(rvd); 5154fcbfa62bSLin Ling 5155fcbfa62bSLin Ling /* 5156fcbfa62bSLin Ling * Reassess the health of our root vdev. 5157fcbfa62bSLin Ling */ 5158fcbfa62bSLin Ling vdev_reopen(rvd); 515988ecc943SGeorge Wilson } 516088ecc943SGeorge Wilson 51613f9d6ad7SLin Ling /* 51623f9d6ad7SLin Ling * Remove a device from the pool - 51633f9d6ad7SLin Ling * 51643f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps 51653f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use 51663f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to 51673f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace 51683f9d6ad7SLin Ling * lock. During each step the configuration is synced out. 51693f9d6ad7SLin Ling */ 51703f9d6ad7SLin Ling 5171fa94a07fSbrendan /* 5172fa94a07fSbrendan * Remove a device from the pool. Currently, this supports removing only hot 517388ecc943SGeorge Wilson * spares, slogs, and level 2 ARC devices. 5174fa94a07fSbrendan */ 5175fa94a07fSbrendan int 5176fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 5177fa94a07fSbrendan { 5178fa94a07fSbrendan vdev_t *vd; 5179a1521560SJeff Bonwick metaslab_group_t *mg; 5180e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv; 51818ad4d6ddSJeff Bonwick uint64_t txg = 0; 518288ecc943SGeorge Wilson uint_t nspares, nl2cache; 5183fa94a07fSbrendan int error = 0; 51848ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 5185fa94a07fSbrendan 5186f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5187f9af39baSGeorge Wilson 51888ad4d6ddSJeff Bonwick if (!locked) 51898ad4d6ddSJeff Bonwick txg = spa_vdev_enter(spa); 5190fa94a07fSbrendan 5191c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE); 5192fa94a07fSbrendan 5193fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL && 5194fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 5195e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 5196e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 5197e14bb325SJeff Bonwick /* 5198e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use 5199e14bb325SJeff Bonwick * in this pool. 5200e14bb325SJeff Bonwick */ 5201e14bb325SJeff Bonwick if (vd == NULL || unspare) { 5202e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config, 5203e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv); 5204e14bb325SJeff Bonwick spa_load_spares(spa); 5205e14bb325SJeff Bonwick spa->spa_spares.sav_sync = B_TRUE; 5206e14bb325SJeff Bonwick } else { 5207e14bb325SJeff Bonwick error = EBUSY; 5208e14bb325SJeff Bonwick } 5209e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL && 5210fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 5211e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 5212e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 5213e14bb325SJeff Bonwick /* 5214e14bb325SJeff Bonwick * Cache devices can always be removed. 5215e14bb325SJeff Bonwick */ 5216e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 5217e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 5218fa94a07fSbrendan spa_load_l2cache(spa); 5219fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE; 522088ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) { 522188ecc943SGeorge Wilson ASSERT(!locked); 5222b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top); 522388ecc943SGeorge Wilson 522488ecc943SGeorge Wilson /* 522588ecc943SGeorge Wilson * XXX - Once we have bp-rewrite this should 522688ecc943SGeorge Wilson * become the common case. 522788ecc943SGeorge Wilson */ 522888ecc943SGeorge Wilson 5229a1521560SJeff Bonwick mg = vd->vdev_mg; 5230a1521560SJeff Bonwick 523188ecc943SGeorge Wilson /* 5232a1521560SJeff Bonwick * Stop allocating from this vdev. 523388ecc943SGeorge Wilson */ 5234a1521560SJeff Bonwick metaslab_group_passivate(mg); 523588ecc943SGeorge Wilson 5236b24ab676SJeff Bonwick /* 5237b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync, 5238b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish. 5239b24ab676SJeff Bonwick */ 5240b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL, 5241b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 5242b24ab676SJeff Bonwick 5243a1521560SJeff Bonwick /* 5244a1521560SJeff Bonwick * Attempt to evacuate the vdev. 5245a1521560SJeff Bonwick */ 5246a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd); 5247a1521560SJeff Bonwick 524888ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa); 524988ecc943SGeorge Wilson 5250a1521560SJeff Bonwick /* 5251a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind. 5252a1521560SJeff Bonwick */ 5253a1521560SJeff Bonwick if (error) { 5254a1521560SJeff Bonwick metaslab_group_activate(mg); 5255a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 5256a1521560SJeff Bonwick } 5257a1521560SJeff Bonwick 5258a1521560SJeff Bonwick /* 5259a1521560SJeff Bonwick * Clean up the vdev namespace. 5260a1521560SJeff Bonwick */ 5261a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd); 526288ecc943SGeorge Wilson 5263e14bb325SJeff Bonwick } else if (vd != NULL) { 5264e14bb325SJeff Bonwick /* 5265e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet). 5266e14bb325SJeff Bonwick */ 5267e14bb325SJeff Bonwick error = ENOTSUP; 5268e14bb325SJeff Bonwick } else { 5269e14bb325SJeff Bonwick /* 5270e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid. 5271e14bb325SJeff Bonwick */ 5272e14bb325SJeff Bonwick error = ENOENT; 5273fa94a07fSbrendan } 527499653d4eSeschrock 52758ad4d6ddSJeff Bonwick if (!locked) 52768ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error)); 52778ad4d6ddSJeff Bonwick 52788ad4d6ddSJeff Bonwick return (error); 5279fa9e4066Sahrens } 5280fa9e4066Sahrens 5281fa9e4066Sahrens /* 52823d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's 52833d7072f8Seschrock * current spared, so we can detach it. 5284fa9e4066Sahrens */ 5285ea8dc4b6Seschrock static vdev_t * 52863d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd) 5287fa9e4066Sahrens { 5288ea8dc4b6Seschrock vdev_t *newvd, *oldvd; 5289fa9e4066Sahrens 5290573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 52913d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 5292ea8dc4b6Seschrock if (oldvd != NULL) 5293ea8dc4b6Seschrock return (oldvd); 5294ea8dc4b6Seschrock } 5295fa9e4066Sahrens 52963d7072f8Seschrock /* 5297cb04b873SMark J Musante * Check for a completed replacement. We always consider the first 5298cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be 5299cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In 5300cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically 5301cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require 5302cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep. 53033d7072f8Seschrock */ 5304cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) { 5305cb04b873SMark J Musante ASSERT(vd->vdev_children > 1); 5306cb04b873SMark J Musante 5307cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1]; 5308ea8dc4b6Seschrock oldvd = vd->vdev_child[0]; 5309ea8dc4b6Seschrock 53108ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) && 5311e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 53128ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd)) 5313ea8dc4b6Seschrock return (oldvd); 5314fa9e4066Sahrens } 5315ea8dc4b6Seschrock 53163d7072f8Seschrock /* 53173d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set. 53183d7072f8Seschrock */ 5319cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) { 5320cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0]; 5321cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 5322cb04b873SMark J Musante 5323cb04b873SMark J Musante if (last->vdev_unspare) { 5324cb04b873SMark J Musante oldvd = first; 5325cb04b873SMark J Musante newvd = last; 5326cb04b873SMark J Musante } else if (first->vdev_unspare) { 5327cb04b873SMark J Musante oldvd = last; 5328cb04b873SMark J Musante newvd = first; 5329cb04b873SMark J Musante } else { 5330cb04b873SMark J Musante oldvd = NULL; 5331cb04b873SMark J Musante } 53323d7072f8Seschrock 5333cb04b873SMark J Musante if (oldvd != NULL && 53348ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) && 5335e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) && 5336cb04b873SMark J Musante !vdev_dtl_required(oldvd)) 53373d7072f8Seschrock return (oldvd); 5338cb04b873SMark J Musante 5339cb04b873SMark J Musante /* 5340cb04b873SMark J Musante * If there are more than two spares attached to a disk, 5341cb04b873SMark J Musante * and those spares are not required, then we want to 5342cb04b873SMark J Musante * attempt to free them up now so that they can be used 5343cb04b873SMark J Musante * by other pools. Once we're back down to a single 5344cb04b873SMark J Musante * disk+spare, we stop removing them. 5345cb04b873SMark J Musante */ 5346cb04b873SMark J Musante if (vd->vdev_children > 2) { 5347cb04b873SMark J Musante newvd = vd->vdev_child[1]; 5348cb04b873SMark J Musante 5349cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare && 5350cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) && 5351cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) && 5352cb04b873SMark J Musante !vdev_dtl_required(newvd)) 5353cb04b873SMark J Musante return (newvd); 53543d7072f8Seschrock } 53553d7072f8Seschrock } 53563d7072f8Seschrock 5357ea8dc4b6Seschrock return (NULL); 5358fa9e4066Sahrens } 5359fa9e4066Sahrens 5360ea8dc4b6Seschrock static void 53613d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa) 5362fa9e4066Sahrens { 53638ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd; 53648ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid; 5365ea8dc4b6Seschrock 53668ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5367ea8dc4b6Seschrock 53683d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 53698ad4d6ddSJeff Bonwick pvd = vd->vdev_parent; 53708ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent; 5371ea8dc4b6Seschrock guid = vd->vdev_guid; 53728ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid; 53738ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid; 53748ad4d6ddSJeff Bonwick sguid = 0; 537599653d4eSeschrock /* 537699653d4eSeschrock * If we have just finished replacing a hot spared device, then 537799653d4eSeschrock * we need to detach the parent's first child (the original hot 537899653d4eSeschrock * spare) as well. 537999653d4eSeschrock */ 5380cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 5381cb04b873SMark J Musante ppvd->vdev_children == 2) { 538299653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 53838ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid; 538499653d4eSeschrock } 53858ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 53868ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 5387ea8dc4b6Seschrock return; 53888ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 538999653d4eSeschrock return; 53908ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5391fa9e4066Sahrens } 5392fa9e4066Sahrens 53938ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 5394fa9e4066Sahrens } 5395fa9e4066Sahrens 5396c67d9675Seschrock /* 5397b3388e4fSEric Taylor * Update the stored path or FRU for this vdev. 5398c67d9675Seschrock */ 5399c67d9675Seschrock int 54006809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 54016809eb4eSEric Schrock boolean_t ispath) 5402c67d9675Seschrock { 5403c5904d13Seschrock vdev_t *vd; 5404208044b8SGeorge Wilson boolean_t sync = B_FALSE; 5405c67d9675Seschrock 5406f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 5407f9af39baSGeorge Wilson 5408b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL); 5409c67d9675Seschrock 54106809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 5411b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT)); 5412c67d9675Seschrock 54130e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf) 5414b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 54150e34b6a7Sbonwick 54166809eb4eSEric Schrock if (ispath) { 5417208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) { 5418208044b8SGeorge Wilson spa_strfree(vd->vdev_path); 5419208044b8SGeorge Wilson vd->vdev_path = spa_strdup(value); 5420208044b8SGeorge Wilson sync = B_TRUE; 5421208044b8SGeorge Wilson } 54226809eb4eSEric Schrock } else { 5423208044b8SGeorge Wilson if (vd->vdev_fru == NULL) { 5424208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5425208044b8SGeorge Wilson sync = B_TRUE; 5426208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) { 54276809eb4eSEric Schrock spa_strfree(vd->vdev_fru); 5428208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value); 5429208044b8SGeorge Wilson sync = B_TRUE; 5430208044b8SGeorge Wilson } 54316809eb4eSEric Schrock } 5432c67d9675Seschrock 5433208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 5434c67d9675Seschrock } 5435c67d9675Seschrock 54366809eb4eSEric Schrock int 54376809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 54386809eb4eSEric Schrock { 54396809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 54406809eb4eSEric Schrock } 54416809eb4eSEric Schrock 54426809eb4eSEric Schrock int 54436809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 54446809eb4eSEric Schrock { 54456809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 54466809eb4eSEric Schrock } 54476809eb4eSEric Schrock 5448fa9e4066Sahrens /* 5449fa9e4066Sahrens * ========================================================================== 54503f9d6ad7SLin Ling * SPA Scanning 5451fa9e4066Sahrens * ========================================================================== 5452fa9e4066Sahrens */ 5453fa9e4066Sahrens 5454ea8dc4b6Seschrock int 54553f9d6ad7SLin Ling spa_scan_stop(spa_t *spa) 5456fa9e4066Sahrens { 5457e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 54583f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool)) 54593f9d6ad7SLin Ling return (EBUSY); 54603f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool)); 54613f9d6ad7SLin Ling } 5462bb8b5132Sek 54633f9d6ad7SLin Ling int 54643f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func) 54653f9d6ad7SLin Ling { 54663f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 54673f9d6ad7SLin Ling 54683f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 5469fa9e4066Sahrens return (ENOTSUP); 5470fa9e4066Sahrens 5471fa9e4066Sahrens /* 5472088f3894Sahrens * If a resilver was requested, but there is no DTL on a 5473088f3894Sahrens * writeable leaf device, we have nothing to do. 5474fa9e4066Sahrens */ 54753f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER && 5476088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 5477088f3894Sahrens spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 5478ea8dc4b6Seschrock return (0); 5479ea8dc4b6Seschrock } 5480fa9e4066Sahrens 54813f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func)); 5482fa9e4066Sahrens } 5483fa9e4066Sahrens 5484ea8dc4b6Seschrock /* 5485ea8dc4b6Seschrock * ========================================================================== 5486ea8dc4b6Seschrock * SPA async task processing 5487ea8dc4b6Seschrock * ========================================================================== 5488ea8dc4b6Seschrock */ 5489ea8dc4b6Seschrock 5490ea8dc4b6Seschrock static void 54913d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd) 5492fa9e4066Sahrens { 549349cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) { 549498d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE; 549598d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE; 549649cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 54971d713200SEric Schrock 54981d713200SEric Schrock /* 54991d713200SEric Schrock * We want to clear the stats, but we don't want to do a full 55001d713200SEric Schrock * vdev_clear() as that will cause us to throw away 55011d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the 55021d713200SEric Schrock * device, all of which is a waste. 55031d713200SEric Schrock */ 55041d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0; 55051d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0; 55061d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0; 55071d713200SEric Schrock 5508e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 5509ea8dc4b6Seschrock } 551049cf58c0SBrendan Gregg - Sun Microsystems 5511e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 551249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]); 5513ea8dc4b6Seschrock } 5514fa9e4066Sahrens 5515e14bb325SJeff Bonwick static void 5516e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd) 5517e14bb325SJeff Bonwick { 5518e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) { 551998d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE; 5520e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */ 5521e14bb325SJeff Bonwick } 5522e14bb325SJeff Bonwick 5523e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) 5524e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]); 5525e14bb325SJeff Bonwick } 5526e14bb325SJeff Bonwick 5527573ca77eSGeorge Wilson static void 5528573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd) 5529573ca77eSGeorge Wilson { 5530573ca77eSGeorge Wilson sysevent_id_t eid; 5531573ca77eSGeorge Wilson nvlist_t *attr; 5532573ca77eSGeorge Wilson char *physpath; 5533573ca77eSGeorge Wilson 5534573ca77eSGeorge Wilson if (!spa->spa_autoexpand) 5535573ca77eSGeorge Wilson return; 5536573ca77eSGeorge Wilson 5537573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) { 5538573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c]; 5539573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd); 5540573ca77eSGeorge Wilson } 5541573ca77eSGeorge Wilson 5542573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 5543573ca77eSGeorge Wilson return; 5544573ca77eSGeorge Wilson 5545573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 5546573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 5547573ca77eSGeorge Wilson 5548573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5549573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 5550573ca77eSGeorge Wilson 5551573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 5552573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 5553573ca77eSGeorge Wilson 5554573ca77eSGeorge Wilson nvlist_free(attr); 5555573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 5556573ca77eSGeorge Wilson } 5557573ca77eSGeorge Wilson 5558ea8dc4b6Seschrock static void 5559ea8dc4b6Seschrock spa_async_thread(spa_t *spa) 5560ea8dc4b6Seschrock { 5561e14bb325SJeff Bonwick int tasks; 5562ea8dc4b6Seschrock 5563ea8dc4b6Seschrock ASSERT(spa->spa_sync_on); 5564ea8dc4b6Seschrock 5565ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5566ea8dc4b6Seschrock tasks = spa->spa_async_tasks; 5567ea8dc4b6Seschrock spa->spa_async_tasks = 0; 5568ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5569ea8dc4b6Seschrock 55700373e76bSbonwick /* 55710373e76bSbonwick * See if the config needs to be updated. 55720373e76bSbonwick */ 55730373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 5574b24ab676SJeff Bonwick uint64_t old_space, new_space; 5575573ca77eSGeorge Wilson 55760373e76bSbonwick mutex_enter(&spa_namespace_lock); 5577b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa)); 55780373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5579b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa)); 55800373e76bSbonwick mutex_exit(&spa_namespace_lock); 5581573ca77eSGeorge Wilson 5582573ca77eSGeorge Wilson /* 5583573ca77eSGeorge Wilson * If the pool grew as a result of the config update, 5584573ca77eSGeorge Wilson * then log an internal history event. 5585573ca77eSGeorge Wilson */ 5586b24ab676SJeff Bonwick if (new_space != old_space) { 55874445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL, 5588c8e1f6d2SMark J Musante "pool '%s' size: %llu(+%llu)", 5589b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space); 5590573ca77eSGeorge Wilson } 55910373e76bSbonwick } 55920373e76bSbonwick 5593ea8dc4b6Seschrock /* 55943d7072f8Seschrock * See if any devices need to be marked REMOVED. 5595ea8dc4b6Seschrock */ 5596e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) { 55978f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 55983d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev); 5599e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 560049cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 5601e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++) 560249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 5603e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 5604e14bb325SJeff Bonwick } 5605e14bb325SJeff Bonwick 5606573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 5607573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5608573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev); 5609573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG); 5610573ca77eSGeorge Wilson } 5611573ca77eSGeorge Wilson 5612e14bb325SJeff Bonwick /* 5613e14bb325SJeff Bonwick * See if any devices need to be probed. 5614e14bb325SJeff Bonwick */ 5615e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) { 56168f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE); 5617e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev); 5618e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0); 56193d7072f8Seschrock } 5620ea8dc4b6Seschrock 5621ea8dc4b6Seschrock /* 5622ea8dc4b6Seschrock * If any devices are done replacing, detach them. 5623ea8dc4b6Seschrock */ 56243d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE) 56253d7072f8Seschrock spa_vdev_resilver_done(spa); 5626fa9e4066Sahrens 5627ea8dc4b6Seschrock /* 5628ea8dc4b6Seschrock * Kick off a resilver. 5629ea8dc4b6Seschrock */ 5630088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER) 56313f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0); 5632ea8dc4b6Seschrock 5633ea8dc4b6Seschrock /* 5634ea8dc4b6Seschrock * Let the world know that we're done. 5635ea8dc4b6Seschrock */ 5636ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5637ea8dc4b6Seschrock spa->spa_async_thread = NULL; 5638ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv); 5639ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5640ea8dc4b6Seschrock thread_exit(); 5641ea8dc4b6Seschrock } 5642ea8dc4b6Seschrock 5643ea8dc4b6Seschrock void 5644ea8dc4b6Seschrock spa_async_suspend(spa_t *spa) 5645ea8dc4b6Seschrock { 5646ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5647ea8dc4b6Seschrock spa->spa_async_suspended++; 5648ea8dc4b6Seschrock while (spa->spa_async_thread != NULL) 5649ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 5650ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5651ea8dc4b6Seschrock } 5652ea8dc4b6Seschrock 5653ea8dc4b6Seschrock void 5654ea8dc4b6Seschrock spa_async_resume(spa_t *spa) 5655ea8dc4b6Seschrock { 5656ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5657ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0); 5658ea8dc4b6Seschrock spa->spa_async_suspended--; 5659ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5660ea8dc4b6Seschrock } 5661ea8dc4b6Seschrock 5662ea8dc4b6Seschrock static void 5663ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa) 5664ea8dc4b6Seschrock { 5665ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5666ea8dc4b6Seschrock if (spa->spa_async_tasks && !spa->spa_async_suspended && 56670373e76bSbonwick spa->spa_async_thread == NULL && 56680373e76bSbonwick rootdir != NULL && !vn_is_readonly(rootdir)) 5669ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0, 5670ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 5671ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5672ea8dc4b6Seschrock } 5673ea8dc4b6Seschrock 5674ea8dc4b6Seschrock void 5675ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task) 5676ea8dc4b6Seschrock { 56773f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 5678ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock); 5679ea8dc4b6Seschrock spa->spa_async_tasks |= task; 5680ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock); 5681fa9e4066Sahrens } 5682fa9e4066Sahrens 5683fa9e4066Sahrens /* 5684fa9e4066Sahrens * ========================================================================== 5685fa9e4066Sahrens * SPA syncing routines 5686fa9e4066Sahrens * ========================================================================== 5687fa9e4066Sahrens */ 5688fa9e4066Sahrens 5689cde58dbcSMatthew Ahrens static int 5690cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5691cde58dbcSMatthew Ahrens { 5692cde58dbcSMatthew Ahrens bpobj_t *bpo = arg; 5693cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx); 5694cde58dbcSMatthew Ahrens return (0); 5695b24ab676SJeff Bonwick } 5696b24ab676SJeff Bonwick 5697cde58dbcSMatthew Ahrens static int 5698cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 5699b24ab676SJeff Bonwick { 5700b24ab676SJeff Bonwick zio_t *zio = arg; 5701b24ab676SJeff Bonwick 5702b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 5703b24ab676SJeff Bonwick zio->io_flags)); 5704cde58dbcSMatthew Ahrens return (0); 5705fa9e4066Sahrens } 5706fa9e4066Sahrens 5707fa9e4066Sahrens static void 570899653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 5709fa9e4066Sahrens { 5710fa9e4066Sahrens char *packed = NULL; 5711f7991ba4STim Haley size_t bufsize; 5712fa9e4066Sahrens size_t nvsize = 0; 5713fa9e4066Sahrens dmu_buf_t *db; 5714fa9e4066Sahrens 571599653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 5716fa9e4066Sahrens 5717f7991ba4STim Haley /* 5718f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 5719f7991ba4STim Haley * information. This avoids the dbuf_will_dirty() path and 5720f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about. 5721f7991ba4STim Haley */ 5722ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 5723f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP); 5724fa9e4066Sahrens 572599653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 5726ea8dc4b6Seschrock KM_SLEEP) == 0); 5727f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize); 5728fa9e4066Sahrens 5729f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 5730fa9e4066Sahrens 5731f7991ba4STim Haley kmem_free(packed, bufsize); 5732fa9e4066Sahrens 573399653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 5734fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 5735fa9e4066Sahrens *(uint64_t *)db->db_data = nvsize; 5736ea8dc4b6Seschrock dmu_buf_rele(db, FTAG); 5737fa9e4066Sahrens } 5738fa9e4066Sahrens 573999653d4eSeschrock static void 5740fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 5741fa94a07fSbrendan const char *config, const char *entry) 574299653d4eSeschrock { 574399653d4eSeschrock nvlist_t *nvroot; 5744fa94a07fSbrendan nvlist_t **list; 574599653d4eSeschrock int i; 574699653d4eSeschrock 5747fa94a07fSbrendan if (!sav->sav_sync) 574899653d4eSeschrock return; 574999653d4eSeschrock 575099653d4eSeschrock /* 5751fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices. 5752fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is 57533d7072f8Seschrock * valid and the vdevs are labeled appropriately. 575499653d4eSeschrock */ 5755fa94a07fSbrendan if (sav->sav_object == 0) { 5756fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 5757fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 5758fa94a07fSbrendan sizeof (uint64_t), tx); 575999653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset, 5760fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 5761fa94a07fSbrendan &sav->sav_object, tx) == 0); 576299653d4eSeschrock } 576399653d4eSeschrock 576499653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5765fa94a07fSbrendan if (sav->sav_count == 0) { 5766fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 576799653d4eSeschrock } else { 5768fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 5769fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5770fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 57713f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE); 5772fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 5773fa94a07fSbrendan sav->sav_count) == 0); 5774fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 5775fa94a07fSbrendan nvlist_free(list[i]); 5776fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *)); 577799653d4eSeschrock } 577899653d4eSeschrock 5779fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 578006eeb2adSek nvlist_free(nvroot); 578199653d4eSeschrock 5782fa94a07fSbrendan sav->sav_sync = B_FALSE; 578399653d4eSeschrock } 578499653d4eSeschrock 578599653d4eSeschrock static void 578699653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 578799653d4eSeschrock { 578899653d4eSeschrock nvlist_t *config; 578999653d4eSeschrock 5790e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) 579199653d4eSeschrock return; 579299653d4eSeschrock 5793e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5794e14bb325SJeff Bonwick 5795e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev, 5796e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE); 5797e14bb325SJeff Bonwick 579825345e46SGeorge Wilson /* 579925345e46SGeorge Wilson * If we're upgrading the spa version then make sure that 580025345e46SGeorge Wilson * the config object gets updated with the correct version. 580125345e46SGeorge Wilson */ 580225345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 580325345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 580425345e46SGeorge Wilson spa->spa_uberblock.ub_version); 580525345e46SGeorge Wilson 5806e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 580799653d4eSeschrock 580899653d4eSeschrock if (spa->spa_config_syncing) 580999653d4eSeschrock nvlist_free(spa->spa_config_syncing); 581099653d4eSeschrock spa->spa_config_syncing = config; 581199653d4eSeschrock 581299653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 581399653d4eSeschrock } 581499653d4eSeschrock 5815ad135b5dSChristopher Siden static void 5816ad135b5dSChristopher Siden spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx) 5817ad135b5dSChristopher Siden { 5818ad135b5dSChristopher Siden spa_t *spa = arg1; 5819ad135b5dSChristopher Siden uint64_t version = *(uint64_t *)arg2; 5820ad135b5dSChristopher Siden 5821ad135b5dSChristopher Siden /* 5822ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool. 5823ad135b5dSChristopher Siden */ 5824ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL); 5825ad135b5dSChristopher Siden 582662eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 5827ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa)); 5828ad135b5dSChristopher Siden 5829ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version; 5830ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 58314445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version); 5832ad135b5dSChristopher Siden } 5833ad135b5dSChristopher Siden 5834990b4856Slling /* 5835990b4856Slling * Set zpool properties. 5836990b4856Slling */ 5837b1b8ab34Slling static void 58383f9d6ad7SLin Ling spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 5839b1b8ab34Slling { 5840b1b8ab34Slling spa_t *spa = arg1; 5841b1b8ab34Slling objset_t *mos = spa->spa_meta_objset; 5842990b4856Slling nvlist_t *nvp = arg2; 5843ad135b5dSChristopher Siden nvpair_t *elem = NULL; 5844b1b8ab34Slling 5845e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 5846e14bb325SJeff Bonwick 5847990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) { 5848ad135b5dSChristopher Siden uint64_t intval; 5849ad135b5dSChristopher Siden char *strval, *fname; 5850ad135b5dSChristopher Siden zpool_prop_t prop; 5851ad135b5dSChristopher Siden const char *propname; 5852ad135b5dSChristopher Siden zprop_type_t proptype; 5853ad135b5dSChristopher Siden zfeature_info_t *feature; 5854ad135b5dSChristopher Siden 5855990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 5856ad135b5dSChristopher Siden case ZPROP_INVAL: 5857ad135b5dSChristopher Siden /* 5858ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate(). 5859ad135b5dSChristopher Siden */ 5860ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem))); 5861ad135b5dSChristopher Siden 5862ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1; 5863b420f3adSRichard Lowe VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature)); 5864ad135b5dSChristopher Siden 5865ad135b5dSChristopher Siden spa_feature_enable(spa, feature, tx); 58664445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 58674445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem)); 5868ad135b5dSChristopher Siden break; 5869ad135b5dSChristopher Siden 5870990b4856Slling case ZPOOL_PROP_VERSION: 5871ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5872990b4856Slling /* 5873ad135b5dSChristopher Siden * The version is synced seperatly before other 5874ad135b5dSChristopher Siden * properties and should be correct by now. 5875990b4856Slling */ 5876ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval); 5877ecd6cf80Smarks break; 5878990b4856Slling 5879990b4856Slling case ZPOOL_PROP_ALTROOT: 5880990b4856Slling /* 5881990b4856Slling * 'altroot' is a non-persistent property. It should 5882990b4856Slling * have been set temporarily at creation or import time. 5883990b4856Slling */ 5884990b4856Slling ASSERT(spa->spa_root != NULL); 5885b1b8ab34Slling break; 58863d7072f8Seschrock 5887f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 58882f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5889990b4856Slling /* 5890f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent 5891f9af39baSGeorge Wilson * properties. 5892990b4856Slling */ 58933d7072f8Seschrock break; 58948704186eSDan McDonald case ZPOOL_PROP_COMMENT: 58958704186eSDan McDonald VERIFY(nvpair_value_string(elem, &strval) == 0); 58968704186eSDan McDonald if (spa->spa_comment != NULL) 58978704186eSDan McDonald spa_strfree(spa->spa_comment); 58988704186eSDan McDonald spa->spa_comment = spa_strdup(strval); 58998704186eSDan McDonald /* 59008704186eSDan McDonald * We need to dirty the configuration on all the vdevs 59018704186eSDan McDonald * so that their labels get updated. It's unnecessary 59028704186eSDan McDonald * to do this for pool creation since the vdev's 59038704186eSDan McDonald * configuratoin has already been dirtied. 59048704186eSDan McDonald */ 59058704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL) 59068704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev); 59074445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59084445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 59098704186eSDan McDonald break; 5910990b4856Slling default: 5911990b4856Slling /* 5912990b4856Slling * Set pool property values in the poolprops mos object. 5913990b4856Slling */ 5914990b4856Slling if (spa->spa_pool_props_object == 0) { 5915ad135b5dSChristopher Siden spa->spa_pool_props_object = 5916ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS, 5917990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 5918ad135b5dSChristopher Siden tx); 5919990b4856Slling } 5920990b4856Slling 5921990b4856Slling /* normalize the property name */ 5922990b4856Slling propname = zpool_prop_to_name(prop); 5923990b4856Slling proptype = zpool_prop_get_type(prop); 5924990b4856Slling 5925990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) { 5926990b4856Slling ASSERT(proptype == PROP_TYPE_STRING); 5927990b4856Slling VERIFY(nvpair_value_string(elem, &strval) == 0); 5928990b4856Slling VERIFY(zap_update(mos, 5929990b4856Slling spa->spa_pool_props_object, propname, 5930990b4856Slling 1, strlen(strval) + 1, strval, tx) == 0); 59314445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59324445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval); 5933990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 5934990b4856Slling VERIFY(nvpair_value_uint64(elem, &intval) == 0); 5935990b4856Slling 5936990b4856Slling if (proptype == PROP_TYPE_INDEX) { 5937990b4856Slling const char *unused; 5938990b4856Slling VERIFY(zpool_prop_index_to_string( 5939990b4856Slling prop, intval, &unused) == 0); 5940990b4856Slling } 5941990b4856Slling VERIFY(zap_update(mos, 5942990b4856Slling spa->spa_pool_props_object, propname, 5943990b4856Slling 8, 1, &intval, tx) == 0); 59444445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, 59454445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval); 5946990b4856Slling } else { 5947990b4856Slling ASSERT(0); /* not allowed */ 5948990b4856Slling } 5949990b4856Slling 59500a4e9518Sgw switch (prop) { 59510a4e9518Sgw case ZPOOL_PROP_DELEGATION: 5952990b4856Slling spa->spa_delegation = intval; 59530a4e9518Sgw break; 59540a4e9518Sgw case ZPOOL_PROP_BOOTFS: 5955990b4856Slling spa->spa_bootfs = intval; 59560a4e9518Sgw break; 59570a4e9518Sgw case ZPOOL_PROP_FAILUREMODE: 59580a4e9518Sgw spa->spa_failmode = intval; 59590a4e9518Sgw break; 5960573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND: 5961573ca77eSGeorge Wilson spa->spa_autoexpand = intval; 5962b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL) 5963b98131cfSEric Taylor spa_async_request(spa, 5964b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND); 5965573ca77eSGeorge Wilson break; 5966b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO: 5967b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval; 5968b24ab676SJeff Bonwick break; 59690a4e9518Sgw default: 59700a4e9518Sgw break; 59710a4e9518Sgw } 5972990b4856Slling } 5973990b4856Slling 5974b1b8ab34Slling } 5975e14bb325SJeff Bonwick 5976e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 5977b1b8ab34Slling } 5978b1b8ab34Slling 5979cde58dbcSMatthew Ahrens /* 5980cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not 5981cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this 5982cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes. 5983cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync 5984cde58dbcSMatthew Ahrens * tasks. 5985cde58dbcSMatthew Ahrens */ 5986cde58dbcSMatthew Ahrens static void 5987cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 5988cde58dbcSMatthew Ahrens { 5989cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool; 5990cde58dbcSMatthew Ahrens 5991cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1); 5992cde58dbcSMatthew Ahrens 5993cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 5994cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 5995cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx); 5996cde58dbcSMatthew Ahrens 5997cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */ 5998cde58dbcSMatthew Ahrens spa->spa_minref += 3; 5999cde58dbcSMatthew Ahrens } 6000cde58dbcSMatthew Ahrens 6001cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 6002cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 6003cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx); 6004cde58dbcSMatthew Ahrens } 6005cde58dbcSMatthew Ahrens 6006cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 6007cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 6008cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx); 6009cde58dbcSMatthew Ahrens 6010cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */ 6011cde58dbcSMatthew Ahrens spa->spa_minref += 3; 6012cde58dbcSMatthew Ahrens } 6013ad135b5dSChristopher Siden 6014ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 6015ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 6016ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx); 6017ad135b5dSChristopher Siden } 6018cde58dbcSMatthew Ahrens } 6019cde58dbcSMatthew Ahrens 6020fa9e4066Sahrens /* 6021fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as 6022fa9e4066Sahrens * part of the process, so we iterate until it converges. 6023fa9e4066Sahrens */ 6024fa9e4066Sahrens void 6025fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg) 6026fa9e4066Sahrens { 6027fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool; 6028fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset; 6029cde58dbcSMatthew Ahrens bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; 6030b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 60310373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 6032fa9e4066Sahrens vdev_t *vd; 6033fa9e4066Sahrens dmu_tx_t *tx; 6034e14bb325SJeff Bonwick int error; 6035fa9e4066Sahrens 6036f9af39baSGeorge Wilson VERIFY(spa_writeable(spa)); 6037f9af39baSGeorge Wilson 6038fa9e4066Sahrens /* 6039fa9e4066Sahrens * Lock out configuration changes. 6040fa9e4066Sahrens */ 6041e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6042fa9e4066Sahrens 6043fa9e4066Sahrens spa->spa_syncing_txg = txg; 6044fa9e4066Sahrens spa->spa_sync_pass = 0; 6045fa9e4066Sahrens 6046e14bb325SJeff Bonwick /* 6047e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them 6048e14bb325SJeff Bonwick * into config changes that go out with this transaction group. 6049e14bb325SJeff Bonwick */ 6050e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 60518ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) { 60528ad4d6ddSJeff Bonwick /* 60538ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs, 60548ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config. 60558ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we 60568ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs 60578ad4d6ddSJeff Bonwick * into the root vdev tree. 60588ad4d6ddSJeff Bonwick */ 60598ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 60608ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 60618ad4d6ddSJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 60628ad4d6ddSJeff Bonwick vdev_state_clean(vd); 60638ad4d6ddSJeff Bonwick vdev_config_dirty(vd); 60648ad4d6ddSJeff Bonwick } 60658ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 60668ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6067e14bb325SJeff Bonwick } 6068e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6069e14bb325SJeff Bonwick 607099653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg); 607199653d4eSeschrock 6072283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime(); 6073283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 6074283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime)); 6075283b8460SGeorge.Wilson 607699653d4eSeschrock /* 6077e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 607899653d4eSeschrock * set spa_deflate if we have no raid-z vdevs. 607999653d4eSeschrock */ 6080e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 6081e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 608299653d4eSeschrock int i; 608399653d4eSeschrock 608499653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) { 608599653d4eSeschrock vd = rvd->vdev_child[i]; 608699653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 608799653d4eSeschrock break; 608899653d4eSeschrock } 608999653d4eSeschrock if (i == rvd->vdev_children) { 609099653d4eSeschrock spa->spa_deflate = TRUE; 609199653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset, 609299653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 609399653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 609499653d4eSeschrock } 609599653d4eSeschrock } 609699653d4eSeschrock 6097fa9e4066Sahrens /* 60983f9d6ad7SLin Ling * If anything has changed in this txg, or if someone is waiting 60993f9d6ad7SLin Ling * for this txg to sync (eg, spa_vdev_remove()), push the 61003f9d6ad7SLin Ling * deferred frees from the previous txg. If not, leave them 61013f9d6ad7SLin Ling * alone so that we don't generate work on an otherwise idle 61023f9d6ad7SLin Ling * system. 6103fa9e4066Sahrens */ 6104fa9e4066Sahrens if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 61051615a317Sek !txg_list_empty(&dp->dp_dirty_dirs, txg) || 61063f9d6ad7SLin Ling !txg_list_empty(&dp->dp_sync_tasks, txg) || 6107cde58dbcSMatthew Ahrens ((dsl_scan_active(dp->dp_scan) || 6108cde58dbcSMatthew Ahrens txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { 6109cde58dbcSMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0); 6110cde58dbcSMatthew Ahrens VERIFY3U(bpobj_iterate(defer_bpo, 6111cde58dbcSMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0); 6112fb09f5aaSMadhav Suresh VERIFY0(zio_wait(zio)); 6113cde58dbcSMatthew Ahrens } 6114fa9e4066Sahrens 6115fa9e4066Sahrens /* 6116fa9e4066Sahrens * Iterate to convergence. 6117fa9e4066Sahrens */ 6118fa9e4066Sahrens do { 6119b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass; 6120fa9e4066Sahrens 6121fa9e4066Sahrens spa_sync_config_object(spa, tx); 6122fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx, 6123fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 6124fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 6125fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 6126ea8dc4b6Seschrock spa_errlog_sync(spa, txg); 6127fa9e4066Sahrens dsl_pool_sync(dp, txg); 6128fa9e4066Sahrens 612901f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) { 6130b24ab676SJeff Bonwick zio_t *zio = zio_root(spa, NULL, NULL, 0); 6131cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, spa_free_sync_cb, 6132cde58dbcSMatthew Ahrens zio, tx); 6133b24ab676SJeff Bonwick VERIFY(zio_wait(zio) == 0); 6134b24ab676SJeff Bonwick } else { 6135cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb, 6136cde58dbcSMatthew Ahrens defer_bpo, tx); 6137fa9e4066Sahrens } 6138fa9e4066Sahrens 6139b24ab676SJeff Bonwick ddt_sync(spa, txg); 61403f9d6ad7SLin Ling dsl_scan_sync(dp, tx); 6141afee20e4SGeorge Wilson 6142b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 6143b24ab676SJeff Bonwick vdev_sync(vd, txg); 6144b24ab676SJeff Bonwick 6145cde58dbcSMatthew Ahrens if (pass == 1) 6146cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx); 6147fa9e4066Sahrens 6148cde58dbcSMatthew Ahrens } while (dmu_objset_is_dirty(mos, txg)); 6149fa9e4066Sahrens 6150fa9e4066Sahrens /* 6151fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock) 6152fa9e4066Sahrens * to commit the transaction group. 61530373e76bSbonwick * 615417f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few 615517f17c2dSbonwick * random top-level vdevs that are known to be visible in the 6156e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description). 6157e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs. 61580373e76bSbonwick */ 6159e14bb325SJeff Bonwick for (;;) { 6160e14bb325SJeff Bonwick /* 6161e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc. 6162e14bb325SJeff Bonwick * while we're attempting to write the vdev labels. 6163e14bb325SJeff Bonwick */ 6164e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6165e14bb325SJeff Bonwick 6166e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) { 6167e14bb325SJeff Bonwick vdev_t *svd[SPA_DVAS_PER_BP]; 6168e14bb325SJeff Bonwick int svdcount = 0; 6169e14bb325SJeff Bonwick int children = rvd->vdev_children; 6170e14bb325SJeff Bonwick int c0 = spa_get_random(children); 6171e14bb325SJeff Bonwick 6172573ca77eSGeorge Wilson for (int c = 0; c < children; c++) { 6173e14bb325SJeff Bonwick vd = rvd->vdev_child[(c0 + c) % children]; 6174e14bb325SJeff Bonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog) 6175e14bb325SJeff Bonwick continue; 6176e14bb325SJeff Bonwick svd[svdcount++] = vd; 6177e14bb325SJeff Bonwick if (svdcount == SPA_DVAS_PER_BP) 6178e14bb325SJeff Bonwick break; 6179e14bb325SJeff Bonwick } 61808956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 61818956713aSEric Schrock if (error != 0) 61828956713aSEric Schrock error = vdev_config_sync(svd, svdcount, txg, 61838956713aSEric Schrock B_TRUE); 6184e14bb325SJeff Bonwick } else { 6185e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child, 61868956713aSEric Schrock rvd->vdev_children, txg, B_FALSE); 61878956713aSEric Schrock if (error != 0) 61888956713aSEric Schrock error = vdev_config_sync(rvd->vdev_child, 61898956713aSEric Schrock rvd->vdev_children, txg, B_TRUE); 61900373e76bSbonwick } 6191e14bb325SJeff Bonwick 6192dfbb9432SGeorge Wilson if (error == 0) 6193dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid; 6194dfbb9432SGeorge Wilson 6195e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 6196e14bb325SJeff Bonwick 6197e14bb325SJeff Bonwick if (error == 0) 6198e14bb325SJeff Bonwick break; 6199e14bb325SJeff Bonwick zio_suspend(spa, NULL); 6200e14bb325SJeff Bonwick zio_resume_wait(spa); 62010373e76bSbonwick } 620299653d4eSeschrock dmu_tx_commit(tx); 620399653d4eSeschrock 6204283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 6205283b8460SGeorge.Wilson 62060373e76bSbonwick /* 62070373e76bSbonwick * Clear the dirty config list. 6208fa9e4066Sahrens */ 6209e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 62100373e76bSbonwick vdev_config_clean(vd); 62110373e76bSbonwick 62120373e76bSbonwick /* 62130373e76bSbonwick * Now that the new config has synced transactionally, 62140373e76bSbonwick * let it become visible to the config cache. 62150373e76bSbonwick */ 62160373e76bSbonwick if (spa->spa_config_syncing != NULL) { 62170373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing); 62180373e76bSbonwick spa->spa_config_txg = txg; 62190373e76bSbonwick spa->spa_config_syncing = NULL; 62200373e76bSbonwick } 6221fa9e4066Sahrens 6222fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock; 6223fa9e4066Sahrens 6224b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg); 6225fa9e4066Sahrens 6226fa9e4066Sahrens /* 6227fa9e4066Sahrens * Update usable space statistics. 6228fa9e4066Sahrens */ 6229fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 6230fa9e4066Sahrens vdev_sync_done(vd, txg); 6231fa9e4066Sahrens 6232485bbbf5SGeorge Wilson spa_update_dspace(spa); 6233485bbbf5SGeorge Wilson 6234fa9e4066Sahrens /* 6235fa9e4066Sahrens * It had better be the case that we didn't dirty anything 623699653d4eSeschrock * since vdev_config_sync(). 6237fa9e4066Sahrens */ 6238fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 6239fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 6240fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 6241b24ab676SJeff Bonwick 6242b24ab676SJeff Bonwick spa->spa_sync_pass = 0; 6243fa9e4066Sahrens 6244e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG); 6245ea8dc4b6Seschrock 6246468c413aSTim Haley spa_handle_ignored_writes(spa); 6247468c413aSTim Haley 6248ea8dc4b6Seschrock /* 6249ea8dc4b6Seschrock * If any async tasks have been requested, kick them off. 6250ea8dc4b6Seschrock */ 6251ea8dc4b6Seschrock spa_async_dispatch(spa); 6252fa9e4066Sahrens } 6253fa9e4066Sahrens 6254fa9e4066Sahrens /* 6255fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these 6256fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the 6257fa9e4066Sahrens * sync. 6258fa9e4066Sahrens */ 6259fa9e4066Sahrens void 6260fa9e4066Sahrens spa_sync_allpools(void) 6261fa9e4066Sahrens { 6262fa9e4066Sahrens spa_t *spa = NULL; 6263fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6264fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 6265f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE || 6266f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa)) 6267fa9e4066Sahrens continue; 6268fa9e4066Sahrens spa_open_ref(spa, FTAG); 6269fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6270fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0); 6271fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6272fa9e4066Sahrens spa_close(spa, FTAG); 6273fa9e4066Sahrens } 6274fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6275fa9e4066Sahrens } 6276fa9e4066Sahrens 6277fa9e4066Sahrens /* 6278fa9e4066Sahrens * ========================================================================== 6279fa9e4066Sahrens * Miscellaneous routines 6280fa9e4066Sahrens * ========================================================================== 6281fa9e4066Sahrens */ 6282fa9e4066Sahrens 6283fa9e4066Sahrens /* 6284fa9e4066Sahrens * Remove all pools in the system. 6285fa9e4066Sahrens */ 6286fa9e4066Sahrens void 6287fa9e4066Sahrens spa_evict_all(void) 6288fa9e4066Sahrens { 6289fa9e4066Sahrens spa_t *spa; 6290fa9e4066Sahrens 6291fa9e4066Sahrens /* 6292fa9e4066Sahrens * Remove all cached state. All pools should be closed now, 6293fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced. 6294fa9e4066Sahrens */ 6295fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6296fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) { 6297fa9e4066Sahrens /* 6298ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach 6299ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing 6300ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here. 6301fa9e4066Sahrens */ 6302fa9e4066Sahrens spa_open_ref(spa, FTAG); 6303fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6304ea8dc4b6Seschrock spa_async_suspend(spa); 6305fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 6306fa9e4066Sahrens spa_close(spa, FTAG); 6307fa9e4066Sahrens 6308fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6309fa9e4066Sahrens spa_unload(spa); 6310fa9e4066Sahrens spa_deactivate(spa); 6311fa9e4066Sahrens } 6312fa9e4066Sahrens spa_remove(spa); 6313fa9e4066Sahrens } 6314fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 6315fa9e4066Sahrens } 6316ea8dc4b6Seschrock 6317ea8dc4b6Seschrock vdev_t * 63186809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 6319ea8dc4b6Seschrock { 6320c5904d13Seschrock vdev_t *vd; 6321c5904d13Seschrock int i; 6322c5904d13Seschrock 6323c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 6324c5904d13Seschrock return (vd); 6325c5904d13Seschrock 63266809eb4eSEric Schrock if (aux) { 6327c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 6328c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i]; 63296809eb4eSEric Schrock if (vd->vdev_guid == guid) 63306809eb4eSEric Schrock return (vd); 63316809eb4eSEric Schrock } 63326809eb4eSEric Schrock 63336809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) { 63346809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i]; 6335c5904d13Seschrock if (vd->vdev_guid == guid) 6336c5904d13Seschrock return (vd); 6337c5904d13Seschrock } 6338c5904d13Seschrock } 6339c5904d13Seschrock 6340c5904d13Seschrock return (NULL); 6341ea8dc4b6Seschrock } 6342eaca9bbdSeschrock 6343eaca9bbdSeschrock void 6344990b4856Slling spa_upgrade(spa_t *spa, uint64_t version) 6345eaca9bbdSeschrock { 6346f9af39baSGeorge Wilson ASSERT(spa_writeable(spa)); 6347f9af39baSGeorge Wilson 6348e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6349eaca9bbdSeschrock 6350eaca9bbdSeschrock /* 6351eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a 6352eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be 6353eaca9bbdSeschrock * possible. 6354eaca9bbdSeschrock */ 635562eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 6356990b4856Slling ASSERT(version >= spa->spa_uberblock.ub_version); 6357eaca9bbdSeschrock 6358990b4856Slling spa->spa_uberblock.ub_version = version; 6359eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev); 6360eaca9bbdSeschrock 6361e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 636299653d4eSeschrock 636399653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0); 636499653d4eSeschrock } 636599653d4eSeschrock 636699653d4eSeschrock boolean_t 636799653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid) 636899653d4eSeschrock { 636999653d4eSeschrock int i; 637039c23413Seschrock uint64_t spareguid; 6371fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares; 637299653d4eSeschrock 6373fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) 6374fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid) 637599653d4eSeschrock return (B_TRUE); 637699653d4eSeschrock 6377fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) { 6378fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 6379fa94a07fSbrendan &spareguid) == 0 && spareguid == guid) 638039c23413Seschrock return (B_TRUE); 638139c23413Seschrock } 638239c23413Seschrock 638399653d4eSeschrock return (B_FALSE); 6384eaca9bbdSeschrock } 6385b1b8ab34Slling 638689a89ebfSlling /* 638789a89ebfSlling * Check if a pool has an active shared spare device. 638889a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace 638989a89ebfSlling */ 639089a89ebfSlling static boolean_t 639189a89ebfSlling spa_has_active_shared_spare(spa_t *spa) 639289a89ebfSlling { 639389a89ebfSlling int i, refcnt; 639489a89ebfSlling uint64_t pool; 639589a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares; 639689a89ebfSlling 639789a89ebfSlling for (i = 0; i < sav->sav_count; i++) { 639889a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 639989a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 640089a89ebfSlling refcnt > 2) 640189a89ebfSlling return (B_TRUE); 640289a89ebfSlling } 640389a89ebfSlling 640489a89ebfSlling return (B_FALSE); 640589a89ebfSlling } 640689a89ebfSlling 64073d7072f8Seschrock /* 64083d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of 64093d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be 64103d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything 64113d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest 64123d7072f8Seschrock * or zdb as real changes. 64133d7072f8Seschrock */ 64143d7072f8Seschrock void 64153d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 64163d7072f8Seschrock { 64173d7072f8Seschrock #ifdef _KERNEL 64183d7072f8Seschrock sysevent_t *ev; 64193d7072f8Seschrock sysevent_attr_list_t *attr = NULL; 64203d7072f8Seschrock sysevent_value_t value; 64213d7072f8Seschrock sysevent_id_t eid; 64223d7072f8Seschrock 64233d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 64243d7072f8Seschrock SE_SLEEP); 64253d7072f8Seschrock 64263d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 64273d7072f8Seschrock value.value.sv_string = spa_name(spa); 64283d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 64293d7072f8Seschrock goto done; 64303d7072f8Seschrock 64313d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 64323d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa); 64333d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 64343d7072f8Seschrock goto done; 64353d7072f8Seschrock 64363d7072f8Seschrock if (vd) { 64373d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64; 64383d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid; 64393d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 64403d7072f8Seschrock SE_SLEEP) != 0) 64413d7072f8Seschrock goto done; 64423d7072f8Seschrock 64433d7072f8Seschrock if (vd->vdev_path) { 64443d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING; 64453d7072f8Seschrock value.value.sv_string = vd->vdev_path; 64463d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 64473d7072f8Seschrock &value, SE_SLEEP) != 0) 64483d7072f8Seschrock goto done; 64493d7072f8Seschrock } 64503d7072f8Seschrock } 64513d7072f8Seschrock 6452b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0) 6453b01c3b58Seschrock goto done; 6454b01c3b58Seschrock attr = NULL; 6455b01c3b58Seschrock 64563d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid); 64573d7072f8Seschrock 64583d7072f8Seschrock done: 64593d7072f8Seschrock if (attr) 64603d7072f8Seschrock sysevent_free_attr(attr); 64613d7072f8Seschrock sysevent_free(ev); 64623d7072f8Seschrock #endif 64633d7072f8Seschrock } 6464