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.
241b497ab8SAdam H. Leventhal * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
2514372834SHans Rosenfeld * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2745818ee1SMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
28c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
295aeb9474SGarrett D'Amore */
30fa9e4066Sahrens
31fa9e4066Sahrens /*
323e30c24aSWill Andrews * SPA: Storage Pool Allocator
333e30c24aSWill Andrews *
34fa9e4066Sahrens * This file contains all the routines used when modifying on-disk SPA state.
35fa9e4066Sahrens * This includes opening, importing, destroying, exporting a pool, and syncing a
36fa9e4066Sahrens * pool.
37fa9e4066Sahrens */
38fa9e4066Sahrens
39fa9e4066Sahrens #include <sys/zfs_context.h>
40ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
41fa9e4066Sahrens #include <sys/spa_impl.h>
42fa9e4066Sahrens #include <sys/zio.h>
43fa9e4066Sahrens #include <sys/zio_checksum.h>
44fa9e4066Sahrens #include <sys/dmu.h>
45fa9e4066Sahrens #include <sys/dmu_tx.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/zil.h>
48b24ab676SJeff Bonwick #include <sys/ddt.h>
49fa9e4066Sahrens #include <sys/vdev_impl.h>
50fa9e4066Sahrens #include <sys/metaslab.h>
5188ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
52fa9e4066Sahrens #include <sys/uberblock_impl.h>
53fa9e4066Sahrens #include <sys/txg.h>
54fa9e4066Sahrens #include <sys/avl.h>
55fa9e4066Sahrens #include <sys/dmu_traverse.h>
56b1b8ab34Slling #include <sys/dmu_objset.h>
57fa9e4066Sahrens #include <sys/unique.h>
58fa9e4066Sahrens #include <sys/dsl_pool.h>
59b1b8ab34Slling #include <sys/dsl_dataset.h>
60fa9e4066Sahrens #include <sys/dsl_dir.h>
61fa9e4066Sahrens #include <sys/dsl_prop.h>
62b1b8ab34Slling #include <sys/dsl_synctask.h>
63fa9e4066Sahrens #include <sys/fs/zfs.h>
64fa94a07fSbrendan #include <sys/arc.h>
65fa9e4066Sahrens #include <sys/callb.h>
6695173954Sek110237 #include <sys/systeminfo.h>
67e7cbe64fSgw25295 #include <sys/spa_boot.h>
68573ca77eSGeorge Wilson #include <sys/zfs_ioctl.h>
693f9d6ad7SLin Ling #include <sys/dsl_scan.h>
70ad135b5dSChristopher Siden #include <sys/zfeature.h>
713b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
72fa9e4066Sahrens
735679c89fSjv227347 #ifdef _KERNEL
74dedec472SJack Meng #include <sys/bootprops.h>
7535a5a358SJonathan Adams #include <sys/callb.h>
7635a5a358SJonathan Adams #include <sys/cpupart.h>
7735a5a358SJonathan Adams #include <sys/pool.h>
7835a5a358SJonathan Adams #include <sys/sysdc.h>
7935a5a358SJonathan Adams #include <sys/zone.h>
805679c89fSjv227347 #endif /* _KERNEL */
815679c89fSjv227347
82990b4856Slling #include "zfs_prop.h"
83b7b97454Sperrin #include "zfs_comutil.h"
84990b4856Slling
853cb69f73SWill Andrews /*
863cb69f73SWill Andrews * The interval, in seconds, at which failed configuration cache file writes
873cb69f73SWill Andrews * should be retried.
883cb69f73SWill Andrews */
893cb69f73SWill Andrews static int zfs_ccw_retry_interval = 300;
903cb69f73SWill Andrews
9135a5a358SJonathan Adams typedef enum zti_modes {
92ec94d322SAdam Leventhal ZTI_MODE_FIXED, /* value is # of threads (min 1) */
93ec94d322SAdam Leventhal ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
94ec94d322SAdam Leventhal ZTI_MODE_NULL, /* don't create a taskq */
95ec94d322SAdam Leventhal ZTI_NMODES
9635a5a358SJonathan Adams } zti_modes_t;
97416e0cd8Sek110237
98ec94d322SAdam Leventhal #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
99ec94d322SAdam Leventhal #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
100ec94d322SAdam Leventhal #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
1012e0c549eSJonathan Adams
102ec94d322SAdam Leventhal #define ZTI_N(n) ZTI_P(n, 1)
103ec94d322SAdam Leventhal #define ZTI_ONE ZTI_N(1)
1042e0c549eSJonathan Adams
1052e0c549eSJonathan Adams typedef struct zio_taskq_info {
106ec94d322SAdam Leventhal zti_modes_t zti_mode;
1072e0c549eSJonathan Adams uint_t zti_value;
108ec94d322SAdam Leventhal uint_t zti_count;
1092e0c549eSJonathan Adams } zio_taskq_info_t;
1102e0c549eSJonathan Adams
1112e0c549eSJonathan Adams static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
11280eb36f2SGeorge Wilson "issue", "issue_high", "intr", "intr_high"
1132e0c549eSJonathan Adams };
1142e0c549eSJonathan Adams
11580eb36f2SGeorge Wilson /*
116ec94d322SAdam Leventhal * This table defines the taskq settings for each ZFS I/O type. When
117ec94d322SAdam Leventhal * initializing a pool, we use this table to create an appropriately sized
118ec94d322SAdam Leventhal * taskq. Some operations are low volume and therefore have a small, static
119ec94d322SAdam Leventhal * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
120ec94d322SAdam Leventhal * macros. Other operations process a large amount of data; the ZTI_BATCH
121ec94d322SAdam Leventhal * macro causes us to create a taskq oriented for throughput. Some operations
122ec94d322SAdam Leventhal * are so high frequency and short-lived that the taskq itself can become a a
123ec94d322SAdam Leventhal * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
124ec94d322SAdam Leventhal * additional degree of parallelism specified by the number of threads per-
125ec94d322SAdam Leventhal * taskq and the number of taskqs; when dispatching an event in this case, the
126ec94d322SAdam Leventhal * particular taskq is chosen at random.
127ec94d322SAdam Leventhal *
128ec94d322SAdam Leventhal * The different taskq priorities are to handle the different contexts (issue
129ec94d322SAdam Leventhal * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
130ec94d322SAdam Leventhal * need to be handled with minimum delay.
13180eb36f2SGeorge Wilson */
13280eb36f2SGeorge Wilson const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
13380eb36f2SGeorge Wilson /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
134ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
1351b497ab8SAdam H. Leventhal { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
136ec94d322SAdam Leventhal { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
137ec94d322SAdam Leventhal { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
138ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
139ec94d322SAdam Leventhal { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
1402e0c549eSJonathan Adams };
1412e0c549eSJonathan Adams
1423b2aab18SMatthew Ahrens static void spa_sync_version(void *arg, dmu_tx_t *tx);
1433b2aab18SMatthew Ahrens static void spa_sync_props(void *arg, dmu_tx_t *tx);
14489a89ebfSlling static boolean_t spa_has_active_shared_spare(spa_t *spa);
1451195e687SMark J Musante static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
1461195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1471195e687SMark J Musante char **ereport);
148cb04b873SMark J Musante static void spa_vdev_resilver_done(spa_t *spa);
149990b4856Slling
15069962b56SMatthew Ahrens uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
15135a5a358SJonathan Adams id_t zio_taskq_psrset_bind = PS_NONE;
15235a5a358SJonathan Adams boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
15335a5a358SJonathan Adams uint_t zio_taskq_basedc = 80; /* base duty cycle */
15435a5a358SJonathan Adams
15535a5a358SJonathan Adams boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
15601f55e48SGeorge Wilson extern int zfs_sync_pass_deferred_free;
15735a5a358SJonathan Adams
15835a5a358SJonathan Adams /*
15935a5a358SJonathan Adams * This (illegal) pool name is used when temporarily importing a spa_t in order
16035a5a358SJonathan Adams * to get the vdev stats associated with the imported devices.
16135a5a358SJonathan Adams */
16235a5a358SJonathan Adams #define TRYIMPORT_NAME "$import"
16335a5a358SJonathan Adams
164990b4856Slling /*
165990b4856Slling * ==========================================================================
166990b4856Slling * SPA properties routines
167990b4856Slling * ==========================================================================
168990b4856Slling */
169990b4856Slling
170990b4856Slling /*
171990b4856Slling * Add a (source=src, propname=propval) list to an nvlist.
172990b4856Slling */
1739d82f4f6Slling static void
spa_prop_add_list(nvlist_t * nvl,zpool_prop_t prop,char * strval,uint64_t intval,zprop_source_t src)174990b4856Slling spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
175990b4856Slling uint64_t intval, zprop_source_t src)
176990b4856Slling {
177990b4856Slling const char *propname = zpool_prop_to_name(prop);
178990b4856Slling nvlist_t *propval;
179990b4856Slling
1809d82f4f6Slling VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1819d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
182990b4856Slling
1839d82f4f6Slling if (strval != NULL)
1849d82f4f6Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
1859d82f4f6Slling else
1869d82f4f6Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
187990b4856Slling
1889d82f4f6Slling VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
189990b4856Slling nvlist_free(propval);
190990b4856Slling }
191990b4856Slling
192990b4856Slling /*
193990b4856Slling * Get property values from the spa configuration.
194990b4856Slling */
1959d82f4f6Slling static void
spa_prop_get_config(spa_t * spa,nvlist_t ** nvp)196990b4856Slling spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
197990b4856Slling {
1984263d13fSGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
199ad135b5dSChristopher Siden dsl_pool_t *pool = spa->spa_dsl_pool;
2002e4c9986SGeorge Wilson uint64_t size, alloc, cap, version;
201990b4856Slling zprop_source_t src = ZPROP_SRC_NONE;
202c5904d13Seschrock spa_config_dirent_t *dp;
2032e4c9986SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa);
204990b4856Slling
205e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa->spa_props_lock));
206e14bb325SJeff Bonwick
2074263d13fSGeorge Wilson if (rvd != NULL) {
208485bbbf5SGeorge Wilson alloc = metaslab_class_get_alloc(spa_normal_class(spa));
209b24ab676SJeff Bonwick size = metaslab_class_get_space(spa_normal_class(spa));
210e14bb325SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
2119d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
212485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
213485bbbf5SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
214485bbbf5SGeorge Wilson size - alloc, src);
2154263d13fSGeorge Wilson
2162e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
2172e4c9986SGeorge Wilson metaslab_class_fragmentation(mc), src);
2182e4c9986SGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
2192e4c9986SGeorge Wilson metaslab_class_expandable_space(mc), src);
220f9af39baSGeorge Wilson spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
221f9af39baSGeorge Wilson (spa_mode(spa) == FREAD), src);
222990b4856Slling
223485bbbf5SGeorge Wilson cap = (size == 0) ? 0 : (alloc * 100 / size);
2249d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
225990b4856Slling
226b24ab676SJeff Bonwick spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
227b24ab676SJeff Bonwick ddt_get_pool_dedup_ratio(spa), src);
228b24ab676SJeff Bonwick
2299d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
2304263d13fSGeorge Wilson rvd->vdev_state, src);
231990b4856Slling
232990b4856Slling version = spa_version(spa);
233990b4856Slling if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
234990b4856Slling src = ZPROP_SRC_DEFAULT;
235990b4856Slling else
236990b4856Slling src = ZPROP_SRC_LOCAL;
2379d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
238379c004dSEric Schrock }
239379c004dSEric Schrock
240ad135b5dSChristopher Siden if (pool != NULL) {
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 */
2457fd05ac4SMatthew Ahrens if (pool->dp_free_dir != NULL) {
246ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
247c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
248c1379625SJustin T. Gibbs src);
249ad135b5dSChristopher Siden } else {
250ad135b5dSChristopher Siden spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
251ad135b5dSChristopher Siden NULL, 0, src);
252ad135b5dSChristopher Siden }
2537fd05ac4SMatthew Ahrens
2547fd05ac4SMatthew Ahrens if (pool->dp_leak_dir != NULL) {
2557fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
256c1379625SJustin T. Gibbs dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
257c1379625SJustin T. Gibbs src);
2587fd05ac4SMatthew Ahrens } else {
2597fd05ac4SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
2607fd05ac4SMatthew Ahrens NULL, 0, src);
2617fd05ac4SMatthew Ahrens }
262ad135b5dSChristopher Siden }
263ad135b5dSChristopher Siden
264379c004dSEric Schrock spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
265990b4856Slling
2668704186eSDan McDonald if (spa->spa_comment != NULL) {
2678704186eSDan McDonald spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
2688704186eSDan McDonald 0, ZPROP_SRC_LOCAL);
2698704186eSDan McDonald }
2708704186eSDan McDonald
2719d82f4f6Slling if (spa->spa_root != NULL)
2729d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
2739d82f4f6Slling 0, ZPROP_SRC_LOCAL);
274990b4856Slling
275b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
276b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
277b5152584SMatthew Ahrens MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
278b5152584SMatthew Ahrens } else {
279b5152584SMatthew Ahrens spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
280b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
281b5152584SMatthew Ahrens }
282b5152584SMatthew Ahrens
283c5904d13Seschrock if ((dp = list_head(&spa->spa_config_list)) != NULL) {
284c5904d13Seschrock if (dp->scd_path == NULL) {
2859d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
286c5904d13Seschrock "none", 0, ZPROP_SRC_LOCAL);
287c5904d13Seschrock } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
2889d82f4f6Slling spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
289c5904d13Seschrock dp->scd_path, 0, ZPROP_SRC_LOCAL);
2902f8aaab3Seschrock }
2912f8aaab3Seschrock }
292990b4856Slling }
293990b4856Slling
294990b4856Slling /*
295990b4856Slling * Get zpool property values.
296990b4856Slling */
297990b4856Slling int
spa_prop_get(spa_t * spa,nvlist_t ** nvp)298990b4856Slling spa_prop_get(spa_t *spa, nvlist_t **nvp)
299990b4856Slling {
300b24ab676SJeff Bonwick objset_t *mos = spa->spa_meta_objset;
301990b4856Slling zap_cursor_t zc;
302990b4856Slling zap_attribute_t za;
303990b4856Slling int err;
304990b4856Slling
3059d82f4f6Slling VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
306990b4856Slling
307e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock);
308e14bb325SJeff Bonwick
309990b4856Slling /*
310990b4856Slling * Get properties from the spa config.
311990b4856Slling */
3129d82f4f6Slling spa_prop_get_config(spa, nvp);
313990b4856Slling
314990b4856Slling /* If no pool property object, no more prop to get. */
315afee20e4SGeorge Wilson if (mos == NULL || spa->spa_pool_props_object == 0) {
316990b4856Slling mutex_exit(&spa->spa_props_lock);
317990b4856Slling return (0);
318990b4856Slling }
319990b4856Slling
320990b4856Slling /*
321990b4856Slling * Get properties from the MOS pool property object.
322990b4856Slling */
323990b4856Slling for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
324990b4856Slling (err = zap_cursor_retrieve(&zc, &za)) == 0;
325990b4856Slling zap_cursor_advance(&zc)) {
326990b4856Slling uint64_t intval = 0;
327990b4856Slling char *strval = NULL;
328990b4856Slling zprop_source_t src = ZPROP_SRC_DEFAULT;
329990b4856Slling zpool_prop_t prop;
330990b4856Slling
331990b4856Slling if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
332990b4856Slling continue;
333990b4856Slling
334990b4856Slling switch (za.za_integer_length) {
335990b4856Slling case 8:
336990b4856Slling /* integer property */
337990b4856Slling if (za.za_first_integer !=
338990b4856Slling zpool_prop_default_numeric(prop))
339990b4856Slling src = ZPROP_SRC_LOCAL;
340990b4856Slling
341990b4856Slling if (prop == ZPOOL_PROP_BOOTFS) {
342990b4856Slling dsl_pool_t *dp;
343990b4856Slling dsl_dataset_t *ds = NULL;
344990b4856Slling
345990b4856Slling dp = spa_get_dsl(spa);
3463b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
347745cd3c5Smaybee if (err = dsl_dataset_hold_obj(dp,
348745cd3c5Smaybee za.za_first_integer, FTAG, &ds)) {
3493b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
350990b4856Slling break;
351990b4856Slling }
352990b4856Slling
353*40a5c998SMatthew Ahrens strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
354990b4856Slling KM_SLEEP);
355990b4856Slling dsl_dataset_name(ds, strval);
356745cd3c5Smaybee dsl_dataset_rele(ds, FTAG);
3573b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
358990b4856Slling } else {
359990b4856Slling strval = NULL;
360990b4856Slling intval = za.za_first_integer;
361990b4856Slling }
362990b4856Slling
3639d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, intval, src);
364990b4856Slling
365990b4856Slling if (strval != NULL)
366*40a5c998SMatthew Ahrens kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
367990b4856Slling
368990b4856Slling break;
369990b4856Slling
370990b4856Slling case 1:
371990b4856Slling /* string property */
372990b4856Slling strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
373990b4856Slling err = zap_lookup(mos, spa->spa_pool_props_object,
374990b4856Slling za.za_name, 1, za.za_num_integers, strval);
375990b4856Slling if (err) {
376990b4856Slling kmem_free(strval, za.za_num_integers);
377990b4856Slling break;
378990b4856Slling }
3799d82f4f6Slling spa_prop_add_list(*nvp, prop, strval, 0, src);
380990b4856Slling kmem_free(strval, za.za_num_integers);
381990b4856Slling break;
382990b4856Slling
383990b4856Slling default:
384990b4856Slling break;
385990b4856Slling }
386990b4856Slling }
387990b4856Slling zap_cursor_fini(&zc);
388990b4856Slling mutex_exit(&spa->spa_props_lock);
389990b4856Slling out:
390990b4856Slling if (err && err != ENOENT) {
391990b4856Slling nvlist_free(*nvp);
3929d82f4f6Slling *nvp = NULL;
393990b4856Slling return (err);
394990b4856Slling }
395990b4856Slling
396990b4856Slling return (0);
397990b4856Slling }
398990b4856Slling
399990b4856Slling /*
400990b4856Slling * Validate the given pool properties nvlist and modify the list
401990b4856Slling * for the property values to be set.
402990b4856Slling */
403990b4856Slling static int
spa_prop_validate(spa_t * spa,nvlist_t * props)404990b4856Slling spa_prop_validate(spa_t *spa, nvlist_t *props)
405990b4856Slling {
406990b4856Slling nvpair_t *elem;
407990b4856Slling int error = 0, reset_bootfs = 0;
408d5285caeSGeorge Wilson uint64_t objnum = 0;
409ad135b5dSChristopher Siden boolean_t has_feature = B_FALSE;
410990b4856Slling
411990b4856Slling elem = NULL;
412990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
413990b4856Slling uint64_t intval;
414ad135b5dSChristopher Siden char *strval, *slash, *check, *fname;
415ad135b5dSChristopher Siden const char *propname = nvpair_name(elem);
416ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(propname);
417990b4856Slling
418990b4856Slling switch (prop) {
419ad135b5dSChristopher Siden case ZPROP_INVAL:
420ad135b5dSChristopher Siden if (!zpool_prop_feature(propname)) {
421be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
422ad135b5dSChristopher Siden break;
423ad135b5dSChristopher Siden }
424ad135b5dSChristopher Siden
425ad135b5dSChristopher Siden /*
426ad135b5dSChristopher Siden * Sanitize the input.
427ad135b5dSChristopher Siden */
428ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_UINT64) {
429be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
430ad135b5dSChristopher Siden break;
431ad135b5dSChristopher Siden }
432ad135b5dSChristopher Siden
433ad135b5dSChristopher Siden if (nvpair_value_uint64(elem, &intval) != 0) {
434be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
435ad135b5dSChristopher Siden break;
436ad135b5dSChristopher Siden }
437ad135b5dSChristopher Siden
438ad135b5dSChristopher Siden if (intval != 0) {
439be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
440ad135b5dSChristopher Siden break;
441ad135b5dSChristopher Siden }
442ad135b5dSChristopher Siden
443ad135b5dSChristopher Siden fname = strchr(propname, '@') + 1;
444ad135b5dSChristopher Siden if (zfeature_lookup_name(fname, NULL) != 0) {
445be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
446ad135b5dSChristopher Siden break;
447ad135b5dSChristopher Siden }
448ad135b5dSChristopher Siden
449ad135b5dSChristopher Siden has_feature = B_TRUE;
450ad135b5dSChristopher Siden break;
451ad135b5dSChristopher Siden
452990b4856Slling case ZPOOL_PROP_VERSION:
453990b4856Slling error = nvpair_value_uint64(elem, &intval);
454990b4856Slling if (!error &&
455ad135b5dSChristopher Siden (intval < spa_version(spa) ||
456ad135b5dSChristopher Siden intval > SPA_VERSION_BEFORE_FEATURES ||
457ad135b5dSChristopher Siden has_feature))
458be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
459990b4856Slling break;
460990b4856Slling
461990b4856Slling case ZPOOL_PROP_DELEGATION:
462990b4856Slling case ZPOOL_PROP_AUTOREPLACE:
463d5b5bb25SRich Morris case ZPOOL_PROP_LISTSNAPS:
464573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND:
465990b4856Slling error = nvpair_value_uint64(elem, &intval);
466990b4856Slling if (!error && intval > 1)
467be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
468990b4856Slling break;
469990b4856Slling
470990b4856Slling case ZPOOL_PROP_BOOTFS:
47125f89ee2SJeff Bonwick /*
47225f89ee2SJeff Bonwick * If the pool version is less than SPA_VERSION_BOOTFS,
47325f89ee2SJeff Bonwick * or the pool is still being created (version == 0),
47425f89ee2SJeff Bonwick * the bootfs property cannot be set.
47525f89ee2SJeff Bonwick */
476990b4856Slling if (spa_version(spa) < SPA_VERSION_BOOTFS) {
477be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
478990b4856Slling break;
479990b4856Slling }
480990b4856Slling
481990b4856Slling /*
48215e6edf1Sgw25295 * Make sure the vdev config is bootable
483990b4856Slling */
48415e6edf1Sgw25295 if (!vdev_is_bootable(spa->spa_root_vdev)) {
485be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
486990b4856Slling break;
487990b4856Slling }
488990b4856Slling
489990b4856Slling reset_bootfs = 1;
490990b4856Slling
491990b4856Slling error = nvpair_value_string(elem, &strval);
492990b4856Slling
493990b4856Slling if (!error) {
494ad135b5dSChristopher Siden objset_t *os;
495b5152584SMatthew Ahrens uint64_t propval;
49615e6edf1Sgw25295
497990b4856Slling if (strval == NULL || strval[0] == '\0') {
498990b4856Slling objnum = zpool_prop_default_numeric(
499990b4856Slling ZPOOL_PROP_BOOTFS);
500990b4856Slling break;
501990b4856Slling }
502990b4856Slling
503503ad85cSMatthew Ahrens if (error = dmu_objset_hold(strval, FTAG, &os))
504990b4856Slling break;
50515e6edf1Sgw25295
506b5152584SMatthew Ahrens /*
507b5152584SMatthew Ahrens * Must be ZPL, and its property settings
508b5152584SMatthew Ahrens * must be supported by GRUB (compression
509b5152584SMatthew Ahrens * is not gzip, and large blocks are not used).
510b5152584SMatthew Ahrens */
511503ad85cSMatthew Ahrens
512503ad85cSMatthew Ahrens if (dmu_objset_type(os) != DMU_OST_ZFS) {
513be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
5143b2aab18SMatthew Ahrens } else if ((error =
5153b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os),
51615e6edf1Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
517b5152584SMatthew Ahrens &propval)) == 0 &&
518b5152584SMatthew Ahrens !BOOTFS_COMPRESS_VALID(propval)) {
519b5152584SMatthew Ahrens error = SET_ERROR(ENOTSUP);
520b5152584SMatthew Ahrens } else if ((error =
521b5152584SMatthew Ahrens dsl_prop_get_int_ds(dmu_objset_ds(os),
522b5152584SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
523b5152584SMatthew Ahrens &propval)) == 0 &&
524b5152584SMatthew Ahrens propval > SPA_OLD_MAXBLOCKSIZE) {
525be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
52615e6edf1Sgw25295 } else {
527990b4856Slling objnum = dmu_objset_id(os);
52815e6edf1Sgw25295 }
529503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG);
530990b4856Slling }
531990b4856Slling break;
532e14bb325SJeff Bonwick
5330a4e9518Sgw25295 case ZPOOL_PROP_FAILUREMODE:
5340a4e9518Sgw25295 error = nvpair_value_uint64(elem, &intval);
5350a4e9518Sgw25295 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
5360a4e9518Sgw25295 intval > ZIO_FAILURE_MODE_PANIC))
537be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
5380a4e9518Sgw25295
5390a4e9518Sgw25295 /*
5400a4e9518Sgw25295 * This is a special case which only occurs when
5410a4e9518Sgw25295 * the pool has completely failed. This allows
5420a4e9518Sgw25295 * the user to change the in-core failmode property
5430a4e9518Sgw25295 * without syncing it out to disk (I/Os might
5440a4e9518Sgw25295 * currently be blocked). We do this by returning
5450a4e9518Sgw25295 * EIO to the caller (spa_prop_set) to trick it
5460a4e9518Sgw25295 * into thinking we encountered a property validation
5470a4e9518Sgw25295 * error.
5480a4e9518Sgw25295 */
549e14bb325SJeff Bonwick if (!error && spa_suspended(spa)) {
5500a4e9518Sgw25295 spa->spa_failmode = intval;
551be6fd75aSMatthew Ahrens error = SET_ERROR(EIO);
5520a4e9518Sgw25295 }
5530a4e9518Sgw25295 break;
5542f8aaab3Seschrock
5552f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE:
5562f8aaab3Seschrock if ((error = nvpair_value_string(elem, &strval)) != 0)
5572f8aaab3Seschrock break;
5582f8aaab3Seschrock
5592f8aaab3Seschrock if (strval[0] == '\0')
5602f8aaab3Seschrock break;
5612f8aaab3Seschrock
5622f8aaab3Seschrock if (strcmp(strval, "none") == 0)
5632f8aaab3Seschrock break;
5642f8aaab3Seschrock
5652f8aaab3Seschrock if (strval[0] != '/') {
566be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
5672f8aaab3Seschrock break;
5682f8aaab3Seschrock }
5692f8aaab3Seschrock
5702f8aaab3Seschrock slash = strrchr(strval, '/');
5712f8aaab3Seschrock ASSERT(slash != NULL);
5722f8aaab3Seschrock
5732f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
5742f8aaab3Seschrock strcmp(slash, "/..") == 0)
575be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
5762f8aaab3Seschrock break;
577b24ab676SJeff Bonwick
5788704186eSDan McDonald case ZPOOL_PROP_COMMENT:
5798704186eSDan McDonald if ((error = nvpair_value_string(elem, &strval)) != 0)
5808704186eSDan McDonald break;
5818704186eSDan McDonald for (check = strval; *check != '\0'; check++) {
5828704186eSDan McDonald /*
5838704186eSDan McDonald * The kernel doesn't have an easy isprint()
5848704186eSDan McDonald * check. For this kernel check, we merely
5858704186eSDan McDonald * check ASCII apart from DEL. Fix this if
5868704186eSDan McDonald * there is an easy-to-use kernel isprint().
5878704186eSDan McDonald */
5888704186eSDan McDonald if (*check >= 0x7f) {
589be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
5908704186eSDan McDonald break;
5918704186eSDan McDonald }
5928704186eSDan McDonald }
5938704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT)
5948704186eSDan McDonald error = E2BIG;
5958704186eSDan McDonald break;
5968704186eSDan McDonald
597b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO:
598b24ab676SJeff Bonwick if (spa_version(spa) < SPA_VERSION_DEDUP)
599be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
600b24ab676SJeff Bonwick else
601b24ab676SJeff Bonwick error = nvpair_value_uint64(elem, &intval);
602b24ab676SJeff Bonwick if (error == 0 &&
603b24ab676SJeff Bonwick intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
604be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
605b24ab676SJeff Bonwick break;
606990b4856Slling }
607990b4856Slling
608990b4856Slling if (error)
609990b4856Slling break;
610990b4856Slling }
611990b4856Slling
612990b4856Slling if (!error && reset_bootfs) {
613990b4856Slling error = nvlist_remove(props,
614990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
615990b4856Slling
616990b4856Slling if (!error) {
617990b4856Slling error = nvlist_add_uint64(props,
618990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
619990b4856Slling }
620990b4856Slling }
621990b4856Slling
622990b4856Slling return (error);
623990b4856Slling }
624990b4856Slling
625379c004dSEric Schrock void
spa_configfile_set(spa_t * spa,nvlist_t * nvp,boolean_t need_sync)626379c004dSEric Schrock spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
627379c004dSEric Schrock {
628379c004dSEric Schrock char *cachefile;
629379c004dSEric Schrock spa_config_dirent_t *dp;
630379c004dSEric Schrock
631379c004dSEric Schrock if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
632379c004dSEric Schrock &cachefile) != 0)
633379c004dSEric Schrock return;
634379c004dSEric Schrock
635379c004dSEric Schrock dp = kmem_alloc(sizeof (spa_config_dirent_t),
636379c004dSEric Schrock KM_SLEEP);
637379c004dSEric Schrock
638379c004dSEric Schrock if (cachefile[0] == '\0')
639379c004dSEric Schrock dp->scd_path = spa_strdup(spa_config_path);
640379c004dSEric Schrock else if (strcmp(cachefile, "none") == 0)
641379c004dSEric Schrock dp->scd_path = NULL;
642379c004dSEric Schrock else
643379c004dSEric Schrock dp->scd_path = spa_strdup(cachefile);
644379c004dSEric Schrock
645379c004dSEric Schrock list_insert_head(&spa->spa_config_list, dp);
646379c004dSEric Schrock if (need_sync)
647379c004dSEric Schrock spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
648379c004dSEric Schrock }
649379c004dSEric Schrock
650990b4856Slling int
spa_prop_set(spa_t * spa,nvlist_t * nvp)651990b4856Slling spa_prop_set(spa_t *spa, nvlist_t *nvp)
652990b4856Slling {
653990b4856Slling int error;
654ad135b5dSChristopher Siden nvpair_t *elem = NULL;
655379c004dSEric Schrock boolean_t need_sync = B_FALSE;
656990b4856Slling
657990b4856Slling if ((error = spa_prop_validate(spa, nvp)) != 0)
658990b4856Slling return (error);
659990b4856Slling
660379c004dSEric Schrock while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
661ad135b5dSChristopher Siden zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
662379c004dSEric Schrock
663f9af39baSGeorge Wilson if (prop == ZPOOL_PROP_CACHEFILE ||
664f9af39baSGeorge Wilson prop == ZPOOL_PROP_ALTROOT ||
665f9af39baSGeorge Wilson prop == ZPOOL_PROP_READONLY)
666379c004dSEric Schrock continue;
667379c004dSEric Schrock
668ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
669ad135b5dSChristopher Siden uint64_t ver;
670ad135b5dSChristopher Siden
671ad135b5dSChristopher Siden if (prop == ZPOOL_PROP_VERSION) {
672ad135b5dSChristopher Siden VERIFY(nvpair_value_uint64(elem, &ver) == 0);
673ad135b5dSChristopher Siden } else {
674ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem)));
675ad135b5dSChristopher Siden ver = SPA_VERSION_FEATURES;
676ad135b5dSChristopher Siden need_sync = B_TRUE;
677ad135b5dSChristopher Siden }
678ad135b5dSChristopher Siden
679ad135b5dSChristopher Siden /* Save time if the version is already set. */
680ad135b5dSChristopher Siden if (ver == spa_version(spa))
681ad135b5dSChristopher Siden continue;
682ad135b5dSChristopher Siden
683ad135b5dSChristopher Siden /*
684ad135b5dSChristopher Siden * In addition to the pool directory object, we might
685ad135b5dSChristopher Siden * create the pool properties object, the features for
686ad135b5dSChristopher Siden * read object, the features for write object, or the
687ad135b5dSChristopher Siden * feature descriptions object.
688ad135b5dSChristopher Siden */
6893b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, NULL,
6907d46dc6cSMatthew Ahrens spa_sync_version, &ver,
6917d46dc6cSMatthew Ahrens 6, ZFS_SPACE_CHECK_RESERVED);
692ad135b5dSChristopher Siden if (error)
693ad135b5dSChristopher Siden return (error);
694ad135b5dSChristopher Siden continue;
695ad135b5dSChristopher Siden }
696ad135b5dSChristopher Siden
697379c004dSEric Schrock need_sync = B_TRUE;
698379c004dSEric Schrock break;
699379c004dSEric Schrock }
700379c004dSEric Schrock
701ad135b5dSChristopher Siden if (need_sync) {
7023b2aab18SMatthew Ahrens return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
7037d46dc6cSMatthew Ahrens nvp, 6, ZFS_SPACE_CHECK_RESERVED));
704ad135b5dSChristopher Siden }
705ad135b5dSChristopher Siden
706379c004dSEric Schrock return (0);
707990b4856Slling }
708990b4856Slling
709990b4856Slling /*
710990b4856Slling * If the bootfs property value is dsobj, clear it.
711990b4856Slling */
712990b4856Slling void
spa_prop_clear_bootfs(spa_t * spa,uint64_t dsobj,dmu_tx_t * tx)713990b4856Slling spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
714990b4856Slling {
715990b4856Slling if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
716990b4856Slling VERIFY(zap_remove(spa->spa_meta_objset,
717990b4856Slling spa->spa_pool_props_object,
718990b4856Slling zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
719990b4856Slling spa->spa_bootfs = 0;
720990b4856Slling }
721990b4856Slling }
722990b4856Slling
723dfbb9432SGeorge Wilson /*ARGSUSED*/
724dfbb9432SGeorge Wilson static int
spa_change_guid_check(void * arg,dmu_tx_t * tx)7253b2aab18SMatthew Ahrens spa_change_guid_check(void *arg, dmu_tx_t *tx)
726dfbb9432SGeorge Wilson {
7273b2aab18SMatthew Ahrens uint64_t *newguid = arg;
7283b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
729dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
730dfbb9432SGeorge Wilson uint64_t vdev_state;
731dfbb9432SGeorge Wilson
732dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
733dfbb9432SGeorge Wilson vdev_state = rvd->vdev_state;
734dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG);
735dfbb9432SGeorge Wilson
736dfbb9432SGeorge Wilson if (vdev_state != VDEV_STATE_HEALTHY)
737be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO));
738dfbb9432SGeorge Wilson
739dfbb9432SGeorge Wilson ASSERT3U(spa_guid(spa), !=, *newguid);
740dfbb9432SGeorge Wilson
741dfbb9432SGeorge Wilson return (0);
742dfbb9432SGeorge Wilson }
743dfbb9432SGeorge Wilson
744dfbb9432SGeorge Wilson static void
spa_change_guid_sync(void * arg,dmu_tx_t * tx)7453b2aab18SMatthew Ahrens spa_change_guid_sync(void *arg, dmu_tx_t *tx)
746dfbb9432SGeorge Wilson {
7473b2aab18SMatthew Ahrens uint64_t *newguid = arg;
7483b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
749dfbb9432SGeorge Wilson uint64_t oldguid;
750dfbb9432SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
751dfbb9432SGeorge Wilson
752dfbb9432SGeorge Wilson oldguid = spa_guid(spa);
753dfbb9432SGeorge Wilson
754dfbb9432SGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
755dfbb9432SGeorge Wilson rvd->vdev_guid = *newguid;
756dfbb9432SGeorge Wilson rvd->vdev_guid_sum += (*newguid - oldguid);
757dfbb9432SGeorge Wilson vdev_config_dirty(rvd);
758dfbb9432SGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG);
759dfbb9432SGeorge Wilson
76020128a08SGeorge Wilson spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
761dfbb9432SGeorge Wilson oldguid, *newguid);
762dfbb9432SGeorge Wilson }
763dfbb9432SGeorge Wilson
764fa9e4066Sahrens /*
765e9103aaeSGarrett D'Amore * Change the GUID for the pool. This is done so that we can later
766e9103aaeSGarrett D'Amore * re-import a pool built from a clone of our own vdevs. We will modify
767e9103aaeSGarrett D'Amore * the root vdev's guid, our own pool guid, and then mark all of our
768e9103aaeSGarrett D'Amore * vdevs dirty. Note that we must make sure that all our vdevs are
769e9103aaeSGarrett D'Amore * online when we do this, or else any vdevs that weren't present
770e9103aaeSGarrett D'Amore * would be orphaned from our pool. We are also going to issue a
771e9103aaeSGarrett D'Amore * sysevent to update any watchers.
772e9103aaeSGarrett D'Amore */
773e9103aaeSGarrett D'Amore int
spa_change_guid(spa_t * spa)774e9103aaeSGarrett D'Amore spa_change_guid(spa_t *spa)
775e9103aaeSGarrett D'Amore {
776dfbb9432SGeorge Wilson int error;
777dfbb9432SGeorge Wilson uint64_t guid;
778e9103aaeSGarrett D'Amore
7792c1e2b44SGeorge Wilson mutex_enter(&spa->spa_vdev_top_lock);
780dfbb9432SGeorge Wilson mutex_enter(&spa_namespace_lock);
781dfbb9432SGeorge Wilson guid = spa_generate_guid(NULL);
782e9103aaeSGarrett D'Amore
7833b2aab18SMatthew Ahrens error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
7847d46dc6cSMatthew Ahrens spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
785e9103aaeSGarrett D'Amore
786dfbb9432SGeorge Wilson if (error == 0) {
787dfbb9432SGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE);
788e9103aaeSGarrett D'Amore spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
789dfbb9432SGeorge Wilson }
790e9103aaeSGarrett D'Amore
791dfbb9432SGeorge Wilson mutex_exit(&spa_namespace_lock);
7922c1e2b44SGeorge Wilson mutex_exit(&spa->spa_vdev_top_lock);
793dfbb9432SGeorge Wilson
794dfbb9432SGeorge Wilson return (error);
795e9103aaeSGarrett D'Amore }
796e9103aaeSGarrett D'Amore
797e9103aaeSGarrett D'Amore /*
798fa9e4066Sahrens * ==========================================================================
799fa9e4066Sahrens * SPA state manipulation (open/create/destroy/import/export)
800fa9e4066Sahrens * ==========================================================================
801fa9e4066Sahrens */
802fa9e4066Sahrens
803ea8dc4b6Seschrock static int
spa_error_entry_compare(const void * a,const void * b)804ea8dc4b6Seschrock spa_error_entry_compare(const void *a, const void *b)
805ea8dc4b6Seschrock {
806ea8dc4b6Seschrock spa_error_entry_t *sa = (spa_error_entry_t *)a;
807ea8dc4b6Seschrock spa_error_entry_t *sb = (spa_error_entry_t *)b;
808ea8dc4b6Seschrock int ret;
809ea8dc4b6Seschrock
810ea8dc4b6Seschrock ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
8117802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t));
812ea8dc4b6Seschrock
813ea8dc4b6Seschrock if (ret < 0)
814ea8dc4b6Seschrock return (-1);
815ea8dc4b6Seschrock else if (ret > 0)
816ea8dc4b6Seschrock return (1);
817ea8dc4b6Seschrock else
818ea8dc4b6Seschrock return (0);
819ea8dc4b6Seschrock }
820ea8dc4b6Seschrock
821ea8dc4b6Seschrock /*
822ea8dc4b6Seschrock * Utility function which retrieves copies of the current logs and
823ea8dc4b6Seschrock * re-initializes them in the process.
824ea8dc4b6Seschrock */
825ea8dc4b6Seschrock void
spa_get_errlists(spa_t * spa,avl_tree_t * last,avl_tree_t * scrub)826ea8dc4b6Seschrock spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
827ea8dc4b6Seschrock {
828ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
829ea8dc4b6Seschrock
830ea8dc4b6Seschrock bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
831ea8dc4b6Seschrock bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
832ea8dc4b6Seschrock
833ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub,
834ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t),
835ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl));
836ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last,
837ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t),
838ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl));
839ea8dc4b6Seschrock }
840ea8dc4b6Seschrock
841ec94d322SAdam Leventhal static void
spa_taskqs_init(spa_t * spa,zio_type_t t,zio_taskq_type_t q)842ec94d322SAdam Leventhal spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
84335a5a358SJonathan Adams {
844ec94d322SAdam Leventhal const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
845ec94d322SAdam Leventhal enum zti_modes mode = ztip->zti_mode;
846ec94d322SAdam Leventhal uint_t value = ztip->zti_value;
847ec94d322SAdam Leventhal uint_t count = ztip->zti_count;
848ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
849ec94d322SAdam Leventhal char name[32];
8505aeb9474SGarrett D'Amore uint_t flags = 0;
85135a5a358SJonathan Adams boolean_t batch = B_FALSE;
85235a5a358SJonathan Adams
853ec94d322SAdam Leventhal if (mode == ZTI_MODE_NULL) {
854ec94d322SAdam Leventhal tqs->stqs_count = 0;
855ec94d322SAdam Leventhal tqs->stqs_taskq = NULL;
856ec94d322SAdam Leventhal return;
857ec94d322SAdam Leventhal }
85835a5a358SJonathan Adams
859ec94d322SAdam Leventhal ASSERT3U(count, >, 0);
860ec94d322SAdam Leventhal
861ec94d322SAdam Leventhal tqs->stqs_count = count;
862ec94d322SAdam Leventhal tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
863ec94d322SAdam Leventhal
864ec94d322SAdam Leventhal switch (mode) {
865ec94d322SAdam Leventhal case ZTI_MODE_FIXED:
86635a5a358SJonathan Adams ASSERT3U(value, >=, 1);
86735a5a358SJonathan Adams value = MAX(value, 1);
86835a5a358SJonathan Adams break;
86935a5a358SJonathan Adams
870ec94d322SAdam Leventhal case ZTI_MODE_BATCH:
87135a5a358SJonathan Adams batch = B_TRUE;
87235a5a358SJonathan Adams flags |= TASKQ_THREADS_CPU_PCT;
87335a5a358SJonathan Adams value = zio_taskq_batch_pct;
87435a5a358SJonathan Adams break;
87535a5a358SJonathan Adams
87635a5a358SJonathan Adams default:
877ec94d322SAdam Leventhal panic("unrecognized mode for %s_%s taskq (%u:%u) in "
87835a5a358SJonathan Adams "spa_activate()",
879ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], mode, value);
88035a5a358SJonathan Adams break;
88135a5a358SJonathan Adams }
88235a5a358SJonathan Adams
88369962b56SMatthew Ahrens for (uint_t i = 0; i < count; i++) {
88469962b56SMatthew Ahrens taskq_t *tq;
88569962b56SMatthew Ahrens
886ec94d322SAdam Leventhal if (count > 1) {
887ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s_%u",
888ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q], i);
889ec94d322SAdam Leventhal } else {
890ec94d322SAdam Leventhal (void) snprintf(name, sizeof (name), "%s_%s",
891ec94d322SAdam Leventhal zio_type_name[t], zio_taskq_types[q]);
892ec94d322SAdam Leventhal }
893ec94d322SAdam Leventhal
89435a5a358SJonathan Adams if (zio_taskq_sysdc && spa->spa_proc != &p0) {
89535a5a358SJonathan Adams if (batch)
89635a5a358SJonathan Adams flags |= TASKQ_DC_BATCH;
89735a5a358SJonathan Adams
898ec94d322SAdam Leventhal tq = taskq_create_sysdc(name, value, 50, INT_MAX,
899ec94d322SAdam Leventhal spa->spa_proc, zio_taskq_basedc, flags);
900ec94d322SAdam Leventhal } else {
90169962b56SMatthew Ahrens pri_t pri = maxclsyspri;
90269962b56SMatthew Ahrens /*
90369962b56SMatthew Ahrens * The write issue taskq can be extremely CPU
90469962b56SMatthew Ahrens * intensive. Run it at slightly lower priority
90569962b56SMatthew Ahrens * than the other taskqs.
90669962b56SMatthew Ahrens */
90769962b56SMatthew Ahrens if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
90869962b56SMatthew Ahrens pri--;
90969962b56SMatthew Ahrens
91069962b56SMatthew Ahrens tq = taskq_create_proc(name, value, pri, 50,
911ec94d322SAdam Leventhal INT_MAX, spa->spa_proc, flags);
91235a5a358SJonathan Adams }
913ec94d322SAdam Leventhal
914ec94d322SAdam Leventhal tqs->stqs_taskq[i] = tq;
915ec94d322SAdam Leventhal }
916ec94d322SAdam Leventhal }
917ec94d322SAdam Leventhal
918ec94d322SAdam Leventhal static void
spa_taskqs_fini(spa_t * spa,zio_type_t t,zio_taskq_type_t q)919ec94d322SAdam Leventhal spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
920ec94d322SAdam Leventhal {
921ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
922ec94d322SAdam Leventhal
923ec94d322SAdam Leventhal if (tqs->stqs_taskq == NULL) {
924ec94d322SAdam Leventhal ASSERT0(tqs->stqs_count);
925ec94d322SAdam Leventhal return;
926ec94d322SAdam Leventhal }
927ec94d322SAdam Leventhal
928ec94d322SAdam Leventhal for (uint_t i = 0; i < tqs->stqs_count; i++) {
929ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
930ec94d322SAdam Leventhal taskq_destroy(tqs->stqs_taskq[i]);
931ec94d322SAdam Leventhal }
932ec94d322SAdam Leventhal
933ec94d322SAdam Leventhal kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
934ec94d322SAdam Leventhal tqs->stqs_taskq = NULL;
935ec94d322SAdam Leventhal }
936ec94d322SAdam Leventhal
937ec94d322SAdam Leventhal /*
938ec94d322SAdam Leventhal * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
939ec94d322SAdam Leventhal * Note that a type may have multiple discrete taskqs to avoid lock contention
940ec94d322SAdam Leventhal * on the taskq itself. In that case we choose which taskq at random by using
941ec94d322SAdam Leventhal * the low bits of gethrtime().
942ec94d322SAdam Leventhal */
943ec94d322SAdam Leventhal void
spa_taskq_dispatch_ent(spa_t * spa,zio_type_t t,zio_taskq_type_t q,task_func_t * func,void * arg,uint_t flags,taskq_ent_t * ent)944ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
945ec94d322SAdam Leventhal task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
946ec94d322SAdam Leventhal {
947ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
948ec94d322SAdam Leventhal taskq_t *tq;
949ec94d322SAdam Leventhal
950ec94d322SAdam Leventhal ASSERT3P(tqs->stqs_taskq, !=, NULL);
951ec94d322SAdam Leventhal ASSERT3U(tqs->stqs_count, !=, 0);
952ec94d322SAdam Leventhal
953ec94d322SAdam Leventhal if (tqs->stqs_count == 1) {
954ec94d322SAdam Leventhal tq = tqs->stqs_taskq[0];
955ec94d322SAdam Leventhal } else {
956ec94d322SAdam Leventhal tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
957ec94d322SAdam Leventhal }
958ec94d322SAdam Leventhal
959ec94d322SAdam Leventhal taskq_dispatch_ent(tq, func, arg, flags, ent);
96035a5a358SJonathan Adams }
96135a5a358SJonathan Adams
96235a5a358SJonathan Adams static void
spa_create_zio_taskqs(spa_t * spa)96335a5a358SJonathan Adams spa_create_zio_taskqs(spa_t *spa)
96435a5a358SJonathan Adams {
96535a5a358SJonathan Adams for (int t = 0; t < ZIO_TYPES; t++) {
96635a5a358SJonathan Adams for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
967ec94d322SAdam Leventhal spa_taskqs_init(spa, t, q);
96835a5a358SJonathan Adams }
96935a5a358SJonathan Adams }
97035a5a358SJonathan Adams }
97135a5a358SJonathan Adams
97235a5a358SJonathan Adams #ifdef _KERNEL
97335a5a358SJonathan Adams static void
spa_thread(void * arg)97435a5a358SJonathan Adams spa_thread(void *arg)
97535a5a358SJonathan Adams {
97635a5a358SJonathan Adams callb_cpr_t cprinfo;
97735a5a358SJonathan Adams
97835a5a358SJonathan Adams spa_t *spa = arg;
97935a5a358SJonathan Adams user_t *pu = PTOU(curproc);
98035a5a358SJonathan Adams
98135a5a358SJonathan Adams CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
98235a5a358SJonathan Adams spa->spa_name);
98335a5a358SJonathan Adams
98435a5a358SJonathan Adams ASSERT(curproc != &p0);
98535a5a358SJonathan Adams (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
98635a5a358SJonathan Adams "zpool-%s", spa->spa_name);
98735a5a358SJonathan Adams (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
98835a5a358SJonathan Adams
98935a5a358SJonathan Adams /* bind this thread to the requested psrset */
99035a5a358SJonathan Adams if (zio_taskq_psrset_bind != PS_NONE) {
99135a5a358SJonathan Adams pool_lock();
99235a5a358SJonathan Adams mutex_enter(&cpu_lock);
99335a5a358SJonathan Adams mutex_enter(&pidlock);
99435a5a358SJonathan Adams mutex_enter(&curproc->p_lock);
99535a5a358SJonathan Adams
99635a5a358SJonathan Adams if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
99735a5a358SJonathan Adams 0, NULL, NULL) == 0) {
99835a5a358SJonathan Adams curthread->t_bind_pset = zio_taskq_psrset_bind;
99935a5a358SJonathan Adams } else {
100035a5a358SJonathan Adams cmn_err(CE_WARN,
100135a5a358SJonathan Adams "Couldn't bind process for zfs pool \"%s\" to "
100235a5a358SJonathan Adams "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
100335a5a358SJonathan Adams }
100435a5a358SJonathan Adams
100535a5a358SJonathan Adams mutex_exit(&curproc->p_lock);
100635a5a358SJonathan Adams mutex_exit(&pidlock);
100735a5a358SJonathan Adams mutex_exit(&cpu_lock);
100835a5a358SJonathan Adams pool_unlock();
100935a5a358SJonathan Adams }
101035a5a358SJonathan Adams
101135a5a358SJonathan Adams if (zio_taskq_sysdc) {
101235a5a358SJonathan Adams sysdc_thread_enter(curthread, 100, 0);
101335a5a358SJonathan Adams }
101435a5a358SJonathan Adams
101535a5a358SJonathan Adams spa->spa_proc = curproc;
101635a5a358SJonathan Adams spa->spa_did = curthread->t_did;
101735a5a358SJonathan Adams
101835a5a358SJonathan Adams spa_create_zio_taskqs(spa);
101935a5a358SJonathan Adams
102035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock);
102135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
102235a5a358SJonathan Adams
102335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_ACTIVE;
102435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv);
102535a5a358SJonathan Adams
102635a5a358SJonathan Adams CALLB_CPR_SAFE_BEGIN(&cprinfo);
102735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_ACTIVE)
102835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
102935a5a358SJonathan Adams CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
103035a5a358SJonathan Adams
103135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
103235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_GONE;
103335a5a358SJonathan Adams spa->spa_proc = &p0;
103435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv);
103535a5a358SJonathan Adams CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
103635a5a358SJonathan Adams
103735a5a358SJonathan Adams mutex_enter(&curproc->p_lock);
103835a5a358SJonathan Adams lwp_exit();
103935a5a358SJonathan Adams }
104035a5a358SJonathan Adams #endif
104135a5a358SJonathan Adams
1042fa9e4066Sahrens /*
1043fa9e4066Sahrens * Activate an uninitialized pool.
1044fa9e4066Sahrens */
1045fa9e4066Sahrens static void
spa_activate(spa_t * spa,int mode)10468ad4d6ddSJeff Bonwick spa_activate(spa_t *spa, int mode)
1047fa9e4066Sahrens {
1048fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1049fa9e4066Sahrens
1050fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE;
10518ad4d6ddSJeff Bonwick spa->spa_mode = mode;
1052fa9e4066Sahrens
105388ecc943SGeorge Wilson spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
105488ecc943SGeorge Wilson spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1055fa9e4066Sahrens
105635a5a358SJonathan Adams /* Try to create a covering process */
105735a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock);
105835a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
105935a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0);
106035a5a358SJonathan Adams spa->spa_did = 0;
10612e0c549eSJonathan Adams
106235a5a358SJonathan Adams /* Only create a process if we're going to be around a while. */
106335a5a358SJonathan Adams if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
106435a5a358SJonathan Adams if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
106535a5a358SJonathan Adams NULL, 0) == 0) {
106635a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_CREATED;
106735a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_CREATED) {
106835a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv,
106935a5a358SJonathan Adams &spa->spa_proc_lock);
10702e0c549eSJonathan Adams }
107135a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
107235a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0);
107335a5a358SJonathan Adams ASSERT(spa->spa_did != 0);
107435a5a358SJonathan Adams } else {
107535a5a358SJonathan Adams #ifdef _KERNEL
107635a5a358SJonathan Adams cmn_err(CE_WARN,
107735a5a358SJonathan Adams "Couldn't create process for zfs pool \"%s\"\n",
107835a5a358SJonathan Adams spa->spa_name);
107935a5a358SJonathan Adams #endif
10802e0c549eSJonathan Adams }
1081e14bb325SJeff Bonwick }
108235a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock);
108335a5a358SJonathan Adams
108435a5a358SJonathan Adams /* If we didn't create a process, we need to create our taskqs. */
108535a5a358SJonathan Adams if (spa->spa_proc == &p0) {
108635a5a358SJonathan Adams spa_create_zio_taskqs(spa);
1087fa9e4066Sahrens }
1088fa9e4066Sahrens
1089e14bb325SJeff Bonwick list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1090e14bb325SJeff Bonwick offsetof(vdev_t, vdev_config_dirty_node));
1091bc9014e6SJustin Gibbs list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1092bc9014e6SJustin Gibbs offsetof(objset_t, os_evicting_node));
1093e14bb325SJeff Bonwick list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1094e14bb325SJeff Bonwick offsetof(vdev_t, vdev_state_dirty_node));
1095fa9e4066Sahrens
1096fa9e4066Sahrens txg_list_create(&spa->spa_vdev_txg_list,
1097fa9e4066Sahrens offsetof(struct vdev, vdev_txg_node));
1098ea8dc4b6Seschrock
1099ea8dc4b6Seschrock avl_create(&spa->spa_errlist_scrub,
1100ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t),
1101ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl));
1102ea8dc4b6Seschrock avl_create(&spa->spa_errlist_last,
1103ea8dc4b6Seschrock spa_error_entry_compare, sizeof (spa_error_entry_t),
1104ea8dc4b6Seschrock offsetof(spa_error_entry_t, se_avl));
1105fa9e4066Sahrens }
1106fa9e4066Sahrens
1107fa9e4066Sahrens /*
1108fa9e4066Sahrens * Opposite of spa_activate().
1109fa9e4066Sahrens */
1110fa9e4066Sahrens static void
spa_deactivate(spa_t * spa)1111fa9e4066Sahrens spa_deactivate(spa_t *spa)
1112fa9e4066Sahrens {
1113fa9e4066Sahrens ASSERT(spa->spa_sync_on == B_FALSE);
1114fa9e4066Sahrens ASSERT(spa->spa_dsl_pool == NULL);
1115fa9e4066Sahrens ASSERT(spa->spa_root_vdev == NULL);
111625f89ee2SJeff Bonwick ASSERT(spa->spa_async_zio_root == NULL);
1117fa9e4066Sahrens ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1118fa9e4066Sahrens
1119bc9014e6SJustin Gibbs spa_evicting_os_wait(spa);
1120bc9014e6SJustin Gibbs
1121fa9e4066Sahrens txg_list_destroy(&spa->spa_vdev_txg_list);
1122fa9e4066Sahrens
1123e14bb325SJeff Bonwick list_destroy(&spa->spa_config_dirty_list);
1124bc9014e6SJustin Gibbs list_destroy(&spa->spa_evicting_os_list);
1125e14bb325SJeff Bonwick list_destroy(&spa->spa_state_dirty_list);
1126fa9e4066Sahrens
1127e14bb325SJeff Bonwick for (int t = 0; t < ZIO_TYPES; t++) {
1128e14bb325SJeff Bonwick for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1129ec94d322SAdam Leventhal spa_taskqs_fini(spa, t, q);
1130e14bb325SJeff Bonwick }
1131fa9e4066Sahrens }
1132fa9e4066Sahrens
1133fa9e4066Sahrens metaslab_class_destroy(spa->spa_normal_class);
1134fa9e4066Sahrens spa->spa_normal_class = NULL;
1135fa9e4066Sahrens
11368654d025Sperrin metaslab_class_destroy(spa->spa_log_class);
11378654d025Sperrin spa->spa_log_class = NULL;
11388654d025Sperrin
1139ea8dc4b6Seschrock /*
1140ea8dc4b6Seschrock * If this was part of an import or the open otherwise failed, we may
1141ea8dc4b6Seschrock * still have errors left in the queues. Empty them just in case.
1142ea8dc4b6Seschrock */
1143ea8dc4b6Seschrock spa_errlog_drain(spa);
1144ea8dc4b6Seschrock
1145ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_scrub);
1146ea8dc4b6Seschrock avl_destroy(&spa->spa_errlist_last);
1147ea8dc4b6Seschrock
1148fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED;
114935a5a358SJonathan Adams
115035a5a358SJonathan Adams mutex_enter(&spa->spa_proc_lock);
115135a5a358SJonathan Adams if (spa->spa_proc_state != SPA_PROC_NONE) {
115235a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
115335a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_DEACTIVATE;
115435a5a358SJonathan Adams cv_broadcast(&spa->spa_proc_cv);
115535a5a358SJonathan Adams while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
115635a5a358SJonathan Adams ASSERT(spa->spa_proc != &p0);
115735a5a358SJonathan Adams cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
115835a5a358SJonathan Adams }
115935a5a358SJonathan Adams ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
116035a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE;
116135a5a358SJonathan Adams }
116235a5a358SJonathan Adams ASSERT(spa->spa_proc == &p0);
116335a5a358SJonathan Adams mutex_exit(&spa->spa_proc_lock);
116435a5a358SJonathan Adams
116535a5a358SJonathan Adams /*
116635a5a358SJonathan Adams * We want to make sure spa_thread() has actually exited the ZFS
116735a5a358SJonathan Adams * module, so that the module can't be unloaded out from underneath
116835a5a358SJonathan Adams * it.
116935a5a358SJonathan Adams */
117035a5a358SJonathan Adams if (spa->spa_did != 0) {
117135a5a358SJonathan Adams thread_join(spa->spa_did);
117235a5a358SJonathan Adams spa->spa_did = 0;
117335a5a358SJonathan Adams }
1174fa9e4066Sahrens }
1175fa9e4066Sahrens
1176fa9e4066Sahrens /*
1177fa9e4066Sahrens * Verify a pool configuration, and construct the vdev tree appropriately. This
1178fa9e4066Sahrens * will create all the necessary vdevs in the appropriate layout, with each vdev
1179fa9e4066Sahrens * in the CLOSED state. This will prep the pool before open/creation/import.
1180fa9e4066Sahrens * All vdev validation is done by the vdev_alloc() routine.
1181fa9e4066Sahrens */
118299653d4eSeschrock static int
spa_config_parse(spa_t * spa,vdev_t ** vdp,nvlist_t * nv,vdev_t * parent,uint_t id,int atype)118399653d4eSeschrock spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
118499653d4eSeschrock uint_t id, int atype)
1185fa9e4066Sahrens {
1186fa9e4066Sahrens nvlist_t **child;
1187573ca77eSGeorge Wilson uint_t children;
118899653d4eSeschrock int error;
1189fa9e4066Sahrens
119099653d4eSeschrock if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
119199653d4eSeschrock return (error);
1192fa9e4066Sahrens
119399653d4eSeschrock if ((*vdp)->vdev_ops->vdev_op_leaf)
119499653d4eSeschrock return (0);
1195fa9e4066Sahrens
1196e14bb325SJeff Bonwick error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1197e14bb325SJeff Bonwick &child, &children);
1198e14bb325SJeff Bonwick
1199e14bb325SJeff Bonwick if (error == ENOENT)
1200e14bb325SJeff Bonwick return (0);
1201e14bb325SJeff Bonwick
1202e14bb325SJeff Bonwick if (error) {
120399653d4eSeschrock vdev_free(*vdp);
120499653d4eSeschrock *vdp = NULL;
1205be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1206fa9e4066Sahrens }
1207fa9e4066Sahrens
1208573ca77eSGeorge Wilson for (int c = 0; c < children; c++) {
120999653d4eSeschrock vdev_t *vd;
121099653d4eSeschrock if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
121199653d4eSeschrock atype)) != 0) {
121299653d4eSeschrock vdev_free(*vdp);
121399653d4eSeschrock *vdp = NULL;
121499653d4eSeschrock return (error);
1215fa9e4066Sahrens }
1216fa9e4066Sahrens }
1217fa9e4066Sahrens
121899653d4eSeschrock ASSERT(*vdp != NULL);
121999653d4eSeschrock
122099653d4eSeschrock return (0);
1221fa9e4066Sahrens }
1222fa9e4066Sahrens
1223fa9e4066Sahrens /*
1224fa9e4066Sahrens * Opposite of spa_load().
1225fa9e4066Sahrens */
1226fa9e4066Sahrens static void
spa_unload(spa_t * spa)1227fa9e4066Sahrens spa_unload(spa_t *spa)
1228fa9e4066Sahrens {
122999653d4eSeschrock int i;
123099653d4eSeschrock
1231e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock));
1232e14bb325SJeff Bonwick
1233fa9e4066Sahrens /*
1234ea8dc4b6Seschrock * Stop async tasks.
1235ea8dc4b6Seschrock */
1236ea8dc4b6Seschrock spa_async_suspend(spa);
1237ea8dc4b6Seschrock
1238ea8dc4b6Seschrock /*
1239fa9e4066Sahrens * Stop syncing.
1240fa9e4066Sahrens */
1241fa9e4066Sahrens if (spa->spa_sync_on) {
1242fa9e4066Sahrens txg_sync_stop(spa->spa_dsl_pool);
1243fa9e4066Sahrens spa->spa_sync_on = B_FALSE;
1244fa9e4066Sahrens }
1245fa9e4066Sahrens
1246fa9e4066Sahrens /*
1247e14bb325SJeff Bonwick * Wait for any outstanding async I/O to complete.
1248fa9e4066Sahrens */
124954d692b7SGeorge Wilson if (spa->spa_async_zio_root != NULL) {
12506f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++)
12516f834bc1SMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]);
12526f834bc1SMatthew Ahrens kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
125354d692b7SGeorge Wilson spa->spa_async_zio_root = NULL;
125454d692b7SGeorge Wilson }
1255fa9e4066Sahrens
1256cde58dbcSMatthew Ahrens bpobj_close(&spa->spa_deferred_bpobj);
1257cde58dbcSMatthew Ahrens
12580713e232SGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
12590713e232SGeorge Wilson
12600713e232SGeorge Wilson /*
12610713e232SGeorge Wilson * Close all vdevs.
12620713e232SGeorge Wilson */
12630713e232SGeorge Wilson if (spa->spa_root_vdev)
12640713e232SGeorge Wilson vdev_free(spa->spa_root_vdev);
12650713e232SGeorge Wilson ASSERT(spa->spa_root_vdev == NULL);
12660713e232SGeorge Wilson
1267fa9e4066Sahrens /*
1268fa9e4066Sahrens * Close the dsl pool.
1269fa9e4066Sahrens */
1270fa9e4066Sahrens if (spa->spa_dsl_pool) {
1271fa9e4066Sahrens dsl_pool_close(spa->spa_dsl_pool);
1272fa9e4066Sahrens spa->spa_dsl_pool = NULL;
1273afee20e4SGeorge Wilson spa->spa_meta_objset = NULL;
1274fa9e4066Sahrens }
1275fa9e4066Sahrens
1276b24ab676SJeff Bonwick ddt_unload(spa);
1277b24ab676SJeff Bonwick
12788ad4d6ddSJeff Bonwick
12798ad4d6ddSJeff Bonwick /*
12808ad4d6ddSJeff Bonwick * Drop and purge level 2 cache
12818ad4d6ddSJeff Bonwick */
12828ad4d6ddSJeff Bonwick spa_l2cache_drop(spa);
12838ad4d6ddSJeff Bonwick
1284fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++)
1285fa94a07fSbrendan vdev_free(spa->spa_spares.sav_vdevs[i]);
1286fa94a07fSbrendan if (spa->spa_spares.sav_vdevs) {
1287fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs,
1288fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *));
1289fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL;
129099653d4eSeschrock }
1291fa94a07fSbrendan if (spa->spa_spares.sav_config) {
1292fa94a07fSbrendan nvlist_free(spa->spa_spares.sav_config);
1293fa94a07fSbrendan spa->spa_spares.sav_config = NULL;
1294fa94a07fSbrendan }
12952ce8af81SEric Schrock spa->spa_spares.sav_count = 0;
1296fa94a07fSbrendan
1297cd0837ccSGeorge Wilson for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1298cd0837ccSGeorge Wilson vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1299fa94a07fSbrendan vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1300cd0837ccSGeorge Wilson }
1301fa94a07fSbrendan if (spa->spa_l2cache.sav_vdevs) {
1302fa94a07fSbrendan kmem_free(spa->spa_l2cache.sav_vdevs,
1303fa94a07fSbrendan spa->spa_l2cache.sav_count * sizeof (void *));
1304fa94a07fSbrendan spa->spa_l2cache.sav_vdevs = NULL;
1305fa94a07fSbrendan }
1306fa94a07fSbrendan if (spa->spa_l2cache.sav_config) {
1307fa94a07fSbrendan nvlist_free(spa->spa_l2cache.sav_config);
1308fa94a07fSbrendan spa->spa_l2cache.sav_config = NULL;
130999653d4eSeschrock }
13102ce8af81SEric Schrock spa->spa_l2cache.sav_count = 0;
131199653d4eSeschrock
1312ea8dc4b6Seschrock spa->spa_async_suspended = 0;
13138ad4d6ddSJeff Bonwick
13148704186eSDan McDonald if (spa->spa_comment != NULL) {
13158704186eSDan McDonald spa_strfree(spa->spa_comment);
13168704186eSDan McDonald spa->spa_comment = NULL;
13178704186eSDan McDonald }
13188704186eSDan McDonald
13198ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
1320fa9e4066Sahrens }
1321fa9e4066Sahrens
1322fa9e4066Sahrens /*
132399653d4eSeschrock * Load (or re-load) the current list of vdevs describing the active spares for
132499653d4eSeschrock * this pool. When this is called, we have some form of basic information in
1325fa94a07fSbrendan * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1326fa94a07fSbrendan * then re-generate a more complete list including status information.
132799653d4eSeschrock */
132899653d4eSeschrock static void
spa_load_spares(spa_t * spa)132999653d4eSeschrock spa_load_spares(spa_t *spa)
133099653d4eSeschrock {
133199653d4eSeschrock nvlist_t **spares;
133299653d4eSeschrock uint_t nspares;
133399653d4eSeschrock int i;
133439c23413Seschrock vdev_t *vd, *tvd;
133599653d4eSeschrock
1336e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1337e14bb325SJeff Bonwick
133899653d4eSeschrock /*
133999653d4eSeschrock * First, close and free any existing spare vdevs.
134099653d4eSeschrock */
1341fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) {
1342fa94a07fSbrendan vd = spa->spa_spares.sav_vdevs[i];
134339c23413Seschrock
134439c23413Seschrock /* Undo the call to spa_activate() below */
1345c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1346c5904d13Seschrock B_FALSE)) != NULL && tvd->vdev_isspare)
134739c23413Seschrock spa_spare_remove(tvd);
134839c23413Seschrock vdev_close(vd);
134939c23413Seschrock vdev_free(vd);
135099653d4eSeschrock }
135139c23413Seschrock
1352fa94a07fSbrendan if (spa->spa_spares.sav_vdevs)
1353fa94a07fSbrendan kmem_free(spa->spa_spares.sav_vdevs,
1354fa94a07fSbrendan spa->spa_spares.sav_count * sizeof (void *));
135599653d4eSeschrock
1356fa94a07fSbrendan if (spa->spa_spares.sav_config == NULL)
135799653d4eSeschrock nspares = 0;
135899653d4eSeschrock else
1359fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
136099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
136199653d4eSeschrock
1362fa94a07fSbrendan spa->spa_spares.sav_count = (int)nspares;
1363fa94a07fSbrendan spa->spa_spares.sav_vdevs = NULL;
136499653d4eSeschrock
136599653d4eSeschrock if (nspares == 0)
136699653d4eSeschrock return;
136799653d4eSeschrock
136899653d4eSeschrock /*
136999653d4eSeschrock * Construct the array of vdevs, opening them to get status in the
137039c23413Seschrock * process. For each spare, there is potentially two different vdev_t
137139c23413Seschrock * structures associated with it: one in the list of spares (used only
137239c23413Seschrock * for basic validation purposes) and one in the active vdev
137339c23413Seschrock * configuration (if it's spared in). During this phase we open and
137439c23413Seschrock * validate each vdev on the spare list. If the vdev also exists in the
137539c23413Seschrock * active configuration, then we also mark this vdev as an active spare.
137699653d4eSeschrock */
1377fa94a07fSbrendan spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1378fa94a07fSbrendan KM_SLEEP);
1379fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++) {
138099653d4eSeschrock VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
138199653d4eSeschrock VDEV_ALLOC_SPARE) == 0);
138299653d4eSeschrock ASSERT(vd != NULL);
138399653d4eSeschrock
1384fa94a07fSbrendan spa->spa_spares.sav_vdevs[i] = vd;
138599653d4eSeschrock
1386c5904d13Seschrock if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1387c5904d13Seschrock B_FALSE)) != NULL) {
138839c23413Seschrock if (!tvd->vdev_isspare)
138939c23413Seschrock spa_spare_add(tvd);
139039c23413Seschrock
139139c23413Seschrock /*
139239c23413Seschrock * We only mark the spare active if we were successfully
139339c23413Seschrock * able to load the vdev. Otherwise, importing a pool
139439c23413Seschrock * with a bad active spare would result in strange
139539c23413Seschrock * behavior, because multiple pool would think the spare
139639c23413Seschrock * is actively in use.
139739c23413Seschrock *
139839c23413Seschrock * There is a vulnerability here to an equally bizarre
139939c23413Seschrock * circumstance, where a dead active spare is later
140039c23413Seschrock * brought back to life (onlined or otherwise). Given
140139c23413Seschrock * the rarity of this scenario, and the extra complexity
140239c23413Seschrock * it adds, we ignore the possibility.
140339c23413Seschrock */
140439c23413Seschrock if (!vdev_is_dead(tvd))
140539c23413Seschrock spa_spare_activate(tvd);
140639c23413Seschrock }
140739c23413Seschrock
1408e14bb325SJeff Bonwick vd->vdev_top = vd;
14096809eb4eSEric Schrock vd->vdev_aux = &spa->spa_spares;
1410e14bb325SJeff Bonwick
141199653d4eSeschrock if (vdev_open(vd) != 0)
141299653d4eSeschrock continue;
141399653d4eSeschrock
1414fa94a07fSbrendan if (vdev_validate_aux(vd) == 0)
1415fa94a07fSbrendan spa_spare_add(vd);
141699653d4eSeschrock }
141799653d4eSeschrock
141899653d4eSeschrock /*
141999653d4eSeschrock * Recompute the stashed list of spares, with status information
142099653d4eSeschrock * this time.
142199653d4eSeschrock */
1422fa94a07fSbrendan VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
142399653d4eSeschrock DATA_TYPE_NVLIST_ARRAY) == 0);
142499653d4eSeschrock
1425fa94a07fSbrendan spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1426fa94a07fSbrendan KM_SLEEP);
1427fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++)
1428fa94a07fSbrendan spares[i] = vdev_config_generate(spa,
14293f9d6ad7SLin Ling spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1430fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1431fa94a07fSbrendan ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1432fa94a07fSbrendan for (i = 0; i < spa->spa_spares.sav_count; i++)
143399653d4eSeschrock nvlist_free(spares[i]);
1434fa94a07fSbrendan kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1435fa94a07fSbrendan }
1436fa94a07fSbrendan
1437fa94a07fSbrendan /*
1438fa94a07fSbrendan * Load (or re-load) the current list of vdevs describing the active l2cache for
1439fa94a07fSbrendan * this pool. When this is called, we have some form of basic information in
1440fa94a07fSbrendan * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1441fa94a07fSbrendan * then re-generate a more complete list including status information.
1442fa94a07fSbrendan * Devices which are already active have their details maintained, and are
1443fa94a07fSbrendan * not re-opened.
1444fa94a07fSbrendan */
1445fa94a07fSbrendan static void
spa_load_l2cache(spa_t * spa)1446fa94a07fSbrendan spa_load_l2cache(spa_t *spa)
1447fa94a07fSbrendan {
1448fa94a07fSbrendan nvlist_t **l2cache;
1449fa94a07fSbrendan uint_t nl2cache;
1450fa94a07fSbrendan int i, j, oldnvdevs;
1451573ca77eSGeorge Wilson uint64_t guid;
1452fa94a07fSbrendan vdev_t *vd, **oldvdevs, **newvdevs;
1453fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache;
1454fa94a07fSbrendan
1455e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1456e14bb325SJeff Bonwick
1457fa94a07fSbrendan if (sav->sav_config != NULL) {
1458fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1459fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1460fa94a07fSbrendan newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1461fa94a07fSbrendan } else {
1462fa94a07fSbrendan nl2cache = 0;
1463d5285caeSGeorge Wilson newvdevs = NULL;
1464fa94a07fSbrendan }
1465fa94a07fSbrendan
1466fa94a07fSbrendan oldvdevs = sav->sav_vdevs;
1467fa94a07fSbrendan oldnvdevs = sav->sav_count;
1468fa94a07fSbrendan sav->sav_vdevs = NULL;
1469fa94a07fSbrendan sav->sav_count = 0;
1470fa94a07fSbrendan
1471fa94a07fSbrendan /*
1472fa94a07fSbrendan * Process new nvlist of vdevs.
1473fa94a07fSbrendan */
1474fa94a07fSbrendan for (i = 0; i < nl2cache; i++) {
1475fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1476fa94a07fSbrendan &guid) == 0);
1477fa94a07fSbrendan
1478fa94a07fSbrendan newvdevs[i] = NULL;
1479fa94a07fSbrendan for (j = 0; j < oldnvdevs; j++) {
1480fa94a07fSbrendan vd = oldvdevs[j];
1481fa94a07fSbrendan if (vd != NULL && guid == vd->vdev_guid) {
1482fa94a07fSbrendan /*
1483fa94a07fSbrendan * Retain previous vdev for add/remove ops.
1484fa94a07fSbrendan */
1485fa94a07fSbrendan newvdevs[i] = vd;
1486fa94a07fSbrendan oldvdevs[j] = NULL;
1487fa94a07fSbrendan break;
1488fa94a07fSbrendan }
1489fa94a07fSbrendan }
1490fa94a07fSbrendan
1491fa94a07fSbrendan if (newvdevs[i] == NULL) {
1492fa94a07fSbrendan /*
1493fa94a07fSbrendan * Create new vdev
1494fa94a07fSbrendan */
1495fa94a07fSbrendan VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1496fa94a07fSbrendan VDEV_ALLOC_L2CACHE) == 0);
1497fa94a07fSbrendan ASSERT(vd != NULL);
1498fa94a07fSbrendan newvdevs[i] = vd;
1499fa94a07fSbrendan
1500fa94a07fSbrendan /*
1501fa94a07fSbrendan * Commit this vdev as an l2cache device,
1502fa94a07fSbrendan * even if it fails to open.
1503fa94a07fSbrendan */
1504fa94a07fSbrendan spa_l2cache_add(vd);
1505fa94a07fSbrendan
1506c5904d13Seschrock vd->vdev_top = vd;
1507c5904d13Seschrock vd->vdev_aux = sav;
1508c5904d13Seschrock
1509c5904d13Seschrock spa_l2cache_activate(vd);
1510c5904d13Seschrock
1511fa94a07fSbrendan if (vdev_open(vd) != 0)
1512fa94a07fSbrendan continue;
1513fa94a07fSbrendan
1514fa94a07fSbrendan (void) vdev_validate_aux(vd);
1515fa94a07fSbrendan
1516c9e5c7a7SSaso Kiselkov if (!vdev_is_dead(vd)) {
1517c9e5c7a7SSaso Kiselkov boolean_t do_rebuild = B_FALSE;
1518c9e5c7a7SSaso Kiselkov
1519c9e5c7a7SSaso Kiselkov (void) nvlist_lookup_boolean_value(l2cache[i],
1520c9e5c7a7SSaso Kiselkov ZPOOL_CONFIG_L2CACHE_PERSISTENT,
1521c9e5c7a7SSaso Kiselkov &do_rebuild);
1522c9e5c7a7SSaso Kiselkov l2arc_add_vdev(spa, vd, do_rebuild);
1523c9e5c7a7SSaso Kiselkov }
1524fa94a07fSbrendan }
1525fa94a07fSbrendan }
1526fa94a07fSbrendan
1527fa94a07fSbrendan /*
1528fa94a07fSbrendan * Purge vdevs that were dropped
1529fa94a07fSbrendan */
1530fa94a07fSbrendan for (i = 0; i < oldnvdevs; i++) {
1531fa94a07fSbrendan uint64_t pool;
1532fa94a07fSbrendan
1533fa94a07fSbrendan vd = oldvdevs[i];
1534fa94a07fSbrendan if (vd != NULL) {
1535cd0837ccSGeorge Wilson ASSERT(vd->vdev_isl2cache);
1536cd0837ccSGeorge Wilson
15378ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
15388ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd))
1539fa94a07fSbrendan l2arc_remove_vdev(vd);
1540cd0837ccSGeorge Wilson vdev_clear_stats(vd);
1541cd0837ccSGeorge Wilson vdev_free(vd);
1542fa94a07fSbrendan }
1543fa94a07fSbrendan }
1544fa94a07fSbrendan
1545fa94a07fSbrendan if (oldvdevs)
1546fa94a07fSbrendan kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1547fa94a07fSbrendan
1548fa94a07fSbrendan if (sav->sav_config == NULL)
1549fa94a07fSbrendan goto out;
1550fa94a07fSbrendan
1551fa94a07fSbrendan sav->sav_vdevs = newvdevs;
1552fa94a07fSbrendan sav->sav_count = (int)nl2cache;
1553fa94a07fSbrendan
1554fa94a07fSbrendan /*
1555fa94a07fSbrendan * Recompute the stashed list of l2cache devices, with status
1556fa94a07fSbrendan * information this time.
1557fa94a07fSbrendan */
1558fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1559fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0);
1560fa94a07fSbrendan
1561fa94a07fSbrendan l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1562fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++)
1563fa94a07fSbrendan l2cache[i] = vdev_config_generate(spa,
15643f9d6ad7SLin Ling sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1565fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1566fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1567fa94a07fSbrendan out:
1568fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++)
1569fa94a07fSbrendan nvlist_free(l2cache[i]);
1570fa94a07fSbrendan if (sav->sav_count)
1571fa94a07fSbrendan kmem_free(l2cache, sav->sav_count * sizeof (void *));
157299653d4eSeschrock }
157399653d4eSeschrock
157499653d4eSeschrock static int
load_nvlist(spa_t * spa,uint64_t obj,nvlist_t ** value)157599653d4eSeschrock load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
157699653d4eSeschrock {
157799653d4eSeschrock dmu_buf_t *db;
157899653d4eSeschrock char *packed = NULL;
157999653d4eSeschrock size_t nvsize = 0;
158099653d4eSeschrock int error;
158199653d4eSeschrock *value = NULL;
158299653d4eSeschrock
1583a45f1c3cSBrian Behlendorf error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1584a45f1c3cSBrian Behlendorf if (error != 0)
1585a45f1c3cSBrian Behlendorf return (error);
1586a45f1c3cSBrian Behlendorf
158799653d4eSeschrock nvsize = *(uint64_t *)db->db_data;
158899653d4eSeschrock dmu_buf_rele(db, FTAG);
158999653d4eSeschrock
159099653d4eSeschrock packed = kmem_alloc(nvsize, KM_SLEEP);
15917bfdf011SNeil Perrin error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
15927bfdf011SNeil Perrin DMU_READ_PREFETCH);
159399653d4eSeschrock if (error == 0)
159499653d4eSeschrock error = nvlist_unpack(packed, nvsize, value, 0);
159599653d4eSeschrock kmem_free(packed, nvsize);
159699653d4eSeschrock
159799653d4eSeschrock return (error);
159899653d4eSeschrock }
159999653d4eSeschrock
160099653d4eSeschrock /*
16013d7072f8Seschrock * Checks to see if the given vdev could not be opened, in which case we post a
16023d7072f8Seschrock * sysevent to notify the autoreplace code that the device has been removed.
16033d7072f8Seschrock */
16043d7072f8Seschrock static void
spa_check_removed(vdev_t * vd)16053d7072f8Seschrock spa_check_removed(vdev_t *vd)
16063d7072f8Seschrock {
1607573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++)
16083d7072f8Seschrock spa_check_removed(vd->vdev_child[c]);
16093d7072f8Seschrock
1610efb4a871SYuri Pankov if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1611efb4a871SYuri Pankov !vd->vdev_ishole) {
16123d7072f8Seschrock zfs_post_autoreplace(vd->vdev_spa, vd);
16133d7072f8Seschrock spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
16143d7072f8Seschrock }
16153d7072f8Seschrock }
16163d7072f8Seschrock
16173d7072f8Seschrock /*
16184b964adaSGeorge Wilson * Validate the current config against the MOS config
1619e6ca193dSGeorge Wilson */
16204b964adaSGeorge Wilson static boolean_t
spa_config_valid(spa_t * spa,nvlist_t * config)16214b964adaSGeorge Wilson spa_config_valid(spa_t *spa, nvlist_t *config)
1622e6ca193dSGeorge Wilson {
16234b964adaSGeorge Wilson vdev_t *mrvd, *rvd = spa->spa_root_vdev;
16244b964adaSGeorge Wilson nvlist_t *nv;
16254b964adaSGeorge Wilson
16264b964adaSGeorge Wilson VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
16274b964adaSGeorge Wilson
16284b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
16294b964adaSGeorge Wilson VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
16304b964adaSGeorge Wilson
16314b964adaSGeorge Wilson ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1632e6ca193dSGeorge Wilson
163388ecc943SGeorge Wilson /*
16344b964adaSGeorge Wilson * If we're doing a normal import, then build up any additional
16354b964adaSGeorge Wilson * diagnostic information about missing devices in this config.
16364b964adaSGeorge Wilson * We'll pass this up to the user for further processing.
163788ecc943SGeorge Wilson */
16384b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
16394b964adaSGeorge Wilson nvlist_t **child, *nv;
16404b964adaSGeorge Wilson uint64_t idx = 0;
16414b964adaSGeorge Wilson
16424b964adaSGeorge Wilson child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
16434b964adaSGeorge Wilson KM_SLEEP);
16444b964adaSGeorge Wilson VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1645e6ca193dSGeorge Wilson
164688ecc943SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
16474b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
16484b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c];
16494b964adaSGeorge Wilson
16504b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops &&
16514b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops &&
16524b964adaSGeorge Wilson mtvd->vdev_islog)
16534b964adaSGeorge Wilson child[idx++] = vdev_config_generate(spa, mtvd,
16544b964adaSGeorge Wilson B_FALSE, 0);
1655e6ca193dSGeorge Wilson }
16564b964adaSGeorge Wilson
16574b964adaSGeorge Wilson if (idx) {
16584b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist_array(nv,
16594b964adaSGeorge Wilson ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
16604b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(spa->spa_load_info,
16614b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
16624b964adaSGeorge Wilson
16634b964adaSGeorge Wilson for (int i = 0; i < idx; i++)
16644b964adaSGeorge Wilson nvlist_free(child[i]);
16654b964adaSGeorge Wilson }
16664b964adaSGeorge Wilson nvlist_free(nv);
16674b964adaSGeorge Wilson kmem_free(child, rvd->vdev_children * sizeof (char **));
16684b964adaSGeorge Wilson }
16694b964adaSGeorge Wilson
16704b964adaSGeorge Wilson /*
16714b964adaSGeorge Wilson * Compare the root vdev tree with the information we have
16724b964adaSGeorge Wilson * from the MOS config (mrvd). Check each top-level vdev
16734b964adaSGeorge Wilson * with the corresponding MOS config top-level (mtvd).
16744b964adaSGeorge Wilson */
16754b964adaSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
16764b964adaSGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
16774b964adaSGeorge Wilson vdev_t *mtvd = mrvd->vdev_child[c];
16784b964adaSGeorge Wilson
16794b964adaSGeorge Wilson /*
16804b964adaSGeorge Wilson * Resolve any "missing" vdevs in the current configuration.
16814b964adaSGeorge Wilson * If we find that the MOS config has more accurate information
16824b964adaSGeorge Wilson * about the top-level vdev then use that vdev instead.
16834b964adaSGeorge Wilson */
16844b964adaSGeorge Wilson if (tvd->vdev_ops == &vdev_missing_ops &&
16854b964adaSGeorge Wilson mtvd->vdev_ops != &vdev_missing_ops) {
16864b964adaSGeorge Wilson
16874b964adaSGeorge Wilson if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
16884b964adaSGeorge Wilson continue;
16894b964adaSGeorge Wilson
16904b964adaSGeorge Wilson /*
16914b964adaSGeorge Wilson * Device specific actions.
16924b964adaSGeorge Wilson */
16934b964adaSGeorge Wilson if (mtvd->vdev_islog) {
16944b964adaSGeorge Wilson spa_set_log_state(spa, SPA_LOG_CLEAR);
16954b964adaSGeorge Wilson } else {
16964b964adaSGeorge Wilson /*
16974b964adaSGeorge Wilson * XXX - once we have 'readonly' pool
16984b964adaSGeorge Wilson * support we should be able to handle
16994b964adaSGeorge Wilson * missing data devices by transitioning
17004b964adaSGeorge Wilson * the pool to readonly.
17014b964adaSGeorge Wilson */
17024b964adaSGeorge Wilson continue;
17034b964adaSGeorge Wilson }
17044b964adaSGeorge Wilson
17054b964adaSGeorge Wilson /*
17064b964adaSGeorge Wilson * Swap the missing vdev with the data we were
17074b964adaSGeorge Wilson * able to obtain from the MOS config.
17084b964adaSGeorge Wilson */
17094b964adaSGeorge Wilson vdev_remove_child(rvd, tvd);
17104b964adaSGeorge Wilson vdev_remove_child(mrvd, mtvd);
17114b964adaSGeorge Wilson
17124b964adaSGeorge Wilson vdev_add_child(rvd, mtvd);
17134b964adaSGeorge Wilson vdev_add_child(mrvd, tvd);
17144b964adaSGeorge Wilson
171588ecc943SGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG);
17164b964adaSGeorge Wilson vdev_load(mtvd);
17174b964adaSGeorge Wilson spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
17184b964adaSGeorge Wilson
17194b964adaSGeorge Wilson vdev_reopen(rvd);
17204b964adaSGeorge Wilson } else if (mtvd->vdev_islog) {
17214b964adaSGeorge Wilson /*
17224b964adaSGeorge Wilson * Load the slog device's state from the MOS config
17234b964adaSGeorge Wilson * since it's possible that the label does not
17244b964adaSGeorge Wilson * contain the most up-to-date information.
17254b964adaSGeorge Wilson */
17264b964adaSGeorge Wilson vdev_load_log_state(tvd, mtvd);
17274b964adaSGeorge Wilson vdev_reopen(tvd);
17284b964adaSGeorge Wilson }
17294b964adaSGeorge Wilson }
17304b964adaSGeorge Wilson vdev_free(mrvd);
17314b964adaSGeorge Wilson spa_config_exit(spa, SCL_ALL, FTAG);
17324b964adaSGeorge Wilson
17334b964adaSGeorge Wilson /*
17344b964adaSGeorge Wilson * Ensure we were able to validate the config.
17354b964adaSGeorge Wilson */
17364b964adaSGeorge Wilson return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1737e6ca193dSGeorge Wilson }
1738e6ca193dSGeorge Wilson
1739e6ca193dSGeorge Wilson /*
1740b87f3af3Sperrin * Check for missing log devices
1741b87f3af3Sperrin */
17423b2aab18SMatthew Ahrens static boolean_t
spa_check_logs(spa_t * spa)1743b87f3af3Sperrin spa_check_logs(spa_t *spa)
1744b87f3af3Sperrin {
17453b2aab18SMatthew Ahrens boolean_t rv = B_FALSE;
174612380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa);
17473b2aab18SMatthew Ahrens
1748b87f3af3Sperrin switch (spa->spa_log_state) {
1749b87f3af3Sperrin case SPA_LOG_MISSING:
1750b87f3af3Sperrin /* need to recheck in case slog has been restored */
1751b87f3af3Sperrin case SPA_LOG_UNKNOWN:
175212380e1eSArne Jansen rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
175312380e1eSArne Jansen zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
17543b2aab18SMatthew Ahrens if (rv)
17551195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_MISSING);
1756b87f3af3Sperrin break;
1757b87f3af3Sperrin }
17583b2aab18SMatthew Ahrens return (rv);
1759b87f3af3Sperrin }
1760b87f3af3Sperrin
17611195e687SMark J Musante static boolean_t
spa_passivate_log(spa_t * spa)17621195e687SMark J Musante spa_passivate_log(spa_t *spa)
17631195e687SMark J Musante {
17641195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev;
17651195e687SMark J Musante boolean_t slog_found = B_FALSE;
17661195e687SMark J Musante
17671195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
17681195e687SMark J Musante
17691195e687SMark J Musante if (!spa_has_slogs(spa))
17701195e687SMark J Musante return (B_FALSE);
17711195e687SMark J Musante
17721195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) {
17731195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c];
17741195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg;
17751195e687SMark J Musante
17761195e687SMark J Musante if (tvd->vdev_islog) {
17771195e687SMark J Musante metaslab_group_passivate(mg);
17781195e687SMark J Musante slog_found = B_TRUE;
17791195e687SMark J Musante }
17801195e687SMark J Musante }
17811195e687SMark J Musante
17821195e687SMark J Musante return (slog_found);
17831195e687SMark J Musante }
17841195e687SMark J Musante
17851195e687SMark J Musante static void
spa_activate_log(spa_t * spa)17861195e687SMark J Musante spa_activate_log(spa_t *spa)
17871195e687SMark J Musante {
17881195e687SMark J Musante vdev_t *rvd = spa->spa_root_vdev;
17891195e687SMark J Musante
17901195e687SMark J Musante ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
17911195e687SMark J Musante
17921195e687SMark J Musante for (int c = 0; c < rvd->vdev_children; c++) {
17931195e687SMark J Musante vdev_t *tvd = rvd->vdev_child[c];
17941195e687SMark J Musante metaslab_group_t *mg = tvd->vdev_mg;
17951195e687SMark J Musante
17961195e687SMark J Musante if (tvd->vdev_islog)
17971195e687SMark J Musante metaslab_group_activate(mg);
17981195e687SMark J Musante }
17991195e687SMark J Musante }
18001195e687SMark J Musante
18011195e687SMark J Musante int
spa_offline_log(spa_t * spa)18021195e687SMark J Musante spa_offline_log(spa_t *spa)
18031195e687SMark J Musante {
18043b2aab18SMatthew Ahrens int error;
18051195e687SMark J Musante
18063b2aab18SMatthew Ahrens error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
18073b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN);
18083b2aab18SMatthew Ahrens if (error == 0) {
18091195e687SMark J Musante /*
18101195e687SMark J Musante * We successfully offlined the log device, sync out the
18111195e687SMark J Musante * current txg so that the "stubby" block can be removed
18121195e687SMark J Musante * by zil_sync().
18131195e687SMark J Musante */
18141195e687SMark J Musante txg_wait_synced(spa->spa_dsl_pool, 0);
18151195e687SMark J Musante }
18161195e687SMark J Musante return (error);
18171195e687SMark J Musante }
18181195e687SMark J Musante
1819b693757aSEric Schrock static void
spa_aux_check_removed(spa_aux_vdev_t * sav)1820b693757aSEric Schrock spa_aux_check_removed(spa_aux_vdev_t *sav)
1821b693757aSEric Schrock {
1822b24ab676SJeff Bonwick for (int i = 0; i < sav->sav_count; i++)
1823b693757aSEric Schrock spa_check_removed(sav->sav_vdevs[i]);
1824b693757aSEric Schrock }
1825b693757aSEric Schrock
1826b24ab676SJeff Bonwick void
spa_claim_notify(zio_t * zio)1827b24ab676SJeff Bonwick spa_claim_notify(zio_t *zio)
1828b24ab676SJeff Bonwick {
1829b24ab676SJeff Bonwick spa_t *spa = zio->io_spa;
1830b24ab676SJeff Bonwick
1831b24ab676SJeff Bonwick if (zio->io_error)
1832b24ab676SJeff Bonwick return;
1833b24ab676SJeff Bonwick
1834b24ab676SJeff Bonwick mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1835b24ab676SJeff Bonwick if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1836b24ab676SJeff Bonwick spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1837b24ab676SJeff Bonwick mutex_exit(&spa->spa_props_lock);
1838b24ab676SJeff Bonwick }
1839b24ab676SJeff Bonwick
1840468c413aSTim Haley typedef struct spa_load_error {
1841c8ee1847SVictor Latushkin uint64_t sle_meta_count;
1842468c413aSTim Haley uint64_t sle_data_count;
1843468c413aSTim Haley } spa_load_error_t;
1844468c413aSTim Haley
1845468c413aSTim Haley static void
spa_load_verify_done(zio_t * zio)1846468c413aSTim Haley spa_load_verify_done(zio_t *zio)
1847468c413aSTim Haley {
1848468c413aSTim Haley blkptr_t *bp = zio->io_bp;
1849468c413aSTim Haley spa_load_error_t *sle = zio->io_private;
1850468c413aSTim Haley dmu_object_type_t type = BP_GET_TYPE(bp);
1851468c413aSTim Haley int error = zio->io_error;
1852e42d2059SMatthew Ahrens spa_t *spa = zio->io_spa;
1853468c413aSTim Haley
1854468c413aSTim Haley if (error) {
1855ad135b5dSChristopher Siden if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1856468c413aSTim Haley type != DMU_OT_INTENT_LOG)
18571a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_meta_count);
1858468c413aSTim Haley else
18591a5e258fSJosef 'Jeff' Sipek atomic_inc_64(&sle->sle_data_count);
1860468c413aSTim Haley }
1861468c413aSTim Haley zio_data_buf_free(zio->io_data, zio->io_size);
1862e42d2059SMatthew Ahrens
1863e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock);
1864e42d2059SMatthew Ahrens spa->spa_scrub_inflight--;
1865e42d2059SMatthew Ahrens cv_broadcast(&spa->spa_scrub_io_cv);
1866e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock);
1867468c413aSTim Haley }
1868468c413aSTim Haley
1869e42d2059SMatthew Ahrens /*
1870e42d2059SMatthew Ahrens * Maximum number of concurrent scrub i/os to create while verifying
1871e42d2059SMatthew Ahrens * a pool while importing it.
1872e42d2059SMatthew Ahrens */
1873e42d2059SMatthew Ahrens int spa_load_verify_maxinflight = 10000;
1874e42d2059SMatthew Ahrens boolean_t spa_load_verify_metadata = B_TRUE;
1875e42d2059SMatthew Ahrens boolean_t spa_load_verify_data = B_TRUE;
1876e42d2059SMatthew Ahrens
1877468c413aSTim Haley /*ARGSUSED*/
1878468c413aSTim Haley static int
spa_load_verify_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)1879b24ab676SJeff Bonwick spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
18807802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1881468c413aSTim Haley {
1882a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1883e42d2059SMatthew Ahrens return (0);
1884e42d2059SMatthew Ahrens /*
1885e42d2059SMatthew Ahrens * Note: normally this routine will not be called if
1886e42d2059SMatthew Ahrens * spa_load_verify_metadata is not set. However, it may be useful
1887e42d2059SMatthew Ahrens * to manually set the flag after the traversal has begun.
1888e42d2059SMatthew Ahrens */
1889e42d2059SMatthew Ahrens if (!spa_load_verify_metadata)
1890e42d2059SMatthew Ahrens return (0);
1891e42d2059SMatthew Ahrens if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1892e42d2059SMatthew Ahrens return (0);
1893e42d2059SMatthew Ahrens
1894468c413aSTim Haley zio_t *rio = arg;
1895468c413aSTim Haley size_t size = BP_GET_PSIZE(bp);
1896468c413aSTim Haley void *data = zio_data_buf_alloc(size);
1897468c413aSTim Haley
1898e42d2059SMatthew Ahrens mutex_enter(&spa->spa_scrub_lock);
1899e42d2059SMatthew Ahrens while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1900e42d2059SMatthew Ahrens cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1901e42d2059SMatthew Ahrens spa->spa_scrub_inflight++;
1902e42d2059SMatthew Ahrens mutex_exit(&spa->spa_scrub_lock);
1903e42d2059SMatthew Ahrens
1904468c413aSTim Haley zio_nowait(zio_read(rio, spa, bp, data, size,
1905468c413aSTim Haley spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1906468c413aSTim Haley ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1907468c413aSTim Haley ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1908468c413aSTim Haley return (0);
1909468c413aSTim Haley }
1910468c413aSTim Haley
1911468c413aSTim Haley static int
spa_load_verify(spa_t * spa)1912468c413aSTim Haley spa_load_verify(spa_t *spa)
1913468c413aSTim Haley {
1914468c413aSTim Haley zio_t *rio;
1915468c413aSTim Haley spa_load_error_t sle = { 0 };
1916468c413aSTim Haley zpool_rewind_policy_t policy;
1917468c413aSTim Haley boolean_t verify_ok = B_FALSE;
1918e42d2059SMatthew Ahrens int error = 0;
1919468c413aSTim Haley
1920c8ee1847SVictor Latushkin zpool_get_rewind_policy(spa->spa_config, &policy);
1921c8ee1847SVictor Latushkin
1922c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1923c8ee1847SVictor Latushkin return (0);
1924c8ee1847SVictor Latushkin
1925468c413aSTim Haley rio = zio_root(spa, NULL, &sle,
1926468c413aSTim Haley ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1927468c413aSTim Haley
1928e42d2059SMatthew Ahrens if (spa_load_verify_metadata) {
1929bbfd46c4SJeff Bonwick error = traverse_pool(spa, spa->spa_verify_min_txg,
1930e42d2059SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1931e42d2059SMatthew Ahrens spa_load_verify_cb, rio);
1932e42d2059SMatthew Ahrens }
1933468c413aSTim Haley
1934468c413aSTim Haley (void) zio_wait(rio);
1935468c413aSTim Haley
1936c8ee1847SVictor Latushkin spa->spa_load_meta_errors = sle.sle_meta_count;
1937468c413aSTim Haley spa->spa_load_data_errors = sle.sle_data_count;
1938468c413aSTim Haley
1939c8ee1847SVictor Latushkin if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1940468c413aSTim Haley sle.sle_data_count <= policy.zrp_maxdata) {
19414b964adaSGeorge Wilson int64_t loss = 0;
19424b964adaSGeorge Wilson
1943468c413aSTim Haley verify_ok = B_TRUE;
1944468c413aSTim Haley spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1945468c413aSTim Haley spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
19464b964adaSGeorge Wilson
19474b964adaSGeorge Wilson loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
19484b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info,
19494b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
19504b964adaSGeorge Wilson VERIFY(nvlist_add_int64(spa->spa_load_info,
19514b964adaSGeorge Wilson ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
19524b964adaSGeorge Wilson VERIFY(nvlist_add_uint64(spa->spa_load_info,
19534b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1954a33cae98STim Haley } else {
1955a33cae98STim Haley spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1956468c413aSTim Haley }
1957468c413aSTim Haley
1958468c413aSTim Haley if (error) {
1959468c413aSTim Haley if (error != ENXIO && error != EIO)
1960be6fd75aSMatthew Ahrens error = SET_ERROR(EIO);
1961468c413aSTim Haley return (error);
1962468c413aSTim Haley }
1963468c413aSTim Haley
1964468c413aSTim Haley return (verify_ok ? 0 : EIO);
1965468c413aSTim Haley }
1966468c413aSTim Haley
1967b87f3af3Sperrin /*
19681195e687SMark J Musante * Find a value in the pool props object.
19691195e687SMark J Musante */
19701195e687SMark J Musante static void
spa_prop_find(spa_t * spa,zpool_prop_t prop,uint64_t * val)19711195e687SMark J Musante spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
19721195e687SMark J Musante {
19731195e687SMark J Musante (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
19741195e687SMark J Musante zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
19751195e687SMark J Musante }
19761195e687SMark J Musante
19771195e687SMark J Musante /*
19781195e687SMark J Musante * Find a value in the pool directory object.
19791195e687SMark J Musante */
19801195e687SMark J Musante static int
spa_dir_prop(spa_t * spa,const char * name,uint64_t * val)19811195e687SMark J Musante spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
19821195e687SMark J Musante {
19831195e687SMark J Musante return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
19841195e687SMark J Musante name, sizeof (uint64_t), 1, val));
19851195e687SMark J Musante }
19861195e687SMark J Musante
19871195e687SMark J Musante static int
spa_vdev_err(vdev_t * vdev,vdev_aux_t aux,int err)19881195e687SMark J Musante spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
19891195e687SMark J Musante {
19901195e687SMark J Musante vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
19911195e687SMark J Musante return (err);
19921195e687SMark J Musante }
19931195e687SMark J Musante
19941195e687SMark J Musante /*
19951195e687SMark J Musante * Fix up config after a partly-completed split. This is done with the
19961195e687SMark J Musante * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
19971195e687SMark J Musante * pool have that entry in their config, but only the splitting one contains
19981195e687SMark J Musante * a list of all the guids of the vdevs that are being split off.
19991195e687SMark J Musante *
20001195e687SMark J Musante * This function determines what to do with that list: either rejoin
20011195e687SMark J Musante * all the disks to the pool, or complete the splitting process. To attempt
20021195e687SMark J Musante * the rejoin, each disk that is offlined is marked online again, and
20031195e687SMark J Musante * we do a reopen() call. If the vdev label for every disk that was
20041195e687SMark J Musante * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
20051195e687SMark J Musante * then we call vdev_split() on each disk, and complete the split.
20061195e687SMark J Musante *
2007d41c4376SMark J Musante * Otherwise we leave the config alone, with all the vdevs in place in
2008d41c4376SMark J Musante * the original pool.
20091195e687SMark J Musante */
20101195e687SMark J Musante static void
spa_try_repair(spa_t * spa,nvlist_t * config)20111195e687SMark J Musante spa_try_repair(spa_t *spa, nvlist_t *config)
20121195e687SMark J Musante {
20131195e687SMark J Musante uint_t extracted;
20141195e687SMark J Musante uint64_t *glist;
20151195e687SMark J Musante uint_t i, gcount;
20161195e687SMark J Musante nvlist_t *nvl;
20171195e687SMark J Musante vdev_t **vd;
20181195e687SMark J Musante boolean_t attempt_reopen;
20191195e687SMark J Musante
20201195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
20211195e687SMark J Musante return;
20221195e687SMark J Musante
20231195e687SMark J Musante /* check that the config is complete */
20241195e687SMark J Musante if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
20251195e687SMark J Musante &glist, &gcount) != 0)
20261195e687SMark J Musante return;
20271195e687SMark J Musante
20281195e687SMark J Musante vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
20291195e687SMark J Musante
20301195e687SMark J Musante /* attempt to online all the vdevs & validate */
20311195e687SMark J Musante attempt_reopen = B_TRUE;
20321195e687SMark J Musante for (i = 0; i < gcount; i++) {
20331195e687SMark J Musante if (glist[i] == 0) /* vdev is hole */
20341195e687SMark J Musante continue;
20351195e687SMark J Musante
20361195e687SMark J Musante vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
20371195e687SMark J Musante if (vd[i] == NULL) {
20381195e687SMark J Musante /*
20391195e687SMark J Musante * Don't bother attempting to reopen the disks;
20401195e687SMark J Musante * just do the split.
20411195e687SMark J Musante */
20421195e687SMark J Musante attempt_reopen = B_FALSE;
20431195e687SMark J Musante } else {
20441195e687SMark J Musante /* attempt to re-online it */
20451195e687SMark J Musante vd[i]->vdev_offline = B_FALSE;
20461195e687SMark J Musante }
20471195e687SMark J Musante }
20481195e687SMark J Musante
20491195e687SMark J Musante if (attempt_reopen) {
20501195e687SMark J Musante vdev_reopen(spa->spa_root_vdev);
20511195e687SMark J Musante
20521195e687SMark J Musante /* check each device to see what state it's in */
20531195e687SMark J Musante for (extracted = 0, i = 0; i < gcount; i++) {
20541195e687SMark J Musante if (vd[i] != NULL &&
20551195e687SMark J Musante vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
20561195e687SMark J Musante break;
20571195e687SMark J Musante ++extracted;
20581195e687SMark J Musante }
20591195e687SMark J Musante }
20601195e687SMark J Musante
20611195e687SMark J Musante /*
20621195e687SMark J Musante * If every disk has been moved to the new pool, or if we never
20631195e687SMark J Musante * even attempted to look at them, then we split them off for
20641195e687SMark J Musante * good.
20651195e687SMark J Musante */
20661195e687SMark J Musante if (!attempt_reopen || gcount == extracted) {
20671195e687SMark J Musante for (i = 0; i < gcount; i++)
20681195e687SMark J Musante if (vd[i] != NULL)
20691195e687SMark J Musante vdev_split(vd[i]);
20701195e687SMark J Musante vdev_reopen(spa->spa_root_vdev);
20711195e687SMark J Musante }
20721195e687SMark J Musante
20731195e687SMark J Musante kmem_free(vd, gcount * sizeof (vdev_t *));
20741195e687SMark J Musante }
20751195e687SMark J Musante
20761195e687SMark J Musante static int
spa_load(spa_t * spa,spa_load_state_t state,spa_import_type_t type,boolean_t mosconfig)20771195e687SMark J Musante spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
20781195e687SMark J Musante boolean_t mosconfig)
20791195e687SMark J Musante {
20801195e687SMark J Musante nvlist_t *config = spa->spa_config;
20811195e687SMark J Musante char *ereport = FM_EREPORT_ZFS_POOL;
20828704186eSDan McDonald char *comment;
20831195e687SMark J Musante int error;
20841195e687SMark J Musante uint64_t pool_guid;
20851195e687SMark J Musante nvlist_t *nvl;
20861195e687SMark J Musante
20871195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2088be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
20891195e687SMark J Musante
20908704186eSDan McDonald ASSERT(spa->spa_comment == NULL);
20918704186eSDan McDonald if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
20928704186eSDan McDonald spa->spa_comment = spa_strdup(comment);
20938704186eSDan McDonald
20941195e687SMark J Musante /*
20951195e687SMark J Musante * Versioning wasn't explicitly added to the label until later, so if
20961195e687SMark J Musante * it's not present treat it as the initial version.
20971195e687SMark J Musante */
20981195e687SMark J Musante if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
20991195e687SMark J Musante &spa->spa_ubsync.ub_version) != 0)
21001195e687SMark J Musante spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
21011195e687SMark J Musante
21021195e687SMark J Musante (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
21031195e687SMark J Musante &spa->spa_config_txg);
21041195e687SMark J Musante
21051195e687SMark J Musante if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
21061195e687SMark J Musante spa_guid_exists(pool_guid, 0)) {
2107be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST);
21081195e687SMark J Musante } else {
2109e9103aaeSGarrett D'Amore spa->spa_config_guid = pool_guid;
21101195e687SMark J Musante
21111195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
21121195e687SMark J Musante &nvl) == 0) {
21131195e687SMark J Musante VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
21141195e687SMark J Musante KM_SLEEP) == 0);
21151195e687SMark J Musante }
21161195e687SMark J Musante
2117ad135b5dSChristopher Siden nvlist_free(spa->spa_load_info);
2118ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc();
2119ad135b5dSChristopher Siden
212011027bc7STim Haley gethrestime(&spa->spa_loaded_ts);
21211195e687SMark J Musante error = spa_load_impl(spa, pool_guid, config, state, type,
21221195e687SMark J Musante mosconfig, &ereport);
21231195e687SMark J Musante }
21241195e687SMark J Musante
2125bc9014e6SJustin Gibbs /*
2126bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed
2127bc9014e6SJustin Gibbs * and are making their way through the eviction process.
2128bc9014e6SJustin Gibbs */
2129bc9014e6SJustin Gibbs spa_evicting_os_wait(spa);
21301195e687SMark J Musante spa->spa_minref = refcount_count(&spa->spa_refcount);
213111027bc7STim Haley if (error) {
213211027bc7STim Haley if (error != EEXIST) {
213311027bc7STim Haley spa->spa_loaded_ts.tv_sec = 0;
213411027bc7STim Haley spa->spa_loaded_ts.tv_nsec = 0;
213511027bc7STim Haley }
213611027bc7STim Haley if (error != EBADF) {
21371195e687SMark J Musante zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
213811027bc7STim Haley }
213911027bc7STim Haley }
21401195e687SMark J Musante spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
21411195e687SMark J Musante spa->spa_ena = 0;
21421195e687SMark J Musante
21431195e687SMark J Musante return (error);
21441195e687SMark J Musante }
21451195e687SMark J Musante
21461195e687SMark J Musante /*
2147fa9e4066Sahrens * Load an existing storage pool, using the pool's builtin spa_config as a
2148ea8dc4b6Seschrock * source of configuration information.
2149fa9e4066Sahrens */
2150fa9e4066Sahrens static int
spa_load_impl(spa_t * spa,uint64_t pool_guid,nvlist_t * config,spa_load_state_t state,spa_import_type_t type,boolean_t mosconfig,char ** ereport)21511195e687SMark J Musante spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
21521195e687SMark J Musante spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
21531195e687SMark J Musante char **ereport)
2154fa9e4066Sahrens {
2155fa9e4066Sahrens int error = 0;
2156871a9500SMark J Musante nvlist_t *nvroot = NULL;
2157ad135b5dSChristopher Siden nvlist_t *label;
2158fa9e4066Sahrens vdev_t *rvd;
2159fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock;
21604b964adaSGeorge Wilson uint64_t children, config_cache_txg = spa->spa_config_txg;
21618ad4d6ddSJeff Bonwick int orig_mode = spa->spa_mode;
21621195e687SMark J Musante int parse;
2163cde58dbcSMatthew Ahrens uint64_t obj;
2164ad135b5dSChristopher Siden boolean_t missing_feat_write = B_FALSE;
2165fa9e4066Sahrens
21668ad4d6ddSJeff Bonwick /*
21678ad4d6ddSJeff Bonwick * If this is an untrusted config, access the pool in read-only mode.
21688ad4d6ddSJeff Bonwick * This prevents things like resilvering recently removed devices.
21698ad4d6ddSJeff Bonwick */
21708ad4d6ddSJeff Bonwick if (!mosconfig)
21718ad4d6ddSJeff Bonwick spa->spa_mode = FREAD;
21728ad4d6ddSJeff Bonwick
2173e14bb325SJeff Bonwick ASSERT(MUTEX_HELD(&spa_namespace_lock));
2174e14bb325SJeff Bonwick
2175ea8dc4b6Seschrock spa->spa_load_state = state;
21760373e76bSbonwick
21771195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2178be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
2179fa9e4066Sahrens
21801195e687SMark J Musante parse = (type == SPA_IMPORT_EXISTING ?
21811195e687SMark J Musante VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2182b5989ec7Seschrock
2183fa9e4066Sahrens /*
218454d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs
218554d692b7SGeorge Wilson */
21866f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
21876f834bc1SMatthew Ahrens KM_SLEEP);
21886f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) {
21896f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
21906f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
21916f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER);
21926f834bc1SMatthew Ahrens }
219354d692b7SGeorge Wilson
219454d692b7SGeorge Wilson /*
219599653d4eSeschrock * Parse the configuration into a vdev tree. We explicitly set the
219699653d4eSeschrock * value that will be returned by spa_version() since parsing the
219799653d4eSeschrock * configuration requires knowing the version number.
2198fa9e4066Sahrens */
2199e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22001195e687SMark J Musante error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2201e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
2202fa9e4066Sahrens
220399653d4eSeschrock if (error != 0)
22041195e687SMark J Musante return (error);
2205fa9e4066Sahrens
22060e34b6a7Sbonwick ASSERT(spa->spa_root_vdev == rvd);
220781cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
220881cd5c55SMatthew Ahrens ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
22091195e687SMark J Musante
22101195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) {
2211fa9e4066Sahrens ASSERT(spa_guid(spa) == pool_guid);
22121195e687SMark J Musante }
2213fa9e4066Sahrens
2214fa9e4066Sahrens /*
2215fa9e4066Sahrens * Try to open all vdevs, loading each label in the process.
2216fa9e4066Sahrens */
2217e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
22180bf246f5Smc142369 error = vdev_open(rvd);
2219e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
22200bf246f5Smc142369 if (error != 0)
22211195e687SMark J Musante return (error);
2222fa9e4066Sahrens
2223fa9e4066Sahrens /*
222477e3a39cSMark J Musante * We need to validate the vdev labels against the configuration that
222577e3a39cSMark J Musante * we have in hand, which is dependent on the setting of mosconfig. If
222677e3a39cSMark J Musante * mosconfig is true then we're validating the vdev labels based on
222777e3a39cSMark J Musante * that config. Otherwise, we're validating against the cached config
222877e3a39cSMark J Musante * (zpool.cache) that was read when we loaded the zfs module, and then
222977e3a39cSMark J Musante * later we will recursively call spa_load() and validate against
223077e3a39cSMark J Musante * the vdev config.
22311195e687SMark J Musante *
22321195e687SMark J Musante * If we're assembling a new pool that's been split off from an
22331195e687SMark J Musante * existing pool, the labels haven't yet been updated so we skip
22341195e687SMark J Musante * validation for now.
2235560e6e96Seschrock */
22361195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) {
2237e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2238d7f601efSGeorge Wilson error = vdev_validate(rvd, mosconfig);
2239e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
2240560e6e96Seschrock
22411195e687SMark J Musante if (error != 0)
22421195e687SMark J Musante return (error);
22431195e687SMark J Musante
22441195e687SMark J Musante if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2245be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO));
2246560e6e96Seschrock }
2247560e6e96Seschrock
2248560e6e96Seschrock /*
2249fa9e4066Sahrens * Find the best uberblock.
2250fa9e4066Sahrens */
2251ad135b5dSChristopher Siden vdev_uberblock_load(rvd, ub, &label);
2252fa9e4066Sahrens
2253fa9e4066Sahrens /*
2254fa9e4066Sahrens * If we weren't able to find a single valid uberblock, return failure.
2255fa9e4066Sahrens */
2256ad135b5dSChristopher Siden if (ub->ub_txg == 0) {
2257ad135b5dSChristopher Siden nvlist_free(label);
22581195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2259ad135b5dSChristopher Siden }
2260ea8dc4b6Seschrock
2261ea8dc4b6Seschrock /*
2262ad135b5dSChristopher Siden * If the pool has an unsupported version we can't open it.
2263ea8dc4b6Seschrock */
2264ad135b5dSChristopher Siden if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2265ad135b5dSChristopher Siden nvlist_free(label);
22661195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2267ad135b5dSChristopher Siden }
2268ad135b5dSChristopher Siden
2269ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) {
2270ad135b5dSChristopher Siden nvlist_t *features;
2271ad135b5dSChristopher Siden
2272ad135b5dSChristopher Siden /*
2273ad135b5dSChristopher Siden * If we weren't able to find what's necessary for reading the
2274ad135b5dSChristopher Siden * MOS in the label, return failure.
2275ad135b5dSChristopher Siden */
2276ad135b5dSChristopher Siden if (label == NULL || nvlist_lookup_nvlist(label,
2277ad135b5dSChristopher Siden ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2278ad135b5dSChristopher Siden nvlist_free(label);
2279ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2280ad135b5dSChristopher Siden ENXIO));
2281ad135b5dSChristopher Siden }
2282ad135b5dSChristopher Siden
2283ad135b5dSChristopher Siden /*
2284ad135b5dSChristopher Siden * Update our in-core representation with the definitive values
2285ad135b5dSChristopher Siden * from the label.
2286ad135b5dSChristopher Siden */
2287ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features);
2288ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2289ad135b5dSChristopher Siden }
2290ad135b5dSChristopher Siden
2291ad135b5dSChristopher Siden nvlist_free(label);
2292ad135b5dSChristopher Siden
2293ad135b5dSChristopher Siden /*
2294ad135b5dSChristopher Siden * Look through entries in the label nvlist's features_for_read. If
2295ad135b5dSChristopher Siden * there is a feature listed there which we don't understand then we
2296ad135b5dSChristopher Siden * cannot open a pool.
2297ad135b5dSChristopher Siden */
2298ad135b5dSChristopher Siden if (ub->ub_version >= SPA_VERSION_FEATURES) {
2299ad135b5dSChristopher Siden nvlist_t *unsup_feat;
2300ad135b5dSChristopher Siden
2301ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2302ad135b5dSChristopher Siden 0);
2303ad135b5dSChristopher Siden
2304ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2305ad135b5dSChristopher Siden NULL); nvp != NULL;
2306ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2307ad135b5dSChristopher Siden if (!zfeature_is_supported(nvpair_name(nvp))) {
2308ad135b5dSChristopher Siden VERIFY(nvlist_add_string(unsup_feat,
2309ad135b5dSChristopher Siden nvpair_name(nvp), "") == 0);
2310ad135b5dSChristopher Siden }
2311ad135b5dSChristopher Siden }
2312ad135b5dSChristopher Siden
2313ad135b5dSChristopher Siden if (!nvlist_empty(unsup_feat)) {
2314ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2315ad135b5dSChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2316ad135b5dSChristopher Siden nvlist_free(unsup_feat);
2317ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2318ad135b5dSChristopher Siden ENOTSUP));
2319ad135b5dSChristopher Siden }
2320ad135b5dSChristopher Siden
2321ad135b5dSChristopher Siden nvlist_free(unsup_feat);
2322ad135b5dSChristopher Siden }
2323fa9e4066Sahrens
2324fa9e4066Sahrens /*
2325fa9e4066Sahrens * If the vdev guid sum doesn't match the uberblock, we have an
23264b964adaSGeorge Wilson * incomplete configuration. We first check to see if the pool
23274b964adaSGeorge Wilson * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
23284b964adaSGeorge Wilson * If it is, defer the vdev_guid_sum check till later so we
23294b964adaSGeorge Wilson * can handle missing vdevs.
2330fa9e4066Sahrens */
23314b964adaSGeorge Wilson if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
23324b964adaSGeorge Wilson &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
23331195e687SMark J Musante rvd->vdev_guid_sum != ub->ub_guid_sum)
23341195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
23351195e687SMark J Musante
23361195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
23371195e687SMark J Musante spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
23381195e687SMark J Musante spa_try_repair(spa, config);
23391195e687SMark J Musante spa_config_exit(spa, SCL_ALL, FTAG);
23401195e687SMark J Musante nvlist_free(spa->spa_config_splitting);
23411195e687SMark J Musante spa->spa_config_splitting = NULL;
2342fa9e4066Sahrens }
2343fa9e4066Sahrens
2344fa9e4066Sahrens /*
2345fa9e4066Sahrens * Initialize internal SPA structures.
2346fa9e4066Sahrens */
2347fa9e4066Sahrens spa->spa_state = POOL_STATE_ACTIVE;
2348fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock;
2349468c413aSTim Haley spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2350c8ee1847SVictor Latushkin TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2351468c413aSTim Haley spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2352468c413aSTim Haley spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2353b24ab676SJeff Bonwick spa->spa_claim_max_txg = spa->spa_first_txg;
23543f9d6ad7SLin Ling spa->spa_prev_software_version = ub->ub_software_version;
2355b24ab676SJeff Bonwick
2356ad135b5dSChristopher Siden error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
23571195e687SMark J Musante if (error)
23581195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2359fa9e4066Sahrens spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2360fa9e4066Sahrens
23611195e687SMark J Musante if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
23621195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2363fa9e4066Sahrens
2364ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2365ad135b5dSChristopher Siden boolean_t missing_feat_read = B_FALSE;
236657221772SChristopher Siden nvlist_t *unsup_feat, *enabled_feat;
2367ad135b5dSChristopher Siden
2368ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2369ad135b5dSChristopher Siden &spa->spa_feat_for_read_obj) != 0) {
2370ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2371ad135b5dSChristopher Siden }
2372ad135b5dSChristopher Siden
2373ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2374ad135b5dSChristopher Siden &spa->spa_feat_for_write_obj) != 0) {
2375ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2376ad135b5dSChristopher Siden }
2377ad135b5dSChristopher Siden
2378ad135b5dSChristopher Siden if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2379ad135b5dSChristopher Siden &spa->spa_feat_desc_obj) != 0) {
2380ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2381ad135b5dSChristopher Siden }
2382ad135b5dSChristopher Siden
238357221772SChristopher Siden enabled_feat = fnvlist_alloc();
238457221772SChristopher Siden unsup_feat = fnvlist_alloc();
2385ad135b5dSChristopher Siden
23862acef22dSMatthew Ahrens if (!spa_features_check(spa, B_FALSE,
238757221772SChristopher Siden unsup_feat, enabled_feat))
2388ad135b5dSChristopher Siden missing_feat_read = B_TRUE;
2389ad135b5dSChristopher Siden
2390ad135b5dSChristopher Siden if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
23912acef22dSMatthew Ahrens if (!spa_features_check(spa, B_TRUE,
239257221772SChristopher Siden unsup_feat, enabled_feat)) {
2393ad135b5dSChristopher Siden missing_feat_write = B_TRUE;
2394ad135b5dSChristopher Siden }
2395ad135b5dSChristopher Siden }
2396ad135b5dSChristopher Siden
239757221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info,
239857221772SChristopher Siden ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
239957221772SChristopher Siden
240057221772SChristopher Siden if (!nvlist_empty(unsup_feat)) {
240157221772SChristopher Siden fnvlist_add_nvlist(spa->spa_load_info,
240257221772SChristopher Siden ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
240357221772SChristopher Siden }
240457221772SChristopher Siden
240557221772SChristopher Siden fnvlist_free(enabled_feat);
240657221772SChristopher Siden fnvlist_free(unsup_feat);
2407ad135b5dSChristopher Siden
2408ad135b5dSChristopher Siden if (!missing_feat_read) {
2409ad135b5dSChristopher Siden fnvlist_add_boolean(spa->spa_load_info,
2410ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY);
2411ad135b5dSChristopher Siden }
2412ad135b5dSChristopher Siden
2413ad135b5dSChristopher Siden /*
2414ad135b5dSChristopher Siden * If the state is SPA_LOAD_TRYIMPORT, our objective is
2415ad135b5dSChristopher Siden * twofold: to determine whether the pool is available for
2416ad135b5dSChristopher Siden * import in read-write mode and (if it is not) whether the
2417ad135b5dSChristopher Siden * pool is available for import in read-only mode. If the pool
2418ad135b5dSChristopher Siden * is available for import in read-write mode, it is displayed
2419ad135b5dSChristopher Siden * as available in userland; if it is not available for import
2420ad135b5dSChristopher Siden * in read-only mode, it is displayed as unavailable in
2421ad135b5dSChristopher Siden * userland. If the pool is available for import in read-only
2422ad135b5dSChristopher Siden * mode but not read-write mode, it is displayed as unavailable
2423ad135b5dSChristopher Siden * in userland with a special note that the pool is actually
2424ad135b5dSChristopher Siden * available for open in read-only mode.
2425ad135b5dSChristopher Siden *
2426ad135b5dSChristopher Siden * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2427ad135b5dSChristopher Siden * missing a feature for write, we must first determine whether
2428ad135b5dSChristopher Siden * the pool can be opened read-only before returning to
2429ad135b5dSChristopher Siden * userland in order to know whether to display the
2430ad135b5dSChristopher Siden * abovementioned note.
2431ad135b5dSChristopher Siden */
2432ad135b5dSChristopher Siden if (missing_feat_read || (missing_feat_write &&
2433ad135b5dSChristopher Siden spa_writeable(spa))) {
2434ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2435ad135b5dSChristopher Siden ENOTSUP));
2436ad135b5dSChristopher Siden }
243743466aaeSMax Grossman
243843466aaeSMax Grossman /*
243943466aaeSMax Grossman * Load refcounts for ZFS features from disk into an in-memory
244043466aaeSMax Grossman * cache during SPA initialization.
244143466aaeSMax Grossman */
244243466aaeSMax Grossman for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
244343466aaeSMax Grossman uint64_t refcount;
244443466aaeSMax Grossman
244543466aaeSMax Grossman error = feature_get_refcount_from_disk(spa,
244643466aaeSMax Grossman &spa_feature_table[i], &refcount);
244743466aaeSMax Grossman if (error == 0) {
244843466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = refcount;
244943466aaeSMax Grossman } else if (error == ENOTSUP) {
245043466aaeSMax Grossman spa->spa_feat_refcount_cache[i] =
245143466aaeSMax Grossman SPA_FEATURE_DISABLED;
245243466aaeSMax Grossman } else {
245343466aaeSMax Grossman return (spa_vdev_err(rvd,
245443466aaeSMax Grossman VDEV_AUX_CORRUPT_DATA, EIO));
245543466aaeSMax Grossman }
245643466aaeSMax Grossman }
245743466aaeSMax Grossman }
245843466aaeSMax Grossman
245943466aaeSMax Grossman if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
246043466aaeSMax Grossman if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
24615d7b4d43SMatthew Ahrens &spa->spa_feat_enabled_txg_obj) != 0)
246243466aaeSMax Grossman return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
246343466aaeSMax Grossman }
2464ad135b5dSChristopher Siden
2465ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE;
2466ad135b5dSChristopher Siden error = dsl_pool_open(spa->spa_dsl_pool);
2467ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE;
2468ad135b5dSChristopher Siden if (error != 0)
2469ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2470ad135b5dSChristopher Siden
247188ecc943SGeorge Wilson if (!mosconfig) {
247288ecc943SGeorge Wilson uint64_t hostid;
2473871a9500SMark J Musante nvlist_t *policy = NULL, *nvconfig;
2474871a9500SMark J Musante
2475871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2476871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
247788ecc943SGeorge Wilson
247888ecc943SGeorge Wilson if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
247977650510SLin Ling ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
248095173954Sek110237 char *hostname;
248195173954Sek110237 unsigned long myhostid = 0;
248295173954Sek110237
248388ecc943SGeorge Wilson VERIFY(nvlist_lookup_string(nvconfig,
248495173954Sek110237 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
248595173954Sek110237
24865679c89fSjv227347 #ifdef _KERNEL
24875679c89fSjv227347 myhostid = zone_get_hostid(NULL);
24885679c89fSjv227347 #else /* _KERNEL */
24895679c89fSjv227347 /*
24905679c89fSjv227347 * We're emulating the system's hostid in userland, so
24915679c89fSjv227347 * we can't use zone_get_hostid().
24925679c89fSjv227347 */
249395173954Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
24945679c89fSjv227347 #endif /* _KERNEL */
249517194a52Slling if (hostid != 0 && myhostid != 0 &&
24965679c89fSjv227347 hostid != myhostid) {
2497871a9500SMark J Musante nvlist_free(nvconfig);
249895173954Sek110237 cmn_err(CE_WARN, "pool '%s' could not be "
249995173954Sek110237 "loaded as it was last accessed by "
250095173954Sek110237 "another system (host: %s hostid: 0x%lx). "
2501654b400cSJoshua M. Clulow "See: http://illumos.org/msg/ZFS-8000-EY",
2502e14bb325SJeff Bonwick spa_name(spa), hostname,
250395173954Sek110237 (unsigned long)hostid);
2504be6fd75aSMatthew Ahrens return (SET_ERROR(EBADF));
250595173954Sek110237 }
250695173954Sek110237 }
2507c8ee1847SVictor Latushkin if (nvlist_lookup_nvlist(spa->spa_config,
2508c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, &policy) == 0)
2509c8ee1847SVictor Latushkin VERIFY(nvlist_add_nvlist(nvconfig,
2510c8ee1847SVictor Latushkin ZPOOL_REWIND_POLICY, policy) == 0);
251195173954Sek110237
251288ecc943SGeorge Wilson spa_config_set(spa, nvconfig);
2513fa9e4066Sahrens spa_unload(spa);
2514fa9e4066Sahrens spa_deactivate(spa);
25158ad4d6ddSJeff Bonwick spa_activate(spa, orig_mode);
2516fa9e4066Sahrens
25171195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2518fa9e4066Sahrens }
2519fa9e4066Sahrens
252045818ee1SMatthew Ahrens /* Grab the secret checksum salt from the MOS. */
252145818ee1SMatthew Ahrens error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
252245818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT, 1,
252345818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes),
252445818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes);
252545818ee1SMatthew Ahrens if (error == ENOENT) {
252645818ee1SMatthew Ahrens /* Generate a new salt for subsequent use */
252745818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
252845818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes));
252945818ee1SMatthew Ahrens } else if (error != 0) {
253045818ee1SMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
253145818ee1SMatthew Ahrens }
253245818ee1SMatthew Ahrens
2533cde58dbcSMatthew Ahrens if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2534cde58dbcSMatthew Ahrens return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2535cde58dbcSMatthew Ahrens error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2536cde58dbcSMatthew Ahrens if (error != 0)
25371195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2538fa9e4066Sahrens
2539fa9e4066Sahrens /*
254099653d4eSeschrock * Load the bit that tells us to use the new accounting function
254199653d4eSeschrock * (raid-z deflation). If we have an older pool, this will not
254299653d4eSeschrock * be present.
254399653d4eSeschrock */
25441195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
25451195e687SMark J Musante if (error != 0 && error != ENOENT)
25461195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
254799653d4eSeschrock
25483f9d6ad7SLin Ling error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
25493f9d6ad7SLin Ling &spa->spa_creation_version);
25503f9d6ad7SLin Ling if (error != 0 && error != ENOENT)
25513f9d6ad7SLin Ling return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25523f9d6ad7SLin Ling
255399653d4eSeschrock /*
2554ea8dc4b6Seschrock * Load the persistent error log. If we have an older pool, this will
2555ea8dc4b6Seschrock * not be present.
2556fa9e4066Sahrens */
25571195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
25581195e687SMark J Musante if (error != 0 && error != ENOENT)
25591195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2560ea8dc4b6Seschrock
25611195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
25621195e687SMark J Musante &spa->spa_errlog_scrub);
25631195e687SMark J Musante if (error != 0 && error != ENOENT)
25641195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2565ea8dc4b6Seschrock
2566ea8dc4b6Seschrock /*
256706eeb2adSek110237 * Load the history object. If we have an older pool, this
256806eeb2adSek110237 * will not be present.
256906eeb2adSek110237 */
25701195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
25711195e687SMark J Musante if (error != 0 && error != ENOENT)
25721195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25731195e687SMark J Musante
25741195e687SMark J Musante /*
25751195e687SMark J Musante * If we're assembling the pool from the split-off vdevs of
25761195e687SMark J Musante * an existing pool, we don't want to attach the spares & cache
25771195e687SMark J Musante * devices.
25781195e687SMark J Musante */
257906eeb2adSek110237
258006eeb2adSek110237 /*
258199653d4eSeschrock * Load any hot spares for this pool.
258299653d4eSeschrock */
25831195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
25841195e687SMark J Musante if (error != 0 && error != ENOENT)
25851195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
25861195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2587e7437265Sahrens ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2588fa94a07fSbrendan if (load_nvlist(spa, spa->spa_spares.sav_object,
25891195e687SMark J Musante &spa->spa_spares.sav_config) != 0)
25901195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
259199653d4eSeschrock
2592e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
259399653d4eSeschrock spa_load_spares(spa);
2594e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
25951195e687SMark J Musante } else if (error == 0) {
25961195e687SMark J Musante spa->spa_spares.sav_sync = B_TRUE;
259799653d4eSeschrock }
259899653d4eSeschrock
2599fa94a07fSbrendan /*
2600fa94a07fSbrendan * Load any level 2 ARC devices for this pool.
2601fa94a07fSbrendan */
26021195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2603fa94a07fSbrendan &spa->spa_l2cache.sav_object);
26041195e687SMark J Musante if (error != 0 && error != ENOENT)
26051195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
26061195e687SMark J Musante if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2607fa94a07fSbrendan ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2608fa94a07fSbrendan if (load_nvlist(spa, spa->spa_l2cache.sav_object,
26091195e687SMark J Musante &spa->spa_l2cache.sav_config) != 0)
26101195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2611fa94a07fSbrendan
2612e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2613fa94a07fSbrendan spa_load_l2cache(spa);
2614e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
26151195e687SMark J Musante } else if (error == 0) {
26161195e687SMark J Musante spa->spa_l2cache.sav_sync = B_TRUE;
2617fa94a07fSbrendan }
2618fa94a07fSbrendan
2619990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2620ecd6cf80Smarks
26211195e687SMark J Musante error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
26221195e687SMark J Musante if (error && error != ENOENT)
26231195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2624b1b8ab34Slling
2625b1b8ab34Slling if (error == 0) {
26261195e687SMark J Musante uint64_t autoreplace;
26271195e687SMark J Musante
26281195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
26291195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
26301195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
26311195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
26321195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
26331195e687SMark J Musante spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
26341195e687SMark J Musante &spa->spa_dedup_ditto);
26351195e687SMark J Musante
2636b693757aSEric Schrock spa->spa_autoreplace = (autoreplace != 0);
2637b1b8ab34Slling }
2638b1b8ab34Slling
263999653d4eSeschrock /*
26403d7072f8Seschrock * If the 'autoreplace' property is set, then post a resource notifying
26413d7072f8Seschrock * the ZFS DE that it should not issue any faults for unopenable
26423d7072f8Seschrock * devices. We also iterate over the vdevs, and post a sysevent for any
26433d7072f8Seschrock * unopenable vdevs so that the normal autoreplace handler can take
26443d7072f8Seschrock * over.
26453d7072f8Seschrock */
2646b693757aSEric Schrock if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
26473d7072f8Seschrock spa_check_removed(spa->spa_root_vdev);
2648b693757aSEric Schrock /*
2649b693757aSEric Schrock * For the import case, this is done in spa_import(), because
2650b693757aSEric Schrock * at this point we're using the spare definitions from
2651b693757aSEric Schrock * the MOS config, not necessarily from the userland config.
2652b693757aSEric Schrock */
2653b693757aSEric Schrock if (state != SPA_LOAD_IMPORT) {
2654b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares);
2655b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache);
2656b693757aSEric Schrock }
2657b693757aSEric Schrock }
26583d7072f8Seschrock
26593d7072f8Seschrock /*
2660560e6e96Seschrock * Load the vdev state for all toplevel vdevs.
2661ea8dc4b6Seschrock */
2662560e6e96Seschrock vdev_load(rvd);
2663fa9e4066Sahrens
2664fa9e4066Sahrens /*
2665fa9e4066Sahrens * Propagate the leaf DTLs we just loaded all the way up the tree.
2666fa9e4066Sahrens */
2667e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2668fa9e4066Sahrens vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2669e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
2670fa9e4066Sahrens
2671fa9e4066Sahrens /*
2672b24ab676SJeff Bonwick * Load the DDTs (dedup tables).
2673b24ab676SJeff Bonwick */
2674b24ab676SJeff Bonwick error = ddt_load(spa);
26751195e687SMark J Musante if (error != 0)
26761195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2677b24ab676SJeff Bonwick
2678485bbbf5SGeorge Wilson spa_update_dspace(spa);
2679485bbbf5SGeorge Wilson
2680b24ab676SJeff Bonwick /*
26814b964adaSGeorge Wilson * Validate the config, using the MOS config to fill in any
26824b964adaSGeorge Wilson * information which might be missing. If we fail to validate
26834b964adaSGeorge Wilson * the config then declare the pool unfit for use. If we're
26844b964adaSGeorge Wilson * assembling a pool from a split, the log is not transferred
26854b964adaSGeorge Wilson * over.
2686b24ab676SJeff Bonwick */
26871195e687SMark J Musante if (type != SPA_IMPORT_ASSEMBLE) {
2688871a9500SMark J Musante nvlist_t *nvconfig;
2689871a9500SMark J Musante
2690871a9500SMark J Musante if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2691871a9500SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2692871a9500SMark J Musante
26934b964adaSGeorge Wilson if (!spa_config_valid(spa, nvconfig)) {
2694b24ab676SJeff Bonwick nvlist_free(nvconfig);
26954b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
26964b964adaSGeorge Wilson ENXIO));
26974b964adaSGeorge Wilson }
26984b964adaSGeorge Wilson nvlist_free(nvconfig);
26994b964adaSGeorge Wilson
27004b964adaSGeorge Wilson /*
2701ad135b5dSChristopher Siden * Now that we've validated the config, check the state of the
27024b964adaSGeorge Wilson * root vdev. If it can't be opened, it indicates one or
27034b964adaSGeorge Wilson * more toplevel vdevs are faulted.
27044b964adaSGeorge Wilson */
27054b964adaSGeorge Wilson if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2706be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO));
2707b24ab676SJeff Bonwick
270823367a2fSMatthew Ahrens if (spa_writeable(spa) && spa_check_logs(spa)) {
27091195e687SMark J Musante *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
27101195e687SMark J Musante return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
27111195e687SMark J Musante }
2712b24ab676SJeff Bonwick }
2713b24ab676SJeff Bonwick
2714ad135b5dSChristopher Siden if (missing_feat_write) {
2715ad135b5dSChristopher Siden ASSERT(state == SPA_LOAD_TRYIMPORT);
2716ad135b5dSChristopher Siden
2717ad135b5dSChristopher Siden /*
2718ad135b5dSChristopher Siden * At this point, we know that we can open the pool in
2719ad135b5dSChristopher Siden * read-only mode but not read-write mode. We now have enough
2720ad135b5dSChristopher Siden * information and can return to userland.
2721ad135b5dSChristopher Siden */
2722ad135b5dSChristopher Siden return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2723ad135b5dSChristopher Siden }
2724ad135b5dSChristopher Siden
27254b964adaSGeorge Wilson /*
27264b964adaSGeorge Wilson * We've successfully opened the pool, verify that we're ready
27274b964adaSGeorge Wilson * to start pushing transactions.
27284b964adaSGeorge Wilson */
27294b964adaSGeorge Wilson if (state != SPA_LOAD_TRYIMPORT) {
27304b964adaSGeorge Wilson if (error = spa_load_verify(spa))
27314b964adaSGeorge Wilson return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
27324b964adaSGeorge Wilson error));
27334b964adaSGeorge Wilson }
27344b964adaSGeorge Wilson
2735468c413aSTim Haley if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2736468c413aSTim Haley spa->spa_load_max_txg == UINT64_MAX)) {
27370373e76bSbonwick dmu_tx_t *tx;
27380373e76bSbonwick int need_update = B_FALSE;
273912380e1eSArne Jansen dsl_pool_t *dp = spa_get_dsl(spa);
27408ad4d6ddSJeff Bonwick
27418ad4d6ddSJeff Bonwick ASSERT(state != SPA_LOAD_TRYIMPORT);
27420373e76bSbonwick
2743fa9e4066Sahrens /*
27440373e76bSbonwick * Claim log blocks that haven't been committed yet.
2745fa9e4066Sahrens * This must all happen in a single txg.
2746b24ab676SJeff Bonwick * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2747b24ab676SJeff Bonwick * invoked from zil_claim_log_block()'s i/o done callback.
2748468c413aSTim Haley * Price of rollback is that we abandon the log.
2749fa9e4066Sahrens */
2750b24ab676SJeff Bonwick spa->spa_claiming = B_TRUE;
2751b24ab676SJeff Bonwick
275212380e1eSArne Jansen tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
275312380e1eSArne Jansen (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
27540b69c2f0Sahrens zil_claim, tx, DS_FIND_CHILDREN);
2755fa9e4066Sahrens dmu_tx_commit(tx);
2756fa9e4066Sahrens
2757b24ab676SJeff Bonwick spa->spa_claiming = B_FALSE;
2758b24ab676SJeff Bonwick
27591195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_GOOD);
2760fa9e4066Sahrens spa->spa_sync_on = B_TRUE;
2761fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool);
2762fa9e4066Sahrens
2763fa9e4066Sahrens /*
2764b24ab676SJeff Bonwick * Wait for all claims to sync. We sync up to the highest
2765b24ab676SJeff Bonwick * claimed log block birth time so that claimed log blocks
2766b24ab676SJeff Bonwick * don't appear to be from the future. spa_claim_max_txg
2767b24ab676SJeff Bonwick * will have been set for us by either zil_check_log_chain()
2768b24ab676SJeff Bonwick * (invoked from spa_check_logs()) or zil_claim() above.
2769fa9e4066Sahrens */
2770b24ab676SJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
27710e34b6a7Sbonwick
27720e34b6a7Sbonwick /*
27730373e76bSbonwick * If the config cache is stale, or we have uninitialized
27740373e76bSbonwick * metaslabs (see spa_vdev_add()), then update the config.
2775bc758434SLin Ling *
27764b964adaSGeorge Wilson * If this is a verbatim import, trust the current
2777bc758434SLin Ling * in-core spa_config and update the disk labels.
27780e34b6a7Sbonwick */
27790373e76bSbonwick if (config_cache_txg != spa->spa_config_txg ||
27804b964adaSGeorge Wilson state == SPA_LOAD_IMPORT ||
27814b964adaSGeorge Wilson state == SPA_LOAD_RECOVER ||
27824b964adaSGeorge Wilson (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
27830373e76bSbonwick need_update = B_TRUE;
27840373e76bSbonwick
27858ad4d6ddSJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++)
27860373e76bSbonwick if (rvd->vdev_child[c]->vdev_ms_array == 0)
27870373e76bSbonwick need_update = B_TRUE;
27880e34b6a7Sbonwick
27890e34b6a7Sbonwick /*
27900373e76bSbonwick * Update the config cache asychronously in case we're the
27910373e76bSbonwick * root pool, in which case the config cache isn't writable yet.
27920e34b6a7Sbonwick */
27930373e76bSbonwick if (need_update)
27940373e76bSbonwick spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
27958ad4d6ddSJeff Bonwick
27968ad4d6ddSJeff Bonwick /*
27978ad4d6ddSJeff Bonwick * Check all DTLs to see if anything needs resilvering.
27988ad4d6ddSJeff Bonwick */
27993f9d6ad7SLin Ling if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
28003f9d6ad7SLin Ling vdev_resilver_needed(rvd, NULL, NULL))
28018ad4d6ddSJeff Bonwick spa_async_request(spa, SPA_ASYNC_RESILVER);
2802503ad85cSMatthew Ahrens
2803503ad85cSMatthew Ahrens /*
28044445fffbSMatthew Ahrens * Log the fact that we booted up (so that we can detect if
28054445fffbSMatthew Ahrens * we rebooted in the middle of an operation).
28064445fffbSMatthew Ahrens */
28074445fffbSMatthew Ahrens spa_history_log_version(spa, "open");
28084445fffbSMatthew Ahrens
28094445fffbSMatthew Ahrens /*
2810503ad85cSMatthew Ahrens * Delete any inconsistent datasets.
2811503ad85cSMatthew Ahrens */
2812503ad85cSMatthew Ahrens (void) dmu_objset_find(spa_name(spa),
2813503ad85cSMatthew Ahrens dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2814ca45db41SChris Kirby
2815ca45db41SChris Kirby /*
2816ca45db41SChris Kirby * Clean up any stale temporary dataset userrefs.
2817ca45db41SChris Kirby */
2818ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2819fa9e4066Sahrens }
2820fa9e4066Sahrens
2821c9e5c7a7SSaso Kiselkov spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2822c9e5c7a7SSaso Kiselkov
28231195e687SMark J Musante return (0);
2824fa9e4066Sahrens }
2825fa9e4066Sahrens
2826468c413aSTim Haley static int
spa_load_retry(spa_t * spa,spa_load_state_t state,int mosconfig)2827468c413aSTim Haley spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2828468c413aSTim Haley {
2829f9af39baSGeorge Wilson int mode = spa->spa_mode;
2830f9af39baSGeorge Wilson
2831468c413aSTim Haley spa_unload(spa);
2832468c413aSTim Haley spa_deactivate(spa);
2833468c413aSTim Haley
2834e42d2059SMatthew Ahrens spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2835468c413aSTim Haley
2836f9af39baSGeorge Wilson spa_activate(spa, mode);
2837468c413aSTim Haley spa_async_suspend(spa);
2838468c413aSTim Haley
28391195e687SMark J Musante return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2840468c413aSTim Haley }
2841468c413aSTim Haley
2842ad135b5dSChristopher Siden /*
2843ad135b5dSChristopher Siden * If spa_load() fails this function will try loading prior txg's. If
2844ad135b5dSChristopher Siden * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2845ad135b5dSChristopher Siden * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2846ad135b5dSChristopher Siden * function will not rewind the pool and will return the same error as
2847ad135b5dSChristopher Siden * spa_load().
2848ad135b5dSChristopher Siden */
2849468c413aSTim Haley static int
spa_load_best(spa_t * spa,spa_load_state_t state,int mosconfig,uint64_t max_request,int rewind_flags)2850468c413aSTim Haley spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2851c8ee1847SVictor Latushkin uint64_t max_request, int rewind_flags)
2852468c413aSTim Haley {
2853ad135b5dSChristopher Siden nvlist_t *loadinfo = NULL;
2854468c413aSTim Haley nvlist_t *config = NULL;
2855468c413aSTim Haley int load_error, rewind_error;
2856c8ee1847SVictor Latushkin uint64_t safe_rewind_txg;
2857468c413aSTim Haley uint64_t min_txg;
2858468c413aSTim Haley
2859a33cae98STim Haley if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2860468c413aSTim Haley spa->spa_load_max_txg = spa->spa_load_txg;
28611195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR);
2862a33cae98STim Haley } else {
2863468c413aSTim Haley spa->spa_load_max_txg = max_request;
2864e42d2059SMatthew Ahrens if (max_request != UINT64_MAX)
2865e42d2059SMatthew Ahrens spa->spa_extreme_rewind = B_TRUE;
2866a33cae98STim Haley }
2867468c413aSTim Haley
28681195e687SMark J Musante load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
28691195e687SMark J Musante mosconfig);
2870468c413aSTim Haley if (load_error == 0)
2871468c413aSTim Haley return (0);
2872468c413aSTim Haley
2873468c413aSTim Haley if (spa->spa_root_vdev != NULL)
2874468c413aSTim Haley config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2875468c413aSTim Haley
2876468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2877468c413aSTim Haley spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2878468c413aSTim Haley
2879c8ee1847SVictor Latushkin if (rewind_flags & ZPOOL_NEVER_REWIND) {
2880468c413aSTim Haley nvlist_free(config);
2881468c413aSTim Haley return (load_error);
2882468c413aSTim Haley }
2883468c413aSTim Haley
2884ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) {
2885468c413aSTim Haley /* Price of rolling back is discarding txgs, including log */
28861195e687SMark J Musante spa_set_log_state(spa, SPA_LOG_CLEAR);
2887ad135b5dSChristopher Siden } else {
2888ad135b5dSChristopher Siden /*
2889ad135b5dSChristopher Siden * If we aren't rolling back save the load info from our first
2890ad135b5dSChristopher Siden * import attempt so that we can restore it after attempting
2891ad135b5dSChristopher Siden * to rewind.
2892ad135b5dSChristopher Siden */
2893ad135b5dSChristopher Siden loadinfo = spa->spa_load_info;
2894ad135b5dSChristopher Siden spa->spa_load_info = fnvlist_alloc();
2895ad135b5dSChristopher Siden }
2896468c413aSTim Haley
2897c8ee1847SVictor Latushkin spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2898c8ee1847SVictor Latushkin safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2899c8ee1847SVictor Latushkin min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2900c8ee1847SVictor Latushkin TXG_INITIAL : safe_rewind_txg;
2901468c413aSTim Haley
2902c8ee1847SVictor Latushkin /*
2903c8ee1847SVictor Latushkin * Continue as long as we're finding errors, we're still within
2904c8ee1847SVictor Latushkin * the acceptable rewind range, and we're still finding uberblocks
2905c8ee1847SVictor Latushkin */
2906c8ee1847SVictor Latushkin while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2907c8ee1847SVictor Latushkin spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2908c8ee1847SVictor Latushkin if (spa->spa_load_max_txg < safe_rewind_txg)
2909468c413aSTim Haley spa->spa_extreme_rewind = B_TRUE;
2910468c413aSTim Haley rewind_error = spa_load_retry(spa, state, mosconfig);
2911468c413aSTim Haley }
2912468c413aSTim Haley
2913468c413aSTim Haley spa->spa_extreme_rewind = B_FALSE;
2914468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX;
2915468c413aSTim Haley
2916468c413aSTim Haley if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2917468c413aSTim Haley spa_config_set(spa, config);
2918468c413aSTim Haley
2919ad135b5dSChristopher Siden if (state == SPA_LOAD_RECOVER) {
2920ad135b5dSChristopher Siden ASSERT3P(loadinfo, ==, NULL);
2921ad135b5dSChristopher Siden return (rewind_error);
2922ad135b5dSChristopher Siden } else {
2923ad135b5dSChristopher Siden /* Store the rewind info as part of the initial load info */
2924ad135b5dSChristopher Siden fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2925ad135b5dSChristopher Siden spa->spa_load_info);
2926ad135b5dSChristopher Siden
2927ad135b5dSChristopher Siden /* Restore the initial load info */
2928ad135b5dSChristopher Siden fnvlist_free(spa->spa_load_info);
2929ad135b5dSChristopher Siden spa->spa_load_info = loadinfo;
2930ad135b5dSChristopher Siden
2931ad135b5dSChristopher Siden return (load_error);
2932ad135b5dSChristopher Siden }
2933468c413aSTim Haley }
2934468c413aSTim Haley
2935fa9e4066Sahrens /*
2936fa9e4066Sahrens * Pool Open/Import
2937fa9e4066Sahrens *
2938fa9e4066Sahrens * The import case is identical to an open except that the configuration is sent
2939fa9e4066Sahrens * down from userland, instead of grabbed from the configuration cache. For the
2940fa9e4066Sahrens * case of an open, the pool configuration will exist in the
29413d7072f8Seschrock * POOL_STATE_UNINITIALIZED state.
2942fa9e4066Sahrens *
2943fa9e4066Sahrens * The stats information (gen/count/ustats) is used to gather vdev statistics at
2944fa9e4066Sahrens * the same time open the pool, without having to keep around the spa_t in some
2945fa9e4066Sahrens * ambiguous state.
2946fa9e4066Sahrens */
2947fa9e4066Sahrens static int
spa_open_common(const char * pool,spa_t ** spapp,void * tag,nvlist_t * nvpolicy,nvlist_t ** config)2948468c413aSTim Haley spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2949468c413aSTim Haley nvlist_t **config)
2950fa9e4066Sahrens {
2951fa9e4066Sahrens spa_t *spa;
29524b964adaSGeorge Wilson spa_load_state_t state = SPA_LOAD_OPEN;
2953fa9e4066Sahrens int error;
2954fa9e4066Sahrens int locked = B_FALSE;
2955fa9e4066Sahrens
2956fa9e4066Sahrens *spapp = NULL;
2957fa9e4066Sahrens
2958fa9e4066Sahrens /*
2959fa9e4066Sahrens * As disgusting as this is, we need to support recursive calls to this
2960fa9e4066Sahrens * function because dsl_dir_open() is called during spa_load(), and ends
2961fa9e4066Sahrens * up calling spa_open() again. The real fix is to figure out how to
2962fa9e4066Sahrens * avoid dsl_dir_open() calling this in the first place.
2963fa9e4066Sahrens */
2964fa9e4066Sahrens if (mutex_owner(&spa_namespace_lock) != curthread) {
2965fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
2966fa9e4066Sahrens locked = B_TRUE;
2967fa9e4066Sahrens }
2968fa9e4066Sahrens
2969fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) {
2970fa9e4066Sahrens if (locked)
2971fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
2972be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
2973fa9e4066Sahrens }
2974468c413aSTim Haley
29754b44c88cSTim Haley if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
29764b44c88cSTim Haley zpool_rewind_policy_t policy;
29774b44c88cSTim Haley
29784b44c88cSTim Haley zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
29794b44c88cSTim Haley &policy);
2980c8ee1847SVictor Latushkin if (policy.zrp_request & ZPOOL_DO_REWIND)
2981c8ee1847SVictor Latushkin state = SPA_LOAD_RECOVER;
2982c8ee1847SVictor Latushkin
29838ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global);
2984fa9e4066Sahrens
2985468c413aSTim Haley if (state != SPA_LOAD_RECOVER)
2986468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2987468c413aSTim Haley
2988468c413aSTim Haley error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2989c8ee1847SVictor Latushkin policy.zrp_request);
2990fa9e4066Sahrens
2991fa9e4066Sahrens if (error == EBADF) {
2992fa9e4066Sahrens /*
2993560e6e96Seschrock * If vdev_validate() returns failure (indicated by
2994560e6e96Seschrock * EBADF), it indicates that one of the vdevs indicates
2995560e6e96Seschrock * that the pool has been exported or destroyed. If
2996560e6e96Seschrock * this is the case, the config cache is out of sync and
2997560e6e96Seschrock * we should remove the pool from the namespace.
2998fa9e4066Sahrens */
2999fa9e4066Sahrens spa_unload(spa);
3000fa9e4066Sahrens spa_deactivate(spa);
3001c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE);
3002fa9e4066Sahrens spa_remove(spa);
3003fa9e4066Sahrens if (locked)
3004fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
3005be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
3006ea8dc4b6Seschrock }
3007ea8dc4b6Seschrock
3008ea8dc4b6Seschrock if (error) {
3009fa9e4066Sahrens /*
3010fa9e4066Sahrens * We can't open the pool, but we still have useful
3011fa9e4066Sahrens * information: the state of each vdev after the
3012fa9e4066Sahrens * attempted vdev_open(). Return this to the user.
3013fa9e4066Sahrens */
30144b964adaSGeorge Wilson if (config != NULL && spa->spa_config) {
3015468c413aSTim Haley VERIFY(nvlist_dup(spa->spa_config, config,
3016468c413aSTim Haley KM_SLEEP) == 0);
30174b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config,
30184b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO,
30194b964adaSGeorge Wilson spa->spa_load_info) == 0);
30204b964adaSGeorge Wilson }
3021fa9e4066Sahrens spa_unload(spa);
3022fa9e4066Sahrens spa_deactivate(spa);
3023468c413aSTim Haley spa->spa_last_open_failed = error;
3024fa9e4066Sahrens if (locked)
3025fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
3026fa9e4066Sahrens *spapp = NULL;
3027fa9e4066Sahrens return (error);
3028fa9e4066Sahrens }
3029fa9e4066Sahrens }
3030fa9e4066Sahrens
3031fa9e4066Sahrens spa_open_ref(spa, tag);
30323d7072f8Seschrock
3033468c413aSTim Haley if (config != NULL)
3034468c413aSTim Haley *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3035468c413aSTim Haley
30364b964adaSGeorge Wilson /*
30374b964adaSGeorge Wilson * If we've recovered the pool, pass back any information we
30384b964adaSGeorge Wilson * gathered while doing the load.
30394b964adaSGeorge Wilson */
30404b964adaSGeorge Wilson if (state == SPA_LOAD_RECOVER) {
30414b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
30424b964adaSGeorge Wilson spa->spa_load_info) == 0);
30434b964adaSGeorge Wilson }
30444b964adaSGeorge Wilson
3045a33cae98STim Haley if (locked) {
3046a33cae98STim Haley spa->spa_last_open_failed = 0;
3047468c413aSTim Haley spa->spa_last_ubsync_txg = 0;
3048468c413aSTim Haley spa->spa_load_txg = 0;
3049fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
3050a33cae98STim Haley }
3051fa9e4066Sahrens
3052fa9e4066Sahrens *spapp = spa;
3053fa9e4066Sahrens
3054fa9e4066Sahrens return (0);
3055fa9e4066Sahrens }
3056fa9e4066Sahrens
3057fa9e4066Sahrens int
spa_open_rewind(const char * name,spa_t ** spapp,void * tag,nvlist_t * policy,nvlist_t ** config)3058468c413aSTim Haley spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3059468c413aSTim Haley nvlist_t **config)
3060468c413aSTim Haley {
3061468c413aSTim Haley return (spa_open_common(name, spapp, tag, policy, config));
3062468c413aSTim Haley }
3063468c413aSTim Haley
3064468c413aSTim Haley int
spa_open(const char * name,spa_t ** spapp,void * tag)3065fa9e4066Sahrens spa_open(const char *name, spa_t **spapp, void *tag)
3066fa9e4066Sahrens {
3067468c413aSTim Haley return (spa_open_common(name, spapp, tag, NULL, NULL));
3068fa9e4066Sahrens }
3069fa9e4066Sahrens
3070ea8dc4b6Seschrock /*
3071ea8dc4b6Seschrock * Lookup the given spa_t, incrementing the inject count in the process,
3072ea8dc4b6Seschrock * preventing it from being exported or destroyed.
3073ea8dc4b6Seschrock */
3074ea8dc4b6Seschrock spa_t *
spa_inject_addref(char * name)3075ea8dc4b6Seschrock spa_inject_addref(char *name)
3076ea8dc4b6Seschrock {
3077ea8dc4b6Seschrock spa_t *spa;
3078ea8dc4b6Seschrock
3079ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock);
3080ea8dc4b6Seschrock if ((spa = spa_lookup(name)) == NULL) {
3081ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
3082ea8dc4b6Seschrock return (NULL);
3083ea8dc4b6Seschrock }
3084ea8dc4b6Seschrock spa->spa_inject_ref++;
3085ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
3086ea8dc4b6Seschrock
3087ea8dc4b6Seschrock return (spa);
3088ea8dc4b6Seschrock }
3089ea8dc4b6Seschrock
3090ea8dc4b6Seschrock void
spa_inject_delref(spa_t * spa)3091ea8dc4b6Seschrock spa_inject_delref(spa_t *spa)
3092ea8dc4b6Seschrock {
3093ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock);
3094ea8dc4b6Seschrock spa->spa_inject_ref--;
3095ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
3096ea8dc4b6Seschrock }
3097ea8dc4b6Seschrock
3098fa94a07fSbrendan /*
3099fa94a07fSbrendan * Add spares device information to the nvlist.
3100fa94a07fSbrendan */
310199653d4eSeschrock static void
spa_add_spares(spa_t * spa,nvlist_t * config)310299653d4eSeschrock spa_add_spares(spa_t *spa, nvlist_t *config)
310399653d4eSeschrock {
310499653d4eSeschrock nvlist_t **spares;
310599653d4eSeschrock uint_t i, nspares;
310699653d4eSeschrock nvlist_t *nvroot;
310799653d4eSeschrock uint64_t guid;
310899653d4eSeschrock vdev_stat_t *vs;
310999653d4eSeschrock uint_t vsc;
311039c23413Seschrock uint64_t pool;
311199653d4eSeschrock
31126809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
31136809eb4eSEric Schrock
3114fa94a07fSbrendan if (spa->spa_spares.sav_count == 0)
311599653d4eSeschrock return;
311699653d4eSeschrock
311799653d4eSeschrock VERIFY(nvlist_lookup_nvlist(config,
311899653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3119fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
312099653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
312199653d4eSeschrock if (nspares != 0) {
312299653d4eSeschrock VERIFY(nvlist_add_nvlist_array(nvroot,
312399653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
312499653d4eSeschrock VERIFY(nvlist_lookup_nvlist_array(nvroot,
312599653d4eSeschrock ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
312699653d4eSeschrock
312799653d4eSeschrock /*
312899653d4eSeschrock * Go through and find any spares which have since been
312999653d4eSeschrock * repurposed as an active spare. If this is the case, update
313099653d4eSeschrock * their status appropriately.
313199653d4eSeschrock */
313299653d4eSeschrock for (i = 0; i < nspares; i++) {
313399653d4eSeschrock VERIFY(nvlist_lookup_uint64(spares[i],
313499653d4eSeschrock ZPOOL_CONFIG_GUID, &guid) == 0);
313589a89ebfSlling if (spa_spare_exists(guid, &pool, NULL) &&
313689a89ebfSlling pool != 0ULL) {
313799653d4eSeschrock VERIFY(nvlist_lookup_uint64_array(
31383f9d6ad7SLin Ling spares[i], ZPOOL_CONFIG_VDEV_STATS,
313999653d4eSeschrock (uint64_t **)&vs, &vsc) == 0);
314099653d4eSeschrock vs->vs_state = VDEV_STATE_CANT_OPEN;
314199653d4eSeschrock vs->vs_aux = VDEV_AUX_SPARED;
314299653d4eSeschrock }
314399653d4eSeschrock }
314499653d4eSeschrock }
314599653d4eSeschrock }
314699653d4eSeschrock
3147fa94a07fSbrendan /*
3148fa94a07fSbrendan * Add l2cache device information to the nvlist, including vdev stats.
3149fa94a07fSbrendan */
3150fa94a07fSbrendan static void
spa_add_l2cache(spa_t * spa,nvlist_t * config)3151fa94a07fSbrendan spa_add_l2cache(spa_t *spa, nvlist_t *config)
3152fa94a07fSbrendan {
3153fa94a07fSbrendan nvlist_t **l2cache;
3154fa94a07fSbrendan uint_t i, j, nl2cache;
3155fa94a07fSbrendan nvlist_t *nvroot;
3156fa94a07fSbrendan uint64_t guid;
3157fa94a07fSbrendan vdev_t *vd;
3158fa94a07fSbrendan vdev_stat_t *vs;
3159fa94a07fSbrendan uint_t vsc;
3160fa94a07fSbrendan
31616809eb4eSEric Schrock ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
31626809eb4eSEric Schrock
3163fa94a07fSbrendan if (spa->spa_l2cache.sav_count == 0)
3164fa94a07fSbrendan return;
3165fa94a07fSbrendan
3166fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist(config,
3167fa94a07fSbrendan ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3168fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3169fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3170fa94a07fSbrendan if (nl2cache != 0) {
3171fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot,
3172fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3173fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(nvroot,
3174fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3175fa94a07fSbrendan
3176fa94a07fSbrendan /*
3177fa94a07fSbrendan * Update level 2 cache device stats.
3178fa94a07fSbrendan */
3179fa94a07fSbrendan
3180fa94a07fSbrendan for (i = 0; i < nl2cache; i++) {
3181fa94a07fSbrendan VERIFY(nvlist_lookup_uint64(l2cache[i],
3182fa94a07fSbrendan ZPOOL_CONFIG_GUID, &guid) == 0);
3183fa94a07fSbrendan
3184fa94a07fSbrendan vd = NULL;
3185fa94a07fSbrendan for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3186fa94a07fSbrendan if (guid ==
3187fa94a07fSbrendan spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3188fa94a07fSbrendan vd = spa->spa_l2cache.sav_vdevs[j];
3189fa94a07fSbrendan break;
3190fa94a07fSbrendan }
3191fa94a07fSbrendan }
3192fa94a07fSbrendan ASSERT(vd != NULL);
3193fa94a07fSbrendan
3194fa94a07fSbrendan VERIFY(nvlist_lookup_uint64_array(l2cache[i],
31953f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
31963f9d6ad7SLin Ling == 0);
3197fa94a07fSbrendan vdev_get_stats(vd, vs);
3198fa94a07fSbrendan }
3199fa94a07fSbrendan }
3200fa94a07fSbrendan }
3201fa94a07fSbrendan
3202ad135b5dSChristopher Siden static void
spa_add_feature_stats(spa_t * spa,nvlist_t * config)3203ad135b5dSChristopher Siden spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3204ad135b5dSChristopher Siden {
3205ad135b5dSChristopher Siden nvlist_t *features;
3206ad135b5dSChristopher Siden zap_cursor_t zc;
3207ad135b5dSChristopher Siden zap_attribute_t za;
3208ad135b5dSChristopher Siden
3209ad135b5dSChristopher Siden ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3210ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3211ad135b5dSChristopher Siden
3212ad135b5dSChristopher Siden if (spa->spa_feat_for_read_obj != 0) {
3213ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset,
3214ad135b5dSChristopher Siden spa->spa_feat_for_read_obj);
3215ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0;
3216ad135b5dSChristopher Siden zap_cursor_advance(&zc)) {
3217ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3218ad135b5dSChristopher Siden za.za_num_integers == 1);
3219b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3220ad135b5dSChristopher Siden za.za_first_integer));
3221ad135b5dSChristopher Siden }
3222ad135b5dSChristopher Siden zap_cursor_fini(&zc);
3223ad135b5dSChristopher Siden }
3224ad135b5dSChristopher Siden
3225ad135b5dSChristopher Siden if (spa->spa_feat_for_write_obj != 0) {
3226ad135b5dSChristopher Siden for (zap_cursor_init(&zc, spa->spa_meta_objset,
3227ad135b5dSChristopher Siden spa->spa_feat_for_write_obj);
3228ad135b5dSChristopher Siden zap_cursor_retrieve(&zc, &za) == 0;
3229ad135b5dSChristopher Siden zap_cursor_advance(&zc)) {
3230ad135b5dSChristopher Siden ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3231ad135b5dSChristopher Siden za.za_num_integers == 1);
3232b420f3adSRichard Lowe VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3233ad135b5dSChristopher Siden za.za_first_integer));
3234ad135b5dSChristopher Siden }
3235ad135b5dSChristopher Siden zap_cursor_fini(&zc);
3236ad135b5dSChristopher Siden }
3237ad135b5dSChristopher Siden
3238ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3239ad135b5dSChristopher Siden features) == 0);
3240ad135b5dSChristopher Siden nvlist_free(features);
3241ad135b5dSChristopher Siden }
3242ad135b5dSChristopher Siden
3243fa9e4066Sahrens int
spa_get_stats(const char * name,nvlist_t ** config,char * altroot,size_t buflen)3244ad135b5dSChristopher Siden spa_get_stats(const char *name, nvlist_t **config,
3245ad135b5dSChristopher Siden char *altroot, size_t buflen)
3246fa9e4066Sahrens {
3247fa9e4066Sahrens int error;
3248fa9e4066Sahrens spa_t *spa;
3249fa9e4066Sahrens
3250fa9e4066Sahrens *config = NULL;
3251468c413aSTim Haley error = spa_open_common(name, &spa, FTAG, NULL, config);
3252fa9e4066Sahrens
32536809eb4eSEric Schrock if (spa != NULL) {
32546809eb4eSEric Schrock /*
32556809eb4eSEric Schrock * This still leaves a window of inconsistency where the spares
32566809eb4eSEric Schrock * or l2cache devices could change and the config would be
32576809eb4eSEric Schrock * self-inconsistent.
32586809eb4eSEric Schrock */
32596809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
32606809eb4eSEric Schrock
32616809eb4eSEric Schrock if (*config != NULL) {
326211027bc7STim Haley uint64_t loadtimes[2];
326311027bc7STim Haley
326411027bc7STim Haley loadtimes[0] = spa->spa_loaded_ts.tv_sec;
326511027bc7STim Haley loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
326611027bc7STim Haley VERIFY(nvlist_add_uint64_array(*config,
326711027bc7STim Haley ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
326811027bc7STim Haley
32696809eb4eSEric Schrock VERIFY(nvlist_add_uint64(*config,
32706809eb4eSEric Schrock ZPOOL_CONFIG_ERRCOUNT,
3271ea8dc4b6Seschrock spa_get_errlog_size(spa)) == 0);
3272ea8dc4b6Seschrock
3273e14bb325SJeff Bonwick if (spa_suspended(spa))
3274e14bb325SJeff Bonwick VERIFY(nvlist_add_uint64(*config,
32756809eb4eSEric Schrock ZPOOL_CONFIG_SUSPENDED,
32766809eb4eSEric Schrock spa->spa_failmode) == 0);
3277e14bb325SJeff Bonwick
327899653d4eSeschrock spa_add_spares(spa, *config);
3279fa94a07fSbrendan spa_add_l2cache(spa, *config);
3280ad135b5dSChristopher Siden spa_add_feature_stats(spa, *config);
328199653d4eSeschrock }
32826809eb4eSEric Schrock }
328399653d4eSeschrock
3284ea8dc4b6Seschrock /*
3285ea8dc4b6Seschrock * We want to get the alternate root even for faulted pools, so we cheat
3286ea8dc4b6Seschrock * and call spa_lookup() directly.
3287ea8dc4b6Seschrock */
3288ea8dc4b6Seschrock if (altroot) {
3289ea8dc4b6Seschrock if (spa == NULL) {
3290ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock);
3291ea8dc4b6Seschrock spa = spa_lookup(name);
3292ea8dc4b6Seschrock if (spa)
3293ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen);
3294ea8dc4b6Seschrock else
3295ea8dc4b6Seschrock altroot[0] = '\0';
3296ea8dc4b6Seschrock spa = NULL;
3297ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
3298ea8dc4b6Seschrock } else {
3299ea8dc4b6Seschrock spa_altroot(spa, altroot, buflen);
3300ea8dc4b6Seschrock }
3301ea8dc4b6Seschrock }
3302ea8dc4b6Seschrock
33036809eb4eSEric Schrock if (spa != NULL) {
33046809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG);
3305fa9e4066Sahrens spa_close(spa, FTAG);
33066809eb4eSEric Schrock }
3307fa9e4066Sahrens
3308fa9e4066Sahrens return (error);
3309fa9e4066Sahrens }
3310fa9e4066Sahrens
3311fa9e4066Sahrens /*
3312fa94a07fSbrendan * Validate that the auxiliary device array is well formed. We must have an
3313fa94a07fSbrendan * array of nvlists, each which describes a valid leaf vdev. If this is an
3314fa94a07fSbrendan * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3315fa94a07fSbrendan * specified, as long as they are well-formed.
331699653d4eSeschrock */
331799653d4eSeschrock static int
spa_validate_aux_devs(spa_t * spa,nvlist_t * nvroot,uint64_t crtxg,int mode,spa_aux_vdev_t * sav,const char * config,uint64_t version,vdev_labeltype_t label)3318fa94a07fSbrendan spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3319fa94a07fSbrendan spa_aux_vdev_t *sav, const char *config, uint64_t version,
3320fa94a07fSbrendan vdev_labeltype_t label)
332199653d4eSeschrock {
3322fa94a07fSbrendan nvlist_t **dev;
3323fa94a07fSbrendan uint_t i, ndev;
332499653d4eSeschrock vdev_t *vd;
332599653d4eSeschrock int error;
332699653d4eSeschrock
3327e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3328e14bb325SJeff Bonwick
332999653d4eSeschrock /*
3330fa94a07fSbrendan * It's acceptable to have no devs specified.
333199653d4eSeschrock */
3332fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
333399653d4eSeschrock return (0);
333499653d4eSeschrock
3335fa94a07fSbrendan if (ndev == 0)
3336be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
333799653d4eSeschrock
333899653d4eSeschrock /*
3339fa94a07fSbrendan * Make sure the pool is formatted with a version that supports this
3340fa94a07fSbrendan * device type.
334199653d4eSeschrock */
3342fa94a07fSbrendan if (spa_version(spa) < version)
3343be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
334499653d4eSeschrock
334539c23413Seschrock /*
3346fa94a07fSbrendan * Set the pending device list so we correctly handle device in-use
334739c23413Seschrock * checking.
334839c23413Seschrock */
3349fa94a07fSbrendan sav->sav_pending = dev;
3350fa94a07fSbrendan sav->sav_npending = ndev;
335139c23413Seschrock
3352fa94a07fSbrendan for (i = 0; i < ndev; i++) {
3353fa94a07fSbrendan if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
335499653d4eSeschrock mode)) != 0)
335539c23413Seschrock goto out;
335699653d4eSeschrock
335799653d4eSeschrock if (!vd->vdev_ops->vdev_op_leaf) {
335899653d4eSeschrock vdev_free(vd);
3359be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
336039c23413Seschrock goto out;
336199653d4eSeschrock }
336299653d4eSeschrock
3363fa94a07fSbrendan /*
3364e14bb325SJeff Bonwick * The L2ARC currently only supports disk devices in
3365e14bb325SJeff Bonwick * kernel context. For user-level testing, we allow it.
3366fa94a07fSbrendan */
3367e14bb325SJeff Bonwick #ifdef _KERNEL
3368fa94a07fSbrendan if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3369fa94a07fSbrendan strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3370be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTBLK);
3371cd0837ccSGeorge Wilson vdev_free(vd);
3372fa94a07fSbrendan goto out;
3373fa94a07fSbrendan }
3374e14bb325SJeff Bonwick #endif
337599653d4eSeschrock vd->vdev_top = vd;
337699653d4eSeschrock
337739c23413Seschrock if ((error = vdev_open(vd)) == 0 &&
3378fa94a07fSbrendan (error = vdev_label_init(vd, crtxg, label)) == 0) {
3379fa94a07fSbrendan VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
338099653d4eSeschrock vd->vdev_guid) == 0);
338199653d4eSeschrock }
338299653d4eSeschrock
338339c23413Seschrock vdev_free(vd);
338439c23413Seschrock
3385fa94a07fSbrendan if (error &&
3386fa94a07fSbrendan (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
338739c23413Seschrock goto out;
338839c23413Seschrock else
338939c23413Seschrock error = 0;
339039c23413Seschrock }
339139c23413Seschrock
339239c23413Seschrock out:
3393fa94a07fSbrendan sav->sav_pending = NULL;
3394fa94a07fSbrendan sav->sav_npending = 0;
339539c23413Seschrock return (error);
339699653d4eSeschrock }
339799653d4eSeschrock
3398fa94a07fSbrendan static int
spa_validate_aux(spa_t * spa,nvlist_t * nvroot,uint64_t crtxg,int mode)3399fa94a07fSbrendan spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3400fa94a07fSbrendan {
3401fa94a07fSbrendan int error;
3402fa94a07fSbrendan
3403e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3404e14bb325SJeff Bonwick
3405fa94a07fSbrendan if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3406fa94a07fSbrendan &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3407fa94a07fSbrendan VDEV_LABEL_SPARE)) != 0) {
3408fa94a07fSbrendan return (error);
3409fa94a07fSbrendan }
3410fa94a07fSbrendan
3411fa94a07fSbrendan return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3412fa94a07fSbrendan &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3413fa94a07fSbrendan VDEV_LABEL_L2CACHE));
3414fa94a07fSbrendan }
3415fa94a07fSbrendan
3416fa94a07fSbrendan static void
spa_set_aux_vdevs(spa_aux_vdev_t * sav,nvlist_t ** devs,int ndevs,const char * config)3417fa94a07fSbrendan spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3418fa94a07fSbrendan const char *config)
3419fa94a07fSbrendan {
3420fa94a07fSbrendan int i;
3421fa94a07fSbrendan
3422fa94a07fSbrendan if (sav->sav_config != NULL) {
3423fa94a07fSbrendan nvlist_t **olddevs;
3424fa94a07fSbrendan uint_t oldndevs;
3425fa94a07fSbrendan nvlist_t **newdevs;
3426fa94a07fSbrendan
3427fa94a07fSbrendan /*
3428fa94a07fSbrendan * Generate new dev list by concatentating with the
3429fa94a07fSbrendan * current dev list.
3430fa94a07fSbrendan */
3431fa94a07fSbrendan VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3432fa94a07fSbrendan &olddevs, &oldndevs) == 0);
3433fa94a07fSbrendan
3434fa94a07fSbrendan newdevs = kmem_alloc(sizeof (void *) *
3435fa94a07fSbrendan (ndevs + oldndevs), KM_SLEEP);
3436fa94a07fSbrendan for (i = 0; i < oldndevs; i++)
3437fa94a07fSbrendan VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3438fa94a07fSbrendan KM_SLEEP) == 0);
3439fa94a07fSbrendan for (i = 0; i < ndevs; i++)
3440fa94a07fSbrendan VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3441fa94a07fSbrendan KM_SLEEP) == 0);
3442fa94a07fSbrendan
3443fa94a07fSbrendan VERIFY(nvlist_remove(sav->sav_config, config,
3444fa94a07fSbrendan DATA_TYPE_NVLIST_ARRAY) == 0);
3445fa94a07fSbrendan
3446fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3447fa94a07fSbrendan config, newdevs, ndevs + oldndevs) == 0);
3448fa94a07fSbrendan for (i = 0; i < oldndevs + ndevs; i++)
3449fa94a07fSbrendan nvlist_free(newdevs[i]);
3450fa94a07fSbrendan kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3451fa94a07fSbrendan } else {
3452fa94a07fSbrendan /*
3453fa94a07fSbrendan * Generate a new dev list.
3454fa94a07fSbrendan */
3455fa94a07fSbrendan VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3456fa94a07fSbrendan KM_SLEEP) == 0);
3457fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3458fa94a07fSbrendan devs, ndevs) == 0);
3459fa94a07fSbrendan }
3460fa94a07fSbrendan }
3461fa94a07fSbrendan
3462fa94a07fSbrendan /*
3463fa94a07fSbrendan * Stop and drop level 2 ARC devices
3464fa94a07fSbrendan */
3465fa94a07fSbrendan void
spa_l2cache_drop(spa_t * spa)3466fa94a07fSbrendan spa_l2cache_drop(spa_t *spa)
3467fa94a07fSbrendan {
3468fa94a07fSbrendan vdev_t *vd;
3469fa94a07fSbrendan int i;
3470fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_l2cache;
3471fa94a07fSbrendan
3472fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++) {
3473fa94a07fSbrendan uint64_t pool;
3474fa94a07fSbrendan
3475fa94a07fSbrendan vd = sav->sav_vdevs[i];
3476fa94a07fSbrendan ASSERT(vd != NULL);
3477fa94a07fSbrendan
34788ad4d6ddSJeff Bonwick if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
34798ad4d6ddSJeff Bonwick pool != 0ULL && l2arc_vdev_present(vd))
3480fa94a07fSbrendan l2arc_remove_vdev(vd);
3481fa94a07fSbrendan }
3482fa94a07fSbrendan }
3483fa94a07fSbrendan
348499653d4eSeschrock /*
3485fa9e4066Sahrens * Pool Creation
3486fa9e4066Sahrens */
3487fa9e4066Sahrens int
spa_create(const char * pool,nvlist_t * nvroot,nvlist_t * props,nvlist_t * zplprops)3488990b4856Slling spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
34894445fffbSMatthew Ahrens nvlist_t *zplprops)
3490fa9e4066Sahrens {
3491fa9e4066Sahrens spa_t *spa;
3492990b4856Slling char *altroot = NULL;
34930373e76bSbonwick vdev_t *rvd;
3494fa9e4066Sahrens dsl_pool_t *dp;
3495fa9e4066Sahrens dmu_tx_t *tx;
3496573ca77eSGeorge Wilson int error = 0;
3497fa9e4066Sahrens uint64_t txg = TXG_INITIAL;
3498fa94a07fSbrendan nvlist_t **spares, **l2cache;
3499fa94a07fSbrendan uint_t nspares, nl2cache;
3500cde58dbcSMatthew Ahrens uint64_t version, obj;
3501ad135b5dSChristopher Siden boolean_t has_features;
3502fa9e4066Sahrens
3503fa9e4066Sahrens /*
3504fa9e4066Sahrens * If this pool already exists, return failure.
3505fa9e4066Sahrens */
3506fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
3507fa9e4066Sahrens if (spa_lookup(pool) != NULL) {
3508fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
3509be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
3510fa9e4066Sahrens }
3511fa9e4066Sahrens
3512fa9e4066Sahrens /*
3513fa9e4066Sahrens * Allocate a new spa_t structure.
3514fa9e4066Sahrens */
3515990b4856Slling (void) nvlist_lookup_string(props,
3516990b4856Slling zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3517468c413aSTim Haley spa = spa_add(pool, NULL, altroot);
35188ad4d6ddSJeff Bonwick spa_activate(spa, spa_mode_global);
3519fa9e4066Sahrens
3520990b4856Slling if (props && (error = spa_prop_validate(spa, props))) {
3521990b4856Slling spa_deactivate(spa);
3522990b4856Slling spa_remove(spa);
3523c5904d13Seschrock mutex_exit(&spa_namespace_lock);
3524990b4856Slling return (error);
3525990b4856Slling }
3526990b4856Slling
3527ad135b5dSChristopher Siden has_features = B_FALSE;
3528ad135b5dSChristopher Siden for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3529ad135b5dSChristopher Siden elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3530ad135b5dSChristopher Siden if (zpool_prop_feature(nvpair_name(elem)))
3531ad135b5dSChristopher Siden has_features = B_TRUE;
3532ad135b5dSChristopher Siden }
3533ad135b5dSChristopher Siden
3534ad135b5dSChristopher Siden if (has_features || nvlist_lookup_uint64(props,
3535ad135b5dSChristopher Siden zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3536990b4856Slling version = SPA_VERSION;
3537ad135b5dSChristopher Siden }
3538ad135b5dSChristopher Siden ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3539b24ab676SJeff Bonwick
3540b24ab676SJeff Bonwick spa->spa_first_txg = txg;
3541b24ab676SJeff Bonwick spa->spa_uberblock.ub_txg = txg - 1;
3542990b4856Slling spa->spa_uberblock.ub_version = version;
3543fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock;
3544fa9e4066Sahrens
35450373e76bSbonwick /*
354654d692b7SGeorge Wilson * Create "The Godfather" zio to hold all async IOs
354754d692b7SGeorge Wilson */
35486f834bc1SMatthew Ahrens spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
35496f834bc1SMatthew Ahrens KM_SLEEP);
35506f834bc1SMatthew Ahrens for (int i = 0; i < max_ncpus; i++) {
35516f834bc1SMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
35526f834bc1SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
35536f834bc1SMatthew Ahrens ZIO_FLAG_GODFATHER);
35546f834bc1SMatthew Ahrens }
355554d692b7SGeorge Wilson
355654d692b7SGeorge Wilson /*
35570373e76bSbonwick * Create the root vdev.
35580373e76bSbonwick */
3559e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
35600373e76bSbonwick
356199653d4eSeschrock error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
35620373e76bSbonwick
356399653d4eSeschrock ASSERT(error != 0 || rvd != NULL);
356499653d4eSeschrock ASSERT(error != 0 || spa->spa_root_vdev == rvd);
35650373e76bSbonwick
3566b7b97454Sperrin if (error == 0 && !zfs_allocatable_devs(nvroot))
3567be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
356899653d4eSeschrock
356999653d4eSeschrock if (error == 0 &&
357099653d4eSeschrock (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3571fa94a07fSbrendan (error = spa_validate_aux(spa, nvroot, txg,
357299653d4eSeschrock VDEV_ALLOC_ADD)) == 0) {
3573573ca77eSGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
3574573ca77eSGeorge Wilson vdev_metaslab_set_size(rvd->vdev_child[c]);
3575573ca77eSGeorge Wilson vdev_expand(rvd->vdev_child[c], txg);
3576573ca77eSGeorge Wilson }
35770373e76bSbonwick }
35780373e76bSbonwick
3579e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
3580fa9e4066Sahrens
358199653d4eSeschrock if (error != 0) {
3582fa9e4066Sahrens spa_unload(spa);
3583fa9e4066Sahrens spa_deactivate(spa);
3584fa9e4066Sahrens spa_remove(spa);
3585fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
3586fa9e4066Sahrens return (error);
3587fa9e4066Sahrens }
3588fa9e4066Sahrens
358999653d4eSeschrock /*
359099653d4eSeschrock * Get the list of spares, if specified.
359199653d4eSeschrock */
359299653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
359399653d4eSeschrock &spares, &nspares) == 0) {
3594fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
359599653d4eSeschrock KM_SLEEP) == 0);
3596fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
359799653d4eSeschrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3598e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
359999653d4eSeschrock spa_load_spares(spa);
3600e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
3601fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE;
3602fa94a07fSbrendan }
3603fa94a07fSbrendan
3604fa94a07fSbrendan /*
3605fa94a07fSbrendan * Get the list of level 2 cache devices, if specified.
3606fa94a07fSbrendan */
3607fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3608fa94a07fSbrendan &l2cache, &nl2cache) == 0) {
3609fa94a07fSbrendan VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3610fa94a07fSbrendan NV_UNIQUE_NAME, KM_SLEEP) == 0);
3611fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3612fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3613e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3614fa94a07fSbrendan spa_load_l2cache(spa);
3615e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
3616fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE;
361799653d4eSeschrock }
361899653d4eSeschrock
3619ad135b5dSChristopher Siden spa->spa_is_initializing = B_TRUE;
36200a48a24eStimh spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3621fa9e4066Sahrens spa->spa_meta_objset = dp->dp_meta_objset;
3622ad135b5dSChristopher Siden spa->spa_is_initializing = B_FALSE;
3623fa9e4066Sahrens
3624485bbbf5SGeorge Wilson /*
3625485bbbf5SGeorge Wilson * Create DDTs (dedup tables).
3626485bbbf5SGeorge Wilson */
3627485bbbf5SGeorge Wilson ddt_create(spa);
3628485bbbf5SGeorge Wilson
3629485bbbf5SGeorge Wilson spa_update_dspace(spa);
3630485bbbf5SGeorge Wilson
3631fa9e4066Sahrens tx = dmu_tx_create_assigned(dp, txg);
3632fa9e4066Sahrens
3633fa9e4066Sahrens /*
3634fa9e4066Sahrens * Create the pool config object.
3635fa9e4066Sahrens */
3636fa9e4066Sahrens spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3637f7991ba4STim Haley DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3638fa9e4066Sahrens DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3639fa9e4066Sahrens
3640ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset,
3641fa9e4066Sahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3642ea8dc4b6Seschrock sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3643ea8dc4b6Seschrock cmn_err(CE_PANIC, "failed to add pool config");
3644ea8dc4b6Seschrock }
3645fa9e4066Sahrens
3646ad135b5dSChristopher Siden if (spa_version(spa) >= SPA_VERSION_FEATURES)
3647ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx);
3648ad135b5dSChristopher Siden
36493f9d6ad7SLin Ling if (zap_add(spa->spa_meta_objset,
36503f9d6ad7SLin Ling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
36513f9d6ad7SLin Ling sizeof (uint64_t), 1, &version, tx) != 0) {
36523f9d6ad7SLin Ling cmn_err(CE_PANIC, "failed to add pool version");
36533f9d6ad7SLin Ling }
36543f9d6ad7SLin Ling
3655990b4856Slling /* Newly created pools with the right version are always deflated. */
3656990b4856Slling if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
365799653d4eSeschrock spa->spa_deflate = TRUE;
365899653d4eSeschrock if (zap_add(spa->spa_meta_objset,
365999653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
366099653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
366199653d4eSeschrock cmn_err(CE_PANIC, "failed to add deflate");
366299653d4eSeschrock }
3663990b4856Slling }
366499653d4eSeschrock
3665fa9e4066Sahrens /*
3666cde58dbcSMatthew Ahrens * Create the deferred-free bpobj. Turn off compression
3667fa9e4066Sahrens * because sync-to-convergence takes longer if the blocksize
3668fa9e4066Sahrens * keeps changing.
3669fa9e4066Sahrens */
3670cde58dbcSMatthew Ahrens obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3671cde58dbcSMatthew Ahrens dmu_object_set_compress(spa->spa_meta_objset, obj,
3672cde58dbcSMatthew Ahrens ZIO_COMPRESS_OFF, tx);
3673ea8dc4b6Seschrock if (zap_add(spa->spa_meta_objset,
3674cde58dbcSMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3675cde58dbcSMatthew Ahrens sizeof (uint64_t), 1, &obj, tx) != 0) {
3676cde58dbcSMatthew Ahrens cmn_err(CE_PANIC, "failed to add bpobj");
3677ea8dc4b6Seschrock }
3678b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3679cde58dbcSMatthew Ahrens spa->spa_meta_objset, obj));
3680fa9e4066Sahrens
368106eeb2adSek110237 /*
368206eeb2adSek110237 * Create the pool's history object.
368306eeb2adSek110237 */
3684990b4856Slling if (version >= SPA_VERSION_ZPOOL_HISTORY)
368506eeb2adSek110237 spa_history_create_obj(spa, tx);
368606eeb2adSek110237
3687990b4856Slling /*
368845818ee1SMatthew Ahrens * Generate some random noise for salted checksums to operate on.
368945818ee1SMatthew Ahrens */
369045818ee1SMatthew Ahrens (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
369145818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes));
369245818ee1SMatthew Ahrens
369345818ee1SMatthew Ahrens /*
3694990b4856Slling * Set pool properties.
3695990b4856Slling */
3696990b4856Slling spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3697990b4856Slling spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
36980a4e9518Sgw25295 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3699573ca77eSGeorge Wilson spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3700b24ab676SJeff Bonwick
3701379c004dSEric Schrock if (props != NULL) {
3702379c004dSEric Schrock spa_configfile_set(spa, props, B_FALSE);
37033b2aab18SMatthew Ahrens spa_sync_props(props, tx);
3704379c004dSEric Schrock }
3705990b4856Slling
3706fa9e4066Sahrens dmu_tx_commit(tx);
3707fa9e4066Sahrens
3708fa9e4066Sahrens spa->spa_sync_on = B_TRUE;
3709fa9e4066Sahrens txg_sync_start(spa->spa_dsl_pool);
3710fa9e4066Sahrens
3711fa9e4066Sahrens /*
3712fa9e4066Sahrens * We explicitly wait for the first transaction to complete so that our
3713fa9e4066Sahrens * bean counters are appropriately updated.
3714fa9e4066Sahrens */
3715fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg);
3716fa9e4066Sahrens
3717c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE);
371814372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3719fa9e4066Sahrens
37204445fffbSMatthew Ahrens spa_history_log_version(spa, "create");
3721228975ccSek110237
3722bc9014e6SJustin Gibbs /*
3723bc9014e6SJustin Gibbs * Don't count references from objsets that are already closed
3724bc9014e6SJustin Gibbs * and are making their way through the eviction process.
3725bc9014e6SJustin Gibbs */
3726bc9014e6SJustin Gibbs spa_evicting_os_wait(spa);
3727088f3894Sahrens spa->spa_minref = refcount_count(&spa->spa_refcount);
3728088f3894Sahrens
3729daaa36a7SGeorge Wilson mutex_exit(&spa_namespace_lock);
3730daaa36a7SGeorge Wilson
3731fa9e4066Sahrens return (0);
3732fa9e4066Sahrens }
3733fa9e4066Sahrens
3734e7cbe64fSgw25295 #ifdef _KERNEL
3735e7cbe64fSgw25295 /*
373621ecdf64SLin Ling * Get the root pool information from the root disk, then import the root pool
373721ecdf64SLin Ling * during the system boot up time.
3738e7cbe64fSgw25295 */
373921ecdf64SLin Ling extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
374021ecdf64SLin Ling
374121ecdf64SLin Ling static nvlist_t *
spa_generate_rootconf(char * devpath,char * devid,uint64_t * guid)374221ecdf64SLin Ling spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3743e7cbe64fSgw25295 {
374421ecdf64SLin Ling nvlist_t *config;
3745e7cbe64fSgw25295 nvlist_t *nvtop, *nvroot;
3746e7cbe64fSgw25295 uint64_t pgid;
3747e7cbe64fSgw25295
374821ecdf64SLin Ling if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
374921ecdf64SLin Ling return (NULL);
375021ecdf64SLin Ling
3751e7cbe64fSgw25295 /*
3752e7cbe64fSgw25295 * Add this top-level vdev to the child array.
3753e7cbe64fSgw25295 */
375421ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
375521ecdf64SLin Ling &nvtop) == 0);
375621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
375721ecdf64SLin Ling &pgid) == 0);
375821ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3759e7cbe64fSgw25295
3760e7cbe64fSgw25295 /*
3761e7cbe64fSgw25295 * Put this pool's top-level vdevs into a root vdev.
3762e7cbe64fSgw25295 */
3763e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
376421ecdf64SLin Ling VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
376521ecdf64SLin Ling VDEV_TYPE_ROOT) == 0);
3766e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3767e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3768e7cbe64fSgw25295 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3769e7cbe64fSgw25295 &nvtop, 1) == 0);
3770e7cbe64fSgw25295
3771e7cbe64fSgw25295 /*
3772e7cbe64fSgw25295 * Replace the existing vdev_tree with the new root vdev in
3773e7cbe64fSgw25295 * this pool's configuration (remove the old, add the new).
3774e7cbe64fSgw25295 */
3775e7cbe64fSgw25295 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3776e7cbe64fSgw25295 nvlist_free(nvroot);
377721ecdf64SLin Ling return (config);
3778e7cbe64fSgw25295 }
3779e7cbe64fSgw25295
3780e7cbe64fSgw25295 /*
378121ecdf64SLin Ling * Walk the vdev tree and see if we can find a device with "better"
378221ecdf64SLin Ling * configuration. A configuration is "better" if the label on that
378321ecdf64SLin Ling * device has a more recent txg.
3784e7cbe64fSgw25295 */
378521ecdf64SLin Ling static void
spa_alt_rootvdev(vdev_t * vd,vdev_t ** avd,uint64_t * txg)378621ecdf64SLin Ling spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3787e7cbe64fSgw25295 {
3788573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++)
378921ecdf64SLin Ling spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3790e7cbe64fSgw25295
379121ecdf64SLin Ling if (vd->vdev_ops->vdev_op_leaf) {
379221ecdf64SLin Ling nvlist_t *label;
379321ecdf64SLin Ling uint64_t label_txg;
3794e7cbe64fSgw25295
379521ecdf64SLin Ling if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
379621ecdf64SLin Ling &label) != 0)
379721ecdf64SLin Ling return;
3798e7cbe64fSgw25295
379921ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
380021ecdf64SLin Ling &label_txg) == 0);
3801051aabe6Staylor
3802051aabe6Staylor /*
380321ecdf64SLin Ling * Do we have a better boot device?
3804051aabe6Staylor */
380521ecdf64SLin Ling if (label_txg > *txg) {
380621ecdf64SLin Ling *txg = label_txg;
380721ecdf64SLin Ling *avd = vd;
3808051aabe6Staylor }
380921ecdf64SLin Ling nvlist_free(label);
3810051aabe6Staylor }
3811051aabe6Staylor }
3812051aabe6Staylor
3813e7cbe64fSgw25295 /*
3814e7cbe64fSgw25295 * Import a root pool.
3815e7cbe64fSgw25295 *
3816051aabe6Staylor * For x86. devpath_list will consist of devid and/or physpath name of
3817051aabe6Staylor * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3818051aabe6Staylor * The GRUB "findroot" command will return the vdev we should boot.
3819e7cbe64fSgw25295 *
3820e7cbe64fSgw25295 * For Sparc, devpath_list consists the physpath name of the booting device
3821e7cbe64fSgw25295 * no matter the rootpool is a single device pool or a mirrored pool.
3822e7cbe64fSgw25295 * e.g.
3823e7cbe64fSgw25295 * "/pci@1f,0/ide@d/disk@0,0:a"
3824e7cbe64fSgw25295 */
3825e7cbe64fSgw25295 int
spa_import_rootpool(char * devpath,char * devid)3826051aabe6Staylor spa_import_rootpool(char *devpath, char *devid)
3827e7cbe64fSgw25295 {
382821ecdf64SLin Ling spa_t *spa;
382921ecdf64SLin Ling vdev_t *rvd, *bvd, *avd = NULL;
383021ecdf64SLin Ling nvlist_t *config, *nvtop;
383121ecdf64SLin Ling uint64_t guid, txg;
3832e7cbe64fSgw25295 char *pname;
3833e7cbe64fSgw25295 int error;
3834e7cbe64fSgw25295
3835e7cbe64fSgw25295 /*
383621ecdf64SLin Ling * Read the label from the boot device and generate a configuration.
3837e7cbe64fSgw25295 */
3838dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid);
3839dedec472SJack Meng #if defined(_OBP) && defined(_KERNEL)
3840dedec472SJack Meng if (config == NULL) {
3841dedec472SJack Meng if (strstr(devpath, "/iscsi/ssd") != NULL) {
3842dedec472SJack Meng /* iscsi boot */
3843dedec472SJack Meng get_iscsi_bootpath_phy(devpath);
3844dedec472SJack Meng config = spa_generate_rootconf(devpath, devid, &guid);
3845dedec472SJack Meng }
3846dedec472SJack Meng }
3847dedec472SJack Meng #endif
3848dedec472SJack Meng if (config == NULL) {
384921ecdf64SLin Ling cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
385021ecdf64SLin Ling devpath);
3851be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
385221ecdf64SLin Ling }
3853e7cbe64fSgw25295
385421ecdf64SLin Ling VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
385521ecdf64SLin Ling &pname) == 0);
385621ecdf64SLin Ling VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3857e7cbe64fSgw25295
38586809eb4eSEric Schrock mutex_enter(&spa_namespace_lock);
38596809eb4eSEric Schrock if ((spa = spa_lookup(pname)) != NULL) {
3860bf82a41bSeschrock /*
38616809eb4eSEric Schrock * Remove the existing root pool from the namespace so that we
38626809eb4eSEric Schrock * can replace it with the correct config we just read in.
3863bf82a41bSeschrock */
38646809eb4eSEric Schrock spa_remove(spa);
38656809eb4eSEric Schrock }
38666809eb4eSEric Schrock
3867468c413aSTim Haley spa = spa_add(pname, config, NULL);
38686809eb4eSEric Schrock spa->spa_is_root = B_TRUE;
38694b964adaSGeorge Wilson spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
387021ecdf64SLin Ling
387121ecdf64SLin Ling /*
387221ecdf64SLin Ling * Build up a vdev tree based on the boot device's label config.
387321ecdf64SLin Ling */
387421ecdf64SLin Ling VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
387521ecdf64SLin Ling &nvtop) == 0);
387621ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
387721ecdf64SLin Ling error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
387821ecdf64SLin Ling VDEV_ALLOC_ROOTPOOL);
387921ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG);
388021ecdf64SLin Ling if (error) {
38816809eb4eSEric Schrock mutex_exit(&spa_namespace_lock);
388221ecdf64SLin Ling nvlist_free(config);
388321ecdf64SLin Ling cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
388421ecdf64SLin Ling pname);
3885e7cbe64fSgw25295 return (error);
3886e7cbe64fSgw25295 }
388721ecdf64SLin Ling
388821ecdf64SLin Ling /*
388921ecdf64SLin Ling * Get the boot vdev.
389021ecdf64SLin Ling */
389121ecdf64SLin Ling if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
389221ecdf64SLin Ling cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
389321ecdf64SLin Ling (u_longlong_t)guid);
3894be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT);
389521ecdf64SLin Ling goto out;
389621ecdf64SLin Ling }
389721ecdf64SLin Ling
389821ecdf64SLin Ling /*
389921ecdf64SLin Ling * Determine if there is a better boot device.
390021ecdf64SLin Ling */
390121ecdf64SLin Ling avd = bvd;
390221ecdf64SLin Ling spa_alt_rootvdev(rvd, &avd, &txg);
390321ecdf64SLin Ling if (avd != bvd) {
390421ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
390521ecdf64SLin Ling "try booting from '%s'", avd->vdev_path);
3906be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
390721ecdf64SLin Ling goto out;
390821ecdf64SLin Ling }
390921ecdf64SLin Ling
391021ecdf64SLin Ling /*
391121ecdf64SLin Ling * If the boot device is part of a spare vdev then ensure that
391221ecdf64SLin Ling * we're booting off the active spare.
391321ecdf64SLin Ling */
391421ecdf64SLin Ling if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
391521ecdf64SLin Ling !bvd->vdev_isspare) {
391621ecdf64SLin Ling cmn_err(CE_NOTE, "The boot device is currently spared. Please "
391721ecdf64SLin Ling "try booting from '%s'",
3918cb04b873SMark J Musante bvd->vdev_parent->
3919cb04b873SMark J Musante vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3920be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
392121ecdf64SLin Ling goto out;
392221ecdf64SLin Ling }
392321ecdf64SLin Ling
392421ecdf64SLin Ling error = 0;
392521ecdf64SLin Ling out:
392621ecdf64SLin Ling spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
392721ecdf64SLin Ling vdev_free(rvd);
392821ecdf64SLin Ling spa_config_exit(spa, SCL_ALL, FTAG);
392921ecdf64SLin Ling mutex_exit(&spa_namespace_lock);
393021ecdf64SLin Ling
393121ecdf64SLin Ling nvlist_free(config);
393221ecdf64SLin Ling return (error);
393321ecdf64SLin Ling }
393421ecdf64SLin Ling
3935e7cbe64fSgw25295 #endif
3936e7cbe64fSgw25295
3937e7cbe64fSgw25295 /*
3938e7cbe64fSgw25295 * Import a non-root pool into the system.
3939e7cbe64fSgw25295 */
3940e7cbe64fSgw25295 int
spa_import(const char * pool,nvlist_t * config,nvlist_t * props,uint64_t flags)39414b964adaSGeorge Wilson spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3942e7cbe64fSgw25295 {
39436809eb4eSEric Schrock spa_t *spa;
39446809eb4eSEric Schrock char *altroot = NULL;
3945468c413aSTim Haley spa_load_state_t state = SPA_LOAD_IMPORT;
3946468c413aSTim Haley zpool_rewind_policy_t policy;
3947f9af39baSGeorge Wilson uint64_t mode = spa_mode_global;
3948f9af39baSGeorge Wilson uint64_t readonly = B_FALSE;
39496809eb4eSEric Schrock int error;
39506809eb4eSEric Schrock nvlist_t *nvroot;
39516809eb4eSEric Schrock nvlist_t **spares, **l2cache;
39526809eb4eSEric Schrock uint_t nspares, nl2cache;
39536809eb4eSEric Schrock
39546809eb4eSEric Schrock /*
39556809eb4eSEric Schrock * If a pool with this name exists, return failure.
39566809eb4eSEric Schrock */
39576809eb4eSEric Schrock mutex_enter(&spa_namespace_lock);
39581195e687SMark J Musante if (spa_lookup(pool) != NULL) {
39596809eb4eSEric Schrock mutex_exit(&spa_namespace_lock);
3960be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
3961e7cbe64fSgw25295 }
3962e7cbe64fSgw25295
39636809eb4eSEric Schrock /*
39646809eb4eSEric Schrock * Create and initialize the spa structure.
39656809eb4eSEric Schrock */
39666809eb4eSEric Schrock (void) nvlist_lookup_string(props,
39676809eb4eSEric Schrock zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3968f9af39baSGeorge Wilson (void) nvlist_lookup_uint64(props,
3969f9af39baSGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3970f9af39baSGeorge Wilson if (readonly)
3971f9af39baSGeorge Wilson mode = FREAD;
3972468c413aSTim Haley spa = spa_add(pool, config, altroot);
39734b964adaSGeorge Wilson spa->spa_import_flags = flags;
39744b964adaSGeorge Wilson
39754b964adaSGeorge Wilson /*
39764b964adaSGeorge Wilson * Verbatim import - Take a pool and insert it into the namespace
39774b964adaSGeorge Wilson * as if it had been loaded at boot.
39784b964adaSGeorge Wilson */
39794b964adaSGeorge Wilson if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
39804b964adaSGeorge Wilson if (props != NULL)
39814b964adaSGeorge Wilson spa_configfile_set(spa, props, B_FALSE);
39824b964adaSGeorge Wilson
39834b964adaSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE);
398414372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
39854b964adaSGeorge Wilson
39864b964adaSGeorge Wilson mutex_exit(&spa_namespace_lock);
39874b964adaSGeorge Wilson return (0);
39884b964adaSGeorge Wilson }
39894b964adaSGeorge Wilson
3990f9af39baSGeorge Wilson spa_activate(spa, mode);
39916809eb4eSEric Schrock
39926809eb4eSEric Schrock /*
399325f89ee2SJeff Bonwick * Don't start async tasks until we know everything is healthy.
399425f89ee2SJeff Bonwick */
399525f89ee2SJeff Bonwick spa_async_suspend(spa);
399625f89ee2SJeff Bonwick
39974b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy);
39984b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_DO_REWIND)
39994b964adaSGeorge Wilson state = SPA_LOAD_RECOVER;
40004b964adaSGeorge Wilson
400125f89ee2SJeff Bonwick /*
40026809eb4eSEric Schrock * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
40036809eb4eSEric Schrock * because the user-supplied config is actually the one to trust when
40046809eb4eSEric Schrock * doing an import.
40056809eb4eSEric Schrock */
4006468c413aSTim Haley if (state != SPA_LOAD_RECOVER)
4007468c413aSTim Haley spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
40084b964adaSGeorge Wilson
4009468c413aSTim Haley error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4010c8ee1847SVictor Latushkin policy.zrp_request);
4011468c413aSTim Haley
4012468c413aSTim Haley /*
40134b964adaSGeorge Wilson * Propagate anything learned while loading the pool and pass it
40144b964adaSGeorge Wilson * back to caller (i.e. rewind info, missing devices, etc).
4015468c413aSTim Haley */
40164b964adaSGeorge Wilson VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
40174b964adaSGeorge Wilson spa->spa_load_info) == 0);
40186809eb4eSEric Schrock
40196809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
40206809eb4eSEric Schrock /*
40216809eb4eSEric Schrock * Toss any existing sparelist, as it doesn't have any validity
40226809eb4eSEric Schrock * anymore, and conflicts with spa_has_spare().
40236809eb4eSEric Schrock */
40246809eb4eSEric Schrock if (spa->spa_spares.sav_config) {
40256809eb4eSEric Schrock nvlist_free(spa->spa_spares.sav_config);
40266809eb4eSEric Schrock spa->spa_spares.sav_config = NULL;
40276809eb4eSEric Schrock spa_load_spares(spa);
40286809eb4eSEric Schrock }
40296809eb4eSEric Schrock if (spa->spa_l2cache.sav_config) {
40306809eb4eSEric Schrock nvlist_free(spa->spa_l2cache.sav_config);
40316809eb4eSEric Schrock spa->spa_l2cache.sav_config = NULL;
40326809eb4eSEric Schrock spa_load_l2cache(spa);
40336809eb4eSEric Schrock }
40346809eb4eSEric Schrock
40356809eb4eSEric Schrock VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
40366809eb4eSEric Schrock &nvroot) == 0);
40376809eb4eSEric Schrock if (error == 0)
40386809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL,
40396809eb4eSEric Schrock VDEV_ALLOC_SPARE);
40406809eb4eSEric Schrock if (error == 0)
40416809eb4eSEric Schrock error = spa_validate_aux(spa, nvroot, -1ULL,
40426809eb4eSEric Schrock VDEV_ALLOC_L2CACHE);
40436809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG);
40446809eb4eSEric Schrock
40456809eb4eSEric Schrock if (props != NULL)
40466809eb4eSEric Schrock spa_configfile_set(spa, props, B_FALSE);
40476809eb4eSEric Schrock
40486809eb4eSEric Schrock if (error != 0 || (props && spa_writeable(spa) &&
40496809eb4eSEric Schrock (error = spa_prop_set(spa, props)))) {
40506809eb4eSEric Schrock spa_unload(spa);
40516809eb4eSEric Schrock spa_deactivate(spa);
40526809eb4eSEric Schrock spa_remove(spa);
40536809eb4eSEric Schrock mutex_exit(&spa_namespace_lock);
40546809eb4eSEric Schrock return (error);
40556809eb4eSEric Schrock }
40566809eb4eSEric Schrock
4057955ef359SLin Ling spa_async_resume(spa);
4058955ef359SLin Ling
40596809eb4eSEric Schrock /*
40606809eb4eSEric Schrock * Override any spares and level 2 cache devices as specified by
40616809eb4eSEric Schrock * the user, as these may have correct device names/devids, etc.
40626809eb4eSEric Schrock */
40636809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
40646809eb4eSEric Schrock &spares, &nspares) == 0) {
40656809eb4eSEric Schrock if (spa->spa_spares.sav_config)
40666809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_spares.sav_config,
40676809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
40686809eb4eSEric Schrock else
40696809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
40706809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0);
40716809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
40726809eb4eSEric Schrock ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
40736809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
40746809eb4eSEric Schrock spa_load_spares(spa);
40756809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG);
40766809eb4eSEric Schrock spa->spa_spares.sav_sync = B_TRUE;
40776809eb4eSEric Schrock }
40786809eb4eSEric Schrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
40796809eb4eSEric Schrock &l2cache, &nl2cache) == 0) {
40806809eb4eSEric Schrock if (spa->spa_l2cache.sav_config)
40816809eb4eSEric Schrock VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
40826809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
40836809eb4eSEric Schrock else
40846809eb4eSEric Schrock VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
40856809eb4eSEric Schrock NV_UNIQUE_NAME, KM_SLEEP) == 0);
40866809eb4eSEric Schrock VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
40876809eb4eSEric Schrock ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
40886809eb4eSEric Schrock spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
40896809eb4eSEric Schrock spa_load_l2cache(spa);
40906809eb4eSEric Schrock spa_config_exit(spa, SCL_ALL, FTAG);
40916809eb4eSEric Schrock spa->spa_l2cache.sav_sync = B_TRUE;
40926809eb4eSEric Schrock }
40936809eb4eSEric Schrock
4094b693757aSEric Schrock /*
4095b693757aSEric Schrock * Check for any removed devices.
4096b693757aSEric Schrock */
4097b693757aSEric Schrock if (spa->spa_autoreplace) {
4098b693757aSEric Schrock spa_aux_check_removed(&spa->spa_spares);
4099b693757aSEric Schrock spa_aux_check_removed(&spa->spa_l2cache);
4100b693757aSEric Schrock }
4101b693757aSEric Schrock
41026809eb4eSEric Schrock if (spa_writeable(spa)) {
41036809eb4eSEric Schrock /*
41046809eb4eSEric Schrock * Update the config cache to include the newly-imported pool.
41056809eb4eSEric Schrock */
4106bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
41076809eb4eSEric Schrock }
41086809eb4eSEric Schrock
4109573ca77eSGeorge Wilson /*
4110573ca77eSGeorge Wilson * It's possible that the pool was expanded while it was exported.
4111573ca77eSGeorge Wilson * We kick off an async task to handle this for us.
4112573ca77eSGeorge Wilson */
4113573ca77eSGeorge Wilson spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4114573ca77eSGeorge Wilson
41154445fffbSMatthew Ahrens spa_history_log_version(spa, "import");
41166809eb4eSEric Schrock
411714372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
411814372834SHans Rosenfeld
411914372834SHans Rosenfeld mutex_exit(&spa_namespace_lock);
412014372834SHans Rosenfeld
41216809eb4eSEric Schrock return (0);
4122c5904d13Seschrock }
4123c5904d13Seschrock
4124fa9e4066Sahrens nvlist_t *
spa_tryimport(nvlist_t * tryconfig)4125fa9e4066Sahrens spa_tryimport(nvlist_t *tryconfig)
4126fa9e4066Sahrens {
4127fa9e4066Sahrens nvlist_t *config = NULL;
4128fa9e4066Sahrens char *poolname;
4129fa9e4066Sahrens spa_t *spa;
4130fa9e4066Sahrens uint64_t state;
41317b7154beSLin Ling int error;
4132fa9e4066Sahrens
4133fa9e4066Sahrens if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4134fa9e4066Sahrens return (NULL);
4135fa9e4066Sahrens
4136fa9e4066Sahrens if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4137fa9e4066Sahrens return (NULL);
4138fa9e4066Sahrens
4139fa9e4066Sahrens /*
41400373e76bSbonwick * Create and initialize the spa structure.
4141fa9e4066Sahrens */
41420373e76bSbonwick mutex_enter(&spa_namespace_lock);
4143468c413aSTim Haley spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
41448ad4d6ddSJeff Bonwick spa_activate(spa, FREAD);
4145fa9e4066Sahrens
4146fa9e4066Sahrens /*
41470373e76bSbonwick * Pass off the heavy lifting to spa_load().
4148ecc2d604Sbonwick * Pass TRUE for mosconfig because the user-supplied config
4149ecc2d604Sbonwick * is actually the one to trust when doing an import.
4150fa9e4066Sahrens */
41511195e687SMark J Musante error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4152fa9e4066Sahrens
4153fa9e4066Sahrens /*
4154fa9e4066Sahrens * If 'tryconfig' was at least parsable, return the current config.
4155fa9e4066Sahrens */
4156fa9e4066Sahrens if (spa->spa_root_vdev != NULL) {
4157fa9e4066Sahrens config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4158fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4159fa9e4066Sahrens poolname) == 0);
4160fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4161fa9e4066Sahrens state) == 0);
416295173954Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
416395173954Sek110237 spa->spa_uberblock.ub_timestamp) == 0);
4164ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4165ad135b5dSChristopher Siden spa->spa_load_info) == 0);
416699653d4eSeschrock
416799653d4eSeschrock /*
4168e7cbe64fSgw25295 * If the bootfs property exists on this pool then we
4169e7cbe64fSgw25295 * copy it out so that external consumers can tell which
4170e7cbe64fSgw25295 * pools are bootable.
4171e7cbe64fSgw25295 */
41727b7154beSLin Ling if ((!error || error == EEXIST) && spa->spa_bootfs) {
4173e7cbe64fSgw25295 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4174e7cbe64fSgw25295
4175e7cbe64fSgw25295 /*
4176e7cbe64fSgw25295 * We have to play games with the name since the
4177e7cbe64fSgw25295 * pool was opened as TRYIMPORT_NAME.
4178e7cbe64fSgw25295 */
4179e14bb325SJeff Bonwick if (dsl_dsobj_to_dsname(spa_name(spa),
4180e7cbe64fSgw25295 spa->spa_bootfs, tmpname) == 0) {
4181e7cbe64fSgw25295 char *cp;
4182e7cbe64fSgw25295 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4183e7cbe64fSgw25295
4184e7cbe64fSgw25295 cp = strchr(tmpname, '/');
4185e7cbe64fSgw25295 if (cp == NULL) {
4186e7cbe64fSgw25295 (void) strlcpy(dsname, tmpname,
4187e7cbe64fSgw25295 MAXPATHLEN);
4188e7cbe64fSgw25295 } else {
4189e7cbe64fSgw25295 (void) snprintf(dsname, MAXPATHLEN,
4190e7cbe64fSgw25295 "%s/%s", poolname, ++cp);
4191e7cbe64fSgw25295 }
4192e7cbe64fSgw25295 VERIFY(nvlist_add_string(config,
4193e7cbe64fSgw25295 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4194e7cbe64fSgw25295 kmem_free(dsname, MAXPATHLEN);
4195e7cbe64fSgw25295 }
4196e7cbe64fSgw25295 kmem_free(tmpname, MAXPATHLEN);
4197e7cbe64fSgw25295 }
4198e7cbe64fSgw25295
4199e7cbe64fSgw25295 /*
4200fa94a07fSbrendan * Add the list of hot spares and level 2 cache devices.
420199653d4eSeschrock */
42026809eb4eSEric Schrock spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
420399653d4eSeschrock spa_add_spares(spa, config);
4204fa94a07fSbrendan spa_add_l2cache(spa, config);
42056809eb4eSEric Schrock spa_config_exit(spa, SCL_CONFIG, FTAG);
4206fa9e4066Sahrens }
4207fa9e4066Sahrens
4208fa9e4066Sahrens spa_unload(spa);
4209fa9e4066Sahrens spa_deactivate(spa);
4210fa9e4066Sahrens spa_remove(spa);
4211fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
4212fa9e4066Sahrens
4213fa9e4066Sahrens return (config);
4214fa9e4066Sahrens }
4215fa9e4066Sahrens
4216fa9e4066Sahrens /*
4217fa9e4066Sahrens * Pool export/destroy
4218fa9e4066Sahrens *
4219fa9e4066Sahrens * The act of destroying or exporting a pool is very simple. We make sure there
4220fa9e4066Sahrens * is no more pending I/O and any references to the pool are gone. Then, we
4221fa9e4066Sahrens * update the pool state and sync all the labels to disk, removing the
4222394ab0cbSGeorge Wilson * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4223394ab0cbSGeorge Wilson * we don't sync the labels or remove the configuration cache.
4224fa9e4066Sahrens */
4225fa9e4066Sahrens static int
spa_export_common(char * pool,int new_state,nvlist_t ** oldconfig,boolean_t force,boolean_t hardforce)422689a89ebfSlling spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4227394ab0cbSGeorge Wilson boolean_t force, boolean_t hardforce)
4228fa9e4066Sahrens {
4229fa9e4066Sahrens spa_t *spa;
4230fa9e4066Sahrens
423144cd46caSbillm if (oldconfig)
423244cd46caSbillm *oldconfig = NULL;
423344cd46caSbillm
42348ad4d6ddSJeff Bonwick if (!(spa_mode_global & FWRITE))
4235be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS));
4236fa9e4066Sahrens
4237fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
4238fa9e4066Sahrens if ((spa = spa_lookup(pool)) == NULL) {
4239fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
4240be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
4241fa9e4066Sahrens }
4242fa9e4066Sahrens
4243fa9e4066Sahrens /*
4244ea8dc4b6Seschrock * Put a hold on the pool, drop the namespace lock, stop async tasks,
4245ea8dc4b6Seschrock * reacquire the namespace lock, and see if we can export.
4246ea8dc4b6Seschrock */
4247ea8dc4b6Seschrock spa_open_ref(spa, FTAG);
4248ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
4249ea8dc4b6Seschrock spa_async_suspend(spa);
4250ea8dc4b6Seschrock mutex_enter(&spa_namespace_lock);
4251ea8dc4b6Seschrock spa_close(spa, FTAG);
4252ea8dc4b6Seschrock
4253ea8dc4b6Seschrock /*
4254fa9e4066Sahrens * The pool will be in core if it's openable,
4255fa9e4066Sahrens * in which case we can modify its state.
4256fa9e4066Sahrens */
4257fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4258fa9e4066Sahrens /*
4259fa9e4066Sahrens * Objsets may be open only because they're dirty, so we
4260fa9e4066Sahrens * have to force it to sync before checking spa_refcnt.
4261fa9e4066Sahrens */
4262fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, 0);
4263bc9014e6SJustin Gibbs spa_evicting_os_wait(spa);
4264fa9e4066Sahrens
4265ea8dc4b6Seschrock /*
4266ea8dc4b6Seschrock * A pool cannot be exported or destroyed if there are active
4267ea8dc4b6Seschrock * references. If we are resetting a pool, allow references by
4268ea8dc4b6Seschrock * fault injection handlers.
4269ea8dc4b6Seschrock */
4270ea8dc4b6Seschrock if (!spa_refcount_zero(spa) ||
4271ea8dc4b6Seschrock (spa->spa_inject_ref != 0 &&
4272ea8dc4b6Seschrock new_state != POOL_STATE_UNINITIALIZED)) {
4273ea8dc4b6Seschrock spa_async_resume(spa);
4274fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
4275be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
4276fa9e4066Sahrens }
4277fa9e4066Sahrens
4278fa9e4066Sahrens /*
427989a89ebfSlling * A pool cannot be exported if it has an active shared spare.
428089a89ebfSlling * This is to prevent other pools stealing the active spare
428189a89ebfSlling * from an exported pool. At user's own will, such pool can
428289a89ebfSlling * be forcedly exported.
428389a89ebfSlling */
428489a89ebfSlling if (!force && new_state == POOL_STATE_EXPORTED &&
428589a89ebfSlling spa_has_active_shared_spare(spa)) {
428689a89ebfSlling spa_async_resume(spa);
428789a89ebfSlling mutex_exit(&spa_namespace_lock);
4288be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV));
428989a89ebfSlling }
429089a89ebfSlling
429189a89ebfSlling /*
4292fa9e4066Sahrens * We want this to be reflected on every label,
4293fa9e4066Sahrens * so mark them all dirty. spa_unload() will do the
4294fa9e4066Sahrens * final sync that pushes these changes out.
4295fa9e4066Sahrens */
4296394ab0cbSGeorge Wilson if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4297e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4298ea8dc4b6Seschrock spa->spa_state = new_state;
42993f9d6ad7SLin Ling spa->spa_final_txg = spa_last_synced_txg(spa) +
43003f9d6ad7SLin Ling TXG_DEFER_SIZE + 1;
4301fa9e4066Sahrens vdev_config_dirty(spa->spa_root_vdev);
4302e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
4303fa9e4066Sahrens }
4304ea8dc4b6Seschrock }
4305fa9e4066Sahrens
43063d7072f8Seschrock spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
43073d7072f8Seschrock
4308fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4309fa9e4066Sahrens spa_unload(spa);
4310fa9e4066Sahrens spa_deactivate(spa);
4311fa9e4066Sahrens }
4312fa9e4066Sahrens
431344cd46caSbillm if (oldconfig && spa->spa_config)
431444cd46caSbillm VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
431544cd46caSbillm
4316ea8dc4b6Seschrock if (new_state != POOL_STATE_UNINITIALIZED) {
4317394ab0cbSGeorge Wilson if (!hardforce)
4318c5904d13Seschrock spa_config_sync(spa, B_TRUE, B_TRUE);
4319fa9e4066Sahrens spa_remove(spa);
4320ea8dc4b6Seschrock }
4321fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
4322fa9e4066Sahrens
4323fa9e4066Sahrens return (0);
4324fa9e4066Sahrens }
4325fa9e4066Sahrens
4326fa9e4066Sahrens /*
4327fa9e4066Sahrens * Destroy a storage pool.
4328fa9e4066Sahrens */
4329fa9e4066Sahrens int
spa_destroy(char * pool)4330fa9e4066Sahrens spa_destroy(char *pool)
4331fa9e4066Sahrens {
4332394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4333394ab0cbSGeorge Wilson B_FALSE, B_FALSE));
4334fa9e4066Sahrens }
4335fa9e4066Sahrens
4336fa9e4066Sahrens /*
4337fa9e4066Sahrens * Export a storage pool.
4338fa9e4066Sahrens */
4339fa9e4066Sahrens int
spa_export(char * pool,nvlist_t ** oldconfig,boolean_t force,boolean_t hardforce)4340394ab0cbSGeorge Wilson spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4341394ab0cbSGeorge Wilson boolean_t hardforce)
4342fa9e4066Sahrens {
4343394ab0cbSGeorge Wilson return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4344394ab0cbSGeorge Wilson force, hardforce));
4345fa9e4066Sahrens }
4346fa9e4066Sahrens
4347fa9e4066Sahrens /*
4348ea8dc4b6Seschrock * Similar to spa_export(), this unloads the spa_t without actually removing it
4349ea8dc4b6Seschrock * from the namespace in any way.
4350ea8dc4b6Seschrock */
4351ea8dc4b6Seschrock int
spa_reset(char * pool)4352ea8dc4b6Seschrock spa_reset(char *pool)
4353ea8dc4b6Seschrock {
435489a89ebfSlling return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4355394ab0cbSGeorge Wilson B_FALSE, B_FALSE));
4356ea8dc4b6Seschrock }
4357ea8dc4b6Seschrock
4358ea8dc4b6Seschrock /*
4359fa9e4066Sahrens * ==========================================================================
4360fa9e4066Sahrens * Device manipulation
4361fa9e4066Sahrens * ==========================================================================
4362fa9e4066Sahrens */
4363fa9e4066Sahrens
4364fa9e4066Sahrens /*
43658654d025Sperrin * Add a device to a storage pool.
4366fa9e4066Sahrens */
4367fa9e4066Sahrens int
spa_vdev_add(spa_t * spa,nvlist_t * nvroot)4368fa9e4066Sahrens spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4369fa9e4066Sahrens {
437088ecc943SGeorge Wilson uint64_t txg, id;
43718ad4d6ddSJeff Bonwick int error;
4372fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev;
43730e34b6a7Sbonwick vdev_t *vd, *tvd;
4374fa94a07fSbrendan nvlist_t **spares, **l2cache;
4375fa94a07fSbrendan uint_t nspares, nl2cache;
4376fa9e4066Sahrens
4377f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
4378f9af39baSGeorge Wilson
4379fa9e4066Sahrens txg = spa_vdev_enter(spa);
4380fa9e4066Sahrens
438199653d4eSeschrock if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
438299653d4eSeschrock VDEV_ALLOC_ADD)) != 0)
438399653d4eSeschrock return (spa_vdev_exit(spa, NULL, txg, error));
4384fa9e4066Sahrens
4385e14bb325SJeff Bonwick spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
438699653d4eSeschrock
4387fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4388fa94a07fSbrendan &nspares) != 0)
438999653d4eSeschrock nspares = 0;
439099653d4eSeschrock
4391fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4392fa94a07fSbrendan &nl2cache) != 0)
4393fa94a07fSbrendan nl2cache = 0;
4394fa94a07fSbrendan
4395e14bb325SJeff Bonwick if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4396fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, EINVAL));
4397fa9e4066Sahrens
4398e14bb325SJeff Bonwick if (vd->vdev_children != 0 &&
4399e14bb325SJeff Bonwick (error = vdev_create(vd, txg, B_FALSE)) != 0)
4400fa9e4066Sahrens return (spa_vdev_exit(spa, vd, txg, error));
440139c23413Seschrock
440239c23413Seschrock /*
4403fa94a07fSbrendan * We must validate the spares and l2cache devices after checking the
4404fa94a07fSbrendan * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
440539c23413Seschrock */
4406e14bb325SJeff Bonwick if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
440739c23413Seschrock return (spa_vdev_exit(spa, vd, txg, error));
4408fa9e4066Sahrens
4409fa9e4066Sahrens /*
44100e34b6a7Sbonwick * Transfer each new top-level vdev from vd to rvd.
4411fa9e4066Sahrens */
44128ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++) {
441388ecc943SGeorge Wilson
441488ecc943SGeorge Wilson /*
441588ecc943SGeorge Wilson * Set the vdev id to the first hole, if one exists.
441688ecc943SGeorge Wilson */
441788ecc943SGeorge Wilson for (id = 0; id < rvd->vdev_children; id++) {
441888ecc943SGeorge Wilson if (rvd->vdev_child[id]->vdev_ishole) {
441988ecc943SGeorge Wilson vdev_free(rvd->vdev_child[id]);
442088ecc943SGeorge Wilson break;
442188ecc943SGeorge Wilson }
442288ecc943SGeorge Wilson }
44230e34b6a7Sbonwick tvd = vd->vdev_child[c];
4424fa9e4066Sahrens vdev_remove_child(vd, tvd);
442588ecc943SGeorge Wilson tvd->vdev_id = id;
4426fa9e4066Sahrens vdev_add_child(rvd, tvd);
4427fa9e4066Sahrens vdev_config_dirty(tvd);
4428fa9e4066Sahrens }
442999653d4eSeschrock
443099653d4eSeschrock if (nspares != 0) {
4431fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4432fa94a07fSbrendan ZPOOL_CONFIG_SPARES);
4433fa94a07fSbrendan spa_load_spares(spa);
4434fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE;
443599653d4eSeschrock }
443699653d4eSeschrock
4437fa94a07fSbrendan if (nl2cache != 0) {
4438fa94a07fSbrendan spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4439fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE);
4440fa94a07fSbrendan spa_load_l2cache(spa);
4441fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE;
444299653d4eSeschrock }
4443fa9e4066Sahrens
4444fa9e4066Sahrens /*
44450e34b6a7Sbonwick * We have to be careful when adding new vdevs to an existing pool.
44460e34b6a7Sbonwick * If other threads start allocating from these vdevs before we
44470e34b6a7Sbonwick * sync the config cache, and we lose power, then upon reboot we may
44480e34b6a7Sbonwick * fail to open the pool because there are DVAs that the config cache
44490e34b6a7Sbonwick * can't translate. Therefore, we first add the vdevs without
44500e34b6a7Sbonwick * initializing metaslabs; sync the config cache (via spa_vdev_exit());
44510373e76bSbonwick * and then let spa_config_update() initialize the new metaslabs.
44520e34b6a7Sbonwick *
44530e34b6a7Sbonwick * spa_load() checks for added-but-not-initialized vdevs, so that
44540e34b6a7Sbonwick * if we lose power at any point in this sequence, the remaining
44550e34b6a7Sbonwick * steps will be completed the next time we load the pool.
4456fa9e4066Sahrens */
44570e34b6a7Sbonwick (void) spa_vdev_exit(spa, vd, txg, 0);
44580e34b6a7Sbonwick
44590373e76bSbonwick mutex_enter(&spa_namespace_lock);
44600373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
446114372834SHans Rosenfeld spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
44620373e76bSbonwick mutex_exit(&spa_namespace_lock);
4463fa9e4066Sahrens
44640373e76bSbonwick return (0);
4465fa9e4066Sahrens }
4466fa9e4066Sahrens
4467fa9e4066Sahrens /*
4468fa9e4066Sahrens * Attach a device to a mirror. The arguments are the path to any device
4469fa9e4066Sahrens * in the mirror, and the nvroot for the new device. If the path specifies
4470fa9e4066Sahrens * a device that is not mirrored, we automatically insert the mirror vdev.
4471fa9e4066Sahrens *
4472fa9e4066Sahrens * If 'replacing' is specified, the new device is intended to replace the
4473fa9e4066Sahrens * existing device; in this case the two devices are made into their own
44743d7072f8Seschrock * mirror using the 'replacing' vdev, which is functionally identical to
4475fa9e4066Sahrens * the mirror vdev (it actually reuses all the same ops) but has a few
4476fa9e4066Sahrens * extra rules: you can't attach to it after it's been created, and upon
4477fa9e4066Sahrens * completion of resilvering, the first disk (the one being replaced)
4478fa9e4066Sahrens * is automatically detached.
4479fa9e4066Sahrens */
4480fa9e4066Sahrens int
spa_vdev_attach(spa_t * spa,uint64_t guid,nvlist_t * nvroot,int replacing)4481ea8dc4b6Seschrock spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4482fa9e4066Sahrens {
44833f9d6ad7SLin Ling uint64_t txg, dtl_max_txg;
4484fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev;
4485fa9e4066Sahrens vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
448699653d4eSeschrock vdev_ops_t *pvops;
44879b3f6b42SEric Kustarz char *oldvdpath, *newvdpath;
44889b3f6b42SEric Kustarz int newvd_isspare;
44899b3f6b42SEric Kustarz int error;
4490fa9e4066Sahrens
4491f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
4492f9af39baSGeorge Wilson
4493fa9e4066Sahrens txg = spa_vdev_enter(spa);
4494fa9e4066Sahrens
4495c5904d13Seschrock oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4496fa9e4066Sahrens
4497fa9e4066Sahrens if (oldvd == NULL)
4498fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4499fa9e4066Sahrens
45000e34b6a7Sbonwick if (!oldvd->vdev_ops->vdev_op_leaf)
45010e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
45020e34b6a7Sbonwick
4503fa9e4066Sahrens pvd = oldvd->vdev_parent;
4504fa9e4066Sahrens
450599653d4eSeschrock if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4506cd0837ccSGeorge Wilson VDEV_ALLOC_ATTACH)) != 0)
45073d7072f8Seschrock return (spa_vdev_exit(spa, NULL, txg, EINVAL));
45083d7072f8Seschrock
45093d7072f8Seschrock if (newrootvd->vdev_children != 1)
4510fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4511fa9e4066Sahrens
4512fa9e4066Sahrens newvd = newrootvd->vdev_child[0];
4513fa9e4066Sahrens
4514fa9e4066Sahrens if (!newvd->vdev_ops->vdev_op_leaf)
4515fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4516fa9e4066Sahrens
451799653d4eSeschrock if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4518fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, error));
4519fa9e4066Sahrens
45208654d025Sperrin /*
45218654d025Sperrin * Spares can't replace logs
45228654d025Sperrin */
4523ee0eb9f2SEric Schrock if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
45248654d025Sperrin return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
45258654d025Sperrin
452699653d4eSeschrock if (!replacing) {
452799653d4eSeschrock /*
452899653d4eSeschrock * For attach, the only allowable parent is a mirror or the root
452999653d4eSeschrock * vdev.
453099653d4eSeschrock */
453199653d4eSeschrock if (pvd->vdev_ops != &vdev_mirror_ops &&
453299653d4eSeschrock pvd->vdev_ops != &vdev_root_ops)
453399653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
453499653d4eSeschrock
453599653d4eSeschrock pvops = &vdev_mirror_ops;
453699653d4eSeschrock } else {
453799653d4eSeschrock /*
453899653d4eSeschrock * Active hot spares can only be replaced by inactive hot
453999653d4eSeschrock * spares.
454099653d4eSeschrock */
454199653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops &&
4542cb04b873SMark J Musante oldvd->vdev_isspare &&
454399653d4eSeschrock !spa_has_spare(spa, newvd->vdev_guid))
454499653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
454599653d4eSeschrock
454699653d4eSeschrock /*
454799653d4eSeschrock * If the source is a hot spare, and the parent isn't already a
454899653d4eSeschrock * spare, then we want to create a new hot spare. Otherwise, we
454939c23413Seschrock * want to create a replacing vdev. The user is not allowed to
455039c23413Seschrock * attach to a spared vdev child unless the 'isspare' state is
455139c23413Seschrock * the same (spare replaces spare, non-spare replaces
455239c23413Seschrock * non-spare).
455399653d4eSeschrock */
4554cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops &&
4555cb04b873SMark J Musante spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
455699653d4eSeschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4557cb04b873SMark J Musante } else if (pvd->vdev_ops == &vdev_spare_ops &&
4558cb04b873SMark J Musante newvd->vdev_isspare != oldvd->vdev_isspare) {
455939c23413Seschrock return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4560cb04b873SMark J Musante }
4561cb04b873SMark J Musante
4562cb04b873SMark J Musante if (newvd->vdev_isspare)
456399653d4eSeschrock pvops = &vdev_spare_ops;
456499653d4eSeschrock else
456599653d4eSeschrock pvops = &vdev_replacing_ops;
456699653d4eSeschrock }
456799653d4eSeschrock
45682a79c5feSlling /*
4569573ca77eSGeorge Wilson * Make sure the new device is big enough.
45702a79c5feSlling */
4571573ca77eSGeorge Wilson if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4572fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4573fa9e4066Sahrens
4574ecc2d604Sbonwick /*
4575ecc2d604Sbonwick * The new device cannot have a higher alignment requirement
4576ecc2d604Sbonwick * than the top-level vdev.
4577ecc2d604Sbonwick */
4578ecc2d604Sbonwick if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4579fa9e4066Sahrens return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4580fa9e4066Sahrens
4581fa9e4066Sahrens /*
4582fa9e4066Sahrens * If this is an in-place replacement, update oldvd's path and devid
4583fa9e4066Sahrens * to make it distinguishable from newvd, and unopenable from now on.
4584fa9e4066Sahrens */
4585fa9e4066Sahrens if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4586fa9e4066Sahrens spa_strfree(oldvd->vdev_path);
4587fa9e4066Sahrens oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4588fa9e4066Sahrens KM_SLEEP);
4589fa9e4066Sahrens (void) sprintf(oldvd->vdev_path, "%s/%s",
4590fa9e4066Sahrens newvd->vdev_path, "old");
4591fa9e4066Sahrens if (oldvd->vdev_devid != NULL) {
4592fa9e4066Sahrens spa_strfree(oldvd->vdev_devid);
4593fa9e4066Sahrens oldvd->vdev_devid = NULL;
4594fa9e4066Sahrens }
4595fa9e4066Sahrens }
4596fa9e4066Sahrens
4597cb04b873SMark J Musante /* mark the device being resilvered */
4598b4952e17SGeorge Wilson newvd->vdev_resilver_txg = txg;
4599cb04b873SMark J Musante
4600fa9e4066Sahrens /*
460199653d4eSeschrock * If the parent is not a mirror, or if we're replacing, insert the new
460299653d4eSeschrock * mirror/replacing/spare vdev above oldvd.
4603fa9e4066Sahrens */
4604fa9e4066Sahrens if (pvd->vdev_ops != pvops)
4605fa9e4066Sahrens pvd = vdev_add_parent(oldvd, pvops);
4606fa9e4066Sahrens
4607fa9e4066Sahrens ASSERT(pvd->vdev_top->vdev_parent == rvd);
4608fa9e4066Sahrens ASSERT(pvd->vdev_ops == pvops);
4609fa9e4066Sahrens ASSERT(oldvd->vdev_parent == pvd);
4610fa9e4066Sahrens
4611fa9e4066Sahrens /*
4612fa9e4066Sahrens * Extract the new device from its root and add it to pvd.
4613fa9e4066Sahrens */
4614fa9e4066Sahrens vdev_remove_child(newrootvd, newvd);
4615fa9e4066Sahrens newvd->vdev_id = pvd->vdev_children;
461688ecc943SGeorge Wilson newvd->vdev_crtxg = oldvd->vdev_crtxg;
4617fa9e4066Sahrens vdev_add_child(pvd, newvd);
4618fa9e4066Sahrens
4619fa9e4066Sahrens tvd = newvd->vdev_top;
4620fa9e4066Sahrens ASSERT(pvd->vdev_top == tvd);
4621fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd);
4622fa9e4066Sahrens
4623fa9e4066Sahrens vdev_config_dirty(tvd);
4624fa9e4066Sahrens
4625fa9e4066Sahrens /*
46263f9d6ad7SLin Ling * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
46273f9d6ad7SLin Ling * for any dmu_sync-ed blocks. It will propagate upward when
46283f9d6ad7SLin Ling * spa_vdev_exit() calls vdev_dtl_reassess().
4629fa9e4066Sahrens */
46303f9d6ad7SLin Ling dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4631fa9e4066Sahrens
46323f9d6ad7SLin Ling vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
46333f9d6ad7SLin Ling dtl_max_txg - TXG_INITIAL);
4634fa9e4066Sahrens
46356809eb4eSEric Schrock if (newvd->vdev_isspare) {
463639c23413Seschrock spa_spare_activate(newvd);
46376809eb4eSEric Schrock spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
46386809eb4eSEric Schrock }
46396809eb4eSEric Schrock
4640e14bb325SJeff Bonwick oldvdpath = spa_strdup(oldvd->vdev_path);
4641e14bb325SJeff Bonwick newvdpath = spa_strdup(newvd->vdev_path);
46429b3f6b42SEric Kustarz newvd_isspare = newvd->vdev_isspare;
4643ea8dc4b6Seschrock
4644fa9e4066Sahrens /*
4645fa9e4066Sahrens * Mark newvd's DTL dirty in this txg.
4646fa9e4066Sahrens */
4647ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, newvd, txg);
4648fa9e4066Sahrens
46493f9d6ad7SLin Ling /*
46500713e232SGeorge Wilson * Schedule the resilver to restart in the future. We do this to
46510713e232SGeorge Wilson * ensure that dmu_sync-ed blocks have been stitched into the
46520713e232SGeorge Wilson * respective datasets.
46533f9d6ad7SLin Ling */
46543f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4655fa9e4066Sahrens
465614372834SHans Rosenfeld if (spa->spa_bootfs)
465714372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
465814372834SHans Rosenfeld
465914372834SHans Rosenfeld spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
466014372834SHans Rosenfeld
46613f9d6ad7SLin Ling /*
46623f9d6ad7SLin Ling * Commit the config
46633f9d6ad7SLin Ling */
46643f9d6ad7SLin Ling (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
46653f9d6ad7SLin Ling
46664445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev attach", NULL,
46673f9d6ad7SLin Ling "%s vdev=%s %s vdev=%s",
46689b3f6b42SEric Kustarz replacing && newvd_isspare ? "spare in" :
46699b3f6b42SEric Kustarz replacing ? "replace" : "attach", newvdpath,
46709b3f6b42SEric Kustarz replacing ? "for" : "to", oldvdpath);
46719b3f6b42SEric Kustarz
46729b3f6b42SEric Kustarz spa_strfree(oldvdpath);
46739b3f6b42SEric Kustarz spa_strfree(newvdpath);
46749b3f6b42SEric Kustarz
4675fa9e4066Sahrens return (0);
4676fa9e4066Sahrens }
4677fa9e4066Sahrens
4678fa9e4066Sahrens /*
4679fa9e4066Sahrens * Detach a device from a mirror or replacing vdev.
4680f7170741SWill Andrews *
4681fa9e4066Sahrens * If 'replace_done' is specified, only detach if the parent
4682fa9e4066Sahrens * is a replacing vdev.
4683fa9e4066Sahrens */
4684fa9e4066Sahrens int
spa_vdev_detach(spa_t * spa,uint64_t guid,uint64_t pguid,int replace_done)46858ad4d6ddSJeff Bonwick spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4686fa9e4066Sahrens {
4687fa9e4066Sahrens uint64_t txg;
46888ad4d6ddSJeff Bonwick int error;
4689fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev;
4690fa9e4066Sahrens vdev_t *vd, *pvd, *cvd, *tvd;
469199653d4eSeschrock boolean_t unspare = B_FALSE;
4692d5285caeSGeorge Wilson uint64_t unspare_guid = 0;
46931195e687SMark J Musante char *vdpath;
4694fa9e4066Sahrens
4695f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
4696f9af39baSGeorge Wilson
4697fa9e4066Sahrens txg = spa_vdev_enter(spa);
4698fa9e4066Sahrens
4699c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4700fa9e4066Sahrens
4701fa9e4066Sahrens if (vd == NULL)
4702fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4703fa9e4066Sahrens
47040e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf)
47050e34b6a7Sbonwick return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
47060e34b6a7Sbonwick
4707fa9e4066Sahrens pvd = vd->vdev_parent;
4708fa9e4066Sahrens
4709fa9e4066Sahrens /*
47108ad4d6ddSJeff Bonwick * If the parent/child relationship is not as expected, don't do it.
47118ad4d6ddSJeff Bonwick * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
47128ad4d6ddSJeff Bonwick * vdev that's replacing B with C. The user's intent in replacing
47138ad4d6ddSJeff Bonwick * is to go from M(A,B) to M(A,C). If the user decides to cancel
47148ad4d6ddSJeff Bonwick * the replace by detaching C, the expected behavior is to end up
47158ad4d6ddSJeff Bonwick * M(A,B). But suppose that right after deciding to detach C,
47168ad4d6ddSJeff Bonwick * the replacement of B completes. We would have M(A,C), and then
47178ad4d6ddSJeff Bonwick * ask to detach C, which would leave us with just A -- not what
47188ad4d6ddSJeff Bonwick * the user wanted. To prevent this, we make sure that the
47198ad4d6ddSJeff Bonwick * parent/child relationship hasn't changed -- in this example,
47208ad4d6ddSJeff Bonwick * that C's parent is still the replacing vdev R.
47218ad4d6ddSJeff Bonwick */
47228ad4d6ddSJeff Bonwick if (pvd->vdev_guid != pguid && pguid != 0)
47238ad4d6ddSJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, EBUSY));
47248ad4d6ddSJeff Bonwick
47258ad4d6ddSJeff Bonwick /*
4726cb04b873SMark J Musante * Only 'replacing' or 'spare' vdevs can be replaced.
4727fa9e4066Sahrens */
4728cb04b873SMark J Musante if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4729cb04b873SMark J Musante pvd->vdev_ops != &vdev_spare_ops)
4730fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
473199653d4eSeschrock
473299653d4eSeschrock ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4733e7437265Sahrens spa_version(spa) >= SPA_VERSION_SPARES);
4734fa9e4066Sahrens
4735fa9e4066Sahrens /*
473699653d4eSeschrock * Only mirror, replacing, and spare vdevs support detach.
4737fa9e4066Sahrens */
4738fa9e4066Sahrens if (pvd->vdev_ops != &vdev_replacing_ops &&
473999653d4eSeschrock pvd->vdev_ops != &vdev_mirror_ops &&
474099653d4eSeschrock pvd->vdev_ops != &vdev_spare_ops)
4741fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4742fa9e4066Sahrens
4743fa9e4066Sahrens /*
47448ad4d6ddSJeff Bonwick * If this device has the only valid copy of some data,
47458ad4d6ddSJeff Bonwick * we cannot safely detach it.
4746fa9e4066Sahrens */
47478ad4d6ddSJeff Bonwick if (vdev_dtl_required(vd))
4748fa9e4066Sahrens return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4749fa9e4066Sahrens
47508ad4d6ddSJeff Bonwick ASSERT(pvd->vdev_children >= 2);
4751fa9e4066Sahrens
4752fa9e4066Sahrens /*
4753bf82a41bSeschrock * If we are detaching the second disk from a replacing vdev, then
4754bf82a41bSeschrock * check to see if we changed the original vdev's path to have "/old"
4755bf82a41bSeschrock * at the end in spa_vdev_attach(). If so, undo that change now.
4756bf82a41bSeschrock */
4757cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4758cb04b873SMark J Musante vd->vdev_path != NULL) {
4759cb04b873SMark J Musante size_t len = strlen(vd->vdev_path);
4760cb04b873SMark J Musante
4761cb04b873SMark J Musante for (int c = 0; c < pvd->vdev_children; c++) {
4762cb04b873SMark J Musante cvd = pvd->vdev_child[c];
4763cb04b873SMark J Musante
4764cb04b873SMark J Musante if (cvd == vd || cvd->vdev_path == NULL)
4765cb04b873SMark J Musante continue;
4766cb04b873SMark J Musante
4767bf82a41bSeschrock if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4768bf82a41bSeschrock strcmp(cvd->vdev_path + len, "/old") == 0) {
4769bf82a41bSeschrock spa_strfree(cvd->vdev_path);
4770bf82a41bSeschrock cvd->vdev_path = spa_strdup(vd->vdev_path);
4771cb04b873SMark J Musante break;
4772cb04b873SMark J Musante }
4773bf82a41bSeschrock }
4774bf82a41bSeschrock }
4775bf82a41bSeschrock
4776bf82a41bSeschrock /*
477799653d4eSeschrock * If we are detaching the original disk from a spare, then it implies
477899653d4eSeschrock * that the spare should become a real disk, and be removed from the
477999653d4eSeschrock * active spare list for the pool.
478099653d4eSeschrock */
478199653d4eSeschrock if (pvd->vdev_ops == &vdev_spare_ops &&
4782cb04b873SMark J Musante vd->vdev_id == 0 &&
4783cb04b873SMark J Musante pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
478499653d4eSeschrock unspare = B_TRUE;
478599653d4eSeschrock
478699653d4eSeschrock /*
4787fa9e4066Sahrens * Erase the disk labels so the disk can be used for other things.
4788fa9e4066Sahrens * This must be done after all other error cases are handled,
4789fa9e4066Sahrens * but before we disembowel vd (so we can still do I/O to it).
4790fa9e4066Sahrens * But if we can't do it, don't treat the error as fatal --
4791fa9e4066Sahrens * it may be that the unwritability of the disk is the reason
4792fa9e4066Sahrens * it's being detached!
4793fa9e4066Sahrens */
479439c23413Seschrock error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4795fa9e4066Sahrens
4796fa9e4066Sahrens /*
4797fa9e4066Sahrens * Remove vd from its parent and compact the parent's children.
4798fa9e4066Sahrens */
4799fa9e4066Sahrens vdev_remove_child(pvd, vd);
4800fa9e4066Sahrens vdev_compact_children(pvd);
4801fa9e4066Sahrens
4802fa9e4066Sahrens /*
4803fa9e4066Sahrens * Remember one of the remaining children so we can get tvd below.
4804fa9e4066Sahrens */
4805cb04b873SMark J Musante cvd = pvd->vdev_child[pvd->vdev_children - 1];
4806fa9e4066Sahrens
4807fa9e4066Sahrens /*
480899653d4eSeschrock * If we need to remove the remaining child from the list of hot spares,
48098ad4d6ddSJeff Bonwick * do it now, marking the vdev as no longer a spare in the process.
48108ad4d6ddSJeff Bonwick * We must do this before vdev_remove_parent(), because that can
48118ad4d6ddSJeff Bonwick * change the GUID if it creates a new toplevel GUID. For a similar
48128ad4d6ddSJeff Bonwick * reason, we must remove the spare now, in the same txg as the detach;
48138ad4d6ddSJeff Bonwick * otherwise someone could attach a new sibling, change the GUID, and
48148ad4d6ddSJeff Bonwick * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
481599653d4eSeschrock */
481699653d4eSeschrock if (unspare) {
481799653d4eSeschrock ASSERT(cvd->vdev_isspare);
481839c23413Seschrock spa_spare_remove(cvd);
481999653d4eSeschrock unspare_guid = cvd->vdev_guid;
48208ad4d6ddSJeff Bonwick (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4821cb04b873SMark J Musante cvd->vdev_unspare = B_TRUE;
482299653d4eSeschrock }
482399653d4eSeschrock
482499653d4eSeschrock /*
4825fa9e4066Sahrens * If the parent mirror/replacing vdev only has one child,
4826fa9e4066Sahrens * the parent is no longer needed. Remove it from the tree.
4827fa9e4066Sahrens */
4828cb04b873SMark J Musante if (pvd->vdev_children == 1) {
4829cb04b873SMark J Musante if (pvd->vdev_ops == &vdev_spare_ops)
4830cb04b873SMark J Musante cvd->vdev_unspare = B_FALSE;
4831fa9e4066Sahrens vdev_remove_parent(cvd);
4832cb04b873SMark J Musante }
4833cb04b873SMark J Musante
4834fa9e4066Sahrens
4835fa9e4066Sahrens /*
4836fa9e4066Sahrens * We don't set tvd until now because the parent we just removed
4837fa9e4066Sahrens * may have been the previous top-level vdev.
4838fa9e4066Sahrens */
4839fa9e4066Sahrens tvd = cvd->vdev_top;
4840fa9e4066Sahrens ASSERT(tvd->vdev_parent == rvd);
4841fa9e4066Sahrens
4842fa9e4066Sahrens /*
484339c23413Seschrock * Reevaluate the parent vdev state.
4844fa9e4066Sahrens */
48453d7072f8Seschrock vdev_propagate_state(cvd);
4846fa9e4066Sahrens
4847fa9e4066Sahrens /*
4848573ca77eSGeorge Wilson * If the 'autoexpand' property is set on the pool then automatically
4849573ca77eSGeorge Wilson * try to expand the size of the pool. For example if the device we
4850573ca77eSGeorge Wilson * just detached was smaller than the others, it may be possible to
4851573ca77eSGeorge Wilson * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4852573ca77eSGeorge Wilson * first so that we can obtain the updated sizes of the leaf vdevs.
4853fa9e4066Sahrens */
4854573ca77eSGeorge Wilson if (spa->spa_autoexpand) {
4855573ca77eSGeorge Wilson vdev_reopen(tvd);
4856573ca77eSGeorge Wilson vdev_expand(tvd, txg);
4857573ca77eSGeorge Wilson }
4858fa9e4066Sahrens
4859fa9e4066Sahrens vdev_config_dirty(tvd);
4860fa9e4066Sahrens
4861fa9e4066Sahrens /*
486239c23413Seschrock * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
486339c23413Seschrock * vd->vdev_detached is set and free vd's DTL object in syncing context.
486439c23413Seschrock * But first make sure we're not on any *other* txg's DTL list, to
486539c23413Seschrock * prevent vd from being accessed after it's freed.
4866fa9e4066Sahrens */
48671195e687SMark J Musante vdpath = spa_strdup(vd->vdev_path);
48688ad4d6ddSJeff Bonwick for (int t = 0; t < TXG_SIZE; t++)
4869fa9e4066Sahrens (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4870ecc2d604Sbonwick vd->vdev_detached = B_TRUE;
4871ecc2d604Sbonwick vdev_dirty(tvd, VDD_DTL, vd, txg);
4872fa9e4066Sahrens
48733d7072f8Seschrock spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
48743d7072f8Seschrock
4875cb04b873SMark J Musante /* hang on to the spa before we release the lock */
4876cb04b873SMark J Musante spa_open_ref(spa, FTAG);
4877cb04b873SMark J Musante
487899653d4eSeschrock error = spa_vdev_exit(spa, vd, txg, 0);
487999653d4eSeschrock
48804445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", NULL,
48811195e687SMark J Musante "vdev=%s", vdpath);
48821195e687SMark J Musante spa_strfree(vdpath);
48831195e687SMark J Musante
488499653d4eSeschrock /*
488539c23413Seschrock * If this was the removal of the original device in a hot spare vdev,
488639c23413Seschrock * then we want to go through and remove the device from the hot spare
488739c23413Seschrock * list of every other pool.
488899653d4eSeschrock */
488999653d4eSeschrock if (unspare) {
4890cb04b873SMark J Musante spa_t *altspa = NULL;
4891cb04b873SMark J Musante
489299653d4eSeschrock mutex_enter(&spa_namespace_lock);
4893cb04b873SMark J Musante while ((altspa = spa_next(altspa)) != NULL) {
4894cb04b873SMark J Musante if (altspa->spa_state != POOL_STATE_ACTIVE ||
4895cb04b873SMark J Musante altspa == spa)
489699653d4eSeschrock continue;
4897cb04b873SMark J Musante
4898cb04b873SMark J Musante spa_open_ref(altspa, FTAG);
48999af0a4dfSJeff Bonwick mutex_exit(&spa_namespace_lock);
4900cb04b873SMark J Musante (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4901cb04b873SMark J Musante mutex_enter(&spa_namespace_lock);
4902cb04b873SMark J Musante spa_close(altspa, FTAG);
4903cb04b873SMark J Musante }
4904cb04b873SMark J Musante mutex_exit(&spa_namespace_lock);
4905cb04b873SMark J Musante
4906cb04b873SMark J Musante /* search the rest of the vdevs for spares to remove */
4907cb04b873SMark J Musante spa_vdev_resilver_done(spa);
4908cb04b873SMark J Musante }
4909cb04b873SMark J Musante
4910cb04b873SMark J Musante /* all done with the spa; OK to release */
49119af0a4dfSJeff Bonwick mutex_enter(&spa_namespace_lock);
49129af0a4dfSJeff Bonwick spa_close(spa, FTAG);
491399653d4eSeschrock mutex_exit(&spa_namespace_lock);
491499653d4eSeschrock
491599653d4eSeschrock return (error);
491699653d4eSeschrock }
491799653d4eSeschrock
49181195e687SMark J Musante /*
49191195e687SMark J Musante * Split a set of devices from their mirrors, and create a new pool from them.
49201195e687SMark J Musante */
49211195e687SMark J Musante int
spa_vdev_split_mirror(spa_t * spa,char * newname,nvlist_t * config,nvlist_t * props,boolean_t exp)49221195e687SMark J Musante spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
49231195e687SMark J Musante nvlist_t *props, boolean_t exp)
49241195e687SMark J Musante {
49251195e687SMark J Musante int error = 0;
49261195e687SMark J Musante uint64_t txg, *glist;
49271195e687SMark J Musante spa_t *newspa;
49281195e687SMark J Musante uint_t c, children, lastlog;
49291195e687SMark J Musante nvlist_t **child, *nvl, *tmp;
49301195e687SMark J Musante dmu_tx_t *tx;
49311195e687SMark J Musante char *altroot = NULL;
49321195e687SMark J Musante vdev_t *rvd, **vml = NULL; /* vdev modify list */
49331195e687SMark J Musante boolean_t activate_slog;
49341195e687SMark J Musante
4935f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
49361195e687SMark J Musante
49371195e687SMark J Musante txg = spa_vdev_enter(spa);
49381195e687SMark J Musante
49391195e687SMark J Musante /* clear the log and flush everything up to now */
49401195e687SMark J Musante activate_slog = spa_passivate_log(spa);
49411195e687SMark J Musante (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
49421195e687SMark J Musante error = spa_offline_log(spa);
49431195e687SMark J Musante txg = spa_vdev_config_enter(spa);
49441195e687SMark J Musante
49451195e687SMark J Musante if (activate_slog)
49461195e687SMark J Musante spa_activate_log(spa);
49471195e687SMark J Musante
49481195e687SMark J Musante if (error != 0)
49491195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error));
49501195e687SMark J Musante
49511195e687SMark J Musante /* check new spa name before going any further */
49521195e687SMark J Musante if (spa_lookup(newname) != NULL)
49531195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EEXIST));
49541195e687SMark J Musante
49551195e687SMark J Musante /*
49561195e687SMark J Musante * scan through all the children to ensure they're all mirrors
49571195e687SMark J Musante */
49581195e687SMark J Musante if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
49591195e687SMark J Musante nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
49601195e687SMark J Musante &children) != 0)
49611195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49621195e687SMark J Musante
49631195e687SMark J Musante /* first, check to ensure we've got the right child count */
49641195e687SMark J Musante rvd = spa->spa_root_vdev;
49651195e687SMark J Musante lastlog = 0;
49661195e687SMark J Musante for (c = 0; c < rvd->vdev_children; c++) {
49671195e687SMark J Musante vdev_t *vd = rvd->vdev_child[c];
49681195e687SMark J Musante
49691195e687SMark J Musante /* don't count the holes & logs as children */
49701195e687SMark J Musante if (vd->vdev_islog || vd->vdev_ishole) {
49711195e687SMark J Musante if (lastlog == 0)
49721195e687SMark J Musante lastlog = c;
49731195e687SMark J Musante continue;
49741195e687SMark J Musante }
49751195e687SMark J Musante
49761195e687SMark J Musante lastlog = 0;
49771195e687SMark J Musante }
49781195e687SMark J Musante if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
49791195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49801195e687SMark J Musante
49811195e687SMark J Musante /* next, ensure no spare or cache devices are part of the split */
49821195e687SMark J Musante if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
49831195e687SMark J Musante nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
49841195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, EINVAL));
49851195e687SMark J Musante
49861195e687SMark J Musante vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
49871195e687SMark J Musante glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
49881195e687SMark J Musante
49891195e687SMark J Musante /* then, loop over each vdev and validate it */
49901195e687SMark J Musante for (c = 0; c < children; c++) {
49911195e687SMark J Musante uint64_t is_hole = 0;
49921195e687SMark J Musante
49931195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
49941195e687SMark J Musante &is_hole);
49951195e687SMark J Musante
49961195e687SMark J Musante if (is_hole != 0) {
49971195e687SMark J Musante if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
49981195e687SMark J Musante spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
49991195e687SMark J Musante continue;
50001195e687SMark J Musante } else {
5001be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
50021195e687SMark J Musante break;
50031195e687SMark J Musante }
50041195e687SMark J Musante }
50051195e687SMark J Musante
50061195e687SMark J Musante /* which disk is going to be split? */
50071195e687SMark J Musante if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
50081195e687SMark J Musante &glist[c]) != 0) {
5009be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
50101195e687SMark J Musante break;
50111195e687SMark J Musante }
50121195e687SMark J Musante
50131195e687SMark J Musante /* look it up in the spa */
50141195e687SMark J Musante vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
50151195e687SMark J Musante if (vml[c] == NULL) {
5016be6fd75aSMatthew Ahrens error = SET_ERROR(ENODEV);
50171195e687SMark J Musante break;
50181195e687SMark J Musante }
50191195e687SMark J Musante
50201195e687SMark J Musante /* make sure there's nothing stopping the split */
50211195e687SMark J Musante if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
50221195e687SMark J Musante vml[c]->vdev_islog ||
50231195e687SMark J Musante vml[c]->vdev_ishole ||
50241195e687SMark J Musante vml[c]->vdev_isspare ||
50251195e687SMark J Musante vml[c]->vdev_isl2cache ||
50261195e687SMark J Musante !vdev_writeable(vml[c]) ||
5027d41c4376SMark J Musante vml[c]->vdev_children != 0 ||
50281195e687SMark J Musante vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
50291195e687SMark J Musante c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5030be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL);
50311195e687SMark J Musante break;
50321195e687SMark J Musante }
50331195e687SMark J Musante
50341195e687SMark J Musante if (vdev_dtl_required(vml[c])) {
5035be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY);
50361195e687SMark J Musante break;
50371195e687SMark J Musante }
50381195e687SMark J Musante
50391195e687SMark J Musante /* we need certain info from the top level */
50401195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
50411195e687SMark J Musante vml[c]->vdev_top->vdev_ms_array) == 0);
50421195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
50431195e687SMark J Musante vml[c]->vdev_top->vdev_ms_shift) == 0);
50441195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
50451195e687SMark J Musante vml[c]->vdev_top->vdev_asize) == 0);
50461195e687SMark J Musante VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
50471195e687SMark J Musante vml[c]->vdev_top->vdev_ashift) == 0);
50481195e687SMark J Musante }
50491195e687SMark J Musante
50501195e687SMark J Musante if (error != 0) {
50511195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *));
50521195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t));
50531195e687SMark J Musante return (spa_vdev_exit(spa, NULL, txg, error));
50541195e687SMark J Musante }
50551195e687SMark J Musante
50561195e687SMark J Musante /* stop writers from using the disks */
50571195e687SMark J Musante for (c = 0; c < children; c++) {
50581195e687SMark J Musante if (vml[c] != NULL)
50591195e687SMark J Musante vml[c]->vdev_offline = B_TRUE;
50601195e687SMark J Musante }
50611195e687SMark J Musante vdev_reopen(spa->spa_root_vdev);
50621195e687SMark J Musante
50631195e687SMark J Musante /*
50641195e687SMark J Musante * Temporarily record the splitting vdevs in the spa config. This
50651195e687SMark J Musante * will disappear once the config is regenerated.
50661195e687SMark J Musante */
50671195e687SMark J Musante VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
50681195e687SMark J Musante VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
50691195e687SMark J Musante glist, children) == 0);
50701195e687SMark J Musante kmem_free(glist, children * sizeof (uint64_t));
50711195e687SMark J Musante
507298295d61SMark J Musante mutex_enter(&spa->spa_props_lock);
50731195e687SMark J Musante VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
50741195e687SMark J Musante nvl) == 0);
507598295d61SMark J Musante mutex_exit(&spa->spa_props_lock);
50761195e687SMark J Musante spa->spa_config_splitting = nvl;
50771195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev);
50781195e687SMark J Musante
50791195e687SMark J Musante /* configure and create the new pool */
50801195e687SMark J Musante VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
50811195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
50821195e687SMark J Musante exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
50831195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
50841195e687SMark J Musante spa_version(spa)) == 0);
50851195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
50861195e687SMark J Musante spa->spa_config_txg) == 0);
50871195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
50881195e687SMark J Musante spa_generate_guid(NULL)) == 0);
50891195e687SMark J Musante (void) nvlist_lookup_string(props,
50901195e687SMark J Musante zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
50911195e687SMark J Musante
5092d41c4376SMark J Musante /* add the new pool to the namespace */
50931195e687SMark J Musante newspa = spa_add(newname, config, altroot);
50941195e687SMark J Musante newspa->spa_config_txg = spa->spa_config_txg;
50951195e687SMark J Musante spa_set_log_state(newspa, SPA_LOG_CLEAR);
50961195e687SMark J Musante
50971195e687SMark J Musante /* release the spa config lock, retaining the namespace lock */
50981195e687SMark J Musante spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
50991195e687SMark J Musante
51001195e687SMark J Musante if (zio_injection_enabled)
51011195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 1);
51021195e687SMark J Musante
51031195e687SMark J Musante spa_activate(newspa, spa_mode_global);
51041195e687SMark J Musante spa_async_suspend(newspa);
51051195e687SMark J Musante
51061195e687SMark J Musante /* create the new pool from the disks of the original pool */
51071195e687SMark J Musante error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
51081195e687SMark J Musante if (error)
51091195e687SMark J Musante goto out;
51101195e687SMark J Musante
51111195e687SMark J Musante /* if that worked, generate a real config for the new pool */
51121195e687SMark J Musante if (newspa->spa_root_vdev != NULL) {
51131195e687SMark J Musante VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
51141195e687SMark J Musante NV_UNIQUE_NAME, KM_SLEEP) == 0);
51151195e687SMark J Musante VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
51161195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
51171195e687SMark J Musante spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
51181195e687SMark J Musante B_TRUE));
51191195e687SMark J Musante }
51201195e687SMark J Musante
51211195e687SMark J Musante /* set the props */
51221195e687SMark J Musante if (props != NULL) {
51231195e687SMark J Musante spa_configfile_set(newspa, props, B_FALSE);
51241195e687SMark J Musante error = spa_prop_set(newspa, props);
51251195e687SMark J Musante if (error)
51261195e687SMark J Musante goto out;
51271195e687SMark J Musante }
51281195e687SMark J Musante
51291195e687SMark J Musante /* flush everything */
51301195e687SMark J Musante txg = spa_vdev_config_enter(newspa);
51311195e687SMark J Musante vdev_config_dirty(newspa->spa_root_vdev);
51321195e687SMark J Musante (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
51331195e687SMark J Musante
51341195e687SMark J Musante if (zio_injection_enabled)
51351195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 2);
51361195e687SMark J Musante
51371195e687SMark J Musante spa_async_resume(newspa);
51381195e687SMark J Musante
51391195e687SMark J Musante /* finally, update the original pool's config */
51401195e687SMark J Musante txg = spa_vdev_config_enter(spa);
51411195e687SMark J Musante tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
51421195e687SMark J Musante error = dmu_tx_assign(tx, TXG_WAIT);
51431195e687SMark J Musante if (error != 0)
51441195e687SMark J Musante dmu_tx_abort(tx);
51451195e687SMark J Musante for (c = 0; c < children; c++) {
51461195e687SMark J Musante if (vml[c] != NULL) {
51471195e687SMark J Musante vdev_split(vml[c]);
51481195e687SMark J Musante if (error == 0)
51494445fffbSMatthew Ahrens spa_history_log_internal(spa, "detach", tx,
51504445fffbSMatthew Ahrens "vdev=%s", vml[c]->vdev_path);
51511195e687SMark J Musante vdev_free(vml[c]);
51521195e687SMark J Musante }
51531195e687SMark J Musante }
51541195e687SMark J Musante vdev_config_dirty(spa->spa_root_vdev);
51551195e687SMark J Musante spa->spa_config_splitting = NULL;
51561195e687SMark J Musante nvlist_free(nvl);
51571195e687SMark J Musante if (error == 0)
51581195e687SMark J Musante dmu_tx_commit(tx);
51591195e687SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, 0);
51601195e687SMark J Musante
51611195e687SMark J Musante if (zio_injection_enabled)
51621195e687SMark J Musante zio_handle_panic_injection(spa, FTAG, 3);
51631195e687SMark J Musante
51641195e687SMark J Musante /* split is complete; log a history record */
51654445fffbSMatthew Ahrens spa_history_log_internal(newspa, "split", NULL,
51664445fffbSMatthew Ahrens "from pool %s", spa_name(spa));
51671195e687SMark J Musante
51681195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *));
51691195e687SMark J Musante
51701195e687SMark J Musante /* if we're not going to mount the filesystems in userland, export */
51711195e687SMark J Musante if (exp)
51721195e687SMark J Musante error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
51731195e687SMark J Musante B_FALSE, B_FALSE);
51741195e687SMark J Musante
51751195e687SMark J Musante return (error);
51761195e687SMark J Musante
51771195e687SMark J Musante out:
51781195e687SMark J Musante spa_unload(newspa);
51791195e687SMark J Musante spa_deactivate(newspa);
51801195e687SMark J Musante spa_remove(newspa);
51811195e687SMark J Musante
51821195e687SMark J Musante txg = spa_vdev_config_enter(spa);
518398295d61SMark J Musante
518498295d61SMark J Musante /* re-online all offlined disks */
518598295d61SMark J Musante for (c = 0; c < children; c++) {
518698295d61SMark J Musante if (vml[c] != NULL)
518798295d61SMark J Musante vml[c]->vdev_offline = B_FALSE;
518898295d61SMark J Musante }
518998295d61SMark J Musante vdev_reopen(spa->spa_root_vdev);
519098295d61SMark J Musante
51911195e687SMark J Musante nvlist_free(spa->spa_config_splitting);
51921195e687SMark J Musante spa->spa_config_splitting = NULL;
5193d41c4376SMark J Musante (void) spa_vdev_exit(spa, NULL, txg, error);
51941195e687SMark J Musante
51951195e687SMark J Musante kmem_free(vml, children * sizeof (vdev_t *));
51961195e687SMark J Musante return (error);
51971195e687SMark J Musante }
51981195e687SMark J Musante
5199e14bb325SJeff Bonwick static nvlist_t *
spa_nvlist_lookup_by_guid(nvlist_t ** nvpp,int count,uint64_t target_guid)5200e14bb325SJeff Bonwick spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
520199653d4eSeschrock {
5202e14bb325SJeff Bonwick for (int i = 0; i < count; i++) {
5203e14bb325SJeff Bonwick uint64_t guid;
520499653d4eSeschrock
5205e14bb325SJeff Bonwick VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5206e14bb325SJeff Bonwick &guid) == 0);
520799653d4eSeschrock
5208e14bb325SJeff Bonwick if (guid == target_guid)
5209e14bb325SJeff Bonwick return (nvpp[i]);
521099653d4eSeschrock }
521199653d4eSeschrock
5212e14bb325SJeff Bonwick return (NULL);
521399653d4eSeschrock }
521499653d4eSeschrock
5215e14bb325SJeff Bonwick static void
spa_vdev_remove_aux(nvlist_t * config,char * name,nvlist_t ** dev,int count,nvlist_t * dev_to_remove)5216e14bb325SJeff Bonwick spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5217e14bb325SJeff Bonwick nvlist_t *dev_to_remove)
5218fa94a07fSbrendan {
5219e14bb325SJeff Bonwick nvlist_t **newdev = NULL;
5220fa94a07fSbrendan
5221e14bb325SJeff Bonwick if (count > 1)
5222e14bb325SJeff Bonwick newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5223fa94a07fSbrendan
5224e14bb325SJeff Bonwick for (int i = 0, j = 0; i < count; i++) {
5225e14bb325SJeff Bonwick if (dev[i] == dev_to_remove)
5226e14bb325SJeff Bonwick continue;
5227e14bb325SJeff Bonwick VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5228fa94a07fSbrendan }
5229fa94a07fSbrendan
5230e14bb325SJeff Bonwick VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5231e14bb325SJeff Bonwick VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5232fa94a07fSbrendan
5233e14bb325SJeff Bonwick for (int i = 0; i < count - 1; i++)
5234e14bb325SJeff Bonwick nvlist_free(newdev[i]);
5235fa94a07fSbrendan
5236e14bb325SJeff Bonwick if (count > 1)
5237e14bb325SJeff Bonwick kmem_free(newdev, (count - 1) * sizeof (void *));
5238fa94a07fSbrendan }
5239fa94a07fSbrendan
5240fa94a07fSbrendan /*
524188ecc943SGeorge Wilson * Evacuate the device.
524288ecc943SGeorge Wilson */
52433f9d6ad7SLin Ling static int
spa_vdev_remove_evacuate(spa_t * spa,vdev_t * vd)524488ecc943SGeorge Wilson spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
524588ecc943SGeorge Wilson {
524688ecc943SGeorge Wilson uint64_t txg;
52473f9d6ad7SLin Ling int error = 0;
524888ecc943SGeorge Wilson
524988ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock));
525088ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5251b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top);
525288ecc943SGeorge Wilson
525388ecc943SGeorge Wilson /*
525488ecc943SGeorge Wilson * Evacuate the device. We don't hold the config lock as writer
525588ecc943SGeorge Wilson * since we need to do I/O but we do keep the
525688ecc943SGeorge Wilson * spa_namespace_lock held. Once this completes the device
525788ecc943SGeorge Wilson * should no longer have any blocks allocated on it.
525888ecc943SGeorge Wilson */
525988ecc943SGeorge Wilson if (vd->vdev_islog) {
52603f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0)
52613f9d6ad7SLin Ling error = spa_offline_log(spa);
5262a1521560SJeff Bonwick } else {
5263be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
526488ecc943SGeorge Wilson }
5265a1521560SJeff Bonwick
5266a1521560SJeff Bonwick if (error)
5267a1521560SJeff Bonwick return (error);
526888ecc943SGeorge Wilson
526988ecc943SGeorge Wilson /*
5270a1521560SJeff Bonwick * The evacuation succeeded. Remove any remaining MOS metadata
5271a1521560SJeff Bonwick * associated with this vdev, and wait for these changes to sync.
527288ecc943SGeorge Wilson */
5273fb09f5aaSMadhav Suresh ASSERT0(vd->vdev_stat.vs_alloc);
527488ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa);
527588ecc943SGeorge Wilson vd->vdev_removing = B_TRUE;
52760713e232SGeorge Wilson vdev_dirty_leaves(vd, VDD_DTL, txg);
527788ecc943SGeorge Wilson vdev_config_dirty(vd);
527888ecc943SGeorge Wilson spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
527988ecc943SGeorge Wilson
528088ecc943SGeorge Wilson return (0);
528188ecc943SGeorge Wilson }
528288ecc943SGeorge Wilson
528388ecc943SGeorge Wilson /*
528488ecc943SGeorge Wilson * Complete the removal by cleaning up the namespace.
528588ecc943SGeorge Wilson */
52863f9d6ad7SLin Ling static void
spa_vdev_remove_from_namespace(spa_t * spa,vdev_t * vd)5287a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
528888ecc943SGeorge Wilson {
528988ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
529088ecc943SGeorge Wilson uint64_t id = vd->vdev_id;
529188ecc943SGeorge Wilson boolean_t last_vdev = (id == (rvd->vdev_children - 1));
529288ecc943SGeorge Wilson
529388ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock));
529488ecc943SGeorge Wilson ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5295b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top);
529688ecc943SGeorge Wilson
52973f9d6ad7SLin Ling /*
52983f9d6ad7SLin Ling * Only remove any devices which are empty.
52993f9d6ad7SLin Ling */
53003f9d6ad7SLin Ling if (vd->vdev_stat.vs_alloc != 0)
53013f9d6ad7SLin Ling return;
53023f9d6ad7SLin Ling
530388ecc943SGeorge Wilson (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5304b24ab676SJeff Bonwick
5305b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_state_dirty_node))
5306b24ab676SJeff Bonwick vdev_state_clean(vd);
5307b24ab676SJeff Bonwick if (list_link_active(&vd->vdev_config_dirty_node))
5308b24ab676SJeff Bonwick vdev_config_clean(vd);
5309b24ab676SJeff Bonwick
531088ecc943SGeorge Wilson vdev_free(vd);
531188ecc943SGeorge Wilson
531288ecc943SGeorge Wilson if (last_vdev) {
531388ecc943SGeorge Wilson vdev_compact_children(rvd);
531488ecc943SGeorge Wilson } else {
531588ecc943SGeorge Wilson vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
531688ecc943SGeorge Wilson vdev_add_child(rvd, vd);
531788ecc943SGeorge Wilson }
5318fcbfa62bSLin Ling vdev_config_dirty(rvd);
5319fcbfa62bSLin Ling
5320fcbfa62bSLin Ling /*
5321fcbfa62bSLin Ling * Reassess the health of our root vdev.
5322fcbfa62bSLin Ling */
5323fcbfa62bSLin Ling vdev_reopen(rvd);
53243f9d6ad7SLin Ling }
532588ecc943SGeorge Wilson
532688ecc943SGeorge Wilson /*
53273f9d6ad7SLin Ling * Remove a device from the pool -
53283f9d6ad7SLin Ling *
53293f9d6ad7SLin Ling * Removing a device from the vdev namespace requires several steps
53303f9d6ad7SLin Ling * and can take a significant amount of time. As a result we use
53313f9d6ad7SLin Ling * the spa_vdev_config_[enter/exit] functions which allow us to
53323f9d6ad7SLin Ling * grab and release the spa_config_lock while still holding the namespace
53333f9d6ad7SLin Ling * lock. During each step the configuration is synced out.
5334f7170741SWill Andrews *
5335f7170741SWill Andrews * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5336f7170741SWill Andrews * devices.
5337fa94a07fSbrendan */
5338fa94a07fSbrendan int
spa_vdev_remove(spa_t * spa,uint64_t guid,boolean_t unspare)5339fa94a07fSbrendan spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5340fa94a07fSbrendan {
5341fa94a07fSbrendan vdev_t *vd;
5342a1521560SJeff Bonwick metaslab_group_t *mg;
5343e14bb325SJeff Bonwick nvlist_t **spares, **l2cache, *nv;
53448ad4d6ddSJeff Bonwick uint64_t txg = 0;
534588ecc943SGeorge Wilson uint_t nspares, nl2cache;
5346fa94a07fSbrendan int error = 0;
53478ad4d6ddSJeff Bonwick boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5348fa94a07fSbrendan
5349f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
5350f9af39baSGeorge Wilson
53518ad4d6ddSJeff Bonwick if (!locked)
5352e14bb325SJeff Bonwick txg = spa_vdev_enter(spa);
5353fa94a07fSbrendan
5354c5904d13Seschrock vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5355fa94a07fSbrendan
5356fa94a07fSbrendan if (spa->spa_spares.sav_vdevs != NULL &&
5357fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5358e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5359e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5360e14bb325SJeff Bonwick /*
5361e14bb325SJeff Bonwick * Only remove the hot spare if it's not currently in use
5362e14bb325SJeff Bonwick * in this pool.
5363e14bb325SJeff Bonwick */
5364e14bb325SJeff Bonwick if (vd == NULL || unspare) {
5365e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_spares.sav_config,
5366e14bb325SJeff Bonwick ZPOOL_CONFIG_SPARES, spares, nspares, nv);
536799653d4eSeschrock spa_load_spares(spa);
5368fa94a07fSbrendan spa->spa_spares.sav_sync = B_TRUE;
5369e14bb325SJeff Bonwick } else {
5370be6fd75aSMatthew Ahrens error = SET_ERROR(EBUSY);
5371fa94a07fSbrendan }
5372e14bb325SJeff Bonwick } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5373fa94a07fSbrendan nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5374e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5375e14bb325SJeff Bonwick (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5376e14bb325SJeff Bonwick /*
5377e14bb325SJeff Bonwick * Cache devices can always be removed.
5378e14bb325SJeff Bonwick */
5379e14bb325SJeff Bonwick spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5380e14bb325SJeff Bonwick ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5381fa94a07fSbrendan spa_load_l2cache(spa);
5382fa94a07fSbrendan spa->spa_l2cache.sav_sync = B_TRUE;
538388ecc943SGeorge Wilson } else if (vd != NULL && vd->vdev_islog) {
538488ecc943SGeorge Wilson ASSERT(!locked);
5385b24ab676SJeff Bonwick ASSERT(vd == vd->vdev_top);
538688ecc943SGeorge Wilson
5387a1521560SJeff Bonwick mg = vd->vdev_mg;
5388a1521560SJeff Bonwick
538988ecc943SGeorge Wilson /*
5390a1521560SJeff Bonwick * Stop allocating from this vdev.
539188ecc943SGeorge Wilson */
5392a1521560SJeff Bonwick metaslab_group_passivate(mg);
539388ecc943SGeorge Wilson
5394b24ab676SJeff Bonwick /*
5395b24ab676SJeff Bonwick * Wait for the youngest allocations and frees to sync,
5396b24ab676SJeff Bonwick * and then wait for the deferral of those frees to finish.
5397b24ab676SJeff Bonwick */
5398b24ab676SJeff Bonwick spa_vdev_config_exit(spa, NULL,
5399b24ab676SJeff Bonwick txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5400b24ab676SJeff Bonwick
5401a1521560SJeff Bonwick /*
5402a1521560SJeff Bonwick * Attempt to evacuate the vdev.
5403a1521560SJeff Bonwick */
5404a1521560SJeff Bonwick error = spa_vdev_remove_evacuate(spa, vd);
5405a1521560SJeff Bonwick
540688ecc943SGeorge Wilson txg = spa_vdev_config_enter(spa);
540788ecc943SGeorge Wilson
5408a1521560SJeff Bonwick /*
5409a1521560SJeff Bonwick * If we couldn't evacuate the vdev, unwind.
5410a1521560SJeff Bonwick */
5411a1521560SJeff Bonwick if (error) {
5412a1521560SJeff Bonwick metaslab_group_activate(mg);
5413a1521560SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error));
5414a1521560SJeff Bonwick }
5415a1521560SJeff Bonwick
5416a1521560SJeff Bonwick /*
5417a1521560SJeff Bonwick * Clean up the vdev namespace.
5418a1521560SJeff Bonwick */
5419a1521560SJeff Bonwick spa_vdev_remove_from_namespace(spa, vd);
542088ecc943SGeorge Wilson
5421e14bb325SJeff Bonwick } else if (vd != NULL) {
5422e14bb325SJeff Bonwick /*
5423e14bb325SJeff Bonwick * Normal vdevs cannot be removed (yet).
5424e14bb325SJeff Bonwick */
5425be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP);
5426e14bb325SJeff Bonwick } else {
5427e14bb325SJeff Bonwick /*
5428e14bb325SJeff Bonwick * There is no vdev of any kind with the specified guid.
5429e14bb325SJeff Bonwick */
5430be6fd75aSMatthew Ahrens error = SET_ERROR(ENOENT);
5431fa94a07fSbrendan }
543299653d4eSeschrock
54338ad4d6ddSJeff Bonwick if (!locked)
5434e14bb325SJeff Bonwick return (spa_vdev_exit(spa, NULL, txg, error));
54358ad4d6ddSJeff Bonwick
54368ad4d6ddSJeff Bonwick return (error);
5437fa9e4066Sahrens }
5438fa9e4066Sahrens
5439fa9e4066Sahrens /*
54403d7072f8Seschrock * Find any device that's done replacing, or a vdev marked 'unspare' that's
5441f7170741SWill Andrews * currently spared, so we can detach it.
5442fa9e4066Sahrens */
5443ea8dc4b6Seschrock static vdev_t *
spa_vdev_resilver_done_hunt(vdev_t * vd)54443d7072f8Seschrock spa_vdev_resilver_done_hunt(vdev_t *vd)
5445fa9e4066Sahrens {
5446ea8dc4b6Seschrock vdev_t *newvd, *oldvd;
5447fa9e4066Sahrens
5448573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) {
54493d7072f8Seschrock oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5450ea8dc4b6Seschrock if (oldvd != NULL)
5451ea8dc4b6Seschrock return (oldvd);
5452ea8dc4b6Seschrock }
5453fa9e4066Sahrens
54543d7072f8Seschrock /*
5455cb04b873SMark J Musante * Check for a completed replacement. We always consider the first
5456cb04b873SMark J Musante * vdev in the list to be the oldest vdev, and the last one to be
5457cb04b873SMark J Musante * the newest (see spa_vdev_attach() for how that works). In
5458cb04b873SMark J Musante * the case where the newest vdev is faulted, we will not automatically
5459cb04b873SMark J Musante * remove it after a resilver completes. This is OK as it will require
5460cb04b873SMark J Musante * user intervention to determine which disk the admin wishes to keep.
54613d7072f8Seschrock */
5462cb04b873SMark J Musante if (vd->vdev_ops == &vdev_replacing_ops) {
5463cb04b873SMark J Musante ASSERT(vd->vdev_children > 1);
5464cb04b873SMark J Musante
5465cb04b873SMark J Musante newvd = vd->vdev_child[vd->vdev_children - 1];
5466ea8dc4b6Seschrock oldvd = vd->vdev_child[0];
5467fa9e4066Sahrens
54688ad4d6ddSJeff Bonwick if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5469e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) &&
54708ad4d6ddSJeff Bonwick !vdev_dtl_required(oldvd))
5471ea8dc4b6Seschrock return (oldvd);
5472fa9e4066Sahrens }
5473fa9e4066Sahrens
54743d7072f8Seschrock /*
54753d7072f8Seschrock * Check for a completed resilver with the 'unspare' flag set.
54763d7072f8Seschrock */
5477cb04b873SMark J Musante if (vd->vdev_ops == &vdev_spare_ops) {
5478cb04b873SMark J Musante vdev_t *first = vd->vdev_child[0];
5479cb04b873SMark J Musante vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
54803d7072f8Seschrock
5481cb04b873SMark J Musante if (last->vdev_unspare) {
5482cb04b873SMark J Musante oldvd = first;
5483cb04b873SMark J Musante newvd = last;
5484cb04b873SMark J Musante } else if (first->vdev_unspare) {
5485cb04b873SMark J Musante oldvd = last;
5486cb04b873SMark J Musante newvd = first;
5487cb04b873SMark J Musante } else {
5488cb04b873SMark J Musante oldvd = NULL;
5489cb04b873SMark J Musante }
5490cb04b873SMark J Musante
5491cb04b873SMark J Musante if (oldvd != NULL &&
54928ad4d6ddSJeff Bonwick vdev_dtl_empty(newvd, DTL_MISSING) &&
5493e69acc92SVictor Latushkin vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5494cb04b873SMark J Musante !vdev_dtl_required(oldvd))
54953d7072f8Seschrock return (oldvd);
5496cb04b873SMark J Musante
5497cb04b873SMark J Musante /*
5498cb04b873SMark J Musante * If there are more than two spares attached to a disk,
5499cb04b873SMark J Musante * and those spares are not required, then we want to
5500cb04b873SMark J Musante * attempt to free them up now so that they can be used
5501cb04b873SMark J Musante * by other pools. Once we're back down to a single
5502cb04b873SMark J Musante * disk+spare, we stop removing them.
5503cb04b873SMark J Musante */
5504cb04b873SMark J Musante if (vd->vdev_children > 2) {
5505cb04b873SMark J Musante newvd = vd->vdev_child[1];
5506cb04b873SMark J Musante
5507cb04b873SMark J Musante if (newvd->vdev_isspare && last->vdev_isspare &&
5508cb04b873SMark J Musante vdev_dtl_empty(last, DTL_MISSING) &&
5509cb04b873SMark J Musante vdev_dtl_empty(last, DTL_OUTAGE) &&
5510cb04b873SMark J Musante !vdev_dtl_required(newvd))
5511cb04b873SMark J Musante return (newvd);
55123d7072f8Seschrock }
55133d7072f8Seschrock }
55143d7072f8Seschrock
5515ea8dc4b6Seschrock return (NULL);
5516ea8dc4b6Seschrock }
5517ea8dc4b6Seschrock
5518ea8dc4b6Seschrock static void
spa_vdev_resilver_done(spa_t * spa)55193d7072f8Seschrock spa_vdev_resilver_done(spa_t *spa)
5520fa9e4066Sahrens {
55218ad4d6ddSJeff Bonwick vdev_t *vd, *pvd, *ppvd;
55228ad4d6ddSJeff Bonwick uint64_t guid, sguid, pguid, ppguid;
5523fa9e4066Sahrens
55248ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5525fa9e4066Sahrens
55263d7072f8Seschrock while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
55278ad4d6ddSJeff Bonwick pvd = vd->vdev_parent;
55288ad4d6ddSJeff Bonwick ppvd = pvd->vdev_parent;
5529ea8dc4b6Seschrock guid = vd->vdev_guid;
55308ad4d6ddSJeff Bonwick pguid = pvd->vdev_guid;
55318ad4d6ddSJeff Bonwick ppguid = ppvd->vdev_guid;
55328ad4d6ddSJeff Bonwick sguid = 0;
553399653d4eSeschrock /*
553499653d4eSeschrock * If we have just finished replacing a hot spared device, then
553599653d4eSeschrock * we need to detach the parent's first child (the original hot
553699653d4eSeschrock * spare) as well.
553799653d4eSeschrock */
5538cb04b873SMark J Musante if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5539cb04b873SMark J Musante ppvd->vdev_children == 2) {
554099653d4eSeschrock ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
55418ad4d6ddSJeff Bonwick sguid = ppvd->vdev_child[1]->vdev_guid;
554299653d4eSeschrock }
5543b4952e17SGeorge Wilson ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5544b4952e17SGeorge Wilson
55458ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
55468ad4d6ddSJeff Bonwick if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5547ea8dc4b6Seschrock return;
55488ad4d6ddSJeff Bonwick if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
554999653d4eSeschrock return;
55508ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5551fa9e4066Sahrens }
5552fa9e4066Sahrens
55538ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
5554fa9e4066Sahrens }
5555fa9e4066Sahrens
5556fa9e4066Sahrens /*
5557b3388e4fSEric Taylor * Update the stored path or FRU for this vdev.
5558c67d9675Seschrock */
5559c67d9675Seschrock int
spa_vdev_set_common(spa_t * spa,uint64_t guid,const char * value,boolean_t ispath)55606809eb4eSEric Schrock spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
55616809eb4eSEric Schrock boolean_t ispath)
5562c67d9675Seschrock {
5563c5904d13Seschrock vdev_t *vd;
5564208044b8SGeorge Wilson boolean_t sync = B_FALSE;
5565c67d9675Seschrock
5566f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
5567f9af39baSGeorge Wilson
5568b3388e4fSEric Taylor spa_vdev_state_enter(spa, SCL_ALL);
5569c67d9675Seschrock
55706809eb4eSEric Schrock if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5571b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOENT));
557299653d4eSeschrock
55730e34b6a7Sbonwick if (!vd->vdev_ops->vdev_op_leaf)
5574b3388e4fSEric Taylor return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
55750e34b6a7Sbonwick
55766809eb4eSEric Schrock if (ispath) {
5577208044b8SGeorge Wilson if (strcmp(value, vd->vdev_path) != 0) {
5578c67d9675Seschrock spa_strfree(vd->vdev_path);
55796809eb4eSEric Schrock vd->vdev_path = spa_strdup(value);
5580208044b8SGeorge Wilson sync = B_TRUE;
5581208044b8SGeorge Wilson }
55826809eb4eSEric Schrock } else {
5583208044b8SGeorge Wilson if (vd->vdev_fru == NULL) {
5584208044b8SGeorge Wilson vd->vdev_fru = spa_strdup(value);
5585208044b8SGeorge Wilson sync = B_TRUE;
5586208044b8SGeorge Wilson } else if (strcmp(value, vd->vdev_fru) != 0) {
55876809eb4eSEric Schrock spa_strfree(vd->vdev_fru);
55886809eb4eSEric Schrock vd->vdev_fru = spa_strdup(value);
5589208044b8SGeorge Wilson sync = B_TRUE;
5590208044b8SGeorge Wilson }
55916809eb4eSEric Schrock }
5592c67d9675Seschrock
5593208044b8SGeorge Wilson return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5594c67d9675Seschrock }
5595c67d9675Seschrock
55966809eb4eSEric Schrock int
spa_vdev_setpath(spa_t * spa,uint64_t guid,const char * newpath)55976809eb4eSEric Schrock spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
55986809eb4eSEric Schrock {
55996809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
56006809eb4eSEric Schrock }
56016809eb4eSEric Schrock
56026809eb4eSEric Schrock int
spa_vdev_setfru(spa_t * spa,uint64_t guid,const char * newfru)56036809eb4eSEric Schrock spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
56046809eb4eSEric Schrock {
56056809eb4eSEric Schrock return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
56066809eb4eSEric Schrock }
56076809eb4eSEric Schrock
5608c67d9675Seschrock /*
5609fa9e4066Sahrens * ==========================================================================
56103f9d6ad7SLin Ling * SPA Scanning
5611fa9e4066Sahrens * ==========================================================================
5612fa9e4066Sahrens */
5613fa9e4066Sahrens
5614ea8dc4b6Seschrock int
spa_scan_stop(spa_t * spa)56153f9d6ad7SLin Ling spa_scan_stop(spa_t *spa)
56163f9d6ad7SLin Ling {
56173f9d6ad7SLin Ling ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
56183f9d6ad7SLin Ling if (dsl_scan_resilvering(spa->spa_dsl_pool))
5619be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
56203f9d6ad7SLin Ling return (dsl_scan_cancel(spa->spa_dsl_pool));
56213f9d6ad7SLin Ling }
56223f9d6ad7SLin Ling
56233f9d6ad7SLin Ling int
spa_scan(spa_t * spa,pool_scan_func_t func)56243f9d6ad7SLin Ling spa_scan(spa_t *spa, pool_scan_func_t func)
5625fa9e4066Sahrens {
5626e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5627bb8b5132Sek110237
56283f9d6ad7SLin Ling if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5629be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
5630fa9e4066Sahrens
5631fa9e4066Sahrens /*
5632088f3894Sahrens * If a resilver was requested, but there is no DTL on a
5633088f3894Sahrens * writeable leaf device, we have nothing to do.
5634fa9e4066Sahrens */
56353f9d6ad7SLin Ling if (func == POOL_SCAN_RESILVER &&
5636088f3894Sahrens !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
56373d7072f8Seschrock spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5638fa9e4066Sahrens return (0);
5639fa9e4066Sahrens }
5640fa9e4066Sahrens
56413f9d6ad7SLin Ling return (dsl_scan(spa->spa_dsl_pool, func));
5642088f3894Sahrens }
5643088f3894Sahrens
5644ea8dc4b6Seschrock /*
5645ea8dc4b6Seschrock * ==========================================================================
5646ea8dc4b6Seschrock * SPA async task processing
5647ea8dc4b6Seschrock * ==========================================================================
5648ea8dc4b6Seschrock */
5649ea8dc4b6Seschrock
5650ea8dc4b6Seschrock static void
spa_async_remove(spa_t * spa,vdev_t * vd)56513d7072f8Seschrock spa_async_remove(spa_t *spa, vdev_t *vd)
5652fa9e4066Sahrens {
565349cf58c0SBrendan Gregg - Sun Microsystems if (vd->vdev_remove_wanted) {
565498d1cbfeSGeorge Wilson vd->vdev_remove_wanted = B_FALSE;
565598d1cbfeSGeorge Wilson vd->vdev_delayed_close = B_FALSE;
565649cf58c0SBrendan Gregg - Sun Microsystems vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
56571d713200SEric Schrock
56581d713200SEric Schrock /*
56591d713200SEric Schrock * We want to clear the stats, but we don't want to do a full
56601d713200SEric Schrock * vdev_clear() as that will cause us to throw away
56611d713200SEric Schrock * degraded/faulted state as well as attempt to reopen the
56621d713200SEric Schrock * device, all of which is a waste.
56631d713200SEric Schrock */
56641d713200SEric Schrock vd->vdev_stat.vs_read_errors = 0;
56651d713200SEric Schrock vd->vdev_stat.vs_write_errors = 0;
56661d713200SEric Schrock vd->vdev_stat.vs_checksum_errors = 0;
56671d713200SEric Schrock
5668e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top);
5669ea8dc4b6Seschrock }
567049cf58c0SBrendan Gregg - Sun Microsystems
5671e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++)
567249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, vd->vdev_child[c]);
5673ea8dc4b6Seschrock }
5674ea8dc4b6Seschrock
5675ea8dc4b6Seschrock static void
spa_async_probe(spa_t * spa,vdev_t * vd)5676e14bb325SJeff Bonwick spa_async_probe(spa_t *spa, vdev_t *vd)
5677e14bb325SJeff Bonwick {
5678e14bb325SJeff Bonwick if (vd->vdev_probe_wanted) {
567998d1cbfeSGeorge Wilson vd->vdev_probe_wanted = B_FALSE;
5680e14bb325SJeff Bonwick vdev_reopen(vd); /* vdev_open() does the actual probe */
5681e14bb325SJeff Bonwick }
5682e14bb325SJeff Bonwick
5683e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++)
5684e14bb325SJeff Bonwick spa_async_probe(spa, vd->vdev_child[c]);
5685e14bb325SJeff Bonwick }
5686e14bb325SJeff Bonwick
5687e14bb325SJeff Bonwick static void
spa_async_autoexpand(spa_t * spa,vdev_t * vd)5688573ca77eSGeorge Wilson spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5689573ca77eSGeorge Wilson {
5690573ca77eSGeorge Wilson sysevent_id_t eid;
5691573ca77eSGeorge Wilson nvlist_t *attr;
5692573ca77eSGeorge Wilson char *physpath;
5693573ca77eSGeorge Wilson
5694573ca77eSGeorge Wilson if (!spa->spa_autoexpand)
5695573ca77eSGeorge Wilson return;
5696573ca77eSGeorge Wilson
5697573ca77eSGeorge Wilson for (int c = 0; c < vd->vdev_children; c++) {
5698573ca77eSGeorge Wilson vdev_t *cvd = vd->vdev_child[c];
5699573ca77eSGeorge Wilson spa_async_autoexpand(spa, cvd);
5700573ca77eSGeorge Wilson }
5701573ca77eSGeorge Wilson
5702573ca77eSGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5703573ca77eSGeorge Wilson return;
5704573ca77eSGeorge Wilson
5705573ca77eSGeorge Wilson physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5706573ca77eSGeorge Wilson (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5707573ca77eSGeorge Wilson
5708573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5709573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5710573ca77eSGeorge Wilson
5711573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5712573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
5713573ca77eSGeorge Wilson
5714573ca77eSGeorge Wilson nvlist_free(attr);
5715573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN);
5716573ca77eSGeorge Wilson }
5717573ca77eSGeorge Wilson
5718573ca77eSGeorge Wilson static void
spa_async_thread(spa_t * spa)5719ea8dc4b6Seschrock spa_async_thread(spa_t *spa)
5720ea8dc4b6Seschrock {
5721e14bb325SJeff Bonwick int tasks;
5722ea8dc4b6Seschrock
5723ea8dc4b6Seschrock ASSERT(spa->spa_sync_on);
5724ea8dc4b6Seschrock
5725ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
5726ea8dc4b6Seschrock tasks = spa->spa_async_tasks;
5727ea8dc4b6Seschrock spa->spa_async_tasks = 0;
5728ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5729ea8dc4b6Seschrock
5730ea8dc4b6Seschrock /*
57310373e76bSbonwick * See if the config needs to be updated.
57320373e76bSbonwick */
57330373e76bSbonwick if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5734b24ab676SJeff Bonwick uint64_t old_space, new_space;
5735573ca77eSGeorge Wilson
57360373e76bSbonwick mutex_enter(&spa_namespace_lock);
5737b24ab676SJeff Bonwick old_space = metaslab_class_get_space(spa_normal_class(spa));
57380373e76bSbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5739b24ab676SJeff Bonwick new_space = metaslab_class_get_space(spa_normal_class(spa));
57400373e76bSbonwick mutex_exit(&spa_namespace_lock);
5741573ca77eSGeorge Wilson
5742573ca77eSGeorge Wilson /*
5743573ca77eSGeorge Wilson * If the pool grew as a result of the config update,
5744573ca77eSGeorge Wilson * then log an internal history event.
5745573ca77eSGeorge Wilson */
5746b24ab676SJeff Bonwick if (new_space != old_space) {
57474445fffbSMatthew Ahrens spa_history_log_internal(spa, "vdev online", NULL,
5748573ca77eSGeorge Wilson "pool '%s' size: %llu(+%llu)",
5749b24ab676SJeff Bonwick spa_name(spa), new_space, new_space - old_space);
5750573ca77eSGeorge Wilson }
57510373e76bSbonwick }
57520373e76bSbonwick
57530373e76bSbonwick /*
57543d7072f8Seschrock * See if any devices need to be marked REMOVED.
5755ea8dc4b6Seschrock */
5756e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_REMOVE) {
57578f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE);
57583d7072f8Seschrock spa_async_remove(spa, spa->spa_root_vdev);
5759e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
576049cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5761e14bb325SJeff Bonwick for (int i = 0; i < spa->spa_spares.sav_count; i++)
576249cf58c0SBrendan Gregg - Sun Microsystems spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5763e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0);
5764e14bb325SJeff Bonwick }
5765e14bb325SJeff Bonwick
5766573ca77eSGeorge Wilson if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5767573ca77eSGeorge Wilson spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5768573ca77eSGeorge Wilson spa_async_autoexpand(spa, spa->spa_root_vdev);
5769573ca77eSGeorge Wilson spa_config_exit(spa, SCL_CONFIG, FTAG);
5770573ca77eSGeorge Wilson }
5771573ca77eSGeorge Wilson
5772e14bb325SJeff Bonwick /*
5773e14bb325SJeff Bonwick * See if any devices need to be probed.
5774e14bb325SJeff Bonwick */
5775e14bb325SJeff Bonwick if (tasks & SPA_ASYNC_PROBE) {
57768f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE);
5777e14bb325SJeff Bonwick spa_async_probe(spa, spa->spa_root_vdev);
5778e14bb325SJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0);
57793d7072f8Seschrock }
5780ea8dc4b6Seschrock
5781ea8dc4b6Seschrock /*
5782ea8dc4b6Seschrock * If any devices are done replacing, detach them.
5783ea8dc4b6Seschrock */
57843d7072f8Seschrock if (tasks & SPA_ASYNC_RESILVER_DONE)
57853d7072f8Seschrock spa_vdev_resilver_done(spa);
5786fa9e4066Sahrens
5787ea8dc4b6Seschrock /*
5788ea8dc4b6Seschrock * Kick off a resilver.
5789ea8dc4b6Seschrock */
5790088f3894Sahrens if (tasks & SPA_ASYNC_RESILVER)
57913f9d6ad7SLin Ling dsl_resilver_restart(spa->spa_dsl_pool, 0);
5792ea8dc4b6Seschrock
5793ea8dc4b6Seschrock /*
5794c9e5c7a7SSaso Kiselkov * Kick off L2 cache rebuilding.
5795c9e5c7a7SSaso Kiselkov */
5796c9e5c7a7SSaso Kiselkov if (tasks & SPA_ASYNC_L2CACHE_REBUILD)
5797c9e5c7a7SSaso Kiselkov l2arc_spa_rebuild_start(spa);
5798c9e5c7a7SSaso Kiselkov
5799c9e5c7a7SSaso Kiselkov /*
5800ea8dc4b6Seschrock * Let the world know that we're done.
5801ea8dc4b6Seschrock */
5802ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
5803ea8dc4b6Seschrock spa->spa_async_thread = NULL;
5804ea8dc4b6Seschrock cv_broadcast(&spa->spa_async_cv);
5805ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5806ea8dc4b6Seschrock thread_exit();
5807ea8dc4b6Seschrock }
5808ea8dc4b6Seschrock
5809ea8dc4b6Seschrock void
spa_async_suspend(spa_t * spa)5810ea8dc4b6Seschrock spa_async_suspend(spa_t *spa)
5811ea8dc4b6Seschrock {
5812ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
5813ea8dc4b6Seschrock spa->spa_async_suspended++;
5814ea8dc4b6Seschrock while (spa->spa_async_thread != NULL)
5815ea8dc4b6Seschrock cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5816ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5817ea8dc4b6Seschrock }
5818ea8dc4b6Seschrock
5819ea8dc4b6Seschrock void
spa_async_resume(spa_t * spa)5820ea8dc4b6Seschrock spa_async_resume(spa_t *spa)
5821ea8dc4b6Seschrock {
5822ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
5823ea8dc4b6Seschrock ASSERT(spa->spa_async_suspended != 0);
5824ea8dc4b6Seschrock spa->spa_async_suspended--;
5825ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5826ea8dc4b6Seschrock }
5827ea8dc4b6Seschrock
58283cb69f73SWill Andrews static boolean_t
spa_async_tasks_pending(spa_t * spa)58293cb69f73SWill Andrews spa_async_tasks_pending(spa_t *spa)
58303cb69f73SWill Andrews {
58313cb69f73SWill Andrews uint_t non_config_tasks;
58323cb69f73SWill Andrews uint_t config_task;
58333cb69f73SWill Andrews boolean_t config_task_suspended;
58343cb69f73SWill Andrews
58353cb69f73SWill Andrews non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
58363cb69f73SWill Andrews config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
58373cb69f73SWill Andrews if (spa->spa_ccw_fail_time == 0) {
58383cb69f73SWill Andrews config_task_suspended = B_FALSE;
58393cb69f73SWill Andrews } else {
58403cb69f73SWill Andrews config_task_suspended =
58413cb69f73SWill Andrews (gethrtime() - spa->spa_ccw_fail_time) <
58423cb69f73SWill Andrews (zfs_ccw_retry_interval * NANOSEC);
58433cb69f73SWill Andrews }
58443cb69f73SWill Andrews
58453cb69f73SWill Andrews return (non_config_tasks || (config_task && !config_task_suspended));
58463cb69f73SWill Andrews }
58473cb69f73SWill Andrews
5848ea8dc4b6Seschrock static void
spa_async_dispatch(spa_t * spa)5849ea8dc4b6Seschrock spa_async_dispatch(spa_t *spa)
5850ea8dc4b6Seschrock {
5851ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
58523cb69f73SWill Andrews if (spa_async_tasks_pending(spa) &&
58533cb69f73SWill Andrews !spa->spa_async_suspended &&
58540373e76bSbonwick spa->spa_async_thread == NULL &&
58553cb69f73SWill Andrews rootdir != NULL)
5856ea8dc4b6Seschrock spa->spa_async_thread = thread_create(NULL, 0,
5857ea8dc4b6Seschrock spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5858ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5859ea8dc4b6Seschrock }
5860ea8dc4b6Seschrock
5861ea8dc4b6Seschrock void
spa_async_request(spa_t * spa,int task)5862ea8dc4b6Seschrock spa_async_request(spa_t *spa, int task)
5863ea8dc4b6Seschrock {
58643f9d6ad7SLin Ling zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5865ea8dc4b6Seschrock mutex_enter(&spa->spa_async_lock);
5866ea8dc4b6Seschrock spa->spa_async_tasks |= task;
5867ea8dc4b6Seschrock mutex_exit(&spa->spa_async_lock);
5868fa9e4066Sahrens }
5869fa9e4066Sahrens
5870fa9e4066Sahrens /*
5871fa9e4066Sahrens * ==========================================================================
5872fa9e4066Sahrens * SPA syncing routines
5873fa9e4066Sahrens * ==========================================================================
5874fa9e4066Sahrens */
5875cde58dbcSMatthew Ahrens
5876cde58dbcSMatthew Ahrens static int
bpobj_enqueue_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)5877cde58dbcSMatthew Ahrens bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5878fa9e4066Sahrens {
5879cde58dbcSMatthew Ahrens bpobj_t *bpo = arg;
5880cde58dbcSMatthew Ahrens bpobj_enqueue(bpo, bp, tx);
5881cde58dbcSMatthew Ahrens return (0);
5882e14bb325SJeff Bonwick }
5883fa9e4066Sahrens
5884cde58dbcSMatthew Ahrens static int
spa_free_sync_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)5885cde58dbcSMatthew Ahrens spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5886b24ab676SJeff Bonwick {
5887b24ab676SJeff Bonwick zio_t *zio = arg;
5888b24ab676SJeff Bonwick
5889b24ab676SJeff Bonwick zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5890b24ab676SJeff Bonwick zio->io_flags));
5891cde58dbcSMatthew Ahrens return (0);
5892fa9e4066Sahrens }
5893fa9e4066Sahrens
589469962b56SMatthew Ahrens /*
589569962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the
589669962b56SMatthew Ahrens * amount of time spent syncing frees.
589769962b56SMatthew Ahrens */
589869962b56SMatthew Ahrens static void
spa_sync_frees(spa_t * spa,bplist_t * bpl,dmu_tx_t * tx)589969962b56SMatthew Ahrens spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
590069962b56SMatthew Ahrens {
590169962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0);
590269962b56SMatthew Ahrens bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
590369962b56SMatthew Ahrens VERIFY(zio_wait(zio) == 0);
590469962b56SMatthew Ahrens }
590569962b56SMatthew Ahrens
590669962b56SMatthew Ahrens /*
590769962b56SMatthew Ahrens * Note: this simple function is not inlined to make it easier to dtrace the
590869962b56SMatthew Ahrens * amount of time spent syncing deferred frees.
590969962b56SMatthew Ahrens */
591069962b56SMatthew Ahrens static void
spa_sync_deferred_frees(spa_t * spa,dmu_tx_t * tx)591169962b56SMatthew Ahrens spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
591269962b56SMatthew Ahrens {
591369962b56SMatthew Ahrens zio_t *zio = zio_root(spa, NULL, NULL, 0);
591469962b56SMatthew Ahrens VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
591569962b56SMatthew Ahrens spa_free_sync_cb, zio, tx), ==, 0);
591669962b56SMatthew Ahrens VERIFY0(zio_wait(zio));
591769962b56SMatthew Ahrens }
591869962b56SMatthew Ahrens
591969962b56SMatthew Ahrens
5920fa9e4066Sahrens static void
spa_sync_nvlist(spa_t * spa,uint64_t obj,nvlist_t * nv,dmu_tx_t * tx)592199653d4eSeschrock spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5922fa9e4066Sahrens {
5923fa9e4066Sahrens char *packed = NULL;
5924f7991ba4STim Haley size_t bufsize;
5925fa9e4066Sahrens size_t nvsize = 0;
5926fa9e4066Sahrens dmu_buf_t *db;
5927fa9e4066Sahrens
592899653d4eSeschrock VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
592999653d4eSeschrock
5930f7991ba4STim Haley /*
5931f7991ba4STim Haley * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
593243466aaeSMax Grossman * information. This avoids the dmu_buf_will_dirty() path and
5933f7991ba4STim Haley * saves us a pre-read to get data we don't actually care about.
5934f7991ba4STim Haley */
5935ad135b5dSChristopher Siden bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5936f7991ba4STim Haley packed = kmem_alloc(bufsize, KM_SLEEP);
593799653d4eSeschrock
593899653d4eSeschrock VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
593999653d4eSeschrock KM_SLEEP) == 0);
5940f7991ba4STim Haley bzero(packed + nvsize, bufsize - nvsize);
594199653d4eSeschrock
5942f7991ba4STim Haley dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
594399653d4eSeschrock
5944f7991ba4STim Haley kmem_free(packed, bufsize);
594599653d4eSeschrock
594699653d4eSeschrock VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
594799653d4eSeschrock dmu_buf_will_dirty(db, tx);
594899653d4eSeschrock *(uint64_t *)db->db_data = nvsize;
594999653d4eSeschrock dmu_buf_rele(db, FTAG);
595099653d4eSeschrock }
595199653d4eSeschrock
595299653d4eSeschrock static void
spa_sync_aux_dev(spa_t * spa,spa_aux_vdev_t * sav,dmu_tx_t * tx,const char * config,const char * entry)5953fa94a07fSbrendan spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5954fa94a07fSbrendan const char *config, const char *entry)
595599653d4eSeschrock {
595699653d4eSeschrock nvlist_t *nvroot;
5957fa94a07fSbrendan nvlist_t **list;
595899653d4eSeschrock int i;
595999653d4eSeschrock
5960fa94a07fSbrendan if (!sav->sav_sync)
596199653d4eSeschrock return;
596299653d4eSeschrock
596399653d4eSeschrock /*
5964fa94a07fSbrendan * Update the MOS nvlist describing the list of available devices.
5965fa94a07fSbrendan * spa_validate_aux() will have already made sure this nvlist is
59663d7072f8Seschrock * valid and the vdevs are labeled appropriately.
596799653d4eSeschrock */
5968fa94a07fSbrendan if (sav->sav_object == 0) {
5969fa94a07fSbrendan sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5970fa94a07fSbrendan DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5971fa94a07fSbrendan sizeof (uint64_t), tx);
597299653d4eSeschrock VERIFY(zap_update(spa->spa_meta_objset,
5973fa94a07fSbrendan DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5974fa94a07fSbrendan &sav->sav_object, tx) == 0);
597599653d4eSeschrock }
597699653d4eSeschrock
597799653d4eSeschrock VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5978fa94a07fSbrendan if (sav->sav_count == 0) {
5979fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
598099653d4eSeschrock } else {
5981fa94a07fSbrendan list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5982fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++)
5983fa94a07fSbrendan list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
59843f9d6ad7SLin Ling B_FALSE, VDEV_CONFIG_L2CACHE);
5985fa94a07fSbrendan VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5986fa94a07fSbrendan sav->sav_count) == 0);
5987fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++)
5988fa94a07fSbrendan nvlist_free(list[i]);
5989fa94a07fSbrendan kmem_free(list, sav->sav_count * sizeof (void *));
599099653d4eSeschrock }
599199653d4eSeschrock
5992fa94a07fSbrendan spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
599306eeb2adSek110237 nvlist_free(nvroot);
599499653d4eSeschrock
5995fa94a07fSbrendan sav->sav_sync = B_FALSE;
599699653d4eSeschrock }
599799653d4eSeschrock
599899653d4eSeschrock static void
spa_sync_config_object(spa_t * spa,dmu_tx_t * tx)599999653d4eSeschrock spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
600099653d4eSeschrock {
600199653d4eSeschrock nvlist_t *config;
600299653d4eSeschrock
6003e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list))
6004fa9e4066Sahrens return;
6005fa9e4066Sahrens
6006e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6007e14bb325SJeff Bonwick
6008e14bb325SJeff Bonwick config = spa_config_generate(spa, spa->spa_root_vdev,
6009e14bb325SJeff Bonwick dmu_tx_get_txg(tx), B_FALSE);
6010e14bb325SJeff Bonwick
601125345e46SGeorge Wilson /*
601225345e46SGeorge Wilson * If we're upgrading the spa version then make sure that
601325345e46SGeorge Wilson * the config object gets updated with the correct version.
601425345e46SGeorge Wilson */
601525345e46SGeorge Wilson if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
601625345e46SGeorge Wilson fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
601725345e46SGeorge Wilson spa->spa_uberblock.ub_version);
601825345e46SGeorge Wilson
6019e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG);
6020fa9e4066Sahrens
60210373e76bSbonwick nvlist_free(spa->spa_config_syncing);
60220373e76bSbonwick spa->spa_config_syncing = config;
6023fa9e4066Sahrens
602499653d4eSeschrock spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6025fa9e4066Sahrens }
6026fa9e4066Sahrens
6027ad135b5dSChristopher Siden static void
spa_sync_version(void * arg,dmu_tx_t * tx)60283b2aab18SMatthew Ahrens spa_sync_version(void *arg, dmu_tx_t *tx)
6029ad135b5dSChristopher Siden {
60303b2aab18SMatthew Ahrens uint64_t *versionp = arg;
60313b2aab18SMatthew Ahrens uint64_t version = *versionp;
60323b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6033ad135b5dSChristopher Siden
6034ad135b5dSChristopher Siden /*
6035ad135b5dSChristopher Siden * Setting the version is special cased when first creating the pool.
6036ad135b5dSChristopher Siden */
6037ad135b5dSChristopher Siden ASSERT(tx->tx_txg != TXG_INITIAL);
6038ad135b5dSChristopher Siden
603962eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6040ad135b5dSChristopher Siden ASSERT(version >= spa_version(spa));
6041ad135b5dSChristopher Siden
6042ad135b5dSChristopher Siden spa->spa_uberblock.ub_version = version;
6043ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev);
60444445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6045ad135b5dSChristopher Siden }
6046ad135b5dSChristopher Siden
6047990b4856Slling /*
6048990b4856Slling * Set zpool properties.
6049990b4856Slling */
6050b1b8ab34Slling static void
spa_sync_props(void * arg,dmu_tx_t * tx)60513b2aab18SMatthew Ahrens spa_sync_props(void *arg, dmu_tx_t *tx)
6052b1b8ab34Slling {
60533b2aab18SMatthew Ahrens nvlist_t *nvp = arg;
60543b2aab18SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6055b1b8ab34Slling objset_t *mos = spa->spa_meta_objset;
6056ad135b5dSChristopher Siden nvpair_t *elem = NULL;
6057b1b8ab34Slling
6058e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock);
6059e14bb325SJeff Bonwick
6060990b4856Slling while ((elem = nvlist_next_nvpair(nvp, elem))) {
6061ad135b5dSChristopher Siden uint64_t intval;
6062ad135b5dSChristopher Siden char *strval, *fname;
6063ad135b5dSChristopher Siden zpool_prop_t prop;
6064ad135b5dSChristopher Siden const char *propname;
6065ad135b5dSChristopher Siden zprop_type_t proptype;
60662acef22dSMatthew Ahrens spa_feature_t fid;
6067ad135b5dSChristopher Siden
6068990b4856Slling switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6069ad135b5dSChristopher Siden case ZPROP_INVAL:
6070990b4856Slling /*
6071ad135b5dSChristopher Siden * We checked this earlier in spa_prop_validate().
6072990b4856Slling */
6073ad135b5dSChristopher Siden ASSERT(zpool_prop_feature(nvpair_name(elem)));
6074ad135b5dSChristopher Siden
6075ad135b5dSChristopher Siden fname = strchr(nvpair_name(elem), '@') + 1;
60762acef22dSMatthew Ahrens VERIFY0(zfeature_lookup_name(fname, &fid));
6077ad135b5dSChristopher Siden
60782acef22dSMatthew Ahrens spa_feature_enable(spa, fid, tx);
60794445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx,
60804445fffbSMatthew Ahrens "%s=enabled", nvpair_name(elem));
6081ad135b5dSChristopher Siden break;
6082ad135b5dSChristopher Siden
6083ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION:
60840713e232SGeorge Wilson intval = fnvpair_value_uint64(elem);
6085ad135b5dSChristopher Siden /*
6086ad135b5dSChristopher Siden * The version is synced seperatly before other
6087ad135b5dSChristopher Siden * properties and should be correct by now.
6088ad135b5dSChristopher Siden */
6089ad135b5dSChristopher Siden ASSERT3U(spa_version(spa), >=, intval);
6090990b4856Slling break;
6091990b4856Slling
6092990b4856Slling case ZPOOL_PROP_ALTROOT:
6093990b4856Slling /*
6094990b4856Slling * 'altroot' is a non-persistent property. It should
6095990b4856Slling * have been set temporarily at creation or import time.
6096990b4856Slling */
6097990b4856Slling ASSERT(spa->spa_root != NULL);
6098990b4856Slling break;
6099990b4856Slling
6100f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY:
61012f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE:
6102990b4856Slling /*
6103f9af39baSGeorge Wilson * 'readonly' and 'cachefile' are also non-persisitent
6104f9af39baSGeorge Wilson * properties.
6105990b4856Slling */
6106990b4856Slling break;
61078704186eSDan McDonald case ZPOOL_PROP_COMMENT:
61080713e232SGeorge Wilson strval = fnvpair_value_string(elem);
61098704186eSDan McDonald if (spa->spa_comment != NULL)
61108704186eSDan McDonald spa_strfree(spa->spa_comment);
61118704186eSDan McDonald spa->spa_comment = spa_strdup(strval);
61128704186eSDan McDonald /*
61138704186eSDan McDonald * We need to dirty the configuration on all the vdevs
61148704186eSDan McDonald * so that their labels get updated. It's unnecessary
61158704186eSDan McDonald * to do this for pool creation since the vdev's
61168704186eSDan McDonald * configuratoin has already been dirtied.
61178704186eSDan McDonald */
61188704186eSDan McDonald if (tx->tx_txg != TXG_INITIAL)
61198704186eSDan McDonald vdev_config_dirty(spa->spa_root_vdev);
61204445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx,
61214445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval);
61228704186eSDan McDonald break;
6123990b4856Slling default:
6124990b4856Slling /*
6125990b4856Slling * Set pool property values in the poolprops mos object.
6126990b4856Slling */
6127b1b8ab34Slling if (spa->spa_pool_props_object == 0) {
6128ad135b5dSChristopher Siden spa->spa_pool_props_object =
6129ad135b5dSChristopher Siden zap_create_link(mos, DMU_OT_POOL_PROPS,
6130990b4856Slling DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6131ad135b5dSChristopher Siden tx);
6132b1b8ab34Slling }
6133b1b8ab34Slling
6134990b4856Slling /* normalize the property name */
6135990b4856Slling propname = zpool_prop_to_name(prop);
6136990b4856Slling proptype = zpool_prop_get_type(prop);
61373d7072f8Seschrock
6138990b4856Slling if (nvpair_type(elem) == DATA_TYPE_STRING) {
6139990b4856Slling ASSERT(proptype == PROP_TYPE_STRING);
61400713e232SGeorge Wilson strval = fnvpair_value_string(elem);
61410713e232SGeorge Wilson VERIFY0(zap_update(mos,
6142990b4856Slling spa->spa_pool_props_object, propname,
61430713e232SGeorge Wilson 1, strlen(strval) + 1, strval, tx));
61444445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx,
61454445fffbSMatthew Ahrens "%s=%s", nvpair_name(elem), strval);
6146990b4856Slling } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
61470713e232SGeorge Wilson intval = fnvpair_value_uint64(elem);
6148990b4856Slling
6149990b4856Slling if (proptype == PROP_TYPE_INDEX) {
6150990b4856Slling const char *unused;
61510713e232SGeorge Wilson VERIFY0(zpool_prop_index_to_string(
61520713e232SGeorge Wilson prop, intval, &unused));
6153b1b8ab34Slling }
61540713e232SGeorge Wilson VERIFY0(zap_update(mos,
6155990b4856Slling spa->spa_pool_props_object, propname,
61560713e232SGeorge Wilson 8, 1, &intval, tx));
61574445fffbSMatthew Ahrens spa_history_log_internal(spa, "set", tx,
61584445fffbSMatthew Ahrens "%s=%lld", nvpair_name(elem), intval);
6159990b4856Slling } else {
6160990b4856Slling ASSERT(0); /* not allowed */
6161990b4856Slling }
6162990b4856Slling
61630a4e9518Sgw25295 switch (prop) {
61640a4e9518Sgw25295 case ZPOOL_PROP_DELEGATION:
6165990b4856Slling spa->spa_delegation = intval;
61660a4e9518Sgw25295 break;
61670a4e9518Sgw25295 case ZPOOL_PROP_BOOTFS:
6168990b4856Slling spa->spa_bootfs = intval;
61690a4e9518Sgw25295 break;
61700a4e9518Sgw25295 case ZPOOL_PROP_FAILUREMODE:
61710a4e9518Sgw25295 spa->spa_failmode = intval;
61720a4e9518Sgw25295 break;
6173573ca77eSGeorge Wilson case ZPOOL_PROP_AUTOEXPAND:
6174573ca77eSGeorge Wilson spa->spa_autoexpand = intval;
6175b98131cfSEric Taylor if (tx->tx_txg != TXG_INITIAL)
6176b98131cfSEric Taylor spa_async_request(spa,
6177b98131cfSEric Taylor SPA_ASYNC_AUTOEXPAND);
6178573ca77eSGeorge Wilson break;
6179b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPDITTO:
6180b24ab676SJeff Bonwick spa->spa_dedup_ditto = intval;
6181b24ab676SJeff Bonwick break;
61820a4e9518Sgw25295 default:
61830a4e9518Sgw25295 break;
61840a4e9518Sgw25295 }
6185990b4856Slling }
6186990b4856Slling
6187b1b8ab34Slling }
6188e14bb325SJeff Bonwick
6189e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock);
6190b1b8ab34Slling }
6191b1b8ab34Slling
6192fa9e4066Sahrens /*
6193cde58dbcSMatthew Ahrens * Perform one-time upgrade on-disk changes. spa_version() does not
6194cde58dbcSMatthew Ahrens * reflect the new version this txg, so there must be no changes this
6195cde58dbcSMatthew Ahrens * txg to anything that the upgrade code depends on after it executes.
6196cde58dbcSMatthew Ahrens * Therefore this must be called after dsl_pool_sync() does the sync
6197cde58dbcSMatthew Ahrens * tasks.
6198cde58dbcSMatthew Ahrens */
6199cde58dbcSMatthew Ahrens static void
spa_sync_upgrades(spa_t * spa,dmu_tx_t * tx)6200cde58dbcSMatthew Ahrens spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6201cde58dbcSMatthew Ahrens {
6202cde58dbcSMatthew Ahrens dsl_pool_t *dp = spa->spa_dsl_pool;
6203cde58dbcSMatthew Ahrens
6204cde58dbcSMatthew Ahrens ASSERT(spa->spa_sync_pass == 1);
6205cde58dbcSMatthew Ahrens
62063b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
62073b2aab18SMatthew Ahrens
6208cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6209cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6210cde58dbcSMatthew Ahrens dsl_pool_create_origin(dp, tx);
6211cde58dbcSMatthew Ahrens
6212cde58dbcSMatthew Ahrens /* Keeping the origin open increases spa_minref */
6213cde58dbcSMatthew Ahrens spa->spa_minref += 3;
6214cde58dbcSMatthew Ahrens }
6215cde58dbcSMatthew Ahrens
6216cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6217cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6218cde58dbcSMatthew Ahrens dsl_pool_upgrade_clones(dp, tx);
6219cde58dbcSMatthew Ahrens }
6220cde58dbcSMatthew Ahrens
6221cde58dbcSMatthew Ahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6222cde58dbcSMatthew Ahrens spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6223cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dp, tx);
6224cde58dbcSMatthew Ahrens
6225cde58dbcSMatthew Ahrens /* Keeping the freedir open increases spa_minref */
6226cde58dbcSMatthew Ahrens spa->spa_minref += 3;
6227cde58dbcSMatthew Ahrens }
6228ad135b5dSChristopher Siden
6229ad135b5dSChristopher Siden if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6230ad135b5dSChristopher Siden spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6231ad135b5dSChristopher Siden spa_feature_create_zap_objects(spa, tx);
6232ad135b5dSChristopher Siden }
6233b8289d24SDaniil Lunev
6234b8289d24SDaniil Lunev /*
6235b8289d24SDaniil Lunev * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6236b8289d24SDaniil Lunev * when possibility to use lz4 compression for metadata was added
6237b8289d24SDaniil Lunev * Old pools that have this feature enabled must be upgraded to have
6238b8289d24SDaniil Lunev * this feature active
6239b8289d24SDaniil Lunev */
6240b8289d24SDaniil Lunev if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6241b8289d24SDaniil Lunev boolean_t lz4_en = spa_feature_is_enabled(spa,
6242b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS);
6243b8289d24SDaniil Lunev boolean_t lz4_ac = spa_feature_is_active(spa,
6244b8289d24SDaniil Lunev SPA_FEATURE_LZ4_COMPRESS);
6245b8289d24SDaniil Lunev
6246b8289d24SDaniil Lunev if (lz4_en && !lz4_ac)
6247b8289d24SDaniil Lunev spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6248b8289d24SDaniil Lunev }
624945818ee1SMatthew Ahrens
625045818ee1SMatthew Ahrens /*
625145818ee1SMatthew Ahrens * If we haven't written the salt, do so now. Note that the
625245818ee1SMatthew Ahrens * feature may not be activated yet, but that's fine since
625345818ee1SMatthew Ahrens * the presence of this ZAP entry is backwards compatible.
625445818ee1SMatthew Ahrens */
625545818ee1SMatthew Ahrens if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
625645818ee1SMatthew Ahrens DMU_POOL_CHECKSUM_SALT) == ENOENT) {
625745818ee1SMatthew Ahrens VERIFY0(zap_add(spa->spa_meta_objset,
625845818ee1SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
625945818ee1SMatthew Ahrens sizeof (spa->spa_cksum_salt.zcs_bytes),
626045818ee1SMatthew Ahrens spa->spa_cksum_salt.zcs_bytes, tx));
626145818ee1SMatthew Ahrens }
626245818ee1SMatthew Ahrens
62633b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG);
6264cde58dbcSMatthew Ahrens }
6265cde58dbcSMatthew Ahrens
6266cde58dbcSMatthew Ahrens /*
6267fa9e4066Sahrens * Sync the specified transaction group. New blocks may be dirtied as
6268fa9e4066Sahrens * part of the process, so we iterate until it converges.
6269fa9e4066Sahrens */
6270fa9e4066Sahrens void
spa_sync(spa_t * spa,uint64_t txg)6271fa9e4066Sahrens spa_sync(spa_t *spa, uint64_t txg)
6272fa9e4066Sahrens {
6273fa9e4066Sahrens dsl_pool_t *dp = spa->spa_dsl_pool;
6274fa9e4066Sahrens objset_t *mos = spa->spa_meta_objset;
6275b24ab676SJeff Bonwick bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
62760373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev;
6277fa9e4066Sahrens vdev_t *vd;
6278fa9e4066Sahrens dmu_tx_t *tx;
6279e14bb325SJeff Bonwick int error;
6280fa9e4066Sahrens
6281f9af39baSGeorge Wilson VERIFY(spa_writeable(spa));
6282f9af39baSGeorge Wilson
6283fa9e4066Sahrens /*
6284fa9e4066Sahrens * Lock out configuration changes.
6285fa9e4066Sahrens */
6286e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6287fa9e4066Sahrens
6288fa9e4066Sahrens spa->spa_syncing_txg = txg;
6289fa9e4066Sahrens spa->spa_sync_pass = 0;
6290fa9e4066Sahrens
6291e14bb325SJeff Bonwick /*
6292e14bb325SJeff Bonwick * If there are any pending vdev state changes, convert them
6293e14bb325SJeff Bonwick * into config changes that go out with this transaction group.
6294e14bb325SJeff Bonwick */
6295e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
62968ad4d6ddSJeff Bonwick while (list_head(&spa->spa_state_dirty_list) != NULL) {
62978ad4d6ddSJeff Bonwick /*
62988ad4d6ddSJeff Bonwick * We need the write lock here because, for aux vdevs,
62998ad4d6ddSJeff Bonwick * calling vdev_config_dirty() modifies sav_config.
63008ad4d6ddSJeff Bonwick * This is ugly and will become unnecessary when we
63018ad4d6ddSJeff Bonwick * eliminate the aux vdev wart by integrating all vdevs
63028ad4d6ddSJeff Bonwick * into the root vdev tree.
63038ad4d6ddSJeff Bonwick */
63048ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
63058ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6306e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6307e14bb325SJeff Bonwick vdev_state_clean(vd);
6308e14bb325SJeff Bonwick vdev_config_dirty(vd);
6309e14bb325SJeff Bonwick }
63108ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
63118ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
63128ad4d6ddSJeff Bonwick }
6313e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG);
6314e14bb325SJeff Bonwick
631599653d4eSeschrock tx = dmu_tx_create_assigned(dp, txg);
631699653d4eSeschrock
6317283b8460SGeorge.Wilson spa->spa_sync_starttime = gethrtime();
6318283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6319283b8460SGeorge.Wilson spa->spa_sync_starttime + spa->spa_deadman_synctime));
6320283b8460SGeorge.Wilson
632199653d4eSeschrock /*
6322e7437265Sahrens * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
632399653d4eSeschrock * set spa_deflate if we have no raid-z vdevs.
632499653d4eSeschrock */
6325e7437265Sahrens if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6326e7437265Sahrens spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
632799653d4eSeschrock int i;
632899653d4eSeschrock
632999653d4eSeschrock for (i = 0; i < rvd->vdev_children; i++) {
633099653d4eSeschrock vd = rvd->vdev_child[i];
633199653d4eSeschrock if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
633299653d4eSeschrock break;
633399653d4eSeschrock }
633499653d4eSeschrock if (i == rvd->vdev_children) {
633599653d4eSeschrock spa->spa_deflate = TRUE;
633699653d4eSeschrock VERIFY(0 == zap_add(spa->spa_meta_objset,
633799653d4eSeschrock DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
633899653d4eSeschrock sizeof (uint64_t), 1, &spa->spa_deflate, tx));
633999653d4eSeschrock }
634099653d4eSeschrock }
634199653d4eSeschrock
6342fa9e4066Sahrens /*
6343fa9e4066Sahrens * Iterate to convergence.
6344fa9e4066Sahrens */
6345fa9e4066Sahrens do {
6346b24ab676SJeff Bonwick int pass = ++spa->spa_sync_pass;
6347fa9e4066Sahrens
6348fa9e4066Sahrens spa_sync_config_object(spa, tx);
6349fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6350fa94a07fSbrendan ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6351fa94a07fSbrendan spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6352fa94a07fSbrendan ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6353ea8dc4b6Seschrock spa_errlog_sync(spa, txg);
6354fa9e4066Sahrens dsl_pool_sync(dp, txg);
6355fa9e4066Sahrens
635601f55e48SGeorge Wilson if (pass < zfs_sync_pass_deferred_free) {
635769962b56SMatthew Ahrens spa_sync_frees(spa, free_bpl, tx);
6358b24ab676SJeff Bonwick } else {
6359231aab85SMatthew Ahrens /*
6360231aab85SMatthew Ahrens * We can not defer frees in pass 1, because
6361231aab85SMatthew Ahrens * we sync the deferred frees later in pass 1.
6362231aab85SMatthew Ahrens */
6363231aab85SMatthew Ahrens ASSERT3U(pass, >, 1);
6364cde58dbcSMatthew Ahrens bplist_iterate(free_bpl, bpobj_enqueue_cb,
636569962b56SMatthew Ahrens &spa->spa_deferred_bpobj, tx);
6366fa9e4066Sahrens }
6367fa9e4066Sahrens
6368b24ab676SJeff Bonwick ddt_sync(spa, txg);
63693f9d6ad7SLin Ling dsl_scan_sync(dp, tx);
6370afee20e4SGeorge Wilson
6371b24ab676SJeff Bonwick while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6372b24ab676SJeff Bonwick vdev_sync(vd, txg);
6373fa9e4066Sahrens
6374231aab85SMatthew Ahrens if (pass == 1) {
6375cde58dbcSMatthew Ahrens spa_sync_upgrades(spa, tx);
6376231aab85SMatthew Ahrens ASSERT3U(txg, >=,
6377231aab85SMatthew Ahrens spa->spa_uberblock.ub_rootbp.blk_birth);
6378231aab85SMatthew Ahrens /*
6379231aab85SMatthew Ahrens * Note: We need to check if the MOS is dirty
6380231aab85SMatthew Ahrens * because we could have marked the MOS dirty
6381231aab85SMatthew Ahrens * without updating the uberblock (e.g. if we
6382231aab85SMatthew Ahrens * have sync tasks but no dirty user data). We
6383231aab85SMatthew Ahrens * need to check the uberblock's rootbp because
6384231aab85SMatthew Ahrens * it is updated if we have synced out dirty
6385231aab85SMatthew Ahrens * data (though in this case the MOS will most
6386231aab85SMatthew Ahrens * likely also be dirty due to second order
6387231aab85SMatthew Ahrens * effects, we don't want to rely on that here).
6388231aab85SMatthew Ahrens */
6389231aab85SMatthew Ahrens if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6390231aab85SMatthew Ahrens !dmu_objset_is_dirty(mos, txg)) {
6391231aab85SMatthew Ahrens /*
6392231aab85SMatthew Ahrens * Nothing changed on the first pass,
6393231aab85SMatthew Ahrens * therefore this TXG is a no-op. Avoid
6394231aab85SMatthew Ahrens * syncing deferred frees, so that we
6395231aab85SMatthew Ahrens * can keep this TXG as a no-op.
6396231aab85SMatthew Ahrens */
6397231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6398231aab85SMatthew Ahrens txg));
6399231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6400231aab85SMatthew Ahrens ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6401231aab85SMatthew Ahrens break;
6402231aab85SMatthew Ahrens }
6403231aab85SMatthew Ahrens spa_sync_deferred_frees(spa, tx);
6404231aab85SMatthew Ahrens }
6405cde58dbcSMatthew Ahrens
6406b24ab676SJeff Bonwick } while (dmu_objset_is_dirty(mos, txg));
6407b24ab676SJeff Bonwick
6408fa9e4066Sahrens /*
6409fa9e4066Sahrens * Rewrite the vdev configuration (which includes the uberblock)
6410fa9e4066Sahrens * to commit the transaction group.
64110373e76bSbonwick *
641217f17c2dSbonwick * If there are no dirty vdevs, we sync the uberblock to a few
641317f17c2dSbonwick * random top-level vdevs that are known to be visible in the
6414e14bb325SJeff Bonwick * config cache (see spa_vdev_add() for a complete description).
6415e14bb325SJeff Bonwick * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6416fa9e4066Sahrens */
6417e14bb325SJeff Bonwick for (;;) {
6418e14bb325SJeff Bonwick /*
6419e14bb325SJeff Bonwick * We hold SCL_STATE to prevent vdev open/close/etc.
6420e14bb325SJeff Bonwick * while we're attempting to write the vdev labels.
6421e14bb325SJeff Bonwick */
6422e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6423e14bb325SJeff Bonwick
6424e14bb325SJeff Bonwick if (list_is_empty(&spa->spa_config_dirty_list)) {
642521bf64a7Sgw25295 vdev_t *svd[SPA_DVAS_PER_BP];
642621bf64a7Sgw25295 int svdcount = 0;
64270373e76bSbonwick int children = rvd->vdev_children;
64280373e76bSbonwick int c0 = spa_get_random(children);
64290373e76bSbonwick
6430573ca77eSGeorge Wilson for (int c = 0; c < children; c++) {
64310373e76bSbonwick vd = rvd->vdev_child[(c0 + c) % children];
643217f17c2dSbonwick if (vd->vdev_ms_array == 0 || vd->vdev_islog)
64330373e76bSbonwick continue;
643417f17c2dSbonwick svd[svdcount++] = vd;
643517f17c2dSbonwick if (svdcount == SPA_DVAS_PER_BP)
64360373e76bSbonwick break;
64370373e76bSbonwick }
6438eb5bb584SWill Andrews error = vdev_config_sync(svd, svdcount, txg);
643921bf64a7Sgw25295 } else {
6440e14bb325SJeff Bonwick error = vdev_config_sync(rvd->vdev_child,
6441eb5bb584SWill Andrews rvd->vdev_children, txg);
6442e14bb325SJeff Bonwick }
6443e14bb325SJeff Bonwick
6444dfbb9432SGeorge Wilson if (error == 0)
6445dfbb9432SGeorge Wilson spa->spa_last_synced_guid = rvd->vdev_guid;
6446dfbb9432SGeorge Wilson
6447e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG);
6448e14bb325SJeff Bonwick
6449e14bb325SJeff Bonwick if (error == 0)
6450e14bb325SJeff Bonwick break;
6451e14bb325SJeff Bonwick zio_suspend(spa, NULL);
6452e14bb325SJeff Bonwick zio_resume_wait(spa);
64530373e76bSbonwick }
645499653d4eSeschrock dmu_tx_commit(tx);
645599653d4eSeschrock
6456283b8460SGeorge.Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6457283b8460SGeorge.Wilson
64580373e76bSbonwick /*
64590373e76bSbonwick * Clear the dirty config list.
64600373e76bSbonwick */
6461e14bb325SJeff Bonwick while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
64620373e76bSbonwick vdev_config_clean(vd);
64630373e76bSbonwick
64640373e76bSbonwick /*
64650373e76bSbonwick * Now that the new config has synced transactionally,
64660373e76bSbonwick * let it become visible to the config cache.
64670373e76bSbonwick */
64680373e76bSbonwick if (spa->spa_config_syncing != NULL) {
64690373e76bSbonwick spa_config_set(spa, spa->spa_config_syncing);
64700373e76bSbonwick spa->spa_config_txg = txg;
64710373e76bSbonwick spa->spa_config_syncing = NULL;
64720373e76bSbonwick }
6473fa9e4066Sahrens
6474fa9e4066Sahrens spa->spa_ubsync = spa->spa_uberblock;
6475fa9e4066Sahrens
6476b24ab676SJeff Bonwick dsl_pool_sync_done(dp, txg);
6477fa9e4066Sahrens
6478fa9e4066Sahrens /*
6479fa9e4066Sahrens * Update usable space statistics.
6480fa9e4066Sahrens */
6481fa9e4066Sahrens while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6482fa9e4066Sahrens vdev_sync_done(vd, txg);
6483fa9e4066Sahrens
6484485bbbf5SGeorge Wilson spa_update_dspace(spa);
6485485bbbf5SGeorge Wilson
6486fa9e4066Sahrens /*
6487fa9e4066Sahrens * It had better be the case that we didn't dirty anything
648899653d4eSeschrock * since vdev_config_sync().
6489fa9e4066Sahrens */
6490fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6491fa9e4066Sahrens ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6492fa9e4066Sahrens ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6493b24ab676SJeff Bonwick
6494b24ab676SJeff Bonwick spa->spa_sync_pass = 0;
6495fa9e4066Sahrens
6496e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG);
6497ea8dc4b6Seschrock
6498468c413aSTim Haley spa_handle_ignored_writes(spa);
6499468c413aSTim Haley
6500ea8dc4b6Seschrock /*
6501ea8dc4b6Seschrock * If any async tasks have been requested, kick them off.
6502ea8dc4b6Seschrock */
6503ea8dc4b6Seschrock spa_async_dispatch(spa);
6504fa9e4066Sahrens }
6505fa9e4066Sahrens
6506fa9e4066Sahrens /*
6507fa9e4066Sahrens * Sync all pools. We don't want to hold the namespace lock across these
6508fa9e4066Sahrens * operations, so we take a reference on the spa_t and drop the lock during the
6509fa9e4066Sahrens * sync.
6510fa9e4066Sahrens */
6511fa9e4066Sahrens void
spa_sync_allpools(void)6512fa9e4066Sahrens spa_sync_allpools(void)
6513fa9e4066Sahrens {
6514fa9e4066Sahrens spa_t *spa = NULL;
6515fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
6516fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) {
6517f9af39baSGeorge Wilson if (spa_state(spa) != POOL_STATE_ACTIVE ||
6518f9af39baSGeorge Wilson !spa_writeable(spa) || spa_suspended(spa))
6519fa9e4066Sahrens continue;
6520fa9e4066Sahrens spa_open_ref(spa, FTAG);
6521fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
6522fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), 0);
6523fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
6524fa9e4066Sahrens spa_close(spa, FTAG);
6525fa9e4066Sahrens }
6526fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
6527fa9e4066Sahrens }
6528fa9e4066Sahrens
6529fa9e4066Sahrens /*
6530fa9e4066Sahrens * ==========================================================================
6531fa9e4066Sahrens * Miscellaneous routines
6532fa9e4066Sahrens * ==========================================================================
6533fa9e4066Sahrens */
6534fa9e4066Sahrens
6535fa9e4066Sahrens /*
6536fa9e4066Sahrens * Remove all pools in the system.
6537fa9e4066Sahrens */
6538fa9e4066Sahrens void
spa_evict_all(void)6539fa9e4066Sahrens spa_evict_all(void)
6540fa9e4066Sahrens {
6541fa9e4066Sahrens spa_t *spa;
6542fa9e4066Sahrens
6543fa9e4066Sahrens /*
6544fa9e4066Sahrens * Remove all cached state. All pools should be closed now,
6545fa9e4066Sahrens * so every spa in the AVL tree should be unreferenced.
6546fa9e4066Sahrens */
6547fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
6548fa9e4066Sahrens while ((spa = spa_next(NULL)) != NULL) {
6549fa9e4066Sahrens /*
6550ea8dc4b6Seschrock * Stop async tasks. The async thread may need to detach
6551ea8dc4b6Seschrock * a device that's been replaced, which requires grabbing
6552ea8dc4b6Seschrock * spa_namespace_lock, so we must drop it here.
6553fa9e4066Sahrens */
6554fa9e4066Sahrens spa_open_ref(spa, FTAG);
6555fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
6556ea8dc4b6Seschrock spa_async_suspend(spa);
6557fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
6558fa9e4066Sahrens spa_close(spa, FTAG);
6559fa9e4066Sahrens
6560fa9e4066Sahrens if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6561fa9e4066Sahrens spa_unload(spa);
6562fa9e4066Sahrens spa_deactivate(spa);
6563fa9e4066Sahrens }
6564fa9e4066Sahrens spa_remove(spa);
6565fa9e4066Sahrens }
6566fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
6567fa9e4066Sahrens }
6568ea8dc4b6Seschrock
6569ea8dc4b6Seschrock vdev_t *
spa_lookup_by_guid(spa_t * spa,uint64_t guid,boolean_t aux)65706809eb4eSEric Schrock spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6571ea8dc4b6Seschrock {
6572c5904d13Seschrock vdev_t *vd;
6573c5904d13Seschrock int i;
6574c5904d13Seschrock
6575c5904d13Seschrock if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6576c5904d13Seschrock return (vd);
6577c5904d13Seschrock
65786809eb4eSEric Schrock if (aux) {
6579c5904d13Seschrock for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6580c5904d13Seschrock vd = spa->spa_l2cache.sav_vdevs[i];
6581c5904d13Seschrock if (vd->vdev_guid == guid)
6582c5904d13Seschrock return (vd);
6583c5904d13Seschrock }
65846809eb4eSEric Schrock
65856809eb4eSEric Schrock for (i = 0; i < spa->spa_spares.sav_count; i++) {
65866809eb4eSEric Schrock vd = spa->spa_spares.sav_vdevs[i];
65876809eb4eSEric Schrock if (vd->vdev_guid == guid)
65886809eb4eSEric Schrock return (vd);
65896809eb4eSEric Schrock }
6590c5904d13Seschrock }
6591c5904d13Seschrock
6592c5904d13Seschrock return (NULL);
6593ea8dc4b6Seschrock }
6594eaca9bbdSeschrock
6595eaca9bbdSeschrock void
spa_upgrade(spa_t * spa,uint64_t version)6596990b4856Slling spa_upgrade(spa_t *spa, uint64_t version)
6597eaca9bbdSeschrock {
6598f9af39baSGeorge Wilson ASSERT(spa_writeable(spa));
6599f9af39baSGeorge Wilson
6600e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6601eaca9bbdSeschrock
6602eaca9bbdSeschrock /*
6603eaca9bbdSeschrock * This should only be called for a non-faulted pool, and since a
6604eaca9bbdSeschrock * future version would result in an unopenable pool, this shouldn't be
6605eaca9bbdSeschrock * possible.
6606eaca9bbdSeschrock */
660762eae887SRichard Yao ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
66085d7b4d43SMatthew Ahrens ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6609eaca9bbdSeschrock
6610990b4856Slling spa->spa_uberblock.ub_version = version;
6611eaca9bbdSeschrock vdev_config_dirty(spa->spa_root_vdev);
6612eaca9bbdSeschrock
6613e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
661499653d4eSeschrock
661599653d4eSeschrock txg_wait_synced(spa_get_dsl(spa), 0);
661699653d4eSeschrock }
661799653d4eSeschrock
661899653d4eSeschrock boolean_t
spa_has_spare(spa_t * spa,uint64_t guid)661999653d4eSeschrock spa_has_spare(spa_t *spa, uint64_t guid)
662099653d4eSeschrock {
662199653d4eSeschrock int i;
662239c23413Seschrock uint64_t spareguid;
6623fa94a07fSbrendan spa_aux_vdev_t *sav = &spa->spa_spares;
662499653d4eSeschrock
6625fa94a07fSbrendan for (i = 0; i < sav->sav_count; i++)
6626fa94a07fSbrendan if (sav->sav_vdevs[i]->vdev_guid == guid)
662799653d4eSeschrock return (B_TRUE);
662899653d4eSeschrock
6629fa94a07fSbrendan for (i = 0; i < sav->sav_npending; i++) {
6630fa94a07fSbrendan if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6631fa94a07fSbrendan &spareguid) == 0 && spareguid == guid)
663239c23413Seschrock return (B_TRUE);
663339c23413Seschrock }
663439c23413Seschrock
663599653d4eSeschrock return (B_FALSE);
6636eaca9bbdSeschrock }
6637b1b8ab34Slling
66383d7072f8Seschrock /*
663989a89ebfSlling * Check if a pool has an active shared spare device.
664089a89ebfSlling * Note: reference count of an active spare is 2, as a spare and as a replace
664189a89ebfSlling */
664289a89ebfSlling static boolean_t
spa_has_active_shared_spare(spa_t * spa)664389a89ebfSlling spa_has_active_shared_spare(spa_t *spa)
664489a89ebfSlling {
664589a89ebfSlling int i, refcnt;
664689a89ebfSlling uint64_t pool;
664789a89ebfSlling spa_aux_vdev_t *sav = &spa->spa_spares;
664889a89ebfSlling
664989a89ebfSlling for (i = 0; i < sav->sav_count; i++) {
665089a89ebfSlling if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
665189a89ebfSlling &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
665289a89ebfSlling refcnt > 2)
665389a89ebfSlling return (B_TRUE);
665489a89ebfSlling }
665589a89ebfSlling
665689a89ebfSlling return (B_FALSE);
665789a89ebfSlling }
665889a89ebfSlling
665989a89ebfSlling /*
66603d7072f8Seschrock * Post a sysevent corresponding to the given event. The 'name' must be one of
66613d7072f8Seschrock * the event definitions in sys/sysevent/eventdefs.h. The payload will be
66623d7072f8Seschrock * filled in from the spa and (optionally) the vdev. This doesn't do anything
66633d7072f8Seschrock * in the userland libzpool, as we don't want consumers to misinterpret ztest
66643d7072f8Seschrock * or zdb as real changes.
66653d7072f8Seschrock */
66663d7072f8Seschrock void
spa_event_notify(spa_t * spa,vdev_t * vd,const char * name)66673d7072f8Seschrock spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
66683d7072f8Seschrock {
66693d7072f8Seschrock #ifdef _KERNEL
66703d7072f8Seschrock sysevent_t *ev;
66713d7072f8Seschrock sysevent_attr_list_t *attr = NULL;
66723d7072f8Seschrock sysevent_value_t value;
66733d7072f8Seschrock sysevent_id_t eid;
66743d7072f8Seschrock
66753d7072f8Seschrock ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
66763d7072f8Seschrock SE_SLEEP);
66773d7072f8Seschrock
66783d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING;
66793d7072f8Seschrock value.value.sv_string = spa_name(spa);
66803d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
66813d7072f8Seschrock goto done;
66823d7072f8Seschrock
66833d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64;
66843d7072f8Seschrock value.value.sv_uint64 = spa_guid(spa);
66853d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
66863d7072f8Seschrock goto done;
66873d7072f8Seschrock
66883d7072f8Seschrock if (vd) {
66893d7072f8Seschrock value.value_type = SE_DATA_TYPE_UINT64;
66903d7072f8Seschrock value.value.sv_uint64 = vd->vdev_guid;
66913d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
66923d7072f8Seschrock SE_SLEEP) != 0)
66933d7072f8Seschrock goto done;
66943d7072f8Seschrock
66953d7072f8Seschrock if (vd->vdev_path) {
66963d7072f8Seschrock value.value_type = SE_DATA_TYPE_STRING;
66973d7072f8Seschrock value.value.sv_string = vd->vdev_path;
66983d7072f8Seschrock if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
66993d7072f8Seschrock &value, SE_SLEEP) != 0)
67003d7072f8Seschrock goto done;
67013d7072f8Seschrock }
67023d7072f8Seschrock }
67033d7072f8Seschrock
6704b01c3b58Seschrock if (sysevent_attach_attributes(ev, attr) != 0)
6705b01c3b58Seschrock goto done;
6706b01c3b58Seschrock attr = NULL;
6707b01c3b58Seschrock
67083d7072f8Seschrock (void) log_sysevent(ev, SE_SLEEP, &eid);
67093d7072f8Seschrock
67103d7072f8Seschrock done:
67113d7072f8Seschrock if (attr)
67123d7072f8Seschrock sysevent_free_attr(attr);
67133d7072f8Seschrock sysevent_free(ev);
67143d7072f8Seschrock #endif
67153d7072f8Seschrock }
6716