]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa.c
ZTS: remove dead cleanup code from snapshot tests
[mirror_zfs.git] / module / zfs / spa.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
4747a7d3 24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
fb390aaf 25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
62bdd5eb 26 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
0c66c32d 27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
3c67d83a 28 * Copyright 2013 Saso Kiselkov. All rights reserved.
e550644f
BB
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Toomas Soome <tsoome@me.com>
a0bd735a 31 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
0ea05c64 32 * Copyright (c) 2017 Datto Inc.
12fa0466 33 * Copyright 2017 Joyent, Inc.
a38718a6 34 */
34dc7c2f 35
34dc7c2f 36/*
e49f1e20
WA
37 * SPA: Storage Pool Allocator
38 *
34dc7c2f
BB
39 * This file contains all the routines used when modifying on-disk SPA state.
40 * This includes opening, importing, destroying, exporting a pool, and syncing a
41 * pool.
42 */
43
44#include <sys/zfs_context.h>
45#include <sys/fm/fs/zfs.h>
46#include <sys/spa_impl.h>
47#include <sys/zio.h>
48#include <sys/zio_checksum.h>
34dc7c2f
BB
49#include <sys/dmu.h>
50#include <sys/dmu_tx.h>
51#include <sys/zap.h>
52#include <sys/zil.h>
428870ff 53#include <sys/ddt.h>
34dc7c2f 54#include <sys/vdev_impl.h>
a1d477c2
MA
55#include <sys/vdev_removal.h>
56#include <sys/vdev_indirect_mapping.h>
57#include <sys/vdev_indirect_births.h>
c28b2279 58#include <sys/vdev_disk.h>
34dc7c2f 59#include <sys/metaslab.h>
428870ff 60#include <sys/metaslab_impl.h>
379ca9cf 61#include <sys/mmp.h>
34dc7c2f
BB
62#include <sys/uberblock_impl.h>
63#include <sys/txg.h>
64#include <sys/avl.h>
a1d477c2 65#include <sys/bpobj.h>
34dc7c2f
BB
66#include <sys/dmu_traverse.h>
67#include <sys/dmu_objset.h>
68#include <sys/unique.h>
69#include <sys/dsl_pool.h>
70#include <sys/dsl_dataset.h>
71#include <sys/dsl_dir.h>
72#include <sys/dsl_prop.h>
73#include <sys/dsl_synctask.h>
74#include <sys/fs/zfs.h>
75#include <sys/arc.h>
76#include <sys/callb.h>
77#include <sys/systeminfo.h>
34dc7c2f 78#include <sys/spa_boot.h>
9babb374 79#include <sys/zfs_ioctl.h>
428870ff 80#include <sys/dsl_scan.h>
9ae529ec 81#include <sys/zfeature.h>
13fe0198 82#include <sys/dsl_destroy.h>
526af785 83#include <sys/zvol.h>
34dc7c2f 84
d164b209 85#ifdef _KERNEL
12fa0466
DE
86#include <sys/fm/protocol.h>
87#include <sys/fm/util.h>
428870ff
BB
88#include <sys/bootprops.h>
89#include <sys/callb.h>
90#include <sys/cpupart.h>
91#include <sys/pool.h>
92#include <sys/sysdc.h>
d164b209
BB
93#include <sys/zone.h>
94#endif /* _KERNEL */
95
34dc7c2f
BB
96#include "zfs_prop.h"
97#include "zfs_comutil.h"
98
e6cfd633
WA
99/*
100 * The interval, in seconds, at which failed configuration cache file writes
101 * should be retried.
102 */
a1d477c2 103int zfs_ccw_retry_interval = 300;
e6cfd633 104
428870ff 105typedef enum zti_modes {
7ef5e54e 106 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
7ef5e54e
AL
107 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
108 ZTI_MODE_NULL, /* don't create a taskq */
109 ZTI_NMODES
428870ff 110} zti_modes_t;
34dc7c2f 111
7ef5e54e
AL
112#define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
113#define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
114#define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
115#define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
9babb374 116
7ef5e54e
AL
117#define ZTI_N(n) ZTI_P(n, 1)
118#define ZTI_ONE ZTI_N(1)
9babb374
BB
119
120typedef struct zio_taskq_info {
7ef5e54e 121 zti_modes_t zti_mode;
428870ff 122 uint_t zti_value;
7ef5e54e 123 uint_t zti_count;
9babb374
BB
124} zio_taskq_info_t;
125
126static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
451041db 127 "iss", "iss_h", "int", "int_h"
9babb374
BB
128};
129
428870ff 130/*
7ef5e54e
AL
131 * This table defines the taskq settings for each ZFS I/O type. When
132 * initializing a pool, we use this table to create an appropriately sized
133 * taskq. Some operations are low volume and therefore have a small, static
134 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
135 * macros. Other operations process a large amount of data; the ZTI_BATCH
136 * macro causes us to create a taskq oriented for throughput. Some operations
137 * are so high frequency and short-lived that the taskq itself can become a a
138 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
139 * additional degree of parallelism specified by the number of threads per-
140 * taskq and the number of taskqs; when dispatching an event in this case, the
141 * particular taskq is chosen at random.
142 *
143 * The different taskq priorities are to handle the different contexts (issue
144 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
145 * need to be handled with minimum delay.
428870ff
BB
146 */
147const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
148 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
7ef5e54e 149 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
aa9af22c
BB
150 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
151 { ZTI_BATCH, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
152 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
7ef5e54e
AL
153 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
154 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
9babb374
BB
155};
156
13fe0198
MA
157static void spa_sync_version(void *arg, dmu_tx_t *tx);
158static void spa_sync_props(void *arg, dmu_tx_t *tx);
b128c09f 159static boolean_t spa_has_active_shared_spare(spa_t *spa);
bf701a83 160static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
a1d477c2 161 spa_load_state_t state, spa_import_type_t type, boolean_t trust_config,
428870ff 162 char **ereport);
572e2857 163static void spa_vdev_resilver_done(spa_t *spa);
428870ff 164
e8b96c60 165uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
428870ff
BB
166id_t zio_taskq_psrset_bind = PS_NONE;
167boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
168uint_t zio_taskq_basedc = 80; /* base duty cycle */
169
170boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
171
172/*
173 * This (illegal) pool name is used when temporarily importing a spa_t in order
174 * to get the vdev stats associated with the imported devices.
175 */
176#define TRYIMPORT_NAME "$import"
34dc7c2f
BB
177
178/*
179 * ==========================================================================
180 * SPA properties routines
181 * ==========================================================================
182 */
183
184/*
185 * Add a (source=src, propname=propval) list to an nvlist.
186 */
187static void
188spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
189 uint64_t intval, zprop_source_t src)
190{
191 const char *propname = zpool_prop_to_name(prop);
192 nvlist_t *propval;
193
79c76d5b 194 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
195 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
196
197 if (strval != NULL)
198 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
199 else
200 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
201
202 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
203 nvlist_free(propval);
204}
205
206/*
207 * Get property values from the spa configuration.
208 */
209static void
210spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
211{
1bd201e7 212 vdev_t *rvd = spa->spa_root_vdev;
9ae529ec 213 dsl_pool_t *pool = spa->spa_dsl_pool;
f3a7f661 214 uint64_t size, alloc, cap, version;
82ab6848 215 const zprop_source_t src = ZPROP_SRC_NONE;
b128c09f 216 spa_config_dirent_t *dp;
f3a7f661 217 metaslab_class_t *mc = spa_normal_class(spa);
b128c09f
BB
218
219 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
34dc7c2f 220
1bd201e7 221 if (rvd != NULL) {
428870ff
BB
222 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
223 size = metaslab_class_get_space(spa_normal_class(spa));
d164b209
BB
224 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
428870ff
BB
226 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
227 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
228 size - alloc, src);
1bd201e7 229
f3a7f661
GW
230 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
231 metaslab_class_fragmentation(mc), src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
233 metaslab_class_expandable_space(mc), src);
572e2857
BB
234 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
235 (spa_mode(spa) == FREAD), src);
d164b209 236
428870ff 237 cap = (size == 0) ? 0 : (alloc * 100 / size);
d164b209
BB
238 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
239
428870ff
BB
240 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
241 ddt_get_pool_dedup_ratio(spa), src);
242
d164b209 243 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1bd201e7 244 rvd->vdev_state, src);
d164b209
BB
245
246 version = spa_version(spa);
82ab6848
HM
247 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) {
248 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
249 version, ZPROP_SRC_DEFAULT);
250 } else {
251 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
252 version, ZPROP_SRC_LOCAL);
253 }
d164b209 254 }
34dc7c2f 255
9ae529ec 256 if (pool != NULL) {
9ae529ec
CS
257 /*
258 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
259 * when opening pools before this version freedir will be NULL.
260 */
fbeddd60 261 if (pool->dp_free_dir != NULL) {
9ae529ec 262 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
d683ddbb
JG
263 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
264 src);
9ae529ec
CS
265 } else {
266 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
267 NULL, 0, src);
268 }
fbeddd60
MA
269
270 if (pool->dp_leak_dir != NULL) {
271 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
d683ddbb
JG
272 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
273 src);
fbeddd60
MA
274 } else {
275 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
276 NULL, 0, src);
277 }
9ae529ec
CS
278 }
279
34dc7c2f 280 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
34dc7c2f 281
d96eb2b1
DM
282 if (spa->spa_comment != NULL) {
283 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
284 0, ZPROP_SRC_LOCAL);
285 }
286
34dc7c2f
BB
287 if (spa->spa_root != NULL)
288 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
289 0, ZPROP_SRC_LOCAL);
290
f1512ee6
MA
291 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
292 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
293 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
294 } else {
295 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
296 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
297 }
298
50c957f7
NB
299 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
300 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
301 DNODE_MAX_SIZE, ZPROP_SRC_NONE);
302 } else {
303 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
304 DNODE_MIN_SIZE, ZPROP_SRC_NONE);
305 }
306
b128c09f
BB
307 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
308 if (dp->scd_path == NULL) {
34dc7c2f 309 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
b128c09f
BB
310 "none", 0, ZPROP_SRC_LOCAL);
311 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
34dc7c2f 312 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
b128c09f 313 dp->scd_path, 0, ZPROP_SRC_LOCAL);
34dc7c2f
BB
314 }
315 }
316}
317
318/*
319 * Get zpool property values.
320 */
321int
322spa_prop_get(spa_t *spa, nvlist_t **nvp)
323{
428870ff 324 objset_t *mos = spa->spa_meta_objset;
34dc7c2f
BB
325 zap_cursor_t zc;
326 zap_attribute_t za;
34dc7c2f
BB
327 int err;
328
79c76d5b 329 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP);
c28b2279 330 if (err)
d1d7e268 331 return (err);
34dc7c2f 332
b128c09f
BB
333 mutex_enter(&spa->spa_props_lock);
334
34dc7c2f
BB
335 /*
336 * Get properties from the spa config.
337 */
338 spa_prop_get_config(spa, nvp);
339
34dc7c2f 340 /* If no pool property object, no more prop to get. */
428870ff 341 if (mos == NULL || spa->spa_pool_props_object == 0) {
34dc7c2f 342 mutex_exit(&spa->spa_props_lock);
c28b2279 343 goto out;
34dc7c2f
BB
344 }
345
346 /*
347 * Get properties from the MOS pool property object.
348 */
349 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
350 (err = zap_cursor_retrieve(&zc, &za)) == 0;
351 zap_cursor_advance(&zc)) {
352 uint64_t intval = 0;
353 char *strval = NULL;
354 zprop_source_t src = ZPROP_SRC_DEFAULT;
355 zpool_prop_t prop;
356
31864e3d 357 if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL)
34dc7c2f
BB
358 continue;
359
360 switch (za.za_integer_length) {
361 case 8:
362 /* integer property */
363 if (za.za_first_integer !=
364 zpool_prop_default_numeric(prop))
365 src = ZPROP_SRC_LOCAL;
366
367 if (prop == ZPOOL_PROP_BOOTFS) {
368 dsl_pool_t *dp;
369 dsl_dataset_t *ds = NULL;
370
371 dp = spa_get_dsl(spa);
13fe0198 372 dsl_pool_config_enter(dp, FTAG);
c65aa5b2
BB
373 if ((err = dsl_dataset_hold_obj(dp,
374 za.za_first_integer, FTAG, &ds))) {
13fe0198 375 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
376 break;
377 }
378
eca7b760 379 strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
79c76d5b 380 KM_SLEEP);
34dc7c2f 381 dsl_dataset_name(ds, strval);
b128c09f 382 dsl_dataset_rele(ds, FTAG);
13fe0198 383 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
384 } else {
385 strval = NULL;
386 intval = za.za_first_integer;
387 }
388
389 spa_prop_add_list(*nvp, prop, strval, intval, src);
390
391 if (strval != NULL)
eca7b760 392 kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
393
394 break;
395
396 case 1:
397 /* string property */
79c76d5b 398 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
34dc7c2f
BB
399 err = zap_lookup(mos, spa->spa_pool_props_object,
400 za.za_name, 1, za.za_num_integers, strval);
401 if (err) {
402 kmem_free(strval, za.za_num_integers);
403 break;
404 }
405 spa_prop_add_list(*nvp, prop, strval, 0, src);
406 kmem_free(strval, za.za_num_integers);
407 break;
408
409 default:
410 break;
411 }
412 }
413 zap_cursor_fini(&zc);
414 mutex_exit(&spa->spa_props_lock);
415out:
416 if (err && err != ENOENT) {
417 nvlist_free(*nvp);
418 *nvp = NULL;
419 return (err);
420 }
421
422 return (0);
423}
424
425/*
426 * Validate the given pool properties nvlist and modify the list
427 * for the property values to be set.
428 */
429static int
430spa_prop_validate(spa_t *spa, nvlist_t *props)
431{
432 nvpair_t *elem;
433 int error = 0, reset_bootfs = 0;
d4ed6673 434 uint64_t objnum = 0;
9ae529ec 435 boolean_t has_feature = B_FALSE;
34dc7c2f
BB
436
437 elem = NULL;
438 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
34dc7c2f 439 uint64_t intval;
9ae529ec
CS
440 char *strval, *slash, *check, *fname;
441 const char *propname = nvpair_name(elem);
442 zpool_prop_t prop = zpool_name_to_prop(propname);
443
31864e3d
BB
444 switch (prop) {
445 case ZPOOL_PROP_INVAL:
9ae529ec 446 if (!zpool_prop_feature(propname)) {
2e528b49 447 error = SET_ERROR(EINVAL);
9ae529ec
CS
448 break;
449 }
450
451 /*
452 * Sanitize the input.
453 */
454 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
2e528b49 455 error = SET_ERROR(EINVAL);
9ae529ec
CS
456 break;
457 }
458
459 if (nvpair_value_uint64(elem, &intval) != 0) {
2e528b49 460 error = SET_ERROR(EINVAL);
9ae529ec
CS
461 break;
462 }
34dc7c2f 463
9ae529ec 464 if (intval != 0) {
2e528b49 465 error = SET_ERROR(EINVAL);
9ae529ec
CS
466 break;
467 }
34dc7c2f 468
9ae529ec
CS
469 fname = strchr(propname, '@') + 1;
470 if (zfeature_lookup_name(fname, NULL) != 0) {
2e528b49 471 error = SET_ERROR(EINVAL);
9ae529ec
CS
472 break;
473 }
474
475 has_feature = B_TRUE;
476 break;
34dc7c2f 477
34dc7c2f
BB
478 case ZPOOL_PROP_VERSION:
479 error = nvpair_value_uint64(elem, &intval);
480 if (!error &&
9ae529ec
CS
481 (intval < spa_version(spa) ||
482 intval > SPA_VERSION_BEFORE_FEATURES ||
483 has_feature))
2e528b49 484 error = SET_ERROR(EINVAL);
34dc7c2f
BB
485 break;
486
487 case ZPOOL_PROP_DELEGATION:
488 case ZPOOL_PROP_AUTOREPLACE:
b128c09f 489 case ZPOOL_PROP_LISTSNAPS:
9babb374 490 case ZPOOL_PROP_AUTOEXPAND:
34dc7c2f
BB
491 error = nvpair_value_uint64(elem, &intval);
492 if (!error && intval > 1)
2e528b49 493 error = SET_ERROR(EINVAL);
34dc7c2f
BB
494 break;
495
379ca9cf
OF
496 case ZPOOL_PROP_MULTIHOST:
497 error = nvpair_value_uint64(elem, &intval);
498 if (!error && intval > 1)
499 error = SET_ERROR(EINVAL);
500
501 if (!error && !spa_get_hostid())
502 error = SET_ERROR(ENOTSUP);
503
504 break;
505
34dc7c2f 506 case ZPOOL_PROP_BOOTFS:
9babb374
BB
507 /*
508 * If the pool version is less than SPA_VERSION_BOOTFS,
509 * or the pool is still being created (version == 0),
510 * the bootfs property cannot be set.
511 */
34dc7c2f 512 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
2e528b49 513 error = SET_ERROR(ENOTSUP);
34dc7c2f
BB
514 break;
515 }
516
517 /*
b128c09f 518 * Make sure the vdev config is bootable
34dc7c2f 519 */
b128c09f 520 if (!vdev_is_bootable(spa->spa_root_vdev)) {
2e528b49 521 error = SET_ERROR(ENOTSUP);
34dc7c2f
BB
522 break;
523 }
524
525 reset_bootfs = 1;
526
527 error = nvpair_value_string(elem, &strval);
528
529 if (!error) {
9ae529ec 530 objset_t *os;
f1512ee6 531 uint64_t propval;
b128c09f 532
34dc7c2f
BB
533 if (strval == NULL || strval[0] == '\0') {
534 objnum = zpool_prop_default_numeric(
535 ZPOOL_PROP_BOOTFS);
536 break;
537 }
538
d1d7e268
MK
539 error = dmu_objset_hold(strval, FTAG, &os);
540 if (error)
34dc7c2f 541 break;
b128c09f 542
f1512ee6
MA
543 /*
544 * Must be ZPL, and its property settings
545 * must be supported by GRUB (compression
50c957f7
NB
546 * is not gzip, and large blocks or large
547 * dnodes are not used).
f1512ee6 548 */
428870ff
BB
549
550 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2e528b49 551 error = SET_ERROR(ENOTSUP);
13fe0198
MA
552 } else if ((error =
553 dsl_prop_get_int_ds(dmu_objset_ds(os),
b128c09f 554 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
f1512ee6
MA
555 &propval)) == 0 &&
556 !BOOTFS_COMPRESS_VALID(propval)) {
557 error = SET_ERROR(ENOTSUP);
50c957f7
NB
558 } else if ((error =
559 dsl_prop_get_int_ds(dmu_objset_ds(os),
560 zfs_prop_to_name(ZFS_PROP_DNODESIZE),
561 &propval)) == 0 &&
562 propval != ZFS_DNSIZE_LEGACY) {
563 error = SET_ERROR(ENOTSUP);
b128c09f
BB
564 } else {
565 objnum = dmu_objset_id(os);
566 }
428870ff 567 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
568 }
569 break;
b128c09f 570
34dc7c2f
BB
571 case ZPOOL_PROP_FAILUREMODE:
572 error = nvpair_value_uint64(elem, &intval);
3bfd95d5 573 if (!error && intval > ZIO_FAILURE_MODE_PANIC)
2e528b49 574 error = SET_ERROR(EINVAL);
34dc7c2f
BB
575
576 /*
577 * This is a special case which only occurs when
578 * the pool has completely failed. This allows
579 * the user to change the in-core failmode property
580 * without syncing it out to disk (I/Os might
581 * currently be blocked). We do this by returning
582 * EIO to the caller (spa_prop_set) to trick it
583 * into thinking we encountered a property validation
584 * error.
585 */
b128c09f 586 if (!error && spa_suspended(spa)) {
34dc7c2f 587 spa->spa_failmode = intval;
2e528b49 588 error = SET_ERROR(EIO);
34dc7c2f
BB
589 }
590 break;
591
592 case ZPOOL_PROP_CACHEFILE:
593 if ((error = nvpair_value_string(elem, &strval)) != 0)
594 break;
595
596 if (strval[0] == '\0')
597 break;
598
599 if (strcmp(strval, "none") == 0)
600 break;
601
602 if (strval[0] != '/') {
2e528b49 603 error = SET_ERROR(EINVAL);
34dc7c2f
BB
604 break;
605 }
606
607 slash = strrchr(strval, '/');
608 ASSERT(slash != NULL);
609
610 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
611 strcmp(slash, "/..") == 0)
2e528b49 612 error = SET_ERROR(EINVAL);
34dc7c2f 613 break;
428870ff 614
d96eb2b1
DM
615 case ZPOOL_PROP_COMMENT:
616 if ((error = nvpair_value_string(elem, &strval)) != 0)
617 break;
618 for (check = strval; *check != '\0'; check++) {
619 if (!isprint(*check)) {
2e528b49 620 error = SET_ERROR(EINVAL);
d96eb2b1
DM
621 break;
622 }
d96eb2b1
DM
623 }
624 if (strlen(strval) > ZPROP_MAX_COMMENT)
2e528b49 625 error = SET_ERROR(E2BIG);
d96eb2b1
DM
626 break;
627
428870ff
BB
628 case ZPOOL_PROP_DEDUPDITTO:
629 if (spa_version(spa) < SPA_VERSION_DEDUP)
2e528b49 630 error = SET_ERROR(ENOTSUP);
428870ff
BB
631 else
632 error = nvpair_value_uint64(elem, &intval);
633 if (error == 0 &&
634 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
2e528b49 635 error = SET_ERROR(EINVAL);
428870ff 636 break;
e75c13c3
BB
637
638 default:
639 break;
34dc7c2f
BB
640 }
641
642 if (error)
643 break;
644 }
645
646 if (!error && reset_bootfs) {
647 error = nvlist_remove(props,
648 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
649
650 if (!error) {
651 error = nvlist_add_uint64(props,
652 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
653 }
654 }
655
656 return (error);
657}
658
d164b209
BB
659void
660spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
661{
662 char *cachefile;
663 spa_config_dirent_t *dp;
664
665 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
666 &cachefile) != 0)
667 return;
668
669 dp = kmem_alloc(sizeof (spa_config_dirent_t),
79c76d5b 670 KM_SLEEP);
d164b209
BB
671
672 if (cachefile[0] == '\0')
673 dp->scd_path = spa_strdup(spa_config_path);
674 else if (strcmp(cachefile, "none") == 0)
675 dp->scd_path = NULL;
676 else
677 dp->scd_path = spa_strdup(cachefile);
678
679 list_insert_head(&spa->spa_config_list, dp);
680 if (need_sync)
681 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
682}
683
34dc7c2f
BB
684int
685spa_prop_set(spa_t *spa, nvlist_t *nvp)
686{
687 int error;
9ae529ec 688 nvpair_t *elem = NULL;
d164b209 689 boolean_t need_sync = B_FALSE;
34dc7c2f
BB
690
691 if ((error = spa_prop_validate(spa, nvp)) != 0)
692 return (error);
693
d164b209 694 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
9ae529ec 695 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
d164b209 696
572e2857
BB
697 if (prop == ZPOOL_PROP_CACHEFILE ||
698 prop == ZPOOL_PROP_ALTROOT ||
699 prop == ZPOOL_PROP_READONLY)
d164b209
BB
700 continue;
701
31864e3d 702 if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
9ae529ec
CS
703 uint64_t ver;
704
705 if (prop == ZPOOL_PROP_VERSION) {
706 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
707 } else {
708 ASSERT(zpool_prop_feature(nvpair_name(elem)));
709 ver = SPA_VERSION_FEATURES;
710 need_sync = B_TRUE;
711 }
712
713 /* Save time if the version is already set. */
714 if (ver == spa_version(spa))
715 continue;
716
717 /*
718 * In addition to the pool directory object, we might
719 * create the pool properties object, the features for
720 * read object, the features for write object, or the
721 * feature descriptions object.
722 */
13fe0198 723 error = dsl_sync_task(spa->spa_name, NULL,
3d45fdd6
MA
724 spa_sync_version, &ver,
725 6, ZFS_SPACE_CHECK_RESERVED);
9ae529ec
CS
726 if (error)
727 return (error);
728 continue;
729 }
730
d164b209
BB
731 need_sync = B_TRUE;
732 break;
733 }
734
9ae529ec 735 if (need_sync) {
13fe0198 736 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
3d45fdd6 737 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
9ae529ec
CS
738 }
739
740 return (0);
34dc7c2f
BB
741}
742
743/*
744 * If the bootfs property value is dsobj, clear it.
745 */
746void
747spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
748{
749 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
750 VERIFY(zap_remove(spa->spa_meta_objset,
751 spa->spa_pool_props_object,
752 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
753 spa->spa_bootfs = 0;
754 }
755}
756
3bc7e0fb
GW
757/*ARGSUSED*/
758static int
13fe0198 759spa_change_guid_check(void *arg, dmu_tx_t *tx)
3bc7e0fb 760{
1c27024e 761 ASSERTV(uint64_t *newguid = arg);
13fe0198 762 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3bc7e0fb
GW
763 vdev_t *rvd = spa->spa_root_vdev;
764 uint64_t vdev_state;
3bc7e0fb
GW
765
766 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
767 vdev_state = rvd->vdev_state;
768 spa_config_exit(spa, SCL_STATE, FTAG);
769
770 if (vdev_state != VDEV_STATE_HEALTHY)
2e528b49 771 return (SET_ERROR(ENXIO));
3bc7e0fb
GW
772
773 ASSERT3U(spa_guid(spa), !=, *newguid);
774
775 return (0);
776}
777
778static void
13fe0198 779spa_change_guid_sync(void *arg, dmu_tx_t *tx)
3bc7e0fb 780{
13fe0198
MA
781 uint64_t *newguid = arg;
782 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3bc7e0fb
GW
783 uint64_t oldguid;
784 vdev_t *rvd = spa->spa_root_vdev;
785
786 oldguid = spa_guid(spa);
787
788 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
789 rvd->vdev_guid = *newguid;
790 rvd->vdev_guid_sum += (*newguid - oldguid);
791 vdev_config_dirty(rvd);
792 spa_config_exit(spa, SCL_STATE, FTAG);
793
6f1ffb06
MA
794 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
795 oldguid, *newguid);
3bc7e0fb
GW
796}
797
3541dc6d
GA
798/*
799 * Change the GUID for the pool. This is done so that we can later
800 * re-import a pool built from a clone of our own vdevs. We will modify
801 * the root vdev's guid, our own pool guid, and then mark all of our
802 * vdevs dirty. Note that we must make sure that all our vdevs are
803 * online when we do this, or else any vdevs that weren't present
804 * would be orphaned from our pool. We are also going to issue a
805 * sysevent to update any watchers.
806 */
807int
808spa_change_guid(spa_t *spa)
809{
3bc7e0fb
GW
810 int error;
811 uint64_t guid;
3541dc6d 812
621dd7bb 813 mutex_enter(&spa->spa_vdev_top_lock);
3bc7e0fb
GW
814 mutex_enter(&spa_namespace_lock);
815 guid = spa_generate_guid(NULL);
3541dc6d 816
13fe0198 817 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
3d45fdd6 818 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
3541dc6d 819
3bc7e0fb 820 if (error == 0) {
a1d477c2 821 spa_write_cachefile(spa, B_FALSE, B_TRUE);
12fa0466 822 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID);
3bc7e0fb 823 }
3541dc6d 824
3bc7e0fb 825 mutex_exit(&spa_namespace_lock);
621dd7bb 826 mutex_exit(&spa->spa_vdev_top_lock);
3541dc6d 827
3bc7e0fb 828 return (error);
3541dc6d
GA
829}
830
34dc7c2f
BB
831/*
832 * ==========================================================================
833 * SPA state manipulation (open/create/destroy/import/export)
834 * ==========================================================================
835 */
836
837static int
838spa_error_entry_compare(const void *a, const void *b)
839{
ee36c709
GN
840 const spa_error_entry_t *sa = (const spa_error_entry_t *)a;
841 const spa_error_entry_t *sb = (const spa_error_entry_t *)b;
34dc7c2f
BB
842 int ret;
843
ee36c709 844 ret = memcmp(&sa->se_bookmark, &sb->se_bookmark,
5dbd68a3 845 sizeof (zbookmark_phys_t));
34dc7c2f 846
ee36c709 847 return (AVL_ISIGN(ret));
34dc7c2f
BB
848}
849
850/*
851 * Utility function which retrieves copies of the current logs and
852 * re-initializes them in the process.
853 */
854void
855spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
856{
857 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
858
859 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
860 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
861
862 avl_create(&spa->spa_errlist_scrub,
863 spa_error_entry_compare, sizeof (spa_error_entry_t),
864 offsetof(spa_error_entry_t, se_avl));
865 avl_create(&spa->spa_errlist_last,
866 spa_error_entry_compare, sizeof (spa_error_entry_t),
867 offsetof(spa_error_entry_t, se_avl));
868}
869
7ef5e54e
AL
870static void
871spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
34dc7c2f 872{
7ef5e54e
AL
873 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
874 enum zti_modes mode = ztip->zti_mode;
875 uint_t value = ztip->zti_value;
876 uint_t count = ztip->zti_count;
877 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
878 char name[32];
1c27024e 879 uint_t flags = 0;
428870ff 880 boolean_t batch = B_FALSE;
34dc7c2f 881
7ef5e54e
AL
882 if (mode == ZTI_MODE_NULL) {
883 tqs->stqs_count = 0;
884 tqs->stqs_taskq = NULL;
885 return;
886 }
428870ff 887
7ef5e54e 888 ASSERT3U(count, >, 0);
428870ff 889
7ef5e54e
AL
890 tqs->stqs_count = count;
891 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
428870ff 892
e8b96c60
MA
893 switch (mode) {
894 case ZTI_MODE_FIXED:
895 ASSERT3U(value, >=, 1);
896 value = MAX(value, 1);
d33931a8 897 flags |= TASKQ_DYNAMIC;
e8b96c60 898 break;
7ef5e54e 899
e8b96c60
MA
900 case ZTI_MODE_BATCH:
901 batch = B_TRUE;
902 flags |= TASKQ_THREADS_CPU_PCT;
dcb6bed1 903 value = MIN(zio_taskq_batch_pct, 100);
e8b96c60 904 break;
7ef5e54e 905
e8b96c60
MA
906 default:
907 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
908 "spa_activate()",
909 zio_type_name[t], zio_taskq_types[q], mode, value);
910 break;
911 }
7ef5e54e 912
1c27024e 913 for (uint_t i = 0; i < count; i++) {
e8b96c60 914 taskq_t *tq;
7ef5e54e
AL
915
916 if (count > 1) {
917 (void) snprintf(name, sizeof (name), "%s_%s_%u",
918 zio_type_name[t], zio_taskq_types[q], i);
919 } else {
920 (void) snprintf(name, sizeof (name), "%s_%s",
921 zio_type_name[t], zio_taskq_types[q]);
922 }
923
924 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
925 if (batch)
926 flags |= TASKQ_DC_BATCH;
927
928 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
929 spa->spa_proc, zio_taskq_basedc, flags);
930 } else {
e8b96c60
MA
931 pri_t pri = maxclsyspri;
932 /*
933 * The write issue taskq can be extremely CPU
1229323d
BB
934 * intensive. Run it at slightly less important
935 * priority than the other taskqs. Under Linux this
936 * means incrementing the priority value on platforms
937 * like illumos it should be decremented.
e8b96c60
MA
938 */
939 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
1229323d 940 pri++;
e8b96c60
MA
941
942 tq = taskq_create_proc(name, value, pri, 50,
7ef5e54e
AL
943 INT_MAX, spa->spa_proc, flags);
944 }
945
946 tqs->stqs_taskq[i] = tq;
947 }
948}
949
950static void
951spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
952{
953 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
7ef5e54e
AL
954
955 if (tqs->stqs_taskq == NULL) {
956 ASSERT3U(tqs->stqs_count, ==, 0);
957 return;
958 }
959
1c27024e 960 for (uint_t i = 0; i < tqs->stqs_count; i++) {
7ef5e54e
AL
961 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
962 taskq_destroy(tqs->stqs_taskq[i]);
428870ff 963 }
34dc7c2f 964
7ef5e54e
AL
965 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
966 tqs->stqs_taskq = NULL;
967}
34dc7c2f 968
7ef5e54e
AL
969/*
970 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
971 * Note that a type may have multiple discrete taskqs to avoid lock contention
972 * on the taskq itself. In that case we choose which taskq at random by using
973 * the low bits of gethrtime().
974 */
975void
976spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
977 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
978{
979 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
980 taskq_t *tq;
981
982 ASSERT3P(tqs->stqs_taskq, !=, NULL);
983 ASSERT3U(tqs->stqs_count, !=, 0);
984
985 if (tqs->stqs_count == 1) {
986 tq = tqs->stqs_taskq[0];
987 } else {
c12936b1 988 tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count];
428870ff 989 }
7ef5e54e
AL
990
991 taskq_dispatch_ent(tq, func, arg, flags, ent);
428870ff
BB
992}
993
044baf00
BB
994/*
995 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
996 */
997void
998spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
999 task_func_t *func, void *arg, uint_t flags)
1000{
1001 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1002 taskq_t *tq;
1003 taskqid_t id;
1004
1005 ASSERT3P(tqs->stqs_taskq, !=, NULL);
1006 ASSERT3U(tqs->stqs_count, !=, 0);
1007
1008 if (tqs->stqs_count == 1) {
1009 tq = tqs->stqs_taskq[0];
1010 } else {
c12936b1 1011 tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count];
044baf00
BB
1012 }
1013
1014 id = taskq_dispatch(tq, func, arg, flags);
1015 if (id)
1016 taskq_wait_id(tq, id);
1017}
1018
428870ff
BB
1019static void
1020spa_create_zio_taskqs(spa_t *spa)
1021{
1c27024e
DB
1022 for (int t = 0; t < ZIO_TYPES; t++) {
1023 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
7ef5e54e 1024 spa_taskqs_init(spa, t, q);
428870ff
BB
1025 }
1026 }
1027}
9babb374 1028
c25b8f99
BB
1029/*
1030 * Disabled until spa_thread() can be adapted for Linux.
1031 */
1032#undef HAVE_SPA_THREAD
1033
7b89a549 1034#if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
428870ff
BB
1035static void
1036spa_thread(void *arg)
1037{
1038 callb_cpr_t cprinfo;
9babb374 1039
428870ff
BB
1040 spa_t *spa = arg;
1041 user_t *pu = PTOU(curproc);
9babb374 1042
428870ff
BB
1043 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1044 spa->spa_name);
9babb374 1045
428870ff
BB
1046 ASSERT(curproc != &p0);
1047 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1048 "zpool-%s", spa->spa_name);
1049 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1050
1051 /* bind this thread to the requested psrset */
1052 if (zio_taskq_psrset_bind != PS_NONE) {
1053 pool_lock();
1054 mutex_enter(&cpu_lock);
1055 mutex_enter(&pidlock);
1056 mutex_enter(&curproc->p_lock);
1057
1058 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1059 0, NULL, NULL) == 0) {
1060 curthread->t_bind_pset = zio_taskq_psrset_bind;
1061 } else {
1062 cmn_err(CE_WARN,
1063 "Couldn't bind process for zfs pool \"%s\" to "
1064 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1065 }
1066
1067 mutex_exit(&curproc->p_lock);
1068 mutex_exit(&pidlock);
1069 mutex_exit(&cpu_lock);
1070 pool_unlock();
1071 }
1072
1073 if (zio_taskq_sysdc) {
1074 sysdc_thread_enter(curthread, 100, 0);
1075 }
1076
1077 spa->spa_proc = curproc;
1078 spa->spa_did = curthread->t_did;
1079
1080 spa_create_zio_taskqs(spa);
1081
1082 mutex_enter(&spa->spa_proc_lock);
1083 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1084
1085 spa->spa_proc_state = SPA_PROC_ACTIVE;
1086 cv_broadcast(&spa->spa_proc_cv);
1087
1088 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1089 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1090 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1091 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1092
1093 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1094 spa->spa_proc_state = SPA_PROC_GONE;
1095 spa->spa_proc = &p0;
1096 cv_broadcast(&spa->spa_proc_cv);
1097 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1098
1099 mutex_enter(&curproc->p_lock);
1100 lwp_exit();
1101}
1102#endif
1103
1104/*
1105 * Activate an uninitialized pool.
1106 */
1107static void
1108spa_activate(spa_t *spa, int mode)
1109{
1110 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1111
1112 spa->spa_state = POOL_STATE_ACTIVE;
1113 spa->spa_mode = mode;
1114
1115 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1116 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1117
1118 /* Try to create a covering process */
1119 mutex_enter(&spa->spa_proc_lock);
1120 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1121 ASSERT(spa->spa_proc == &p0);
1122 spa->spa_did = 0;
1123
7b89a549 1124#ifdef HAVE_SPA_THREAD
428870ff
BB
1125 /* Only create a process if we're going to be around a while. */
1126 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1127 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1128 NULL, 0) == 0) {
1129 spa->spa_proc_state = SPA_PROC_CREATED;
1130 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1131 cv_wait(&spa->spa_proc_cv,
1132 &spa->spa_proc_lock);
9babb374 1133 }
428870ff
BB
1134 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1135 ASSERT(spa->spa_proc != &p0);
1136 ASSERT(spa->spa_did != 0);
1137 } else {
1138#ifdef _KERNEL
1139 cmn_err(CE_WARN,
1140 "Couldn't create process for zfs pool \"%s\"\n",
1141 spa->spa_name);
1142#endif
b128c09f 1143 }
34dc7c2f 1144 }
7b89a549 1145#endif /* HAVE_SPA_THREAD */
428870ff
BB
1146 mutex_exit(&spa->spa_proc_lock);
1147
1148 /* If we didn't create a process, we need to create our taskqs. */
1149 if (spa->spa_proc == &p0) {
1150 spa_create_zio_taskqs(spa);
1151 }
34dc7c2f 1152
a1d477c2
MA
1153 for (size_t i = 0; i < TXG_SIZE; i++)
1154 spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, 0);
1155
b128c09f
BB
1156 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1157 offsetof(vdev_t, vdev_config_dirty_node));
0c66c32d
JG
1158 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1159 offsetof(objset_t, os_evicting_node));
b128c09f
BB
1160 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1161 offsetof(vdev_t, vdev_state_dirty_node));
34dc7c2f 1162
4747a7d3 1163 txg_list_create(&spa->spa_vdev_txg_list, spa,
34dc7c2f
BB
1164 offsetof(struct vdev, vdev_txg_node));
1165
1166 avl_create(&spa->spa_errlist_scrub,
1167 spa_error_entry_compare, sizeof (spa_error_entry_t),
1168 offsetof(spa_error_entry_t, se_avl));
1169 avl_create(&spa->spa_errlist_last,
1170 spa_error_entry_compare, sizeof (spa_error_entry_t),
1171 offsetof(spa_error_entry_t, se_avl));
a0bd735a 1172
b5256303
TC
1173 spa_keystore_init(&spa->spa_keystore);
1174
a0bd735a
BP
1175 /*
1176 * This taskq is used to perform zvol-minor-related tasks
1177 * asynchronously. This has several advantages, including easy
1178 * resolution of various deadlocks (zfsonlinux bug #3681).
1179 *
1180 * The taskq must be single threaded to ensure tasks are always
1181 * processed in the order in which they were dispatched.
1182 *
1183 * A taskq per pool allows one to keep the pools independent.
1184 * This way if one pool is suspended, it will not impact another.
1185 *
1186 * The preferred location to dispatch a zvol minor task is a sync
1187 * task. In this context, there is easy access to the spa_t and minimal
1188 * error handling is required because the sync task must succeed.
1189 */
1190 spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri,
1191 1, INT_MAX, 0);
1de321e6 1192
77d8a0f1 1193 /*
1194 * Taskq dedicated to prefetcher threads: this is used to prevent the
1195 * pool traverse code from monopolizing the global (and limited)
1196 * system_taskq by inappropriately scheduling long running tasks on it.
1197 */
1198 spa->spa_prefetch_taskq = taskq_create("z_prefetch", boot_ncpus,
1199 defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC);
1200
1de321e6
JX
1201 /*
1202 * The taskq to upgrade datasets in this pool. Currently used by
9c5167d1 1203 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1de321e6
JX
1204 */
1205 spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus,
1206 defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC);
34dc7c2f
BB
1207}
1208
1209/*
1210 * Opposite of spa_activate().
1211 */
1212static void
1213spa_deactivate(spa_t *spa)
1214{
34dc7c2f
BB
1215 ASSERT(spa->spa_sync_on == B_FALSE);
1216 ASSERT(spa->spa_dsl_pool == NULL);
1217 ASSERT(spa->spa_root_vdev == NULL);
9babb374 1218 ASSERT(spa->spa_async_zio_root == NULL);
34dc7c2f
BB
1219 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1220
0c66c32d
JG
1221 spa_evicting_os_wait(spa);
1222
a0bd735a
BP
1223 if (spa->spa_zvol_taskq) {
1224 taskq_destroy(spa->spa_zvol_taskq);
1225 spa->spa_zvol_taskq = NULL;
1226 }
1227
77d8a0f1 1228 if (spa->spa_prefetch_taskq) {
1229 taskq_destroy(spa->spa_prefetch_taskq);
1230 spa->spa_prefetch_taskq = NULL;
1231 }
1232
1de321e6
JX
1233 if (spa->spa_upgrade_taskq) {
1234 taskq_destroy(spa->spa_upgrade_taskq);
1235 spa->spa_upgrade_taskq = NULL;
1236 }
1237
34dc7c2f
BB
1238 txg_list_destroy(&spa->spa_vdev_txg_list);
1239
b128c09f 1240 list_destroy(&spa->spa_config_dirty_list);
0c66c32d 1241 list_destroy(&spa->spa_evicting_os_list);
b128c09f 1242 list_destroy(&spa->spa_state_dirty_list);
34dc7c2f 1243
57ddcda1 1244 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
cc92e9d0 1245
1c27024e
DB
1246 for (int t = 0; t < ZIO_TYPES; t++) {
1247 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
7ef5e54e 1248 spa_taskqs_fini(spa, t, q);
b128c09f 1249 }
34dc7c2f
BB
1250 }
1251
a1d477c2
MA
1252 for (size_t i = 0; i < TXG_SIZE; i++) {
1253 ASSERT3P(spa->spa_txg_zio[i], !=, NULL);
1254 VERIFY0(zio_wait(spa->spa_txg_zio[i]));
1255 spa->spa_txg_zio[i] = NULL;
1256 }
1257
34dc7c2f
BB
1258 metaslab_class_destroy(spa->spa_normal_class);
1259 spa->spa_normal_class = NULL;
1260
1261 metaslab_class_destroy(spa->spa_log_class);
1262 spa->spa_log_class = NULL;
1263
1264 /*
1265 * If this was part of an import or the open otherwise failed, we may
1266 * still have errors left in the queues. Empty them just in case.
1267 */
1268 spa_errlog_drain(spa);
34dc7c2f
BB
1269 avl_destroy(&spa->spa_errlist_scrub);
1270 avl_destroy(&spa->spa_errlist_last);
1271
b5256303
TC
1272 spa_keystore_fini(&spa->spa_keystore);
1273
34dc7c2f 1274 spa->spa_state = POOL_STATE_UNINITIALIZED;
428870ff
BB
1275
1276 mutex_enter(&spa->spa_proc_lock);
1277 if (spa->spa_proc_state != SPA_PROC_NONE) {
1278 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1279 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1280 cv_broadcast(&spa->spa_proc_cv);
1281 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1282 ASSERT(spa->spa_proc != &p0);
1283 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1284 }
1285 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1286 spa->spa_proc_state = SPA_PROC_NONE;
1287 }
1288 ASSERT(spa->spa_proc == &p0);
1289 mutex_exit(&spa->spa_proc_lock);
1290
1291 /*
1292 * We want to make sure spa_thread() has actually exited the ZFS
1293 * module, so that the module can't be unloaded out from underneath
1294 * it.
1295 */
1296 if (spa->spa_did != 0) {
1297 thread_join(spa->spa_did);
1298 spa->spa_did = 0;
1299 }
34dc7c2f
BB
1300}
1301
1302/*
1303 * Verify a pool configuration, and construct the vdev tree appropriately. This
1304 * will create all the necessary vdevs in the appropriate layout, with each vdev
1305 * in the CLOSED state. This will prep the pool before open/creation/import.
1306 * All vdev validation is done by the vdev_alloc() routine.
1307 */
1308static int
1309spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1310 uint_t id, int atype)
1311{
1312 nvlist_t **child;
9babb374 1313 uint_t children;
34dc7c2f
BB
1314 int error;
1315
1316 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1317 return (error);
1318
1319 if ((*vdp)->vdev_ops->vdev_op_leaf)
1320 return (0);
1321
b128c09f
BB
1322 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1323 &child, &children);
1324
1325 if (error == ENOENT)
1326 return (0);
1327
1328 if (error) {
34dc7c2f
BB
1329 vdev_free(*vdp);
1330 *vdp = NULL;
2e528b49 1331 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1332 }
1333
1c27024e 1334 for (int c = 0; c < children; c++) {
34dc7c2f
BB
1335 vdev_t *vd;
1336 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1337 atype)) != 0) {
1338 vdev_free(*vdp);
1339 *vdp = NULL;
1340 return (error);
1341 }
1342 }
1343
1344 ASSERT(*vdp != NULL);
1345
1346 return (0);
1347}
1348
1349/*
1350 * Opposite of spa_load().
1351 */
1352static void
1353spa_unload(spa_t *spa)
1354{
1c27024e 1355 int i;
34dc7c2f 1356
b128c09f
BB
1357 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1358
34dc7c2f
BB
1359 /*
1360 * Stop async tasks.
1361 */
1362 spa_async_suspend(spa);
1363
1364 /*
1365 * Stop syncing.
1366 */
1367 if (spa->spa_sync_on) {
1368 txg_sync_stop(spa->spa_dsl_pool);
1369 spa->spa_sync_on = B_FALSE;
1370 }
1371
4e21fd06
DB
1372 /*
1373 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1374 * to call it earlier, before we wait for async i/o to complete.
1375 * This ensures that there is no async metaslab prefetching, by
1376 * calling taskq_wait(mg_taskq).
1377 */
1378 if (spa->spa_root_vdev != NULL) {
1379 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1c27024e 1380 for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++)
4e21fd06
DB
1381 vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]);
1382 spa_config_exit(spa, SCL_ALL, FTAG);
1383 }
1384
379ca9cf
OF
1385 if (spa->spa_mmp.mmp_thread)
1386 mmp_thread_stop(spa);
1387
34dc7c2f 1388 /*
b128c09f 1389 * Wait for any outstanding async I/O to complete.
34dc7c2f 1390 */
9babb374 1391 if (spa->spa_async_zio_root != NULL) {
1c27024e 1392 for (int i = 0; i < max_ncpus; i++)
e022864d
MA
1393 (void) zio_wait(spa->spa_async_zio_root[i]);
1394 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
9babb374
BB
1395 spa->spa_async_zio_root = NULL;
1396 }
34dc7c2f 1397
a1d477c2
MA
1398 if (spa->spa_vdev_removal != NULL) {
1399 spa_vdev_removal_destroy(spa->spa_vdev_removal);
1400 spa->spa_vdev_removal = NULL;
1401 }
1402
9d5b5245
SD
1403 if (spa->spa_condense_zthr != NULL) {
1404 ASSERT(!zthr_isrunning(spa->spa_condense_zthr));
1405 zthr_destroy(spa->spa_condense_zthr);
1406 spa->spa_condense_zthr = NULL;
1407 }
1408
a1d477c2
MA
1409 spa_condense_fini(spa);
1410
428870ff
BB
1411 bpobj_close(&spa->spa_deferred_bpobj);
1412
93cf2076
GW
1413 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1414
1415 /*
1416 * Close all vdevs.
1417 */
1418 if (spa->spa_root_vdev)
1419 vdev_free(spa->spa_root_vdev);
1420 ASSERT(spa->spa_root_vdev == NULL);
1421
34dc7c2f
BB
1422 /*
1423 * Close the dsl pool.
1424 */
1425 if (spa->spa_dsl_pool) {
1426 dsl_pool_close(spa->spa_dsl_pool);
1427 spa->spa_dsl_pool = NULL;
428870ff 1428 spa->spa_meta_objset = NULL;
34dc7c2f
BB
1429 }
1430
428870ff
BB
1431 ddt_unload(spa);
1432
fb5f0bc8
BB
1433 /*
1434 * Drop and purge level 2 cache
1435 */
1436 spa_l2cache_drop(spa);
1437
34dc7c2f
BB
1438 for (i = 0; i < spa->spa_spares.sav_count; i++)
1439 vdev_free(spa->spa_spares.sav_vdevs[i]);
1440 if (spa->spa_spares.sav_vdevs) {
1441 kmem_free(spa->spa_spares.sav_vdevs,
1442 spa->spa_spares.sav_count * sizeof (void *));
1443 spa->spa_spares.sav_vdevs = NULL;
1444 }
1445 if (spa->spa_spares.sav_config) {
1446 nvlist_free(spa->spa_spares.sav_config);
1447 spa->spa_spares.sav_config = NULL;
1448 }
b128c09f 1449 spa->spa_spares.sav_count = 0;
34dc7c2f 1450
5ffb9d1d
GW
1451 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1452 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
34dc7c2f 1453 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
5ffb9d1d 1454 }
34dc7c2f
BB
1455 if (spa->spa_l2cache.sav_vdevs) {
1456 kmem_free(spa->spa_l2cache.sav_vdevs,
1457 spa->spa_l2cache.sav_count * sizeof (void *));
1458 spa->spa_l2cache.sav_vdevs = NULL;
1459 }
1460 if (spa->spa_l2cache.sav_config) {
1461 nvlist_free(spa->spa_l2cache.sav_config);
1462 spa->spa_l2cache.sav_config = NULL;
1463 }
b128c09f 1464 spa->spa_l2cache.sav_count = 0;
34dc7c2f
BB
1465
1466 spa->spa_async_suspended = 0;
fb5f0bc8 1467
a1d477c2
MA
1468 spa->spa_indirect_vdevs_loaded = B_FALSE;
1469
d96eb2b1
DM
1470 if (spa->spa_comment != NULL) {
1471 spa_strfree(spa->spa_comment);
1472 spa->spa_comment = NULL;
1473 }
1474
fb5f0bc8 1475 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1476}
1477
1478/*
1479 * Load (or re-load) the current list of vdevs describing the active spares for
1480 * this pool. When this is called, we have some form of basic information in
1481 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1482 * then re-generate a more complete list including status information.
1483 */
a1d477c2 1484void
34dc7c2f
BB
1485spa_load_spares(spa_t *spa)
1486{
1487 nvlist_t **spares;
1488 uint_t nspares;
1489 int i;
1490 vdev_t *vd, *tvd;
1491
b128c09f
BB
1492 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1493
34dc7c2f
BB
1494 /*
1495 * First, close and free any existing spare vdevs.
1496 */
1497 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1498 vd = spa->spa_spares.sav_vdevs[i];
1499
1500 /* Undo the call to spa_activate() below */
b128c09f
BB
1501 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1502 B_FALSE)) != NULL && tvd->vdev_isspare)
34dc7c2f
BB
1503 spa_spare_remove(tvd);
1504 vdev_close(vd);
1505 vdev_free(vd);
1506 }
1507
1508 if (spa->spa_spares.sav_vdevs)
1509 kmem_free(spa->spa_spares.sav_vdevs,
1510 spa->spa_spares.sav_count * sizeof (void *));
1511
1512 if (spa->spa_spares.sav_config == NULL)
1513 nspares = 0;
1514 else
1515 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1516 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1517
1518 spa->spa_spares.sav_count = (int)nspares;
1519 spa->spa_spares.sav_vdevs = NULL;
1520
1521 if (nspares == 0)
1522 return;
1523
1524 /*
1525 * Construct the array of vdevs, opening them to get status in the
1526 * process. For each spare, there is potentially two different vdev_t
1527 * structures associated with it: one in the list of spares (used only
1528 * for basic validation purposes) and one in the active vdev
1529 * configuration (if it's spared in). During this phase we open and
1530 * validate each vdev on the spare list. If the vdev also exists in the
1531 * active configuration, then we also mark this vdev as an active spare.
1532 */
904ea276 1533 spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *),
79c76d5b 1534 KM_SLEEP);
34dc7c2f
BB
1535 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1536 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1537 VDEV_ALLOC_SPARE) == 0);
1538 ASSERT(vd != NULL);
1539
1540 spa->spa_spares.sav_vdevs[i] = vd;
1541
b128c09f
BB
1542 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1543 B_FALSE)) != NULL) {
34dc7c2f
BB
1544 if (!tvd->vdev_isspare)
1545 spa_spare_add(tvd);
1546
1547 /*
1548 * We only mark the spare active if we were successfully
1549 * able to load the vdev. Otherwise, importing a pool
1550 * with a bad active spare would result in strange
1551 * behavior, because multiple pool would think the spare
1552 * is actively in use.
1553 *
1554 * There is a vulnerability here to an equally bizarre
1555 * circumstance, where a dead active spare is later
1556 * brought back to life (onlined or otherwise). Given
1557 * the rarity of this scenario, and the extra complexity
1558 * it adds, we ignore the possibility.
1559 */
1560 if (!vdev_is_dead(tvd))
1561 spa_spare_activate(tvd);
1562 }
1563
b128c09f 1564 vd->vdev_top = vd;
9babb374 1565 vd->vdev_aux = &spa->spa_spares;
b128c09f 1566
34dc7c2f
BB
1567 if (vdev_open(vd) != 0)
1568 continue;
1569
34dc7c2f
BB
1570 if (vdev_validate_aux(vd) == 0)
1571 spa_spare_add(vd);
1572 }
1573
1574 /*
1575 * Recompute the stashed list of spares, with status information
1576 * this time.
1577 */
1578 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1579 DATA_TYPE_NVLIST_ARRAY) == 0);
1580
1581 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
79c76d5b 1582 KM_SLEEP);
34dc7c2f
BB
1583 for (i = 0; i < spa->spa_spares.sav_count; i++)
1584 spares[i] = vdev_config_generate(spa,
428870ff 1585 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
34dc7c2f
BB
1586 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1587 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1588 for (i = 0; i < spa->spa_spares.sav_count; i++)
1589 nvlist_free(spares[i]);
1590 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1591}
1592
1593/*
1594 * Load (or re-load) the current list of vdevs describing the active l2cache for
1595 * this pool. When this is called, we have some form of basic information in
1596 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1597 * then re-generate a more complete list including status information.
1598 * Devices which are already active have their details maintained, and are
1599 * not re-opened.
1600 */
a1d477c2 1601void
34dc7c2f
BB
1602spa_load_l2cache(spa_t *spa)
1603{
460f239e 1604 nvlist_t **l2cache = NULL;
34dc7c2f
BB
1605 uint_t nl2cache;
1606 int i, j, oldnvdevs;
9babb374 1607 uint64_t guid;
a117a6d6 1608 vdev_t *vd, **oldvdevs, **newvdevs;
34dc7c2f
BB
1609 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1610
b128c09f
BB
1611 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1612
34dc7c2f
BB
1613 oldvdevs = sav->sav_vdevs;
1614 oldnvdevs = sav->sav_count;
1615 sav->sav_vdevs = NULL;
1616 sav->sav_count = 0;
1617
67d60824
NB
1618 if (sav->sav_config == NULL) {
1619 nl2cache = 0;
1620 newvdevs = NULL;
1621 goto out;
1622 }
1623
1624 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1625 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1626 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1627
34dc7c2f
BB
1628 /*
1629 * Process new nvlist of vdevs.
1630 */
1631 for (i = 0; i < nl2cache; i++) {
1632 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1633 &guid) == 0);
1634
1635 newvdevs[i] = NULL;
1636 for (j = 0; j < oldnvdevs; j++) {
1637 vd = oldvdevs[j];
1638 if (vd != NULL && guid == vd->vdev_guid) {
1639 /*
1640 * Retain previous vdev for add/remove ops.
1641 */
1642 newvdevs[i] = vd;
1643 oldvdevs[j] = NULL;
1644 break;
1645 }
1646 }
1647
1648 if (newvdevs[i] == NULL) {
1649 /*
1650 * Create new vdev
1651 */
1652 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1653 VDEV_ALLOC_L2CACHE) == 0);
1654 ASSERT(vd != NULL);
1655 newvdevs[i] = vd;
1656
1657 /*
1658 * Commit this vdev as an l2cache device,
1659 * even if it fails to open.
1660 */
1661 spa_l2cache_add(vd);
1662
b128c09f
BB
1663 vd->vdev_top = vd;
1664 vd->vdev_aux = sav;
1665
1666 spa_l2cache_activate(vd);
1667
34dc7c2f
BB
1668 if (vdev_open(vd) != 0)
1669 continue;
1670
34dc7c2f
BB
1671 (void) vdev_validate_aux(vd);
1672
9babb374
BB
1673 if (!vdev_is_dead(vd))
1674 l2arc_add_vdev(spa, vd);
34dc7c2f
BB
1675 }
1676 }
1677
67d60824
NB
1678 sav->sav_vdevs = newvdevs;
1679 sav->sav_count = (int)nl2cache;
1680
1681 /*
1682 * Recompute the stashed list of l2cache devices, with status
1683 * information this time.
1684 */
1685 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1686 DATA_TYPE_NVLIST_ARRAY) == 0);
1687
460f239e
D
1688 if (sav->sav_count > 0)
1689 l2cache = kmem_alloc(sav->sav_count * sizeof (void *),
1690 KM_SLEEP);
67d60824
NB
1691 for (i = 0; i < sav->sav_count; i++)
1692 l2cache[i] = vdev_config_generate(spa,
1693 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1694 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1695 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1696
1697out:
34dc7c2f
BB
1698 /*
1699 * Purge vdevs that were dropped
1700 */
1701 for (i = 0; i < oldnvdevs; i++) {
1702 uint64_t pool;
1703
1704 vd = oldvdevs[i];
1705 if (vd != NULL) {
5ffb9d1d
GW
1706 ASSERT(vd->vdev_isl2cache);
1707
fb5f0bc8
BB
1708 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1709 pool != 0ULL && l2arc_vdev_present(vd))
34dc7c2f 1710 l2arc_remove_vdev(vd);
5ffb9d1d
GW
1711 vdev_clear_stats(vd);
1712 vdev_free(vd);
34dc7c2f
BB
1713 }
1714 }
1715
1716 if (oldvdevs)
1717 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1718
34dc7c2f
BB
1719 for (i = 0; i < sav->sav_count; i++)
1720 nvlist_free(l2cache[i]);
1721 if (sav->sav_count)
1722 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1723}
1724
1725static int
1726load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1727{
1728 dmu_buf_t *db;
1729 char *packed = NULL;
1730 size_t nvsize = 0;
1731 int error;
1732 *value = NULL;
1733
c3275b56
BB
1734 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1735 if (error)
1736 return (error);
1737
34dc7c2f
BB
1738 nvsize = *(uint64_t *)db->db_data;
1739 dmu_buf_rele(db, FTAG);
1740
77aef6f6 1741 packed = vmem_alloc(nvsize, KM_SLEEP);
9babb374
BB
1742 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1743 DMU_READ_PREFETCH);
34dc7c2f
BB
1744 if (error == 0)
1745 error = nvlist_unpack(packed, nvsize, value, 0);
77aef6f6 1746 vmem_free(packed, nvsize);
34dc7c2f
BB
1747
1748 return (error);
1749}
1750
1751/*
1752 * Checks to see if the given vdev could not be opened, in which case we post a
1753 * sysevent to notify the autoreplace code that the device has been removed.
1754 */
1755static void
1756spa_check_removed(vdev_t *vd)
1757{
1c27024e 1758 for (int c = 0; c < vd->vdev_children; c++)
34dc7c2f
BB
1759 spa_check_removed(vd->vdev_child[c]);
1760
7011fb60 1761 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
a1d477c2 1762 vdev_is_concrete(vd)) {
fb390aaf 1763 zfs_post_autoreplace(vd->vdev_spa, vd);
12fa0466 1764 spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK);
34dc7c2f
BB
1765 }
1766}
1767
e0ab3ab5
JS
1768static void
1769spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd)
1770{
e0ab3ab5
JS
1771 ASSERT3U(vd->vdev_children, ==, mvd->vdev_children);
1772
1773 vd->vdev_top_zap = mvd->vdev_top_zap;
1774 vd->vdev_leaf_zap = mvd->vdev_leaf_zap;
1775
1c27024e 1776 for (uint64_t i = 0; i < vd->vdev_children; i++) {
e0ab3ab5
JS
1777 spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]);
1778 }
1779}
1780
9babb374 1781/*
572e2857 1782 * Validate the current config against the MOS config
9babb374 1783 */
572e2857
BB
1784static boolean_t
1785spa_config_valid(spa_t *spa, nvlist_t *config)
9babb374 1786{
572e2857
BB
1787 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1788 nvlist_t *nv;
1789
1790 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1791
1792 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1793 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1794
1795 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
9babb374 1796
428870ff 1797 /*
572e2857
BB
1798 * If we're doing a normal import, then build up any additional
1799 * diagnostic information about missing devices in this config.
1800 * We'll pass this up to the user for further processing.
428870ff 1801 */
572e2857
BB
1802 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1803 nvlist_t **child, *nv;
1804 uint64_t idx = 0;
1805
160987b5 1806 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *),
79c76d5b
BB
1807 KM_SLEEP);
1808 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
572e2857 1809
1c27024e 1810 for (int c = 0; c < rvd->vdev_children; c++) {
572e2857
BB
1811 vdev_t *tvd = rvd->vdev_child[c];
1812 vdev_t *mtvd = mrvd->vdev_child[c];
1813
1814 if (tvd->vdev_ops == &vdev_missing_ops &&
1815 mtvd->vdev_ops != &vdev_missing_ops &&
1816 mtvd->vdev_islog)
1817 child[idx++] = vdev_config_generate(spa, mtvd,
1818 B_FALSE, 0);
1819 }
9babb374 1820
572e2857
BB
1821 if (idx) {
1822 VERIFY(nvlist_add_nvlist_array(nv,
1823 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1824 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1825 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1826
1c27024e 1827 for (int i = 0; i < idx; i++)
572e2857
BB
1828 nvlist_free(child[i]);
1829 }
1830 nvlist_free(nv);
1831 kmem_free(child, rvd->vdev_children * sizeof (char **));
1832 }
1833
1834 /*
1835 * Compare the root vdev tree with the information we have
1836 * from the MOS config (mrvd). Check each top-level vdev
1837 * with the corresponding MOS config top-level (mtvd).
1838 */
1c27024e 1839 for (int c = 0; c < rvd->vdev_children; c++) {
572e2857
BB
1840 vdev_t *tvd = rvd->vdev_child[c];
1841 vdev_t *mtvd = mrvd->vdev_child[c];
1842
1843 /*
1844 * Resolve any "missing" vdevs in the current configuration.
a1d477c2 1845 * Also trust the MOS config about any "indirect" vdevs.
572e2857
BB
1846 * If we find that the MOS config has more accurate information
1847 * about the top-level vdev then use that vdev instead.
1848 */
a1d477c2
MA
1849 if ((tvd->vdev_ops == &vdev_missing_ops &&
1850 mtvd->vdev_ops != &vdev_missing_ops) ||
1851 (mtvd->vdev_ops == &vdev_indirect_ops &&
1852 tvd->vdev_ops != &vdev_indirect_ops)) {
572e2857
BB
1853
1854 /*
1855 * Device specific actions.
1856 */
1857 if (mtvd->vdev_islog) {
a1d477c2
MA
1858 if (!(spa->spa_import_flags &
1859 ZFS_IMPORT_MISSING_LOG)) {
1860 continue;
1861 }
1862
572e2857 1863 spa_set_log_state(spa, SPA_LOG_CLEAR);
a1d477c2 1864 } else if (mtvd->vdev_ops != &vdev_indirect_ops) {
572e2857
BB
1865 continue;
1866 }
1867
1868 /*
1869 * Swap the missing vdev with the data we were
1870 * able to obtain from the MOS config.
1871 */
1872 vdev_remove_child(rvd, tvd);
1873 vdev_remove_child(mrvd, mtvd);
1874
1875 vdev_add_child(rvd, mtvd);
1876 vdev_add_child(mrvd, tvd);
1877
572e2857 1878 vdev_reopen(rvd);
e0ab3ab5
JS
1879 } else {
1880 if (mtvd->vdev_islog) {
1881 /*
1882 * Load the slog device's state from the MOS
1883 * config since it's possible that the label
1884 * does not contain the most up-to-date
1885 * information.
1886 */
1887 vdev_load_log_state(tvd, mtvd);
1888 vdev_reopen(tvd);
1889 }
1890
572e2857 1891 /*
e0ab3ab5 1892 * Per-vdev ZAP info is stored exclusively in the MOS.
572e2857 1893 */
e0ab3ab5 1894 spa_config_valid_zaps(tvd, mtvd);
572e2857 1895 }
a1d477c2
MA
1896
1897 /*
1898 * Never trust this info from userland; always use what's
1899 * in the MOS. This prevents it from getting out of sync
1900 * with the rest of the info in the MOS.
1901 */
1902 tvd->vdev_removing = mtvd->vdev_removing;
1903 tvd->vdev_indirect_config = mtvd->vdev_indirect_config;
9babb374 1904 }
e0ab3ab5 1905
572e2857 1906 vdev_free(mrvd);
428870ff 1907 spa_config_exit(spa, SCL_ALL, FTAG);
572e2857
BB
1908
1909 /*
1910 * Ensure we were able to validate the config.
1911 */
1912 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
9babb374
BB
1913}
1914
b128c09f
BB
1915/*
1916 * Check for missing log devices
1917 */
13fe0198 1918static boolean_t
b128c09f
BB
1919spa_check_logs(spa_t *spa)
1920{
13fe0198 1921 boolean_t rv = B_FALSE;
9c43027b 1922 dsl_pool_t *dp = spa_get_dsl(spa);
13fe0198 1923
b128c09f 1924 switch (spa->spa_log_state) {
e75c13c3
BB
1925 default:
1926 break;
b128c09f
BB
1927 case SPA_LOG_MISSING:
1928 /* need to recheck in case slog has been restored */
1929 case SPA_LOG_UNKNOWN:
9c43027b
AJ
1930 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1931 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
13fe0198 1932 if (rv)
428870ff 1933 spa_set_log_state(spa, SPA_LOG_MISSING);
b128c09f 1934 break;
b128c09f 1935 }
13fe0198 1936 return (rv);
b128c09f
BB
1937}
1938
428870ff
BB
1939static boolean_t
1940spa_passivate_log(spa_t *spa)
34dc7c2f 1941{
428870ff
BB
1942 vdev_t *rvd = spa->spa_root_vdev;
1943 boolean_t slog_found = B_FALSE;
b128c09f 1944
428870ff 1945 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
fb5f0bc8 1946
428870ff
BB
1947 if (!spa_has_slogs(spa))
1948 return (B_FALSE);
34dc7c2f 1949
1c27024e 1950 for (int c = 0; c < rvd->vdev_children; c++) {
428870ff
BB
1951 vdev_t *tvd = rvd->vdev_child[c];
1952 metaslab_group_t *mg = tvd->vdev_mg;
34dc7c2f 1953
428870ff
BB
1954 if (tvd->vdev_islog) {
1955 metaslab_group_passivate(mg);
1956 slog_found = B_TRUE;
1957 }
34dc7c2f
BB
1958 }
1959
428870ff
BB
1960 return (slog_found);
1961}
34dc7c2f 1962
428870ff
BB
1963static void
1964spa_activate_log(spa_t *spa)
1965{
1966 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 1967
428870ff
BB
1968 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1969
1c27024e 1970 for (int c = 0; c < rvd->vdev_children; c++) {
428870ff
BB
1971 vdev_t *tvd = rvd->vdev_child[c];
1972 metaslab_group_t *mg = tvd->vdev_mg;
1973
1974 if (tvd->vdev_islog)
1975 metaslab_group_activate(mg);
34dc7c2f 1976 }
428870ff 1977}
34dc7c2f 1978
428870ff 1979int
a1d477c2 1980spa_reset_logs(spa_t *spa)
428870ff 1981{
13fe0198 1982 int error;
9babb374 1983
a1d477c2 1984 error = dmu_objset_find(spa_name(spa), zil_reset,
13fe0198
MA
1985 NULL, DS_FIND_CHILDREN);
1986 if (error == 0) {
428870ff
BB
1987 /*
1988 * We successfully offlined the log device, sync out the
1989 * current txg so that the "stubby" block can be removed
1990 * by zil_sync().
1991 */
1992 txg_wait_synced(spa->spa_dsl_pool, 0);
1993 }
1994 return (error);
1995}
34dc7c2f 1996
428870ff
BB
1997static void
1998spa_aux_check_removed(spa_aux_vdev_t *sav)
1999{
1c27024e 2000 for (int i = 0; i < sav->sav_count; i++)
428870ff
BB
2001 spa_check_removed(sav->sav_vdevs[i]);
2002}
34dc7c2f 2003
428870ff
BB
2004void
2005spa_claim_notify(zio_t *zio)
2006{
2007 spa_t *spa = zio->io_spa;
34dc7c2f 2008
428870ff
BB
2009 if (zio->io_error)
2010 return;
34dc7c2f 2011
428870ff
BB
2012 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
2013 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
2014 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
2015 mutex_exit(&spa->spa_props_lock);
2016}
34dc7c2f 2017
428870ff
BB
2018typedef struct spa_load_error {
2019 uint64_t sle_meta_count;
2020 uint64_t sle_data_count;
2021} spa_load_error_t;
34dc7c2f 2022
428870ff
BB
2023static void
2024spa_load_verify_done(zio_t *zio)
2025{
2026 blkptr_t *bp = zio->io_bp;
2027 spa_load_error_t *sle = zio->io_private;
2028 dmu_object_type_t type = BP_GET_TYPE(bp);
2029 int error = zio->io_error;
dea377c0 2030 spa_t *spa = zio->io_spa;
34dc7c2f 2031
a6255b7f 2032 abd_free(zio->io_abd);
428870ff 2033 if (error) {
9ae529ec 2034 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
428870ff 2035 type != DMU_OT_INTENT_LOG)
bc89ac84 2036 atomic_inc_64(&sle->sle_meta_count);
428870ff 2037 else
bc89ac84 2038 atomic_inc_64(&sle->sle_data_count);
34dc7c2f 2039 }
dea377c0
MA
2040
2041 mutex_enter(&spa->spa_scrub_lock);
d4a72f23 2042 spa->spa_load_verify_ios--;
dea377c0
MA
2043 cv_broadcast(&spa->spa_scrub_io_cv);
2044 mutex_exit(&spa->spa_scrub_lock);
428870ff 2045}
34dc7c2f 2046
dea377c0
MA
2047/*
2048 * Maximum number of concurrent scrub i/os to create while verifying
2049 * a pool while importing it.
2050 */
2051int spa_load_verify_maxinflight = 10000;
2052int spa_load_verify_metadata = B_TRUE;
2053int spa_load_verify_data = B_TRUE;
2054
428870ff
BB
2055/*ARGSUSED*/
2056static int
2057spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 2058 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
428870ff 2059{
fcff0f35 2060 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
dea377c0
MA
2061 return (0);
2062 /*
2063 * Note: normally this routine will not be called if
2064 * spa_load_verify_metadata is not set. However, it may be useful
2065 * to manually set the flag after the traversal has begun.
2066 */
2067 if (!spa_load_verify_metadata)
2068 return (0);
a6255b7f 2069 if (!BP_IS_METADATA(bp) && !spa_load_verify_data)
dea377c0
MA
2070 return (0);
2071
1c27024e
DB
2072 zio_t *rio = arg;
2073 size_t size = BP_GET_PSIZE(bp);
dea377c0
MA
2074
2075 mutex_enter(&spa->spa_scrub_lock);
d4a72f23 2076 while (spa->spa_load_verify_ios >= spa_load_verify_maxinflight)
dea377c0 2077 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
d4a72f23 2078 spa->spa_load_verify_ios++;
dea377c0
MA
2079 mutex_exit(&spa->spa_scrub_lock);
2080
a6255b7f 2081 zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size,
dea377c0
MA
2082 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
2083 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
2084 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
428870ff
BB
2085 return (0);
2086}
34dc7c2f 2087
d1d19c78
PD
2088/* ARGSUSED */
2089int
2090verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2091{
2092 if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2093 return (SET_ERROR(ENAMETOOLONG));
2094
2095 return (0);
2096}
2097
428870ff
BB
2098static int
2099spa_load_verify(spa_t *spa)
2100{
2101 zio_t *rio;
2102 spa_load_error_t sle = { 0 };
2103 zpool_rewind_policy_t policy;
2104 boolean_t verify_ok = B_FALSE;
dea377c0 2105 int error = 0;
34dc7c2f 2106
428870ff 2107 zpool_get_rewind_policy(spa->spa_config, &policy);
34dc7c2f 2108
428870ff
BB
2109 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
2110 return (0);
34dc7c2f 2111
d1d19c78
PD
2112 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2113 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2114 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2115 DS_FIND_CHILDREN);
2116 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2117 if (error != 0)
2118 return (error);
2119
428870ff
BB
2120 rio = zio_root(spa, NULL, &sle,
2121 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
34dc7c2f 2122
dea377c0
MA
2123 if (spa_load_verify_metadata) {
2124 error = traverse_pool(spa, spa->spa_verify_min_txg,
b5256303
TC
2125 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
2126 TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio);
dea377c0 2127 }
428870ff
BB
2128
2129 (void) zio_wait(rio);
2130
2131 spa->spa_load_meta_errors = sle.sle_meta_count;
2132 spa->spa_load_data_errors = sle.sle_data_count;
2133
2134 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2135 sle.sle_data_count <= policy.zrp_maxdata) {
572e2857
BB
2136 int64_t loss = 0;
2137
428870ff
BB
2138 verify_ok = B_TRUE;
2139 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2140 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
572e2857
BB
2141
2142 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2143 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2144 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2145 VERIFY(nvlist_add_int64(spa->spa_load_info,
2146 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2147 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2148 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
428870ff
BB
2149 } else {
2150 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2151 }
2152
2153 if (error) {
2154 if (error != ENXIO && error != EIO)
2e528b49 2155 error = SET_ERROR(EIO);
428870ff
BB
2156 return (error);
2157 }
2158
2159 return (verify_ok ? 0 : EIO);
2160}
2161
2162/*
2163 * Find a value in the pool props object.
2164 */
2165static void
2166spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2167{
2168 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2169 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2170}
2171
2172/*
2173 * Find a value in the pool directory object.
2174 */
2175static int
2176spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2177{
2178 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2179 name, sizeof (uint64_t), 1, val));
2180}
2181
2182static int
2183spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2184{
2185 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
a1d477c2 2186 return (SET_ERROR(err));
428870ff
BB
2187}
2188
9d5b5245
SD
2189static void
2190spa_spawn_aux_threads(spa_t *spa)
2191{
2192 ASSERT(spa_writeable(spa));
2193
2194 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2195
2196 spa_start_indirect_condensing_thread(spa);
2197}
2198
428870ff
BB
2199/*
2200 * Fix up config after a partly-completed split. This is done with the
2201 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2202 * pool have that entry in their config, but only the splitting one contains
2203 * a list of all the guids of the vdevs that are being split off.
2204 *
2205 * This function determines what to do with that list: either rejoin
2206 * all the disks to the pool, or complete the splitting process. To attempt
2207 * the rejoin, each disk that is offlined is marked online again, and
2208 * we do a reopen() call. If the vdev label for every disk that was
2209 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2210 * then we call vdev_split() on each disk, and complete the split.
2211 *
2212 * Otherwise we leave the config alone, with all the vdevs in place in
2213 * the original pool.
2214 */
2215static void
2216spa_try_repair(spa_t *spa, nvlist_t *config)
2217{
2218 uint_t extracted;
2219 uint64_t *glist;
2220 uint_t i, gcount;
2221 nvlist_t *nvl;
2222 vdev_t **vd;
2223 boolean_t attempt_reopen;
2224
2225 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2226 return;
2227
2228 /* check that the config is complete */
2229 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2230 &glist, &gcount) != 0)
2231 return;
2232
79c76d5b 2233 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
428870ff
BB
2234
2235 /* attempt to online all the vdevs & validate */
2236 attempt_reopen = B_TRUE;
2237 for (i = 0; i < gcount; i++) {
2238 if (glist[i] == 0) /* vdev is hole */
2239 continue;
2240
2241 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2242 if (vd[i] == NULL) {
2243 /*
2244 * Don't bother attempting to reopen the disks;
2245 * just do the split.
2246 */
2247 attempt_reopen = B_FALSE;
2248 } else {
2249 /* attempt to re-online it */
2250 vd[i]->vdev_offline = B_FALSE;
2251 }
2252 }
2253
2254 if (attempt_reopen) {
2255 vdev_reopen(spa->spa_root_vdev);
2256
2257 /* check each device to see what state it's in */
2258 for (extracted = 0, i = 0; i < gcount; i++) {
2259 if (vd[i] != NULL &&
2260 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2261 break;
2262 ++extracted;
2263 }
2264 }
2265
2266 /*
2267 * If every disk has been moved to the new pool, or if we never
2268 * even attempted to look at them, then we split them off for
2269 * good.
2270 */
2271 if (!attempt_reopen || gcount == extracted) {
2272 for (i = 0; i < gcount; i++)
2273 if (vd[i] != NULL)
2274 vdev_split(vd[i]);
2275 vdev_reopen(spa->spa_root_vdev);
2276 }
2277
2278 kmem_free(vd, gcount * sizeof (vdev_t *));
2279}
2280
2281static int
2282spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
9eb7b46e 2283 boolean_t trust_config)
428870ff
BB
2284{
2285 nvlist_t *config = spa->spa_config;
2286 char *ereport = FM_EREPORT_ZFS_POOL;
d96eb2b1 2287 char *comment;
428870ff
BB
2288 int error;
2289 uint64_t pool_guid;
2290 nvlist_t *nvl;
2291
2292 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2e528b49 2293 return (SET_ERROR(EINVAL));
428870ff 2294
d96eb2b1
DM
2295 ASSERT(spa->spa_comment == NULL);
2296 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2297 spa->spa_comment = spa_strdup(comment);
2298
428870ff
BB
2299 /*
2300 * Versioning wasn't explicitly added to the label until later, so if
2301 * it's not present treat it as the initial version.
2302 */
2303 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2304 &spa->spa_ubsync.ub_version) != 0)
2305 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2306
2307 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2308 &spa->spa_config_txg);
2309
2310 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2311 spa_guid_exists(pool_guid, 0)) {
2e528b49 2312 error = SET_ERROR(EEXIST);
428870ff 2313 } else {
3541dc6d 2314 spa->spa_config_guid = pool_guid;
428870ff
BB
2315
2316 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2317 &nvl) == 0) {
2318 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
79c76d5b 2319 KM_SLEEP) == 0);
428870ff
BB
2320 }
2321
9ae529ec
CS
2322 nvlist_free(spa->spa_load_info);
2323 spa->spa_load_info = fnvlist_alloc();
2324
572e2857 2325 gethrestime(&spa->spa_loaded_ts);
428870ff 2326 error = spa_load_impl(spa, pool_guid, config, state, type,
9eb7b46e 2327 trust_config, &ereport);
428870ff
BB
2328 }
2329
0c66c32d
JG
2330 /*
2331 * Don't count references from objsets that are already closed
2332 * and are making their way through the eviction process.
2333 */
2334 spa_evicting_os_wait(spa);
428870ff 2335 spa->spa_minref = refcount_count(&spa->spa_refcount);
572e2857
BB
2336 if (error) {
2337 if (error != EEXIST) {
2338 spa->spa_loaded_ts.tv_sec = 0;
2339 spa->spa_loaded_ts.tv_nsec = 0;
2340 }
2341 if (error != EBADF) {
b5256303 2342 zfs_ereport_post(ereport, spa, NULL, NULL, NULL, 0, 0);
572e2857
BB
2343 }
2344 }
428870ff
BB
2345 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2346 spa->spa_ena = 0;
2347
2348 return (error);
2349}
2350
33cf67cd 2351#ifdef ZFS_DEBUG
e0ab3ab5
JS
2352/*
2353 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2354 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2355 * spa's per-vdev ZAP list.
2356 */
2357static uint64_t
2358vdev_count_verify_zaps(vdev_t *vd)
2359{
2360 spa_t *spa = vd->vdev_spa;
2361 uint64_t total = 0;
e0ab3ab5
JS
2362
2363 if (vd->vdev_top_zap != 0) {
2364 total++;
2365 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2366 spa->spa_all_vdev_zaps, vd->vdev_top_zap));
2367 }
2368 if (vd->vdev_leaf_zap != 0) {
2369 total++;
2370 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2371 spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
2372 }
2373
1c27024e 2374 for (uint64_t i = 0; i < vd->vdev_children; i++) {
e0ab3ab5
JS
2375 total += vdev_count_verify_zaps(vd->vdev_child[i]);
2376 }
2377
2378 return (total);
2379}
33cf67cd 2380#endif
e0ab3ab5 2381
379ca9cf
OF
2382/*
2383 * Determine whether the activity check is required.
2384 */
2385static boolean_t
bbffb59e
BB
2386spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label,
2387 nvlist_t *config)
379ca9cf
OF
2388{
2389 uint64_t state = 0;
2390 uint64_t hostid = 0;
2391 uint64_t tryconfig_txg = 0;
2392 uint64_t tryconfig_timestamp = 0;
2393 nvlist_t *nvinfo;
2394
2395 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2396 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2397 (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG,
2398 &tryconfig_txg);
2399 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
2400 &tryconfig_timestamp);
2401 }
2402
2403 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state);
379ca9cf
OF
2404
2405 /*
2406 * Disable the MMP activity check - This is used by zdb which
2407 * is intended to be used on potentially active pools.
2408 */
2409 if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP)
2410 return (B_FALSE);
2411
2412 /*
2413 * Skip the activity check when the MMP feature is disabled.
2414 */
2415 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0)
2416 return (B_FALSE);
2417 /*
2418 * If the tryconfig_* values are nonzero, they are the results of an
2419 * earlier tryimport. If they match the uberblock we just found, then
2420 * the pool has not changed and we return false so we do not test a
2421 * second time.
2422 */
2423 if (tryconfig_txg && tryconfig_txg == ub->ub_txg &&
2424 tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp)
2425 return (B_FALSE);
2426
2427 /*
2428 * Allow the activity check to be skipped when importing the pool
bbffb59e
BB
2429 * on the same host which last imported it. Since the hostid from
2430 * configuration may be stale use the one read from the label.
379ca9cf 2431 */
bbffb59e
BB
2432 if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID))
2433 hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID);
2434
379ca9cf
OF
2435 if (hostid == spa_get_hostid())
2436 return (B_FALSE);
2437
2438 /*
2439 * Skip the activity test when the pool was cleanly exported.
2440 */
2441 if (state != POOL_STATE_ACTIVE)
2442 return (B_FALSE);
2443
2444 return (B_TRUE);
2445}
2446
2447/*
2448 * Perform the import activity check. If the user canceled the import or
2449 * we detected activity then fail.
2450 */
2451static int
2452spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config)
2453{
2454 uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1);
2455 uint64_t txg = ub->ub_txg;
2456 uint64_t timestamp = ub->ub_timestamp;
2457 uint64_t import_delay = NANOSEC;
2458 hrtime_t import_expire;
2459 nvlist_t *mmp_label = NULL;
2460 vdev_t *rvd = spa->spa_root_vdev;
2461 kcondvar_t cv;
2462 kmutex_t mtx;
2463 int error = 0;
2464
2465 cv_init(&cv, NULL, CV_DEFAULT, NULL);
2466 mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL);
2467 mutex_enter(&mtx);
2468
2469 /*
2470 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2471 * during the earlier tryimport. If the txg recorded there is 0 then
2472 * the pool is known to be active on another host.
2473 *
2474 * Otherwise, the pool might be in use on another node. Check for
2475 * changes in the uberblocks on disk if necessary.
2476 */
2477 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
2478 nvlist_t *nvinfo = fnvlist_lookup_nvlist(config,
2479 ZPOOL_CONFIG_LOAD_INFO);
2480
2481 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) &&
2482 fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) {
2483 vdev_uberblock_load(rvd, ub, &mmp_label);
2484 error = SET_ERROR(EREMOTEIO);
2485 goto out;
2486 }
2487 }
2488
2489 /*
2490 * Preferentially use the zfs_multihost_interval from the node which
2491 * last imported the pool. This value is stored in an MMP uberblock as.
2492 *
2493 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2494 */
2495 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay)
2496 import_delay = MAX(import_delay, import_intervals *
802ae562 2497 ub->ub_mmp_delay * MAX(vdev_count_leaves(spa), 1));
379ca9cf
OF
2498
2499 /* Apply a floor using the local default values. */
2500 import_delay = MAX(import_delay, import_intervals *
2501 MSEC2NSEC(MAX(zfs_multihost_interval, MMP_MIN_INTERVAL)));
2502
533ea041
OF
2503 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2504 "leaves=%u", import_delay, ub->ub_mmp_delay, import_intervals,
2505 vdev_count_leaves(spa));
2506
379ca9cf
OF
2507 /* Add a small random factor in case of simultaneous imports (0-25%) */
2508 import_expire = gethrtime() + import_delay +
2509 (import_delay * spa_get_random(250) / 1000);
2510
2511 while (gethrtime() < import_expire) {
2512 vdev_uberblock_load(rvd, ub, &mmp_label);
2513
2514 if (txg != ub->ub_txg || timestamp != ub->ub_timestamp) {
2515 error = SET_ERROR(EREMOTEIO);
2516 break;
2517 }
2518
2519 if (mmp_label) {
2520 nvlist_free(mmp_label);
2521 mmp_label = NULL;
2522 }
2523
2524 error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz);
2525 if (error != -1) {
2526 error = SET_ERROR(EINTR);
2527 break;
2528 }
2529 error = 0;
2530 }
2531
2532out:
2533 mutex_exit(&mtx);
2534 mutex_destroy(&mtx);
2535 cv_destroy(&cv);
2536
2537 /*
2538 * If the pool is determined to be active store the status in the
2539 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2540 * available from configuration read from disk store them as well.
2541 * This allows 'zpool import' to generate a more useful message.
2542 *
2543 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2544 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2545 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2546 */
2547 if (error == EREMOTEIO) {
2548 char *hostname = "<unknown>";
2549 uint64_t hostid = 0;
2550
2551 if (mmp_label) {
2552 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) {
2553 hostname = fnvlist_lookup_string(mmp_label,
2554 ZPOOL_CONFIG_HOSTNAME);
2555 fnvlist_add_string(spa->spa_load_info,
2556 ZPOOL_CONFIG_MMP_HOSTNAME, hostname);
2557 }
2558
2559 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) {
2560 hostid = fnvlist_lookup_uint64(mmp_label,
2561 ZPOOL_CONFIG_HOSTID);
2562 fnvlist_add_uint64(spa->spa_load_info,
2563 ZPOOL_CONFIG_MMP_HOSTID, hostid);
2564 }
2565 }
2566
2567 fnvlist_add_uint64(spa->spa_load_info,
2568 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE);
2569 fnvlist_add_uint64(spa->spa_load_info,
2570 ZPOOL_CONFIG_MMP_TXG, 0);
2571
2572 error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO);
2573 }
2574
2575 if (mmp_label)
2576 nvlist_free(mmp_label);
2577
2578 return (error);
2579}
2580
9eb7b46e
PZ
2581static int
2582spa_ld_parse_config(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2583 spa_load_state_t state, spa_import_type_t type)
428870ff
BB
2584{
2585 int error = 0;
9eb7b46e 2586 nvlist_t *nvtree = NULL;
1c27024e 2587 int parse;
9eb7b46e 2588 vdev_t *rvd;
428870ff
BB
2589
2590 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2591
2592 spa->spa_load_state = state;
2593
9eb7b46e 2594 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree))
2e528b49 2595 return (SET_ERROR(EINVAL));
428870ff
BB
2596
2597 parse = (type == SPA_IMPORT_EXISTING ?
2598 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2599
2600 /*
2601 * Create "The Godfather" zio to hold all async IOs
2602 */
e022864d
MA
2603 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2604 KM_SLEEP);
1c27024e 2605 for (int i = 0; i < max_ncpus; i++) {
e022864d
MA
2606 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2607 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2608 ZIO_FLAG_GODFATHER);
2609 }
428870ff
BB
2610
2611 /*
2612 * Parse the configuration into a vdev tree. We explicitly set the
2613 * value that will be returned by spa_version() since parsing the
2614 * configuration requires knowing the version number.
2615 */
2616 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9eb7b46e 2617 error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse);
428870ff
BB
2618 spa_config_exit(spa, SCL_ALL, FTAG);
2619
2620 if (error != 0)
2621 return (error);
2622
2623 ASSERT(spa->spa_root_vdev == rvd);
c3520e7f
MA
2624 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2625 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
428870ff
BB
2626
2627 if (type != SPA_IMPORT_ASSEMBLE) {
2628 ASSERT(spa_guid(spa) == pool_guid);
2629 }
2630
9eb7b46e
PZ
2631 return (0);
2632}
2633
2634static int
2635spa_ld_open_vdevs(spa_t *spa)
2636{
2637 int error = 0;
2638
428870ff 2639 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9eb7b46e 2640 error = vdev_open(spa->spa_root_vdev);
428870ff 2641 spa_config_exit(spa, SCL_ALL, FTAG);
9eb7b46e
PZ
2642
2643 return (error);
2644}
2645
2646static int
2647spa_ld_validate_vdevs(spa_t *spa, spa_import_type_t type,
2648 boolean_t trust_config)
2649{
2650 int error = 0;
2651 vdev_t *rvd = spa->spa_root_vdev;
428870ff
BB
2652
2653 /*
2654 * We need to validate the vdev labels against the configuration that
9eb7b46e
PZ
2655 * we have in hand, which is dependent on the setting of trust_config.
2656 * If trust_config is true then we're validating the vdev labels based
2657 * on that config. Otherwise, we're validating against the cached
2658 * config (zpool.cache) that was read when we loaded the zfs module, and
2659 * then later we will recursively call spa_load() and validate against
428870ff
BB
2660 * the vdev config.
2661 *
2662 * If we're assembling a new pool that's been split off from an
2663 * existing pool, the labels haven't yet been updated so we skip
2664 * validation for now.
2665 */
2666 if (type != SPA_IMPORT_ASSEMBLE) {
2667 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
a1d477c2 2668 error = vdev_validate(rvd, trust_config);
428870ff
BB
2669 spa_config_exit(spa, SCL_ALL, FTAG);
2670
2671 if (error != 0)
2672 return (error);
2673
2674 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2e528b49 2675 return (SET_ERROR(ENXIO));
428870ff
BB
2676 }
2677
9eb7b46e
PZ
2678 return (0);
2679}
2680
2681static int
2682spa_ld_select_uberblock(spa_t *spa, nvlist_t *config, spa_import_type_t type,
2683 boolean_t trust_config)
2684{
2685 vdev_t *rvd = spa->spa_root_vdev;
2686 nvlist_t *label;
2687 uberblock_t *ub = &spa->spa_uberblock;
2688 uint64_t children;
2689 boolean_t activity_check = B_FALSE;
2690
428870ff
BB
2691 /*
2692 * Find the best uberblock.
2693 */
9ae529ec 2694 vdev_uberblock_load(rvd, ub, &label);
428870ff
BB
2695
2696 /*
2697 * If we weren't able to find a single valid uberblock, return failure.
2698 */
9ae529ec
CS
2699 if (ub->ub_txg == 0) {
2700 nvlist_free(label);
428870ff 2701 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
9ae529ec 2702 }
428870ff 2703
379ca9cf
OF
2704 /*
2705 * For pools which have the multihost property on determine if the
2706 * pool is truly inactive and can be safely imported. Prevent
2707 * hosts which don't have a hostid set from importing the pool.
2708 */
bbffb59e 2709 activity_check = spa_activity_check_required(spa, ub, label, config);
379ca9cf 2710 if (activity_check) {
379ca9cf
OF
2711 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay &&
2712 spa_get_hostid() == 0) {
2713 nvlist_free(label);
2714 fnvlist_add_uint64(spa->spa_load_info,
2715 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
2716 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
2717 }
2718
9eb7b46e 2719 int error = spa_activity_check(spa, ub, config);
e889f0f5
OF
2720 if (error) {
2721 nvlist_free(label);
2722 return (error);
2723 }
2724
379ca9cf
OF
2725 fnvlist_add_uint64(spa->spa_load_info,
2726 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE);
2727 fnvlist_add_uint64(spa->spa_load_info,
2728 ZPOOL_CONFIG_MMP_TXG, ub->ub_txg);
2729 }
2730
428870ff 2731 /*
9ae529ec 2732 * If the pool has an unsupported version we can't open it.
428870ff 2733 */
9ae529ec
CS
2734 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2735 nvlist_free(label);
428870ff 2736 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
9ae529ec
CS
2737 }
2738
2739 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2740 nvlist_t *features;
2741
2742 /*
2743 * If we weren't able to find what's necessary for reading the
2744 * MOS in the label, return failure.
2745 */
2746 if (label == NULL || nvlist_lookup_nvlist(label,
2747 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2748 nvlist_free(label);
2749 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2750 ENXIO));
2751 }
2752
2753 /*
2754 * Update our in-core representation with the definitive values
2755 * from the label.
2756 */
2757 nvlist_free(spa->spa_label_features);
2758 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2759 }
2760
2761 nvlist_free(label);
2762
2763 /*
2764 * Look through entries in the label nvlist's features_for_read. If
2765 * there is a feature listed there which we don't understand then we
2766 * cannot open a pool.
2767 */
2768 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2769 nvlist_t *unsup_feat;
9ae529ec
CS
2770
2771 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2772 0);
2773
1c27024e
DB
2774 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2775 NULL); nvp != NULL;
9ae529ec
CS
2776 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2777 if (!zfeature_is_supported(nvpair_name(nvp))) {
2778 VERIFY(nvlist_add_string(unsup_feat,
2779 nvpair_name(nvp), "") == 0);
2780 }
2781 }
2782
2783 if (!nvlist_empty(unsup_feat)) {
2784 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2785 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2786 nvlist_free(unsup_feat);
2787 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2788 ENOTSUP));
2789 }
2790
2791 nvlist_free(unsup_feat);
2792 }
428870ff
BB
2793
2794 /*
2795 * If the vdev guid sum doesn't match the uberblock, we have an
572e2857
BB
2796 * incomplete configuration. We first check to see if the pool
2797 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2798 * If it is, defer the vdev_guid_sum check till later so we
2799 * can handle missing vdevs.
428870ff 2800 */
572e2857 2801 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
a1d477c2 2802 &children) != 0 && trust_config && type != SPA_IMPORT_ASSEMBLE &&
428870ff
BB
2803 rvd->vdev_guid_sum != ub->ub_guid_sum)
2804 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2805
2806 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2807 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2808 spa_try_repair(spa, config);
2809 spa_config_exit(spa, SCL_ALL, FTAG);
2810 nvlist_free(spa->spa_config_splitting);
2811 spa->spa_config_splitting = NULL;
2812 }
2813
2814 /*
2815 * Initialize internal SPA structures.
2816 */
2817 spa->spa_state = POOL_STATE_ACTIVE;
2818 spa->spa_ubsync = spa->spa_uberblock;
2819 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2820 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2821 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2822 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2823 spa->spa_claim_max_txg = spa->spa_first_txg;
2824 spa->spa_prev_software_version = ub->ub_software_version;
2825
9eb7b46e
PZ
2826 return (0);
2827}
2828
2829static int
2830spa_ld_open_rootbp(spa_t *spa)
2831{
2832 int error = 0;
2833 vdev_t *rvd = spa->spa_root_vdev;
a1d477c2 2834
9ae529ec 2835 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
428870ff
BB
2836 if (error)
2837 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2838 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2839
9eb7b46e
PZ
2840 return (0);
2841}
2842
2843static int
2844spa_ld_validate_config(spa_t *spa, spa_import_type_t type)
2845{
2846 vdev_t *rvd = spa->spa_root_vdev;
2847
428870ff
BB
2848 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2849 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2850
a1d477c2
MA
2851 /*
2852 * Validate the config, using the MOS config to fill in any
2853 * information which might be missing. If we fail to validate
2854 * the config then declare the pool unfit for use. If we're
2855 * assembling a pool from a split, the log is not transferred
2856 * over.
2857 */
2858 if (type != SPA_IMPORT_ASSEMBLE) {
2859 nvlist_t *mos_config;
2860 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
2861 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2862
2863 if (!spa_config_valid(spa, mos_config)) {
2864 nvlist_free(mos_config);
2865 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2866 ENXIO));
2867 }
2868 nvlist_free(mos_config);
2869
2870 /*
2871 * Now that we've validated the config, check the state of the
2872 * root vdev. If it can't be opened, it indicates one or
2873 * more toplevel vdevs are faulted.
2874 */
2875 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2876 return (SET_ERROR(ENXIO));
2877 }
2878
9eb7b46e
PZ
2879 return (0);
2880}
2881
2882static int
2883spa_ld_open_indirect_vdev_metadata(spa_t *spa)
2884{
2885 int error = 0;
2886 vdev_t *rvd = spa->spa_root_vdev;
2887
a1d477c2
MA
2888 /*
2889 * Everything that we read before spa_remove_init() must be stored
2890 * on concreted vdevs. Therefore we do this as early as possible.
2891 */
2892 if (spa_remove_init(spa) != 0)
2893 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2894
9eb7b46e
PZ
2895 /*
2896 * Retrieve information needed to condense indirect vdev mappings.
2897 */
2898 error = spa_condense_init(spa);
2899 if (error != 0) {
2900 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
2901 }
2902
2903 return (0);
2904}
2905
2906static int
2907spa_ld_check_features(spa_t *spa, spa_load_state_t state,
2908 boolean_t *missing_feat_writep)
2909{
2910 int error = 0;
2911 vdev_t *rvd = spa->spa_root_vdev;
2912
9ae529ec
CS
2913 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2914 boolean_t missing_feat_read = B_FALSE;
b9b24bb4 2915 nvlist_t *unsup_feat, *enabled_feat;
9ae529ec
CS
2916
2917 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2918 &spa->spa_feat_for_read_obj) != 0) {
2919 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2920 }
2921
2922 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2923 &spa->spa_feat_for_write_obj) != 0) {
2924 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2925 }
2926
2927 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2928 &spa->spa_feat_desc_obj) != 0) {
2929 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2930 }
2931
b9b24bb4
CS
2932 enabled_feat = fnvlist_alloc();
2933 unsup_feat = fnvlist_alloc();
9ae529ec 2934
fa86b5db 2935 if (!spa_features_check(spa, B_FALSE,
b9b24bb4 2936 unsup_feat, enabled_feat))
9ae529ec
CS
2937 missing_feat_read = B_TRUE;
2938
2939 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
fa86b5db 2940 if (!spa_features_check(spa, B_TRUE,
b9b24bb4 2941 unsup_feat, enabled_feat)) {
9eb7b46e 2942 *missing_feat_writep = B_TRUE;
b9b24bb4 2943 }
9ae529ec
CS
2944 }
2945
b9b24bb4
CS
2946 fnvlist_add_nvlist(spa->spa_load_info,
2947 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2948
9ae529ec 2949 if (!nvlist_empty(unsup_feat)) {
b9b24bb4
CS
2950 fnvlist_add_nvlist(spa->spa_load_info,
2951 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
9ae529ec
CS
2952 }
2953
b9b24bb4
CS
2954 fnvlist_free(enabled_feat);
2955 fnvlist_free(unsup_feat);
9ae529ec
CS
2956
2957 if (!missing_feat_read) {
2958 fnvlist_add_boolean(spa->spa_load_info,
2959 ZPOOL_CONFIG_CAN_RDONLY);
2960 }
2961
2962 /*
2963 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2964 * twofold: to determine whether the pool is available for
2965 * import in read-write mode and (if it is not) whether the
2966 * pool is available for import in read-only mode. If the pool
2967 * is available for import in read-write mode, it is displayed
2968 * as available in userland; if it is not available for import
2969 * in read-only mode, it is displayed as unavailable in
2970 * userland. If the pool is available for import in read-only
2971 * mode but not read-write mode, it is displayed as unavailable
2972 * in userland with a special note that the pool is actually
2973 * available for open in read-only mode.
2974 *
2975 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2976 * missing a feature for write, we must first determine whether
2977 * the pool can be opened read-only before returning to
2978 * userland in order to know whether to display the
2979 * abovementioned note.
2980 */
9eb7b46e 2981 if (missing_feat_read || (*missing_feat_writep &&
9ae529ec
CS
2982 spa_writeable(spa))) {
2983 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2984 ENOTSUP));
2985 }
b0bc7a84
MG
2986
2987 /*
2988 * Load refcounts for ZFS features from disk into an in-memory
2989 * cache during SPA initialization.
2990 */
1c27024e 2991 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
b0bc7a84
MG
2992 uint64_t refcount;
2993
2994 error = feature_get_refcount_from_disk(spa,
2995 &spa_feature_table[i], &refcount);
2996 if (error == 0) {
2997 spa->spa_feat_refcount_cache[i] = refcount;
2998 } else if (error == ENOTSUP) {
2999 spa->spa_feat_refcount_cache[i] =
3000 SPA_FEATURE_DISABLED;
3001 } else {
3002 return (spa_vdev_err(rvd,
3003 VDEV_AUX_CORRUPT_DATA, EIO));
3004 }
3005 }
3006 }
3007
3008 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
3009 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
9b67f605 3010 &spa->spa_feat_enabled_txg_obj) != 0)
b0bc7a84 3011 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
9ae529ec
CS
3012 }
3013
9eb7b46e
PZ
3014 return (0);
3015}
3016
3017static int
3018spa_ld_load_special_directories(spa_t *spa)
3019{
3020 int error = 0;
3021 vdev_t *rvd = spa->spa_root_vdev;
3022
9ae529ec
CS
3023 spa->spa_is_initializing = B_TRUE;
3024 error = dsl_pool_open(spa->spa_dsl_pool);
3025 spa->spa_is_initializing = B_FALSE;
3026 if (error != 0)
3027 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3028
9eb7b46e
PZ
3029 return (0);
3030}
428870ff 3031
9eb7b46e
PZ
3032static int
3033spa_ld_prepare_for_reload(spa_t *spa, int orig_mode)
3034{
3035 vdev_t *rvd = spa->spa_root_vdev;
428870ff 3036
9eb7b46e
PZ
3037 uint64_t hostid;
3038 nvlist_t *policy = NULL;
3039 nvlist_t *mos_config;
34dc7c2f 3040
9eb7b46e
PZ
3041 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
3042 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 3043
9eb7b46e
PZ
3044 if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config,
3045 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
3046 char *hostname;
3047 unsigned long myhostid = 0;
34dc7c2f 3048
9eb7b46e
PZ
3049 VERIFY(nvlist_lookup_string(mos_config,
3050 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
34dc7c2f 3051
9eb7b46e
PZ
3052 myhostid = spa_get_hostid();
3053 if (hostid && myhostid && hostid != myhostid) {
3054 nvlist_free(mos_config);
3055 return (SET_ERROR(EBADF));
3056 }
34dc7c2f 3057 }
9eb7b46e
PZ
3058 if (nvlist_lookup_nvlist(spa->spa_config,
3059 ZPOOL_REWIND_POLICY, &policy) == 0)
3060 VERIFY(nvlist_add_nvlist(mos_config,
3061 ZPOOL_REWIND_POLICY, policy) == 0);
3062
3063 spa_config_set(spa, mos_config);
3064 spa_unload(spa);
3065 spa_deactivate(spa);
3066 spa_activate(spa, orig_mode);
3067
3068 return (0);
3069}
3070
3071static int
3072spa_ld_get_props(spa_t *spa)
3073{
3074 int error = 0;
3075 uint64_t obj;
3076 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 3077
3c67d83a
TH
3078 /* Grab the checksum salt from the MOS. */
3079 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3080 DMU_POOL_CHECKSUM_SALT, 1,
3081 sizeof (spa->spa_cksum_salt.zcs_bytes),
3082 spa->spa_cksum_salt.zcs_bytes);
3083 if (error == ENOENT) {
3084 /* Generate a new salt for subsequent use */
3085 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3086 sizeof (spa->spa_cksum_salt.zcs_bytes));
3087 } else if (error != 0) {
3088 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3089 }
3090
428870ff
BB
3091 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
3092 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3093 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
3094 if (error != 0)
3095 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
3096
3097 /*
3098 * Load the bit that tells us to use the new accounting function
3099 * (raid-z deflation). If we have an older pool, this will not
3100 * be present.
3101 */
428870ff
BB
3102 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
3103 if (error != 0 && error != ENOENT)
3104 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3105
3106 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
3107 &spa->spa_creation_version);
3108 if (error != 0 && error != ENOENT)
3109 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
3110
3111 /*
3112 * Load the persistent error log. If we have an older pool, this will
3113 * not be present.
3114 */
428870ff
BB
3115 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
3116 if (error != 0 && error != ENOENT)
3117 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 3118
428870ff
BB
3119 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
3120 &spa->spa_errlog_scrub);
3121 if (error != 0 && error != ENOENT)
3122 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
3123
3124 /*
3125 * Load the history object. If we have an older pool, this
3126 * will not be present.
3127 */
428870ff
BB
3128 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
3129 if (error != 0 && error != ENOENT)
3130 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3131
e0ab3ab5
JS
3132 /*
3133 * Load the per-vdev ZAP map. If we have an older pool, this will not
3134 * be present; in this case, defer its creation to a later time to
3135 * avoid dirtying the MOS this early / out of sync context. See
3136 * spa_sync_config_object.
3137 */
3138
3139 /* The sentinel is only available in the MOS config. */
1c27024e 3140 nvlist_t *mos_config;
e0ab3ab5
JS
3141 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
3142 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3143
3144 error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
3145 &spa->spa_all_vdev_zaps);
3146
38640550
DB
3147 if (error == ENOENT) {
3148 VERIFY(!nvlist_exists(mos_config,
3149 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
3150 spa->spa_avz_action = AVZ_ACTION_INITIALIZE;
3151 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
3152 } else if (error != 0) {
e0ab3ab5 3153 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
38640550 3154 } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
e0ab3ab5
JS
3155 /*
3156 * An older version of ZFS overwrote the sentinel value, so
3157 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3158 * destruction to later; see spa_sync_config_object.
3159 */
3160 spa->spa_avz_action = AVZ_ACTION_DESTROY;
3161 /*
3162 * We're assuming that no vdevs have had their ZAPs created
3163 * before this. Better be sure of it.
3164 */
3165 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
3166 }
3167 nvlist_free(mos_config);
3168
9eb7b46e
PZ
3169 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3170
3171 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
3172 if (error && error != ENOENT)
3173 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3174
3175 if (error == 0) {
3176 uint64_t autoreplace;
3177
3178 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
3179 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
3180 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
3181 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
3182 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
3183 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
3184 &spa->spa_dedup_ditto);
3185
3186 spa->spa_autoreplace = (autoreplace != 0);
3187 }
3188
3189 return (0);
3190}
3191
3192static int
3193spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type)
3194{
3195 int error = 0;
3196 vdev_t *rvd = spa->spa_root_vdev;
3197
428870ff
BB
3198 /*
3199 * If we're assembling the pool from the split-off vdevs of
3200 * an existing pool, we don't want to attach the spares & cache
3201 * devices.
3202 */
34dc7c2f
BB
3203
3204 /*
3205 * Load any hot spares for this pool.
3206 */
428870ff
BB
3207 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
3208 if (error != 0 && error != ENOENT)
3209 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3210 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
3211 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
3212 if (load_nvlist(spa, spa->spa_spares.sav_object,
428870ff
BB
3213 &spa->spa_spares.sav_config) != 0)
3214 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 3215
b128c09f 3216 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3217 spa_load_spares(spa);
b128c09f 3218 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
3219 } else if (error == 0) {
3220 spa->spa_spares.sav_sync = B_TRUE;
34dc7c2f
BB
3221 }
3222
3223 /*
3224 * Load any level 2 ARC devices for this pool.
3225 */
428870ff 3226 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
34dc7c2f 3227 &spa->spa_l2cache.sav_object);
428870ff
BB
3228 if (error != 0 && error != ENOENT)
3229 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3230 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
3231 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
3232 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
428870ff
BB
3233 &spa->spa_l2cache.sav_config) != 0)
3234 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 3235
b128c09f 3236 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3237 spa_load_l2cache(spa);
b128c09f 3238 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
3239 } else if (error == 0) {
3240 spa->spa_l2cache.sav_sync = B_TRUE;
b128c09f
BB
3241 }
3242
9eb7b46e
PZ
3243 return (0);
3244}
428870ff 3245
9eb7b46e
PZ
3246static int
3247spa_ld_load_vdev_metadata(spa_t *spa, spa_load_state_t state)
3248{
3249 int error = 0;
3250 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 3251
379ca9cf
OF
3252 /*
3253 * If the 'multihost' property is set, then never allow a pool to
3254 * be imported when the system hostid is zero. The exception to
3255 * this rule is zdb which is always allowed to access pools.
3256 */
3257 if (spa_multihost(spa) && spa_get_hostid() == 0 &&
3258 (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) {
3259 fnvlist_add_uint64(spa->spa_load_info,
3260 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
3261 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
3262 }
3263
34dc7c2f
BB
3264 /*
3265 * If the 'autoreplace' property is set, then post a resource notifying
3266 * the ZFS DE that it should not issue any faults for unopenable
3267 * devices. We also iterate over the vdevs, and post a sysevent for any
3268 * unopenable vdevs so that the normal autoreplace handler can take
3269 * over.
3270 */
428870ff 3271 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
34dc7c2f 3272 spa_check_removed(spa->spa_root_vdev);
428870ff
BB
3273 /*
3274 * For the import case, this is done in spa_import(), because
3275 * at this point we're using the spare definitions from
3276 * the MOS config, not necessarily from the userland config.
3277 */
3278 if (state != SPA_LOAD_IMPORT) {
3279 spa_aux_check_removed(&spa->spa_spares);
3280 spa_aux_check_removed(&spa->spa_l2cache);
3281 }
3282 }
34dc7c2f
BB
3283
3284 /*
9eb7b46e 3285 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
34dc7c2f 3286 */
a1d477c2
MA
3287 error = vdev_load(rvd);
3288 if (error != 0) {
3289 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
3290 }
3291
34dc7c2f 3292 /*
9eb7b46e 3293 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
34dc7c2f 3294 */
b128c09f 3295 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3296 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
b128c09f 3297 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 3298
9eb7b46e
PZ
3299 return (0);
3300}
3301
3302static int
3303spa_ld_load_dedup_tables(spa_t *spa)
3304{
3305 int error = 0;
3306 vdev_t *rvd = spa->spa_root_vdev;
3307
428870ff
BB
3308 error = ddt_load(spa);
3309 if (error != 0)
3310 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
3311
9eb7b46e
PZ
3312 return (0);
3313}
3314
3315static int
3316spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport)
3317{
3318 vdev_t *rvd = spa->spa_root_vdev;
428870ff 3319
a1d477c2
MA
3320 if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa) &&
3321 spa_check_logs(spa)) {
3322 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
3323 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
428870ff
BB
3324 }
3325
9eb7b46e
PZ
3326 return (0);
3327}
3328
3329static int
3330spa_ld_verify_pool_data(spa_t *spa, spa_load_state_t state)
3331{
3332 int error = 0;
3333 vdev_t *rvd = spa->spa_root_vdev;
3334
3335 /*
3336 * We've successfully opened the pool, verify that we're ready
3337 * to start pushing transactions.
3338 */
3339 if (state != SPA_LOAD_TRYIMPORT) {
3340 error = spa_load_verify(spa);
3341 if (error != 0) {
3342 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
3343 error));
3344 }
3345 }
3346
3347 return (0);
3348}
3349
3350static void
3351spa_ld_claim_log_blocks(spa_t *spa)
3352{
3353 dmu_tx_t *tx;
3354 dsl_pool_t *dp = spa_get_dsl(spa);
3355
3356 /*
3357 * Claim log blocks that haven't been committed yet.
3358 * This must all happen in a single txg.
3359 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3360 * invoked from zil_claim_log_block()'s i/o done callback.
3361 * Price of rollback is that we abandon the log.
3362 */
3363 spa->spa_claiming = B_TRUE;
3364
3365 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
3366 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
3367 zil_claim, tx, DS_FIND_CHILDREN);
3368 dmu_tx_commit(tx);
3369
3370 spa->spa_claiming = B_FALSE;
3371
3372 spa_set_log_state(spa, SPA_LOG_GOOD);
3373}
3374
3375static void
3376spa_ld_check_for_config_update(spa_t *spa, spa_load_state_t state,
3377 int64_t config_cache_txg)
3378{
3379 vdev_t *rvd = spa->spa_root_vdev;
3380 int need_update = B_FALSE;
3381
3382 /*
3383 * If the config cache is stale, or we have uninitialized
3384 * metaslabs (see spa_vdev_add()), then update the config.
3385 *
3386 * If this is a verbatim import, trust the current
3387 * in-core spa_config and update the disk labels.
3388 */
3389 if (config_cache_txg != spa->spa_config_txg ||
3390 state == SPA_LOAD_IMPORT ||
3391 state == SPA_LOAD_RECOVER ||
3392 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
3393 need_update = B_TRUE;
3394
3395 for (int c = 0; c < rvd->vdev_children; c++)
3396 if (rvd->vdev_child[c]->vdev_ms_array == 0)
3397 need_update = B_TRUE;
3398
3399 /*
3400 * Update the config cache asychronously in case we're the
3401 * root pool, in which case the config cache isn't writable yet.
3402 */
3403 if (need_update)
3404 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3405}
3406
3407/*
3408 * Load an existing storage pool, using the config provided. This config
3409 * describes which vdevs are part of the pool and is later validated against
3410 * partial configs present in each vdev's label and an entire copy of the
3411 * config stored in the MOS.
3412 */
3413static int
3414spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
3415 spa_load_state_t state, spa_import_type_t type, boolean_t trust_config,
3416 char **ereport)
3417{
3418 int error = 0;
3419 uint64_t config_cache_txg = spa->spa_config_txg;
3420 int orig_mode = spa->spa_mode;
3421 boolean_t missing_feat_write = B_FALSE;
3422
3423 /*
3424 * If this is an untrusted config, first access the pool in read-only
3425 * mode. We will then retrieve a trusted copy of the config from the MOS
3426 * and use it to reopen the pool in read-write mode.
3427 */
3428 if (!trust_config)
3429 spa->spa_mode = FREAD;
3430
3431 /*
3432 * Parse the config provided to create a vdev tree.
3433 */
3434 error = spa_ld_parse_config(spa, pool_guid, config, state, type);
3435 if (error != 0)
3436 return (error);
3437
3438 /*
3439 * Now that we have the vdev tree, try to open each vdev. This involves
3440 * opening the underlying physical device, retrieving its geometry and
3441 * probing the vdev with a dummy I/O. The state of each vdev will be set
3442 * based on the success of those operations. After this we'll be ready
3443 * to read from the vdevs.
3444 */
3445 error = spa_ld_open_vdevs(spa);
3446 if (error != 0)
3447 return (error);
3448
3449 /*
3450 * Read the label of each vdev and make sure that the GUIDs stored
3451 * there match the GUIDs in the config provided.
3452 */
3453 error = spa_ld_validate_vdevs(spa, type, trust_config);
3454 if (error != 0)
3455 return (error);
3456
3457 /*
3458 * Read vdev labels to find the best uberblock (i.e. latest, unless
3459 * spa_load_max_txg is set) and store it in spa_uberblock. We get the
3460 * list of features required to read blkptrs in the MOS from the vdev
3461 * label with the best uberblock and verify that our version of zfs
3462 * supports them all.
3463 */
3464 error = spa_ld_select_uberblock(spa, config, type, trust_config);
3465 if (error != 0)
3466 return (error);
3467
3468 /*
3469 * Pass that uberblock to the dsl_pool layer which will open the root
3470 * blkptr. This blkptr points to the latest version of the MOS and will
3471 * allow us to read its contents.
3472 */
3473 error = spa_ld_open_rootbp(spa);
3474 if (error != 0)
3475 return (error);
3476
3477 /*
3478 * Retrieve the config stored in the MOS and use it to validate the
3479 * config provided. Also extract some information from the MOS config
3480 * to update our vdev tree.
3481 */
3482 error = spa_ld_validate_config(spa, type);
3483 if (error != 0)
3484 return (error);
3485
3486 /*
3487 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
3488 * from the pool and their contents were re-mapped to other vdevs. Note
3489 * that everything that we read before this step must have been
3490 * rewritten on concrete vdevs after the last device removal was
3491 * initiated. Otherwise we could be reading from indirect vdevs before
3492 * we have loaded their mappings.
3493 */
3494 error = spa_ld_open_indirect_vdev_metadata(spa);
3495 if (error != 0)
3496 return (error);
3497
3498 /*
3499 * Retrieve the full list of active features from the MOS and check if
3500 * they are all supported.
3501 */
3502 error = spa_ld_check_features(spa, state, &missing_feat_write);
3503 if (error != 0)
3504 return (error);
3505
3506 /*
3507 * Load several special directories from the MOS needed by the dsl_pool
3508 * layer.
3509 */
3510 error = spa_ld_load_special_directories(spa);
3511 if (error != 0)
3512 return (error);
3513
3514 /*
3515 * If the config provided is not trusted, discard it and use the config
3516 * from the MOS to reload the pool.
3517 */
3518 if (!trust_config) {
3519 error = spa_ld_prepare_for_reload(spa, orig_mode);
3520 if (error != 0)
3521 return (error);
3522 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
3523 }
3524
3525 /*
3526 * Retrieve pool properties from the MOS.
3527 */
3528 error = spa_ld_get_props(spa);
3529 if (error != 0)
3530 return (error);
3531
3532 /*
3533 * Retrieve the list of auxiliary devices - cache devices and spares -
3534 * and open them.
3535 */
3536 error = spa_ld_open_aux_vdevs(spa, type);
3537 if (error != 0)
3538 return (error);
3539
3540 /*
3541 * Load the metadata for all vdevs. Also check if unopenable devices
3542 * should be autoreplaced.
3543 */
3544 error = spa_ld_load_vdev_metadata(spa, state);
3545 if (error != 0)
3546 return (error);
3547
3548 error = spa_ld_load_dedup_tables(spa);
3549 if (error != 0)
3550 return (error);
3551
3552 /*
3553 * Verify the logs now to make sure we don't have any unexpected errors
3554 * when we claim log blocks later.
3555 */
3556 error = spa_ld_verify_logs(spa, type, ereport);
3557 if (error != 0)
3558 return (error);
3559
9ae529ec
CS
3560 if (missing_feat_write) {
3561 ASSERT(state == SPA_LOAD_TRYIMPORT);
3562
3563 /*
3564 * At this point, we know that we can open the pool in
3565 * read-only mode but not read-write mode. We now have enough
3566 * information and can return to userland.
3567 */
9eb7b46e
PZ
3568 return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT,
3569 ENOTSUP));
9ae529ec
CS
3570 }
3571
572e2857 3572 /*
9eb7b46e
PZ
3573 * Traverse the last txgs to make sure the pool was left off in a safe
3574 * state. When performing an extreme rewind, we verify the whole pool,
3575 * which can take a very long time.
572e2857 3576 */
9eb7b46e
PZ
3577 error = spa_ld_verify_pool_data(spa, state);
3578 if (error != 0)
3579 return (error);
572e2857 3580
9eb7b46e
PZ
3581 /*
3582 * Calculate the deflated space for the pool. This must be done before
3583 * we write anything to the pool because we'd need to update the space
3584 * accounting using the deflated sizes.
3585 */
3586 spa_update_dspace(spa);
3587
3588 /*
3589 * We have now retrieved all the information we needed to open the
3590 * pool. If we are importing the pool in read-write mode, a few
3591 * additional steps must be performed to finish the import.
3592 */
428870ff
BB
3593 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
3594 spa->spa_load_max_txg == UINT64_MAX)) {
fb5f0bc8 3595 ASSERT(state != SPA_LOAD_TRYIMPORT);
34dc7c2f
BB
3596
3597 /*
9eb7b46e 3598 * Traverse the ZIL and claim all blocks.
34dc7c2f 3599 */
9eb7b46e 3600 spa_ld_claim_log_blocks(spa);
428870ff 3601
9eb7b46e
PZ
3602 /*
3603 * Kick-off the syncing thread.
3604 */
34dc7c2f
BB
3605 spa->spa_sync_on = B_TRUE;
3606 txg_sync_start(spa->spa_dsl_pool);
379ca9cf 3607 mmp_thread_start(spa);
34dc7c2f
BB
3608
3609 /*
428870ff
BB
3610 * Wait for all claims to sync. We sync up to the highest
3611 * claimed log block birth time so that claimed log blocks
3612 * don't appear to be from the future. spa_claim_max_txg
9eb7b46e
PZ
3613 * will have been set for us by ZIL traversal operations
3614 * performed above.
34dc7c2f 3615 */
428870ff 3616 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
34dc7c2f
BB
3617
3618 /*
9eb7b46e
PZ
3619 * Check if we need to request an update of the config. On the
3620 * next sync, we would update the config stored in vdev labels
3621 * and the cachefile (by default /etc/zfs/zpool.cache).
34dc7c2f 3622 */
9eb7b46e 3623 spa_ld_check_for_config_update(spa, state, config_cache_txg);
fb5f0bc8
BB
3624
3625 /*
3626 * Check all DTLs to see if anything needs resilvering.
3627 */
428870ff 3628 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
9eb7b46e 3629 vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
fb5f0bc8 3630 spa_async_request(spa, SPA_ASYNC_RESILVER);
428870ff 3631
6f1ffb06
MA
3632 /*
3633 * Log the fact that we booted up (so that we can detect if
3634 * we rebooted in the middle of an operation).
3635 */
d5e024cb 3636 spa_history_log_version(spa, "open", NULL);
6f1ffb06 3637
428870ff
BB
3638 /*
3639 * Delete any inconsistent datasets.
3640 */
3641 (void) dmu_objset_find(spa_name(spa),
3642 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
3643
3644 /*
3645 * Clean up any stale temporary dataset userrefs.
3646 */
3647 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
a1d477c2 3648
a1d477c2
MA
3649 spa_restart_removal(spa);
3650
9d5b5245 3651 spa_spawn_aux_threads(spa);
34dc7c2f
BB
3652 }
3653
428870ff
BB
3654 return (0);
3655}
34dc7c2f 3656
428870ff 3657static int
9eb7b46e 3658spa_load_retry(spa_t *spa, spa_load_state_t state, int trust_config)
428870ff 3659{
572e2857
BB
3660 int mode = spa->spa_mode;
3661
428870ff
BB
3662 spa_unload(spa);
3663 spa_deactivate(spa);
3664
dea377c0 3665 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
428870ff 3666
572e2857 3667 spa_activate(spa, mode);
428870ff
BB
3668 spa_async_suspend(spa);
3669
9eb7b46e 3670 return (spa_load(spa, state, SPA_IMPORT_EXISTING, trust_config));
428870ff
BB
3671}
3672
9ae529ec
CS
3673/*
3674 * If spa_load() fails this function will try loading prior txg's. If
3675 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3676 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3677 * function will not rewind the pool and will return the same error as
3678 * spa_load().
3679 */
428870ff 3680static int
9eb7b46e 3681spa_load_best(spa_t *spa, spa_load_state_t state, int trust_config,
428870ff
BB
3682 uint64_t max_request, int rewind_flags)
3683{
9ae529ec 3684 nvlist_t *loadinfo = NULL;
428870ff
BB
3685 nvlist_t *config = NULL;
3686 int load_error, rewind_error;
3687 uint64_t safe_rewind_txg;
3688 uint64_t min_txg;
3689
3690 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
3691 spa->spa_load_max_txg = spa->spa_load_txg;
3692 spa_set_log_state(spa, SPA_LOG_CLEAR);
3693 } else {
3694 spa->spa_load_max_txg = max_request;
dea377c0
MA
3695 if (max_request != UINT64_MAX)
3696 spa->spa_extreme_rewind = B_TRUE;
428870ff
BB
3697 }
3698
3699 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
9eb7b46e 3700 trust_config);
428870ff
BB
3701 if (load_error == 0)
3702 return (0);
3703
3704 if (spa->spa_root_vdev != NULL)
3705 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3706
3707 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
3708 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
3709
3710 if (rewind_flags & ZPOOL_NEVER_REWIND) {
3711 nvlist_free(config);
3712 return (load_error);
3713 }
3714
9ae529ec
CS
3715 if (state == SPA_LOAD_RECOVER) {
3716 /* Price of rolling back is discarding txgs, including log */
428870ff 3717 spa_set_log_state(spa, SPA_LOG_CLEAR);
9ae529ec
CS
3718 } else {
3719 /*
3720 * If we aren't rolling back save the load info from our first
3721 * import attempt so that we can restore it after attempting
3722 * to rewind.
3723 */
3724 loadinfo = spa->spa_load_info;
3725 spa->spa_load_info = fnvlist_alloc();
3726 }
428870ff
BB
3727
3728 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
3729 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
3730 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
3731 TXG_INITIAL : safe_rewind_txg;
3732
3733 /*
3734 * Continue as long as we're finding errors, we're still within
3735 * the acceptable rewind range, and we're still finding uberblocks
3736 */
3737 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
3738 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
3739 if (spa->spa_load_max_txg < safe_rewind_txg)
3740 spa->spa_extreme_rewind = B_TRUE;
9eb7b46e 3741 rewind_error = spa_load_retry(spa, state, trust_config);
428870ff
BB
3742 }
3743
428870ff
BB
3744 spa->spa_extreme_rewind = B_FALSE;
3745 spa->spa_load_max_txg = UINT64_MAX;
3746
3747 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
3748 spa_config_set(spa, config);
ee6370a7 3749 else
3750 nvlist_free(config);
428870ff 3751
9ae529ec
CS
3752 if (state == SPA_LOAD_RECOVER) {
3753 ASSERT3P(loadinfo, ==, NULL);
3754 return (rewind_error);
3755 } else {
3756 /* Store the rewind info as part of the initial load info */
3757 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
3758 spa->spa_load_info);
3759
3760 /* Restore the initial load info */
3761 fnvlist_free(spa->spa_load_info);
3762 spa->spa_load_info = loadinfo;
3763
3764 return (load_error);
3765 }
34dc7c2f
BB
3766}
3767
3768/*
3769 * Pool Open/Import
3770 *
3771 * The import case is identical to an open except that the configuration is sent
3772 * down from userland, instead of grabbed from the configuration cache. For the
3773 * case of an open, the pool configuration will exist in the
3774 * POOL_STATE_UNINITIALIZED state.
3775 *
3776 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3777 * the same time open the pool, without having to keep around the spa_t in some
3778 * ambiguous state.
3779 */
3780static int
428870ff
BB
3781spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3782 nvlist_t **config)
34dc7c2f
BB
3783{
3784 spa_t *spa;
572e2857 3785 spa_load_state_t state = SPA_LOAD_OPEN;
34dc7c2f 3786 int error;
34dc7c2f 3787 int locked = B_FALSE;
526af785 3788 int firstopen = B_FALSE;
34dc7c2f
BB
3789
3790 *spapp = NULL;
3791
3792 /*
3793 * As disgusting as this is, we need to support recursive calls to this
3794 * function because dsl_dir_open() is called during spa_load(), and ends
3795 * up calling spa_open() again. The real fix is to figure out how to
3796 * avoid dsl_dir_open() calling this in the first place.
3797 */
c25b8f99 3798 if (MUTEX_NOT_HELD(&spa_namespace_lock)) {
34dc7c2f
BB
3799 mutex_enter(&spa_namespace_lock);
3800 locked = B_TRUE;
3801 }
3802
3803 if ((spa = spa_lookup(pool)) == NULL) {
3804 if (locked)
3805 mutex_exit(&spa_namespace_lock);
2e528b49 3806 return (SET_ERROR(ENOENT));
34dc7c2f 3807 }
428870ff 3808
34dc7c2f 3809 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
428870ff
BB
3810 zpool_rewind_policy_t policy;
3811
526af785
PJD
3812 firstopen = B_TRUE;
3813
428870ff
BB
3814 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3815 &policy);
3816 if (policy.zrp_request & ZPOOL_DO_REWIND)
3817 state = SPA_LOAD_RECOVER;
34dc7c2f 3818
fb5f0bc8 3819 spa_activate(spa, spa_mode_global);
34dc7c2f 3820
428870ff
BB
3821 if (state != SPA_LOAD_RECOVER)
3822 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3823
3824 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3825 policy.zrp_request);
34dc7c2f
BB
3826
3827 if (error == EBADF) {
3828 /*
3829 * If vdev_validate() returns failure (indicated by
3830 * EBADF), it indicates that one of the vdevs indicates
3831 * that the pool has been exported or destroyed. If
3832 * this is the case, the config cache is out of sync and
3833 * we should remove the pool from the namespace.
3834 */
34dc7c2f
BB
3835 spa_unload(spa);
3836 spa_deactivate(spa);
a1d477c2 3837 spa_write_cachefile(spa, B_TRUE, B_TRUE);
34dc7c2f 3838 spa_remove(spa);
34dc7c2f
BB
3839 if (locked)
3840 mutex_exit(&spa_namespace_lock);
2e528b49 3841 return (SET_ERROR(ENOENT));
34dc7c2f
BB
3842 }
3843
3844 if (error) {
3845 /*
3846 * We can't open the pool, but we still have useful
3847 * information: the state of each vdev after the
3848 * attempted vdev_open(). Return this to the user.
3849 */
572e2857 3850 if (config != NULL && spa->spa_config) {
428870ff 3851 VERIFY(nvlist_dup(spa->spa_config, config,
79c76d5b 3852 KM_SLEEP) == 0);
572e2857
BB
3853 VERIFY(nvlist_add_nvlist(*config,
3854 ZPOOL_CONFIG_LOAD_INFO,
3855 spa->spa_load_info) == 0);
3856 }
34dc7c2f
BB
3857 spa_unload(spa);
3858 spa_deactivate(spa);
428870ff 3859 spa->spa_last_open_failed = error;
34dc7c2f
BB
3860 if (locked)
3861 mutex_exit(&spa_namespace_lock);
3862 *spapp = NULL;
3863 return (error);
34dc7c2f 3864 }
34dc7c2f
BB
3865 }
3866
3867 spa_open_ref(spa, tag);
3868
b128c09f 3869 if (config != NULL)
34dc7c2f 3870 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
34dc7c2f 3871
572e2857
BB
3872 /*
3873 * If we've recovered the pool, pass back any information we
3874 * gathered while doing the load.
3875 */
3876 if (state == SPA_LOAD_RECOVER) {
3877 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3878 spa->spa_load_info) == 0);
3879 }
3880
428870ff
BB
3881 if (locked) {
3882 spa->spa_last_open_failed = 0;
3883 spa->spa_last_ubsync_txg = 0;
3884 spa->spa_load_txg = 0;
3885 mutex_exit(&spa_namespace_lock);
3886 }
3887
526af785 3888 if (firstopen)
a0bd735a 3889 zvol_create_minors(spa, spa_name(spa), B_TRUE);
526af785 3890
428870ff
BB
3891 *spapp = spa;
3892
34dc7c2f
BB
3893 return (0);
3894}
3895
428870ff
BB
3896int
3897spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3898 nvlist_t **config)
3899{
3900 return (spa_open_common(name, spapp, tag, policy, config));
3901}
3902
34dc7c2f
BB
3903int
3904spa_open(const char *name, spa_t **spapp, void *tag)
3905{
428870ff 3906 return (spa_open_common(name, spapp, tag, NULL, NULL));
34dc7c2f
BB
3907}
3908
3909/*
3910 * Lookup the given spa_t, incrementing the inject count in the process,
3911 * preventing it from being exported or destroyed.
3912 */
3913spa_t *
3914spa_inject_addref(char *name)
3915{
3916 spa_t *spa;
3917
3918 mutex_enter(&spa_namespace_lock);
3919 if ((spa = spa_lookup(name)) == NULL) {
3920 mutex_exit(&spa_namespace_lock);
3921 return (NULL);
3922 }
3923 spa->spa_inject_ref++;
3924 mutex_exit(&spa_namespace_lock);
3925
3926 return (spa);
3927}
3928
3929void
3930spa_inject_delref(spa_t *spa)
3931{
3932 mutex_enter(&spa_namespace_lock);
3933 spa->spa_inject_ref--;
3934 mutex_exit(&spa_namespace_lock);
3935}
3936
3937/*
3938 * Add spares device information to the nvlist.
3939 */
3940static void
3941spa_add_spares(spa_t *spa, nvlist_t *config)
3942{
3943 nvlist_t **spares;
3944 uint_t i, nspares;
3945 nvlist_t *nvroot;
3946 uint64_t guid;
3947 vdev_stat_t *vs;
3948 uint_t vsc;
3949 uint64_t pool;
3950
9babb374
BB
3951 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3952
34dc7c2f
BB
3953 if (spa->spa_spares.sav_count == 0)
3954 return;
3955
3956 VERIFY(nvlist_lookup_nvlist(config,
3957 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3958 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3959 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3960 if (nspares != 0) {
3961 VERIFY(nvlist_add_nvlist_array(nvroot,
3962 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3963 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3964 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3965
3966 /*
3967 * Go through and find any spares which have since been
3968 * repurposed as an active spare. If this is the case, update
3969 * their status appropriately.
3970 */
3971 for (i = 0; i < nspares; i++) {
3972 VERIFY(nvlist_lookup_uint64(spares[i],
3973 ZPOOL_CONFIG_GUID, &guid) == 0);
b128c09f
BB
3974 if (spa_spare_exists(guid, &pool, NULL) &&
3975 pool != 0ULL) {
34dc7c2f 3976 VERIFY(nvlist_lookup_uint64_array(
428870ff 3977 spares[i], ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
3978 (uint64_t **)&vs, &vsc) == 0);
3979 vs->vs_state = VDEV_STATE_CANT_OPEN;
3980 vs->vs_aux = VDEV_AUX_SPARED;
3981 }
3982 }
3983 }
3984}
3985
3986/*
3987 * Add l2cache device information to the nvlist, including vdev stats.
3988 */
3989static void
3990spa_add_l2cache(spa_t *spa, nvlist_t *config)
3991{
3992 nvlist_t **l2cache;
3993 uint_t i, j, nl2cache;
3994 nvlist_t *nvroot;
3995 uint64_t guid;
3996 vdev_t *vd;
3997 vdev_stat_t *vs;
3998 uint_t vsc;
3999
9babb374
BB
4000 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
4001
34dc7c2f
BB
4002 if (spa->spa_l2cache.sav_count == 0)
4003 return;
4004
34dc7c2f
BB
4005 VERIFY(nvlist_lookup_nvlist(config,
4006 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4007 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4008 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
4009 if (nl2cache != 0) {
4010 VERIFY(nvlist_add_nvlist_array(nvroot,
4011 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4012 VERIFY(nvlist_lookup_nvlist_array(nvroot,
4013 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
4014
4015 /*
4016 * Update level 2 cache device stats.
4017 */
4018
4019 for (i = 0; i < nl2cache; i++) {
4020 VERIFY(nvlist_lookup_uint64(l2cache[i],
4021 ZPOOL_CONFIG_GUID, &guid) == 0);
4022
4023 vd = NULL;
4024 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
4025 if (guid ==
4026 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
4027 vd = spa->spa_l2cache.sav_vdevs[j];
4028 break;
4029 }
4030 }
4031 ASSERT(vd != NULL);
4032
4033 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
428870ff
BB
4034 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
4035 == 0);
34dc7c2f 4036 vdev_get_stats(vd, vs);
193a37cb
TH
4037 vdev_config_generate_stats(vd, l2cache[i]);
4038
34dc7c2f
BB
4039 }
4040 }
34dc7c2f
BB
4041}
4042
9ae529ec 4043static void
417104bd 4044spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features)
9ae529ec 4045{
9ae529ec
CS
4046 zap_cursor_t zc;
4047 zap_attribute_t za;
4048
9ae529ec
CS
4049 if (spa->spa_feat_for_read_obj != 0) {
4050 for (zap_cursor_init(&zc, spa->spa_meta_objset,
4051 spa->spa_feat_for_read_obj);
4052 zap_cursor_retrieve(&zc, &za) == 0;
4053 zap_cursor_advance(&zc)) {
4054 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
4055 za.za_num_integers == 1);
417104bd 4056 VERIFY0(nvlist_add_uint64(features, za.za_name,
9ae529ec
CS
4057 za.za_first_integer));
4058 }
4059 zap_cursor_fini(&zc);
4060 }
4061
4062 if (spa->spa_feat_for_write_obj != 0) {
4063 for (zap_cursor_init(&zc, spa->spa_meta_objset,
4064 spa->spa_feat_for_write_obj);
4065 zap_cursor_retrieve(&zc, &za) == 0;
4066 zap_cursor_advance(&zc)) {
4067 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
4068 za.za_num_integers == 1);
417104bd 4069 VERIFY0(nvlist_add_uint64(features, za.za_name,
9ae529ec
CS
4070 za.za_first_integer));
4071 }
4072 zap_cursor_fini(&zc);
4073 }
417104bd
NB
4074}
4075
4076static void
4077spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features)
4078{
4079 int i;
4080
4081 for (i = 0; i < SPA_FEATURES; i++) {
4082 zfeature_info_t feature = spa_feature_table[i];
4083 uint64_t refcount;
4084
4085 if (feature_get_refcount(spa, &feature, &refcount) != 0)
4086 continue;
4087
4088 VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount));
4089 }
4090}
4091
4092/*
4093 * Store a list of pool features and their reference counts in the
4094 * config.
4095 *
4096 * The first time this is called on a spa, allocate a new nvlist, fetch
4097 * the pool features and reference counts from disk, then save the list
4098 * in the spa. In subsequent calls on the same spa use the saved nvlist
4099 * and refresh its values from the cached reference counts. This
4100 * ensures we don't block here on I/O on a suspended pool so 'zpool
4101 * clear' can resume the pool.
4102 */
4103static void
4104spa_add_feature_stats(spa_t *spa, nvlist_t *config)
4105{
4eb30c68 4106 nvlist_t *features;
417104bd
NB
4107
4108 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
4109
4eb30c68
NB
4110 mutex_enter(&spa->spa_feat_stats_lock);
4111 features = spa->spa_feat_stats;
4112
417104bd
NB
4113 if (features != NULL) {
4114 spa_feature_stats_from_cache(spa, features);
4115 } else {
4116 VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP));
4117 spa->spa_feat_stats = features;
4118 spa_feature_stats_from_disk(spa, features);
4119 }
9ae529ec 4120
417104bd
NB
4121 VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
4122 features));
4eb30c68
NB
4123
4124 mutex_exit(&spa->spa_feat_stats_lock);
9ae529ec
CS
4125}
4126
34dc7c2f 4127int
9ae529ec
CS
4128spa_get_stats(const char *name, nvlist_t **config,
4129 char *altroot, size_t buflen)
34dc7c2f
BB
4130{
4131 int error;
4132 spa_t *spa;
4133
4134 *config = NULL;
428870ff 4135 error = spa_open_common(name, &spa, FTAG, NULL, config);
34dc7c2f 4136
9babb374
BB
4137 if (spa != NULL) {
4138 /*
4139 * This still leaves a window of inconsistency where the spares
4140 * or l2cache devices could change and the config would be
4141 * self-inconsistent.
4142 */
4143 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f 4144
9babb374 4145 if (*config != NULL) {
572e2857
BB
4146 uint64_t loadtimes[2];
4147
4148 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
4149 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
4150 VERIFY(nvlist_add_uint64_array(*config,
4151 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
4152
b128c09f 4153 VERIFY(nvlist_add_uint64(*config,
9babb374
BB
4154 ZPOOL_CONFIG_ERRCOUNT,
4155 spa_get_errlog_size(spa)) == 0);
4156
cec3a0a1 4157 if (spa_suspended(spa)) {
9babb374
BB
4158 VERIFY(nvlist_add_uint64(*config,
4159 ZPOOL_CONFIG_SUSPENDED,
4160 spa->spa_failmode) == 0);
cec3a0a1
OF
4161 VERIFY(nvlist_add_uint64(*config,
4162 ZPOOL_CONFIG_SUSPENDED_REASON,
4163 spa->spa_suspended) == 0);
4164 }
b128c09f 4165
9babb374
BB
4166 spa_add_spares(spa, *config);
4167 spa_add_l2cache(spa, *config);
9ae529ec 4168 spa_add_feature_stats(spa, *config);
9babb374 4169 }
34dc7c2f
BB
4170 }
4171
4172 /*
4173 * We want to get the alternate root even for faulted pools, so we cheat
4174 * and call spa_lookup() directly.
4175 */
4176 if (altroot) {
4177 if (spa == NULL) {
4178 mutex_enter(&spa_namespace_lock);
4179 spa = spa_lookup(name);
4180 if (spa)
4181 spa_altroot(spa, altroot, buflen);
4182 else
4183 altroot[0] = '\0';
4184 spa = NULL;
4185 mutex_exit(&spa_namespace_lock);
4186 } else {
4187 spa_altroot(spa, altroot, buflen);
4188 }
4189 }
4190
9babb374
BB
4191 if (spa != NULL) {
4192 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 4193 spa_close(spa, FTAG);
9babb374 4194 }
34dc7c2f
BB
4195
4196 return (error);
4197}
4198
4199/*
4200 * Validate that the auxiliary device array is well formed. We must have an
4201 * array of nvlists, each which describes a valid leaf vdev. If this is an
4202 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
4203 * specified, as long as they are well-formed.
4204 */
4205static int
4206spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
4207 spa_aux_vdev_t *sav, const char *config, uint64_t version,
4208 vdev_labeltype_t label)
4209{
4210 nvlist_t **dev;
4211 uint_t i, ndev;
4212 vdev_t *vd;
4213 int error;
4214
b128c09f
BB
4215 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4216
34dc7c2f
BB
4217 /*
4218 * It's acceptable to have no devs specified.
4219 */
4220 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
4221 return (0);
4222
4223 if (ndev == 0)
2e528b49 4224 return (SET_ERROR(EINVAL));
34dc7c2f
BB
4225
4226 /*
4227 * Make sure the pool is formatted with a version that supports this
4228 * device type.
4229 */
4230 if (spa_version(spa) < version)
2e528b49 4231 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
4232
4233 /*
4234 * Set the pending device list so we correctly handle device in-use
4235 * checking.
4236 */
4237 sav->sav_pending = dev;
4238 sav->sav_npending = ndev;
4239
4240 for (i = 0; i < ndev; i++) {
4241 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
4242 mode)) != 0)
4243 goto out;
4244
4245 if (!vd->vdev_ops->vdev_op_leaf) {
4246 vdev_free(vd);
2e528b49 4247 error = SET_ERROR(EINVAL);
34dc7c2f
BB
4248 goto out;
4249 }
4250
34dc7c2f
BB
4251 vd->vdev_top = vd;
4252
4253 if ((error = vdev_open(vd)) == 0 &&
4254 (error = vdev_label_init(vd, crtxg, label)) == 0) {
4255 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
4256 vd->vdev_guid) == 0);
4257 }
4258
4259 vdev_free(vd);
4260
4261 if (error &&
4262 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
4263 goto out;
4264 else
4265 error = 0;
4266 }
4267
4268out:
4269 sav->sav_pending = NULL;
4270 sav->sav_npending = 0;
4271 return (error);
4272}
4273
4274static int
4275spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
4276{
4277 int error;
4278
b128c09f
BB
4279 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4280
34dc7c2f
BB
4281 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
4282 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
4283 VDEV_LABEL_SPARE)) != 0) {
4284 return (error);
4285 }
4286
4287 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
4288 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
4289 VDEV_LABEL_L2CACHE));
4290}
4291
4292static void
4293spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
4294 const char *config)
4295{
4296 int i;
4297
4298 if (sav->sav_config != NULL) {
4299 nvlist_t **olddevs;
4300 uint_t oldndevs;
4301 nvlist_t **newdevs;
4302
4303 /*
4e33ba4c 4304 * Generate new dev list by concatenating with the
34dc7c2f
BB
4305 * current dev list.
4306 */
4307 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
4308 &olddevs, &oldndevs) == 0);
4309
4310 newdevs = kmem_alloc(sizeof (void *) *
79c76d5b 4311 (ndevs + oldndevs), KM_SLEEP);
34dc7c2f
BB
4312 for (i = 0; i < oldndevs; i++)
4313 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
79c76d5b 4314 KM_SLEEP) == 0);
34dc7c2f
BB
4315 for (i = 0; i < ndevs; i++)
4316 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
79c76d5b 4317 KM_SLEEP) == 0);
34dc7c2f
BB
4318
4319 VERIFY(nvlist_remove(sav->sav_config, config,
4320 DATA_TYPE_NVLIST_ARRAY) == 0);
4321
4322 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
4323 config, newdevs, ndevs + oldndevs) == 0);
4324 for (i = 0; i < oldndevs + ndevs; i++)
4325 nvlist_free(newdevs[i]);
4326 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
4327 } else {
4328 /*
4329 * Generate a new dev list.
4330 */
4331 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
79c76d5b 4332 KM_SLEEP) == 0);
34dc7c2f
BB
4333 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
4334 devs, ndevs) == 0);
4335 }
4336}
4337
4338/*
4339 * Stop and drop level 2 ARC devices
4340 */
4341void
4342spa_l2cache_drop(spa_t *spa)
4343{
4344 vdev_t *vd;
4345 int i;
4346 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4347
4348 for (i = 0; i < sav->sav_count; i++) {
4349 uint64_t pool;
4350
4351 vd = sav->sav_vdevs[i];
4352 ASSERT(vd != NULL);
4353
fb5f0bc8
BB
4354 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
4355 pool != 0ULL && l2arc_vdev_present(vd))
34dc7c2f 4356 l2arc_remove_vdev(vd);
34dc7c2f
BB
4357 }
4358}
4359
b5256303
TC
4360/*
4361 * Verify encryption parameters for spa creation. If we are encrypting, we must
4362 * have the encryption feature flag enabled.
4363 */
4364static int
4365spa_create_check_encryption_params(dsl_crypto_params_t *dcp,
4366 boolean_t has_encryption)
4367{
4368 if (dcp->cp_crypt != ZIO_CRYPT_OFF &&
4369 dcp->cp_crypt != ZIO_CRYPT_INHERIT &&
4370 !has_encryption)
4371 return (SET_ERROR(ENOTSUP));
4372
4373 return (dmu_objset_create_crypt_check(NULL, dcp));
4374}
4375
34dc7c2f
BB
4376/*
4377 * Pool Creation
4378 */
4379int
4380spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
b5256303 4381 nvlist_t *zplprops, dsl_crypto_params_t *dcp)
34dc7c2f
BB
4382{
4383 spa_t *spa;
4384 char *altroot = NULL;
4385 vdev_t *rvd;
4386 dsl_pool_t *dp;
4387 dmu_tx_t *tx;
9babb374 4388 int error = 0;
34dc7c2f
BB
4389 uint64_t txg = TXG_INITIAL;
4390 nvlist_t **spares, **l2cache;
4391 uint_t nspares, nl2cache;
b5256303 4392 uint64_t version, obj, root_dsobj = 0;
9ae529ec 4393 boolean_t has_features;
b5256303
TC
4394 boolean_t has_encryption;
4395 spa_feature_t feat;
4396 char *feat_name;
83e9986f
RY
4397 char *poolname;
4398 nvlist_t *nvl;
4399
4400 if (nvlist_lookup_string(props, "tname", &poolname) != 0)
4401 poolname = (char *)pool;
34dc7c2f
BB
4402
4403 /*
4404 * If this pool already exists, return failure.
4405 */
4406 mutex_enter(&spa_namespace_lock);
83e9986f 4407 if (spa_lookup(poolname) != NULL) {
34dc7c2f 4408 mutex_exit(&spa_namespace_lock);
2e528b49 4409 return (SET_ERROR(EEXIST));
34dc7c2f
BB
4410 }
4411
4412 /*
4413 * Allocate a new spa_t structure.
4414 */
83e9986f
RY
4415 nvl = fnvlist_alloc();
4416 fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool);
34dc7c2f
BB
4417 (void) nvlist_lookup_string(props,
4418 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
83e9986f
RY
4419 spa = spa_add(poolname, nvl, altroot);
4420 fnvlist_free(nvl);
fb5f0bc8 4421 spa_activate(spa, spa_mode_global);
34dc7c2f 4422
34dc7c2f 4423 if (props && (error = spa_prop_validate(spa, props))) {
34dc7c2f
BB
4424 spa_deactivate(spa);
4425 spa_remove(spa);
b128c09f 4426 mutex_exit(&spa_namespace_lock);
34dc7c2f
BB
4427 return (error);
4428 }
4429
83e9986f
RY
4430 /*
4431 * Temporary pool names should never be written to disk.
4432 */
4433 if (poolname != pool)
4434 spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME;
4435
9ae529ec 4436 has_features = B_FALSE;
b5256303 4437 has_encryption = B_FALSE;
1c27024e 4438 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
9ae529ec 4439 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
b5256303 4440 if (zpool_prop_feature(nvpair_name(elem))) {
9ae529ec 4441 has_features = B_TRUE;
b5256303
TC
4442
4443 feat_name = strchr(nvpair_name(elem), '@') + 1;
4444 VERIFY0(zfeature_lookup_name(feat_name, &feat));
4445 if (feat == SPA_FEATURE_ENCRYPTION)
4446 has_encryption = B_TRUE;
4447 }
4448 }
4449
4450 /* verify encryption params, if they were provided */
4451 if (dcp != NULL) {
4452 error = spa_create_check_encryption_params(dcp, has_encryption);
4453 if (error != 0) {
4454 spa_deactivate(spa);
4455 spa_remove(spa);
4456 mutex_exit(&spa_namespace_lock);
4457 return (error);
4458 }
9ae529ec
CS
4459 }
4460
4461 if (has_features || nvlist_lookup_uint64(props,
4462 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
34dc7c2f 4463 version = SPA_VERSION;
9ae529ec
CS
4464 }
4465 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
428870ff
BB
4466
4467 spa->spa_first_txg = txg;
4468 spa->spa_uberblock.ub_txg = txg - 1;
34dc7c2f
BB
4469 spa->spa_uberblock.ub_version = version;
4470 spa->spa_ubsync = spa->spa_uberblock;
3dfb57a3 4471 spa->spa_load_state = SPA_LOAD_CREATE;
a1d477c2
MA
4472 spa->spa_removing_phys.sr_state = DSS_NONE;
4473 spa->spa_removing_phys.sr_removing_vdev = -1;
4474 spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
34dc7c2f 4475
9babb374
BB
4476 /*
4477 * Create "The Godfather" zio to hold all async IOs
4478 */
e022864d
MA
4479 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
4480 KM_SLEEP);
1c27024e 4481 for (int i = 0; i < max_ncpus; i++) {
e022864d
MA
4482 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
4483 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
4484 ZIO_FLAG_GODFATHER);
4485 }
9babb374 4486
34dc7c2f
BB
4487 /*
4488 * Create the root vdev.
4489 */
b128c09f 4490 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
4491
4492 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
4493
4494 ASSERT(error != 0 || rvd != NULL);
4495 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
4496
4497 if (error == 0 && !zfs_allocatable_devs(nvroot))
2e528b49 4498 error = SET_ERROR(EINVAL);
34dc7c2f
BB
4499
4500 if (error == 0 &&
4501 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
4502 (error = spa_validate_aux(spa, nvroot, txg,
4503 VDEV_ALLOC_ADD)) == 0) {
1c27024e 4504 for (int c = 0; c < rvd->vdev_children; c++) {
9babb374
BB
4505 vdev_metaslab_set_size(rvd->vdev_child[c]);
4506 vdev_expand(rvd->vdev_child[c], txg);
4507 }
34dc7c2f
BB
4508 }
4509
b128c09f 4510 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4511
4512 if (error != 0) {
4513 spa_unload(spa);
4514 spa_deactivate(spa);
4515 spa_remove(spa);
4516 mutex_exit(&spa_namespace_lock);
4517 return (error);
4518 }
4519
4520 /*
4521 * Get the list of spares, if specified.
4522 */
4523 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4524 &spares, &nspares) == 0) {
4525 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
79c76d5b 4526 KM_SLEEP) == 0);
34dc7c2f
BB
4527 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4528 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
b128c09f 4529 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4530 spa_load_spares(spa);
b128c09f 4531 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4532 spa->spa_spares.sav_sync = B_TRUE;
4533 }
4534
4535 /*
4536 * Get the list of level 2 cache devices, if specified.
4537 */
4538 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4539 &l2cache, &nl2cache) == 0) {
4540 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
79c76d5b 4541 NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
4542 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4543 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
b128c09f 4544 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4545 spa_load_l2cache(spa);
b128c09f 4546 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4547 spa->spa_l2cache.sav_sync = B_TRUE;
4548 }
4549
9ae529ec 4550 spa->spa_is_initializing = B_TRUE;
b5256303 4551 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg);
9ae529ec 4552 spa->spa_is_initializing = B_FALSE;
34dc7c2f 4553
428870ff
BB
4554 /*
4555 * Create DDTs (dedup tables).
4556 */
4557 ddt_create(spa);
4558
4559 spa_update_dspace(spa);
4560
34dc7c2f
BB
4561 tx = dmu_tx_create_assigned(dp, txg);
4562
d5e024cb
BB
4563 /*
4564 * Create the pool's history object.
4565 */
4566 if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history)
4567 spa_history_create_obj(spa, tx);
4568
4569 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE);
4570 spa_history_log_version(spa, "create", tx);
4571
34dc7c2f
BB
4572 /*
4573 * Create the pool config object.
4574 */
4575 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
b128c09f 4576 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
34dc7c2f
BB
4577 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
4578
4579 if (zap_add(spa->spa_meta_objset,
4580 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
4581 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
4582 cmn_err(CE_PANIC, "failed to add pool config");
4583 }
4584
428870ff
BB
4585 if (zap_add(spa->spa_meta_objset,
4586 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
4587 sizeof (uint64_t), 1, &version, tx) != 0) {
4588 cmn_err(CE_PANIC, "failed to add pool version");
4589 }
4590
34dc7c2f
BB
4591 /* Newly created pools with the right version are always deflated. */
4592 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
4593 spa->spa_deflate = TRUE;
4594 if (zap_add(spa->spa_meta_objset,
4595 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
4596 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
4597 cmn_err(CE_PANIC, "failed to add deflate");
4598 }
4599 }
4600
4601 /*
428870ff 4602 * Create the deferred-free bpobj. Turn off compression
34dc7c2f
BB
4603 * because sync-to-convergence takes longer if the blocksize
4604 * keeps changing.
4605 */
428870ff
BB
4606 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
4607 dmu_object_set_compress(spa->spa_meta_objset, obj,
34dc7c2f 4608 ZIO_COMPRESS_OFF, tx);
34dc7c2f 4609 if (zap_add(spa->spa_meta_objset,
428870ff
BB
4610 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
4611 sizeof (uint64_t), 1, &obj, tx) != 0) {
4612 cmn_err(CE_PANIC, "failed to add bpobj");
34dc7c2f 4613 }
428870ff
BB
4614 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
4615 spa->spa_meta_objset, obj));
34dc7c2f 4616
3c67d83a
TH
4617 /*
4618 * Generate some random noise for salted checksums to operate on.
4619 */
4620 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
4621 sizeof (spa->spa_cksum_salt.zcs_bytes));
4622
34dc7c2f
BB
4623 /*
4624 * Set pool properties.
4625 */
4626 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
4627 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
4628 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
9babb374 4629 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
379ca9cf 4630 spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST);
428870ff 4631
d164b209
BB
4632 if (props != NULL) {
4633 spa_configfile_set(spa, props, B_FALSE);
13fe0198 4634 spa_sync_props(props, tx);
d164b209 4635 }
34dc7c2f
BB
4636
4637 dmu_tx_commit(tx);
4638
b5256303
TC
4639 /*
4640 * If the root dataset is encrypted we will need to create key mappings
4641 * for the zio layer before we start to write any data to disk and hold
4642 * them until after the first txg has been synced. Waiting for the first
4643 * transaction to complete also ensures that our bean counters are
4644 * appropriately updated.
4645 */
4646 if (dp->dp_root_dir->dd_crypto_obj != 0) {
4647 root_dsobj = dsl_dir_phys(dp->dp_root_dir)->dd_head_dataset_obj;
4648 VERIFY0(spa_keystore_create_mapping_impl(spa, root_dsobj,
4649 dp->dp_root_dir, FTAG));
4650 }
4651
34dc7c2f 4652 spa->spa_sync_on = B_TRUE;
b5256303 4653 txg_sync_start(dp);
379ca9cf 4654 mmp_thread_start(spa);
b5256303 4655 txg_wait_synced(dp, txg);
34dc7c2f 4656
b5256303
TC
4657 if (dp->dp_root_dir->dd_crypto_obj != 0)
4658 VERIFY0(spa_keystore_remove_mapping(spa, root_dsobj, FTAG));
34dc7c2f 4659
9d5b5245
SD
4660 spa_spawn_aux_threads(spa);
4661
a1d477c2 4662 spa_write_cachefile(spa, B_FALSE, B_TRUE);
34dc7c2f 4663
0c66c32d
JG
4664 /*
4665 * Don't count references from objsets that are already closed
4666 * and are making their way through the eviction process.
4667 */
4668 spa_evicting_os_wait(spa);
b128c09f 4669 spa->spa_minref = refcount_count(&spa->spa_refcount);
3dfb57a3 4670 spa->spa_load_state = SPA_LOAD_NONE;
b128c09f 4671
d164b209
BB
4672 mutex_exit(&spa_namespace_lock);
4673
34dc7c2f
BB
4674 return (0);
4675}
4676
9babb374
BB
4677/*
4678 * Import a non-root pool into the system.
4679 */
4680int
13fe0198 4681spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
34dc7c2f
BB
4682{
4683 spa_t *spa;
4684 char *altroot = NULL;
428870ff
BB
4685 spa_load_state_t state = SPA_LOAD_IMPORT;
4686 zpool_rewind_policy_t policy;
572e2857
BB
4687 uint64_t mode = spa_mode_global;
4688 uint64_t readonly = B_FALSE;
9babb374 4689 int error;
34dc7c2f
BB
4690 nvlist_t *nvroot;
4691 nvlist_t **spares, **l2cache;
4692 uint_t nspares, nl2cache;
34dc7c2f
BB
4693
4694 /*
4695 * If a pool with this name exists, return failure.
4696 */
4697 mutex_enter(&spa_namespace_lock);
428870ff 4698 if (spa_lookup(pool) != NULL) {
9babb374 4699 mutex_exit(&spa_namespace_lock);
2e528b49 4700 return (SET_ERROR(EEXIST));
34dc7c2f
BB
4701 }
4702
4703 /*
4704 * Create and initialize the spa structure.
4705 */
4706 (void) nvlist_lookup_string(props,
4707 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
572e2857
BB
4708 (void) nvlist_lookup_uint64(props,
4709 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4710 if (readonly)
4711 mode = FREAD;
428870ff 4712 spa = spa_add(pool, config, altroot);
572e2857
BB
4713 spa->spa_import_flags = flags;
4714
4715 /*
4716 * Verbatim import - Take a pool and insert it into the namespace
4717 * as if it had been loaded at boot.
4718 */
4719 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4720 if (props != NULL)
4721 spa_configfile_set(spa, props, B_FALSE);
4722
a1d477c2 4723 spa_write_cachefile(spa, B_FALSE, B_TRUE);
12fa0466 4724 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
572e2857
BB
4725
4726 mutex_exit(&spa_namespace_lock);
572e2857
BB
4727 return (0);
4728 }
4729
4730 spa_activate(spa, mode);
34dc7c2f 4731
9babb374
BB
4732 /*
4733 * Don't start async tasks until we know everything is healthy.
4734 */
4735 spa_async_suspend(spa);
b128c09f 4736
572e2857
BB
4737 zpool_get_rewind_policy(config, &policy);
4738 if (policy.zrp_request & ZPOOL_DO_REWIND)
4739 state = SPA_LOAD_RECOVER;
4740
34dc7c2f 4741 /*
9eb7b46e 4742 * Pass off the heavy lifting to spa_load(). Pass TRUE for trust_config
9babb374 4743 * because the user-supplied config is actually the one to trust when
b128c09f 4744 * doing an import.
34dc7c2f 4745 */
428870ff
BB
4746 if (state != SPA_LOAD_RECOVER)
4747 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
572e2857 4748
428870ff
BB
4749 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4750 policy.zrp_request);
4751
4752 /*
572e2857
BB
4753 * Propagate anything learned while loading the pool and pass it
4754 * back to caller (i.e. rewind info, missing devices, etc).
428870ff 4755 */
572e2857
BB
4756 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4757 spa->spa_load_info) == 0);
34dc7c2f 4758
b128c09f 4759 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4760 /*
9babb374
BB
4761 * Toss any existing sparelist, as it doesn't have any validity
4762 * anymore, and conflicts with spa_has_spare().
34dc7c2f 4763 */
9babb374 4764 if (spa->spa_spares.sav_config) {
34dc7c2f
BB
4765 nvlist_free(spa->spa_spares.sav_config);
4766 spa->spa_spares.sav_config = NULL;
4767 spa_load_spares(spa);
4768 }
9babb374 4769 if (spa->spa_l2cache.sav_config) {
34dc7c2f
BB
4770 nvlist_free(spa->spa_l2cache.sav_config);
4771 spa->spa_l2cache.sav_config = NULL;
4772 spa_load_l2cache(spa);
4773 }
4774
4775 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4776 &nvroot) == 0);
b128c09f 4777 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 4778
d164b209
BB
4779 if (props != NULL)
4780 spa_configfile_set(spa, props, B_FALSE);
4781
fb5f0bc8
BB
4782 if (error != 0 || (props && spa_writeable(spa) &&
4783 (error = spa_prop_set(spa, props)))) {
9babb374
BB
4784 spa_unload(spa);
4785 spa_deactivate(spa);
4786 spa_remove(spa);
34dc7c2f
BB
4787 mutex_exit(&spa_namespace_lock);
4788 return (error);
4789 }
4790
572e2857
BB
4791 spa_async_resume(spa);
4792
34dc7c2f
BB
4793 /*
4794 * Override any spares and level 2 cache devices as specified by
4795 * the user, as these may have correct device names/devids, etc.
4796 */
4797 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4798 &spares, &nspares) == 0) {
4799 if (spa->spa_spares.sav_config)
4800 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4801 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4802 else
4803 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
79c76d5b 4804 NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
4805 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4806 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
b128c09f 4807 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4808 spa_load_spares(spa);
b128c09f 4809 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4810 spa->spa_spares.sav_sync = B_TRUE;
4811 }
4812 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4813 &l2cache, &nl2cache) == 0) {
4814 if (spa->spa_l2cache.sav_config)
4815 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4816 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4817 else
4818 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
79c76d5b 4819 NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
4820 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4821 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
b128c09f 4822 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4823 spa_load_l2cache(spa);
b128c09f 4824 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4825 spa->spa_l2cache.sav_sync = B_TRUE;
4826 }
4827
428870ff
BB
4828 /*
4829 * Check for any removed devices.
4830 */
4831 if (spa->spa_autoreplace) {
4832 spa_aux_check_removed(&spa->spa_spares);
4833 spa_aux_check_removed(&spa->spa_l2cache);
4834 }
4835
fb5f0bc8 4836 if (spa_writeable(spa)) {
b128c09f
BB
4837 /*
4838 * Update the config cache to include the newly-imported pool.
4839 */
45d1cae3 4840 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
b128c09f 4841 }
34dc7c2f 4842
34dc7c2f 4843 /*
9babb374
BB
4844 * It's possible that the pool was expanded while it was exported.
4845 * We kick off an async task to handle this for us.
34dc7c2f 4846 */
9babb374 4847 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
b128c09f 4848
d5e024cb 4849 spa_history_log_version(spa, "import", NULL);
fb390aaf 4850
12fa0466 4851 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
fb390aaf 4852
a0bd735a 4853 zvol_create_minors(spa, pool, B_TRUE);
526af785 4854
fb390aaf
HR
4855 mutex_exit(&spa_namespace_lock);
4856
b128c09f
BB
4857 return (0);
4858}
4859
34dc7c2f
BB
4860nvlist_t *
4861spa_tryimport(nvlist_t *tryconfig)
4862{
4863 nvlist_t *config = NULL;
4864 char *poolname;
4865 spa_t *spa;
4866 uint64_t state;
d164b209 4867 int error;
34dc7c2f
BB
4868
4869 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4870 return (NULL);
4871
4872 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4873 return (NULL);
4874
4875 /*
4876 * Create and initialize the spa structure.
4877 */
4878 mutex_enter(&spa_namespace_lock);
428870ff 4879 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
fb5f0bc8 4880 spa_activate(spa, FREAD);
34dc7c2f
BB
4881
4882 /*
4883 * Pass off the heavy lifting to spa_load().
9eb7b46e 4884 * Pass TRUE for trust_config because the user-supplied config
34dc7c2f
BB
4885 * is actually the one to trust when doing an import.
4886 */
428870ff 4887 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
34dc7c2f
BB
4888
4889 /*
4890 * If 'tryconfig' was at least parsable, return the current config.
4891 */
4892 if (spa->spa_root_vdev != NULL) {
34dc7c2f 4893 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
34dc7c2f
BB
4894 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4895 poolname) == 0);
4896 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4897 state) == 0);
4898 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4899 spa->spa_uberblock.ub_timestamp) == 0);
9ae529ec
CS
4900 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4901 spa->spa_load_info) == 0);
ffe9d382
BB
4902 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA,
4903 spa->spa_errata) == 0);
34dc7c2f
BB
4904
4905 /*
4906 * If the bootfs property exists on this pool then we
4907 * copy it out so that external consumers can tell which
4908 * pools are bootable.
4909 */
d164b209 4910 if ((!error || error == EEXIST) && spa->spa_bootfs) {
79c76d5b 4911 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
34dc7c2f
BB
4912
4913 /*
4914 * We have to play games with the name since the
4915 * pool was opened as TRYIMPORT_NAME.
4916 */
b128c09f 4917 if (dsl_dsobj_to_dsname(spa_name(spa),
34dc7c2f
BB
4918 spa->spa_bootfs, tmpname) == 0) {
4919 char *cp;
d1d7e268
MK
4920 char *dsname;
4921
79c76d5b 4922 dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
34dc7c2f
BB
4923
4924 cp = strchr(tmpname, '/');
4925 if (cp == NULL) {
4926 (void) strlcpy(dsname, tmpname,
4927 MAXPATHLEN);
4928 } else {
4929 (void) snprintf(dsname, MAXPATHLEN,
4930 "%s/%s", poolname, ++cp);
4931 }
4932 VERIFY(nvlist_add_string(config,
4933 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4934 kmem_free(dsname, MAXPATHLEN);
4935 }
4936 kmem_free(tmpname, MAXPATHLEN);
4937 }
4938
4939 /*
4940 * Add the list of hot spares and level 2 cache devices.
4941 */
9babb374 4942 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f
BB
4943 spa_add_spares(spa, config);
4944 spa_add_l2cache(spa, config);
9babb374 4945 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f
BB
4946 }
4947
4948 spa_unload(spa);
4949 spa_deactivate(spa);
4950 spa_remove(spa);
4951 mutex_exit(&spa_namespace_lock);
4952
4953 return (config);
4954}
4955
4956/*
4957 * Pool export/destroy
4958 *
4959 * The act of destroying or exporting a pool is very simple. We make sure there
4960 * is no more pending I/O and any references to the pool are gone. Then, we
4961 * update the pool state and sync all the labels to disk, removing the
fb5f0bc8
BB
4962 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4963 * we don't sync the labels or remove the configuration cache.
34dc7c2f
BB
4964 */
4965static int
b128c09f 4966spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
fb5f0bc8 4967 boolean_t force, boolean_t hardforce)
34dc7c2f
BB
4968{
4969 spa_t *spa;
4970
4971 if (oldconfig)
4972 *oldconfig = NULL;
4973
fb5f0bc8 4974 if (!(spa_mode_global & FWRITE))
2e528b49 4975 return (SET_ERROR(EROFS));
34dc7c2f
BB
4976
4977 mutex_enter(&spa_namespace_lock);
4978 if ((spa = spa_lookup(pool)) == NULL) {
4979 mutex_exit(&spa_namespace_lock);
2e528b49 4980 return (SET_ERROR(ENOENT));
34dc7c2f
BB
4981 }
4982
4983 /*
4984 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4985 * reacquire the namespace lock, and see if we can export.
4986 */
4987 spa_open_ref(spa, FTAG);
4988 mutex_exit(&spa_namespace_lock);
4989 spa_async_suspend(spa);
a0bd735a
BP
4990 if (spa->spa_zvol_taskq) {
4991 zvol_remove_minors(spa, spa_name(spa), B_TRUE);
4992 taskq_wait(spa->spa_zvol_taskq);
4993 }
34dc7c2f
BB
4994 mutex_enter(&spa_namespace_lock);
4995 spa_close(spa, FTAG);
4996
d14cfd83
IH
4997 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
4998 goto export_spa;
34dc7c2f 4999 /*
d14cfd83
IH
5000 * The pool will be in core if it's openable, in which case we can
5001 * modify its state. Objsets may be open only because they're dirty,
5002 * so we have to force it to sync before checking spa_refcnt.
34dc7c2f 5003 */
0c66c32d 5004 if (spa->spa_sync_on) {
34dc7c2f 5005 txg_wait_synced(spa->spa_dsl_pool, 0);
0c66c32d
JG
5006 spa_evicting_os_wait(spa);
5007 }
34dc7c2f 5008
d14cfd83
IH
5009 /*
5010 * A pool cannot be exported or destroyed if there are active
5011 * references. If we are resetting a pool, allow references by
5012 * fault injection handlers.
5013 */
5014 if (!spa_refcount_zero(spa) ||
5015 (spa->spa_inject_ref != 0 &&
5016 new_state != POOL_STATE_UNINITIALIZED)) {
5017 spa_async_resume(spa);
5018 mutex_exit(&spa_namespace_lock);
5019 return (SET_ERROR(EBUSY));
5020 }
34dc7c2f 5021
d14cfd83 5022 if (spa->spa_sync_on) {
b128c09f
BB
5023 /*
5024 * A pool cannot be exported if it has an active shared spare.
5025 * This is to prevent other pools stealing the active spare
5026 * from an exported pool. At user's own will, such pool can
5027 * be forcedly exported.
5028 */
5029 if (!force && new_state == POOL_STATE_EXPORTED &&
5030 spa_has_active_shared_spare(spa)) {
5031 spa_async_resume(spa);
5032 mutex_exit(&spa_namespace_lock);
2e528b49 5033 return (SET_ERROR(EXDEV));
b128c09f 5034 }
34dc7c2f
BB
5035
5036 /*
5037 * We want this to be reflected on every label,
5038 * so mark them all dirty. spa_unload() will do the
5039 * final sync that pushes these changes out.
5040 */
fb5f0bc8 5041 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
b128c09f 5042 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 5043 spa->spa_state = new_state;
428870ff
BB
5044 spa->spa_final_txg = spa_last_synced_txg(spa) +
5045 TXG_DEFER_SIZE + 1;
34dc7c2f 5046 vdev_config_dirty(spa->spa_root_vdev);
b128c09f 5047 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
5048 }
5049 }
5050
d14cfd83 5051export_spa:
d5e024cb
BB
5052 if (new_state == POOL_STATE_DESTROYED)
5053 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY);
5054 else if (new_state == POOL_STATE_EXPORTED)
5055 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT);
34dc7c2f
BB
5056
5057 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5058 spa_unload(spa);
5059 spa_deactivate(spa);
5060 }
5061
5062 if (oldconfig && spa->spa_config)
5063 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
5064
5065 if (new_state != POOL_STATE_UNINITIALIZED) {
fb5f0bc8 5066 if (!hardforce)
a1d477c2 5067 spa_write_cachefile(spa, B_TRUE, B_TRUE);
34dc7c2f 5068 spa_remove(spa);
34dc7c2f
BB
5069 }
5070 mutex_exit(&spa_namespace_lock);
5071
5072 return (0);
5073}
5074
5075/*
5076 * Destroy a storage pool.
5077 */
5078int
5079spa_destroy(char *pool)
5080{
fb5f0bc8
BB
5081 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
5082 B_FALSE, B_FALSE));
34dc7c2f
BB
5083}
5084
5085/*
5086 * Export a storage pool.
5087 */
5088int
fb5f0bc8
BB
5089spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
5090 boolean_t hardforce)
34dc7c2f 5091{
fb5f0bc8
BB
5092 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
5093 force, hardforce));
34dc7c2f
BB
5094}
5095
5096/*
5097 * Similar to spa_export(), this unloads the spa_t without actually removing it
5098 * from the namespace in any way.
5099 */
5100int
5101spa_reset(char *pool)
5102{
b128c09f 5103 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
fb5f0bc8 5104 B_FALSE, B_FALSE));
34dc7c2f
BB
5105}
5106
34dc7c2f
BB
5107/*
5108 * ==========================================================================
5109 * Device manipulation
5110 * ==========================================================================
5111 */
5112
5113/*
5114 * Add a device to a storage pool.
5115 */
5116int
5117spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
5118{
428870ff 5119 uint64_t txg, id;
fb5f0bc8 5120 int error;
34dc7c2f
BB
5121 vdev_t *rvd = spa->spa_root_vdev;
5122 vdev_t *vd, *tvd;
5123 nvlist_t **spares, **l2cache;
5124 uint_t nspares, nl2cache;
5125
572e2857
BB
5126 ASSERT(spa_writeable(spa));
5127
34dc7c2f
BB
5128 txg = spa_vdev_enter(spa);
5129
5130 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
5131 VDEV_ALLOC_ADD)) != 0)
5132 return (spa_vdev_exit(spa, NULL, txg, error));
5133
b128c09f 5134 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
34dc7c2f
BB
5135
5136 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
5137 &nspares) != 0)
5138 nspares = 0;
5139
5140 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
5141 &nl2cache) != 0)
5142 nl2cache = 0;
5143
b128c09f 5144 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
34dc7c2f 5145 return (spa_vdev_exit(spa, vd, txg, EINVAL));
34dc7c2f 5146
b128c09f
BB
5147 if (vd->vdev_children != 0 &&
5148 (error = vdev_create(vd, txg, B_FALSE)) != 0)
5149 return (spa_vdev_exit(spa, vd, txg, error));
34dc7c2f
BB
5150
5151 /*
5152 * We must validate the spares and l2cache devices after checking the
5153 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
5154 */
b128c09f 5155 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
34dc7c2f 5156 return (spa_vdev_exit(spa, vd, txg, error));
34dc7c2f
BB
5157
5158 /*
a1d477c2
MA
5159 * If we are in the middle of a device removal, we can only add
5160 * devices which match the existing devices in the pool.
5161 * If we are in the middle of a removal, or have some indirect
5162 * vdevs, we can not add raidz toplevels.
34dc7c2f 5163 */
a1d477c2
MA
5164 if (spa->spa_vdev_removal != NULL ||
5165 spa->spa_removing_phys.sr_prev_indirect_vdev != -1) {
5166 for (int c = 0; c < vd->vdev_children; c++) {
5167 tvd = vd->vdev_child[c];
5168 if (spa->spa_vdev_removal != NULL &&
9e052db4 5169 tvd->vdev_ashift != spa->spa_max_ashift) {
a1d477c2
MA
5170 return (spa_vdev_exit(spa, vd, txg, EINVAL));
5171 }
5172 /* Fail if top level vdev is raidz */
5173 if (tvd->vdev_ops == &vdev_raidz_ops) {
5174 return (spa_vdev_exit(spa, vd, txg, EINVAL));
5175 }
5176 /*
5177 * Need the top level mirror to be
5178 * a mirror of leaf vdevs only
5179 */
5180 if (tvd->vdev_ops == &vdev_mirror_ops) {
5181 for (uint64_t cid = 0;
5182 cid < tvd->vdev_children; cid++) {
5183 vdev_t *cvd = tvd->vdev_child[cid];
5184 if (!cvd->vdev_ops->vdev_op_leaf) {
5185 return (spa_vdev_exit(spa, vd,
5186 txg, EINVAL));
5187 }
5188 }
5189 }
5190 }
5191 }
5192
1c27024e 5193 for (int c = 0; c < vd->vdev_children; c++) {
428870ff
BB
5194
5195 /*
5196 * Set the vdev id to the first hole, if one exists.
5197 */
5198 for (id = 0; id < rvd->vdev_children; id++) {
5199 if (rvd->vdev_child[id]->vdev_ishole) {
5200 vdev_free(rvd->vdev_child[id]);
5201 break;
5202 }
5203 }
34dc7c2f
BB
5204 tvd = vd->vdev_child[c];
5205 vdev_remove_child(vd, tvd);
428870ff 5206 tvd->vdev_id = id;
34dc7c2f
BB
5207 vdev_add_child(rvd, tvd);
5208 vdev_config_dirty(tvd);
5209 }
5210
5211 if (nspares != 0) {
5212 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
5213 ZPOOL_CONFIG_SPARES);
5214 spa_load_spares(spa);
5215 spa->spa_spares.sav_sync = B_TRUE;
5216 }
5217
5218 if (nl2cache != 0) {
5219 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
5220 ZPOOL_CONFIG_L2CACHE);
5221 spa_load_l2cache(spa);
5222 spa->spa_l2cache.sav_sync = B_TRUE;
5223 }
5224
5225 /*
5226 * We have to be careful when adding new vdevs to an existing pool.
5227 * If other threads start allocating from these vdevs before we
5228 * sync the config cache, and we lose power, then upon reboot we may
5229 * fail to open the pool because there are DVAs that the config cache
5230 * can't translate. Therefore, we first add the vdevs without
5231 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
5232 * and then let spa_config_update() initialize the new metaslabs.
5233 *
5234 * spa_load() checks for added-but-not-initialized vdevs, so that
5235 * if we lose power at any point in this sequence, the remaining
5236 * steps will be completed the next time we load the pool.
5237 */
5238 (void) spa_vdev_exit(spa, vd, txg, 0);
5239
5240 mutex_enter(&spa_namespace_lock);
5241 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
12fa0466 5242 spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD);
34dc7c2f
BB
5243 mutex_exit(&spa_namespace_lock);
5244
5245 return (0);
5246}
5247
5248/*
5249 * Attach a device to a mirror. The arguments are the path to any device
5250 * in the mirror, and the nvroot for the new device. If the path specifies
5251 * a device that is not mirrored, we automatically insert the mirror vdev.
5252 *
5253 * If 'replacing' is specified, the new device is intended to replace the
5254 * existing device; in this case the two devices are made into their own
5255 * mirror using the 'replacing' vdev, which is functionally identical to
5256 * the mirror vdev (it actually reuses all the same ops) but has a few
5257 * extra rules: you can't attach to it after it's been created, and upon
5258 * completion of resilvering, the first disk (the one being replaced)
5259 * is automatically detached.
5260 */
5261int
5262spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
5263{
428870ff 5264 uint64_t txg, dtl_max_txg;
1c27024e 5265 ASSERTV(vdev_t *rvd = spa->spa_root_vdev);
34dc7c2f
BB
5266 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
5267 vdev_ops_t *pvops;
b128c09f
BB
5268 char *oldvdpath, *newvdpath;
5269 int newvd_isspare;
5270 int error;
34dc7c2f 5271
572e2857
BB
5272 ASSERT(spa_writeable(spa));
5273
34dc7c2f
BB
5274 txg = spa_vdev_enter(spa);
5275
b128c09f 5276 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f 5277
9e052db4 5278 if (spa->spa_vdev_removal != NULL)
a1d477c2 5279 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
a1d477c2 5280
34dc7c2f
BB
5281 if (oldvd == NULL)
5282 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5283
5284 if (!oldvd->vdev_ops->vdev_op_leaf)
5285 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5286
5287 pvd = oldvd->vdev_parent;
5288
5289 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
5ffb9d1d 5290 VDEV_ALLOC_ATTACH)) != 0)
34dc7c2f
BB
5291 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5292
5293 if (newrootvd->vdev_children != 1)
5294 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
5295
5296 newvd = newrootvd->vdev_child[0];
5297
5298 if (!newvd->vdev_ops->vdev_op_leaf)
5299 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
5300
5301 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
5302 return (spa_vdev_exit(spa, newrootvd, txg, error));
5303
5304 /*
5305 * Spares can't replace logs
5306 */
b128c09f 5307 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
34dc7c2f
BB
5308 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
5309
5310 if (!replacing) {
5311 /*
5312 * For attach, the only allowable parent is a mirror or the root
5313 * vdev.
5314 */
5315 if (pvd->vdev_ops != &vdev_mirror_ops &&
5316 pvd->vdev_ops != &vdev_root_ops)
5317 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
5318
5319 pvops = &vdev_mirror_ops;
5320 } else {
5321 /*
5322 * Active hot spares can only be replaced by inactive hot
5323 * spares.
5324 */
5325 if (pvd->vdev_ops == &vdev_spare_ops &&
572e2857 5326 oldvd->vdev_isspare &&
34dc7c2f
BB
5327 !spa_has_spare(spa, newvd->vdev_guid))
5328 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
5329
5330 /*
5331 * If the source is a hot spare, and the parent isn't already a
5332 * spare, then we want to create a new hot spare. Otherwise, we
5333 * want to create a replacing vdev. The user is not allowed to
5334 * attach to a spared vdev child unless the 'isspare' state is
5335 * the same (spare replaces spare, non-spare replaces
5336 * non-spare).
5337 */
572e2857
BB
5338 if (pvd->vdev_ops == &vdev_replacing_ops &&
5339 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
34dc7c2f 5340 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
5341 } else if (pvd->vdev_ops == &vdev_spare_ops &&
5342 newvd->vdev_isspare != oldvd->vdev_isspare) {
34dc7c2f 5343 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
5344 }
5345
5346 if (newvd->vdev_isspare)
34dc7c2f
BB
5347 pvops = &vdev_spare_ops;
5348 else
5349 pvops = &vdev_replacing_ops;
5350 }
5351
5352 /*
9babb374 5353 * Make sure the new device is big enough.
34dc7c2f 5354 */
9babb374 5355 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
34dc7c2f
BB
5356 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
5357
5358 /*
5359 * The new device cannot have a higher alignment requirement
5360 * than the top-level vdev.
5361 */
5362 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
5363 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
5364
5365 /*
5366 * If this is an in-place replacement, update oldvd's path and devid
5367 * to make it distinguishable from newvd, and unopenable from now on.
5368 */
5369 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
5370 spa_strfree(oldvd->vdev_path);
5371 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
79c76d5b 5372 KM_SLEEP);
34dc7c2f
BB
5373 (void) sprintf(oldvd->vdev_path, "%s/%s",
5374 newvd->vdev_path, "old");
5375 if (oldvd->vdev_devid != NULL) {
5376 spa_strfree(oldvd->vdev_devid);
5377 oldvd->vdev_devid = NULL;
5378 }
5379 }
5380
572e2857 5381 /* mark the device being resilvered */
5d1f7fb6 5382 newvd->vdev_resilver_txg = txg;
572e2857 5383
34dc7c2f
BB
5384 /*
5385 * If the parent is not a mirror, or if we're replacing, insert the new
5386 * mirror/replacing/spare vdev above oldvd.
5387 */
5388 if (pvd->vdev_ops != pvops)
5389 pvd = vdev_add_parent(oldvd, pvops);
5390
5391 ASSERT(pvd->vdev_top->vdev_parent == rvd);
5392 ASSERT(pvd->vdev_ops == pvops);
5393 ASSERT(oldvd->vdev_parent == pvd);
5394
5395 /*
5396 * Extract the new device from its root and add it to pvd.
5397 */
5398 vdev_remove_child(newrootvd, newvd);
5399 newvd->vdev_id = pvd->vdev_children;
428870ff 5400 newvd->vdev_crtxg = oldvd->vdev_crtxg;
34dc7c2f
BB
5401 vdev_add_child(pvd, newvd);
5402
6d82f98c
IH
5403 /*
5404 * Reevaluate the parent vdev state.
5405 */
5406 vdev_propagate_state(pvd);
5407
34dc7c2f
BB
5408 tvd = newvd->vdev_top;
5409 ASSERT(pvd->vdev_top == tvd);
5410 ASSERT(tvd->vdev_parent == rvd);
5411
5412 vdev_config_dirty(tvd);
5413
5414 /*
428870ff
BB
5415 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
5416 * for any dmu_sync-ed blocks. It will propagate upward when
5417 * spa_vdev_exit() calls vdev_dtl_reassess().
34dc7c2f 5418 */
428870ff 5419 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
34dc7c2f 5420
428870ff
BB
5421 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
5422 dtl_max_txg - TXG_INITIAL);
34dc7c2f 5423
9babb374 5424 if (newvd->vdev_isspare) {
34dc7c2f 5425 spa_spare_activate(newvd);
12fa0466 5426 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE);
9babb374
BB
5427 }
5428
b128c09f
BB
5429 oldvdpath = spa_strdup(oldvd->vdev_path);
5430 newvdpath = spa_strdup(newvd->vdev_path);
5431 newvd_isspare = newvd->vdev_isspare;
34dc7c2f
BB
5432
5433 /*
5434 * Mark newvd's DTL dirty in this txg.
5435 */
5436 vdev_dirty(tvd, VDD_DTL, newvd, txg);
5437
428870ff 5438 /*
93cf2076
GW
5439 * Schedule the resilver to restart in the future. We do this to
5440 * ensure that dmu_sync-ed blocks have been stitched into the
5441 * respective datasets.
428870ff
BB
5442 */
5443 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
5444
fb390aaf 5445 if (spa->spa_bootfs)
12fa0466 5446 spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH);
fb390aaf 5447
12fa0466 5448 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH);
fb390aaf 5449
428870ff
BB
5450 /*
5451 * Commit the config
5452 */
5453 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
34dc7c2f 5454
6f1ffb06 5455 spa_history_log_internal(spa, "vdev attach", NULL,
428870ff 5456 "%s vdev=%s %s vdev=%s",
45d1cae3
BB
5457 replacing && newvd_isspare ? "spare in" :
5458 replacing ? "replace" : "attach", newvdpath,
5459 replacing ? "for" : "to", oldvdpath);
b128c09f
BB
5460
5461 spa_strfree(oldvdpath);
5462 spa_strfree(newvdpath);
5463
34dc7c2f
BB
5464 return (0);
5465}
5466
5467/*
5468 * Detach a device from a mirror or replacing vdev.
d3cc8b15 5469 *
34dc7c2f
BB
5470 * If 'replace_done' is specified, only detach if the parent
5471 * is a replacing vdev.
5472 */
5473int
fb5f0bc8 5474spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
34dc7c2f
BB
5475{
5476 uint64_t txg;
fb5f0bc8 5477 int error;
1c27024e 5478 ASSERTV(vdev_t *rvd = spa->spa_root_vdev);
34dc7c2f
BB
5479 vdev_t *vd, *pvd, *cvd, *tvd;
5480 boolean_t unspare = B_FALSE;
d4ed6673 5481 uint64_t unspare_guid = 0;
428870ff 5482 char *vdpath;
1c27024e 5483
572e2857
BB
5484 ASSERT(spa_writeable(spa));
5485
34dc7c2f
BB
5486 txg = spa_vdev_enter(spa);
5487
b128c09f 5488 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f
BB
5489
5490 if (vd == NULL)
5491 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5492
5493 if (!vd->vdev_ops->vdev_op_leaf)
5494 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5495
5496 pvd = vd->vdev_parent;
5497
fb5f0bc8
BB
5498 /*
5499 * If the parent/child relationship is not as expected, don't do it.
5500 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5501 * vdev that's replacing B with C. The user's intent in replacing
5502 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5503 * the replace by detaching C, the expected behavior is to end up
5504 * M(A,B). But suppose that right after deciding to detach C,
5505 * the replacement of B completes. We would have M(A,C), and then
5506 * ask to detach C, which would leave us with just A -- not what
5507 * the user wanted. To prevent this, we make sure that the
5508 * parent/child relationship hasn't changed -- in this example,
5509 * that C's parent is still the replacing vdev R.
5510 */
5511 if (pvd->vdev_guid != pguid && pguid != 0)
5512 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5513
34dc7c2f 5514 /*
572e2857 5515 * Only 'replacing' or 'spare' vdevs can be replaced.
34dc7c2f 5516 */
572e2857
BB
5517 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5518 pvd->vdev_ops != &vdev_spare_ops)
5519 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34dc7c2f
BB
5520
5521 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5522 spa_version(spa) >= SPA_VERSION_SPARES);
5523
5524 /*
5525 * Only mirror, replacing, and spare vdevs support detach.
5526 */
5527 if (pvd->vdev_ops != &vdev_replacing_ops &&
5528 pvd->vdev_ops != &vdev_mirror_ops &&
5529 pvd->vdev_ops != &vdev_spare_ops)
5530 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5531
5532 /*
fb5f0bc8
BB
5533 * If this device has the only valid copy of some data,
5534 * we cannot safely detach it.
34dc7c2f 5535 */
fb5f0bc8 5536 if (vdev_dtl_required(vd))
34dc7c2f
BB
5537 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5538
fb5f0bc8 5539 ASSERT(pvd->vdev_children >= 2);
34dc7c2f 5540
b128c09f
BB
5541 /*
5542 * If we are detaching the second disk from a replacing vdev, then
5543 * check to see if we changed the original vdev's path to have "/old"
5544 * at the end in spa_vdev_attach(). If so, undo that change now.
5545 */
572e2857
BB
5546 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5547 vd->vdev_path != NULL) {
5548 size_t len = strlen(vd->vdev_path);
5549
1c27024e 5550 for (int c = 0; c < pvd->vdev_children; c++) {
572e2857
BB
5551 cvd = pvd->vdev_child[c];
5552
5553 if (cvd == vd || cvd->vdev_path == NULL)
5554 continue;
5555
5556 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5557 strcmp(cvd->vdev_path + len, "/old") == 0) {
5558 spa_strfree(cvd->vdev_path);
5559 cvd->vdev_path = spa_strdup(vd->vdev_path);
5560 break;
5561 }
b128c09f
BB
5562 }
5563 }
5564
34dc7c2f
BB
5565 /*
5566 * If we are detaching the original disk from a spare, then it implies
5567 * that the spare should become a real disk, and be removed from the
5568 * active spare list for the pool.
5569 */
5570 if (pvd->vdev_ops == &vdev_spare_ops &&
572e2857
BB
5571 vd->vdev_id == 0 &&
5572 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
34dc7c2f
BB
5573 unspare = B_TRUE;
5574
5575 /*
5576 * Erase the disk labels so the disk can be used for other things.
5577 * This must be done after all other error cases are handled,
5578 * but before we disembowel vd (so we can still do I/O to it).
5579 * But if we can't do it, don't treat the error as fatal --
5580 * it may be that the unwritability of the disk is the reason
5581 * it's being detached!
5582 */
5583 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5584
5585 /*
5586 * Remove vd from its parent and compact the parent's children.
5587 */
5588 vdev_remove_child(pvd, vd);
5589 vdev_compact_children(pvd);
5590
5591 /*
5592 * Remember one of the remaining children so we can get tvd below.
5593 */
572e2857 5594 cvd = pvd->vdev_child[pvd->vdev_children - 1];
34dc7c2f
BB
5595
5596 /*
5597 * If we need to remove the remaining child from the list of hot spares,
fb5f0bc8
BB
5598 * do it now, marking the vdev as no longer a spare in the process.
5599 * We must do this before vdev_remove_parent(), because that can
5600 * change the GUID if it creates a new toplevel GUID. For a similar
5601 * reason, we must remove the spare now, in the same txg as the detach;
5602 * otherwise someone could attach a new sibling, change the GUID, and
5603 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
34dc7c2f
BB
5604 */
5605 if (unspare) {
5606 ASSERT(cvd->vdev_isspare);
5607 spa_spare_remove(cvd);
5608 unspare_guid = cvd->vdev_guid;
fb5f0bc8 5609 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
572e2857 5610 cvd->vdev_unspare = B_TRUE;
34dc7c2f
BB
5611 }
5612
428870ff
BB
5613 /*
5614 * If the parent mirror/replacing vdev only has one child,
5615 * the parent is no longer needed. Remove it from the tree.
5616 */
572e2857
BB
5617 if (pvd->vdev_children == 1) {
5618 if (pvd->vdev_ops == &vdev_spare_ops)
5619 cvd->vdev_unspare = B_FALSE;
428870ff 5620 vdev_remove_parent(cvd);
572e2857
BB
5621 }
5622
428870ff
BB
5623
5624 /*
5625 * We don't set tvd until now because the parent we just removed
5626 * may have been the previous top-level vdev.
5627 */
5628 tvd = cvd->vdev_top;
5629 ASSERT(tvd->vdev_parent == rvd);
5630
5631 /*
5632 * Reevaluate the parent vdev state.
5633 */
5634 vdev_propagate_state(cvd);
5635
5636 /*
5637 * If the 'autoexpand' property is set on the pool then automatically
5638 * try to expand the size of the pool. For example if the device we
5639 * just detached was smaller than the others, it may be possible to
5640 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5641 * first so that we can obtain the updated sizes of the leaf vdevs.
5642 */
5643 if (spa->spa_autoexpand) {
5644 vdev_reopen(tvd);
5645 vdev_expand(tvd, txg);
5646 }
5647
5648 vdev_config_dirty(tvd);
5649
5650 /*
5651 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5652 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5653 * But first make sure we're not on any *other* txg's DTL list, to
5654 * prevent vd from being accessed after it's freed.
5655 */
b6ca6193 5656 vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none");
1c27024e 5657 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
5658 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5659 vd->vdev_detached = B_TRUE;
5660 vdev_dirty(tvd, VDD_DTL, vd, txg);
5661
12fa0466 5662 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE);
428870ff 5663
572e2857
BB
5664 /* hang on to the spa before we release the lock */
5665 spa_open_ref(spa, FTAG);
5666
428870ff
BB
5667 error = spa_vdev_exit(spa, vd, txg, 0);
5668
6f1ffb06 5669 spa_history_log_internal(spa, "detach", NULL,
428870ff
BB
5670 "vdev=%s", vdpath);
5671 spa_strfree(vdpath);
5672
5673 /*
5674 * If this was the removal of the original device in a hot spare vdev,
5675 * then we want to go through and remove the device from the hot spare
5676 * list of every other pool.
5677 */
5678 if (unspare) {
572e2857
BB
5679 spa_t *altspa = NULL;
5680
428870ff 5681 mutex_enter(&spa_namespace_lock);
572e2857
BB
5682 while ((altspa = spa_next(altspa)) != NULL) {
5683 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5684 altspa == spa)
428870ff 5685 continue;
572e2857
BB
5686
5687 spa_open_ref(altspa, FTAG);
428870ff 5688 mutex_exit(&spa_namespace_lock);
572e2857 5689 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
428870ff 5690 mutex_enter(&spa_namespace_lock);
572e2857 5691 spa_close(altspa, FTAG);
428870ff
BB
5692 }
5693 mutex_exit(&spa_namespace_lock);
572e2857
BB
5694
5695 /* search the rest of the vdevs for spares to remove */
5696 spa_vdev_resilver_done(spa);
428870ff
BB
5697 }
5698
572e2857
BB
5699 /* all done with the spa; OK to release */
5700 mutex_enter(&spa_namespace_lock);
5701 spa_close(spa, FTAG);
5702 mutex_exit(&spa_namespace_lock);
5703
428870ff
BB
5704 return (error);
5705}
5706
5707/*
5708 * Split a set of devices from their mirrors, and create a new pool from them.
5709 */
5710int
5711spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5712 nvlist_t *props, boolean_t exp)
5713{
5714 int error = 0;
5715 uint64_t txg, *glist;
5716 spa_t *newspa;
5717 uint_t c, children, lastlog;
5718 nvlist_t **child, *nvl, *tmp;
5719 dmu_tx_t *tx;
5720 char *altroot = NULL;
5721 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5722 boolean_t activate_slog;
5723
572e2857 5724 ASSERT(spa_writeable(spa));
428870ff
BB
5725
5726 txg = spa_vdev_enter(spa);
5727
5728 /* clear the log and flush everything up to now */
5729 activate_slog = spa_passivate_log(spa);
5730 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
a1d477c2 5731 error = spa_reset_logs(spa);
428870ff
BB
5732 txg = spa_vdev_config_enter(spa);
5733
5734 if (activate_slog)
5735 spa_activate_log(spa);
5736
5737 if (error != 0)
5738 return (spa_vdev_exit(spa, NULL, txg, error));
5739
5740 /* check new spa name before going any further */
5741 if (spa_lookup(newname) != NULL)
5742 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5743
5744 /*
5745 * scan through all the children to ensure they're all mirrors
5746 */
5747 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5748 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5749 &children) != 0)
5750 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5751
5752 /* first, check to ensure we've got the right child count */
5753 rvd = spa->spa_root_vdev;
5754 lastlog = 0;
5755 for (c = 0; c < rvd->vdev_children; c++) {
5756 vdev_t *vd = rvd->vdev_child[c];
5757
5758 /* don't count the holes & logs as children */
a1d477c2 5759 if (vd->vdev_islog || !vdev_is_concrete(vd)) {
428870ff
BB
5760 if (lastlog == 0)
5761 lastlog = c;
5762 continue;
5763 }
5764
5765 lastlog = 0;
5766 }
5767 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5768 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5769
5770 /* next, ensure no spare or cache devices are part of the split */
5771 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5772 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5773 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5774
79c76d5b
BB
5775 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5776 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
428870ff
BB
5777
5778 /* then, loop over each vdev and validate it */
5779 for (c = 0; c < children; c++) {
5780 uint64_t is_hole = 0;
5781
5782 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5783 &is_hole);
5784
5785 if (is_hole != 0) {
5786 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5787 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5788 continue;
5789 } else {
2e528b49 5790 error = SET_ERROR(EINVAL);
428870ff
BB
5791 break;
5792 }
5793 }
5794
5795 /* which disk is going to be split? */
5796 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5797 &glist[c]) != 0) {
2e528b49 5798 error = SET_ERROR(EINVAL);
428870ff
BB
5799 break;
5800 }
5801
5802 /* look it up in the spa */
5803 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5804 if (vml[c] == NULL) {
2e528b49 5805 error = SET_ERROR(ENODEV);
428870ff
BB
5806 break;
5807 }
5808
5809 /* make sure there's nothing stopping the split */
5810 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5811 vml[c]->vdev_islog ||
a1d477c2 5812 !vdev_is_concrete(vml[c]) ||
428870ff
BB
5813 vml[c]->vdev_isspare ||
5814 vml[c]->vdev_isl2cache ||
5815 !vdev_writeable(vml[c]) ||
5816 vml[c]->vdev_children != 0 ||
5817 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5818 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
2e528b49 5819 error = SET_ERROR(EINVAL);
428870ff
BB
5820 break;
5821 }
5822
5823 if (vdev_dtl_required(vml[c])) {
2e528b49 5824 error = SET_ERROR(EBUSY);
428870ff
BB
5825 break;
5826 }
5827
5828 /* we need certain info from the top level */
5829 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5830 vml[c]->vdev_top->vdev_ms_array) == 0);
5831 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5832 vml[c]->vdev_top->vdev_ms_shift) == 0);
5833 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5834 vml[c]->vdev_top->vdev_asize) == 0);
5835 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5836 vml[c]->vdev_top->vdev_ashift) == 0);
e0ab3ab5
JS
5837
5838 /* transfer per-vdev ZAPs */
5839 ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
5840 VERIFY0(nvlist_add_uint64(child[c],
5841 ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
5842
5843 ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
5844 VERIFY0(nvlist_add_uint64(child[c],
5845 ZPOOL_CONFIG_VDEV_TOP_ZAP,
5846 vml[c]->vdev_parent->vdev_top_zap));
428870ff
BB
5847 }
5848
5849 if (error != 0) {
5850 kmem_free(vml, children * sizeof (vdev_t *));
5851 kmem_free(glist, children * sizeof (uint64_t));
5852 return (spa_vdev_exit(spa, NULL, txg, error));
5853 }
5854
5855 /* stop writers from using the disks */
5856 for (c = 0; c < children; c++) {
5857 if (vml[c] != NULL)
5858 vml[c]->vdev_offline = B_TRUE;
5859 }
5860 vdev_reopen(spa->spa_root_vdev);
34dc7c2f
BB
5861
5862 /*
428870ff
BB
5863 * Temporarily record the splitting vdevs in the spa config. This
5864 * will disappear once the config is regenerated.
34dc7c2f 5865 */
79c76d5b 5866 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
428870ff
BB
5867 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5868 glist, children) == 0);
5869 kmem_free(glist, children * sizeof (uint64_t));
34dc7c2f 5870
428870ff
BB
5871 mutex_enter(&spa->spa_props_lock);
5872 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5873 nvl) == 0);
5874 mutex_exit(&spa->spa_props_lock);
5875 spa->spa_config_splitting = nvl;
5876 vdev_config_dirty(spa->spa_root_vdev);
5877
5878 /* configure and create the new pool */
5879 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5880 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5881 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5882 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5883 spa_version(spa)) == 0);
5884 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5885 spa->spa_config_txg) == 0);
5886 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5887 spa_generate_guid(NULL)) == 0);
e0ab3ab5 5888 VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
428870ff
BB
5889 (void) nvlist_lookup_string(props,
5890 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
34dc7c2f 5891
428870ff
BB
5892 /* add the new pool to the namespace */
5893 newspa = spa_add(newname, config, altroot);
e0ab3ab5 5894 newspa->spa_avz_action = AVZ_ACTION_REBUILD;
428870ff
BB
5895 newspa->spa_config_txg = spa->spa_config_txg;
5896 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5897
5898 /* release the spa config lock, retaining the namespace lock */
5899 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5900
5901 if (zio_injection_enabled)
5902 zio_handle_panic_injection(spa, FTAG, 1);
5903
5904 spa_activate(newspa, spa_mode_global);
5905 spa_async_suspend(newspa);
5906
5907 /* create the new pool from the disks of the original pool */
5908 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5909 if (error)
5910 goto out;
5911
5912 /* if that worked, generate a real config for the new pool */
5913 if (newspa->spa_root_vdev != NULL) {
5914 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
79c76d5b 5915 NV_UNIQUE_NAME, KM_SLEEP) == 0);
428870ff
BB
5916 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5917 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5918 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5919 B_TRUE));
9babb374 5920 }
34dc7c2f 5921
428870ff
BB
5922 /* set the props */
5923 if (props != NULL) {
5924 spa_configfile_set(newspa, props, B_FALSE);
5925 error = spa_prop_set(newspa, props);
5926 if (error)
5927 goto out;
5928 }
34dc7c2f 5929
428870ff
BB
5930 /* flush everything */
5931 txg = spa_vdev_config_enter(newspa);
5932 vdev_config_dirty(newspa->spa_root_vdev);
5933 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
34dc7c2f 5934
428870ff
BB
5935 if (zio_injection_enabled)
5936 zio_handle_panic_injection(spa, FTAG, 2);
34dc7c2f 5937
428870ff 5938 spa_async_resume(newspa);
34dc7c2f 5939
428870ff
BB
5940 /* finally, update the original pool's config */
5941 txg = spa_vdev_config_enter(spa);
5942 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5943 error = dmu_tx_assign(tx, TXG_WAIT);
5944 if (error != 0)
5945 dmu_tx_abort(tx);
5946 for (c = 0; c < children; c++) {
5947 if (vml[c] != NULL) {
5948 vdev_split(vml[c]);
5949 if (error == 0)
6f1ffb06
MA
5950 spa_history_log_internal(spa, "detach", tx,
5951 "vdev=%s", vml[c]->vdev_path);
e0ab3ab5 5952
428870ff 5953 vdev_free(vml[c]);
34dc7c2f 5954 }
34dc7c2f 5955 }
e0ab3ab5 5956 spa->spa_avz_action = AVZ_ACTION_REBUILD;
428870ff
BB
5957 vdev_config_dirty(spa->spa_root_vdev);
5958 spa->spa_config_splitting = NULL;
5959 nvlist_free(nvl);
5960 if (error == 0)
5961 dmu_tx_commit(tx);
5962 (void) spa_vdev_exit(spa, NULL, txg, 0);
5963
5964 if (zio_injection_enabled)
5965 zio_handle_panic_injection(spa, FTAG, 3);
5966
5967 /* split is complete; log a history record */
6f1ffb06
MA
5968 spa_history_log_internal(newspa, "split", NULL,
5969 "from pool %s", spa_name(spa));
428870ff
BB
5970
5971 kmem_free(vml, children * sizeof (vdev_t *));
5972
5973 /* if we're not going to mount the filesystems in userland, export */
5974 if (exp)
5975 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5976 B_FALSE, B_FALSE);
5977
5978 return (error);
5979
5980out:
5981 spa_unload(newspa);
5982 spa_deactivate(newspa);
5983 spa_remove(newspa);
5984
5985 txg = spa_vdev_config_enter(spa);
5986
5987 /* re-online all offlined disks */
5988 for (c = 0; c < children; c++) {
5989 if (vml[c] != NULL)
5990 vml[c]->vdev_offline = B_FALSE;
5991 }
5992 vdev_reopen(spa->spa_root_vdev);
5993
5994 nvlist_free(spa->spa_config_splitting);
5995 spa->spa_config_splitting = NULL;
5996 (void) spa_vdev_exit(spa, NULL, txg, error);
34dc7c2f 5997
428870ff 5998 kmem_free(vml, children * sizeof (vdev_t *));
34dc7c2f
BB
5999 return (error);
6000}
6001
34dc7c2f
BB
6002/*
6003 * Find any device that's done replacing, or a vdev marked 'unspare' that's
d3cc8b15 6004 * currently spared, so we can detach it.
34dc7c2f
BB
6005 */
6006static vdev_t *
6007spa_vdev_resilver_done_hunt(vdev_t *vd)
6008{
6009 vdev_t *newvd, *oldvd;
34dc7c2f 6010
1c27024e 6011 for (int c = 0; c < vd->vdev_children; c++) {
34dc7c2f
BB
6012 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
6013 if (oldvd != NULL)
6014 return (oldvd);
6015 }
6016
6017 /*
572e2857
BB
6018 * Check for a completed replacement. We always consider the first
6019 * vdev in the list to be the oldest vdev, and the last one to be
6020 * the newest (see spa_vdev_attach() for how that works). In
6021 * the case where the newest vdev is faulted, we will not automatically
6022 * remove it after a resilver completes. This is OK as it will require
6023 * user intervention to determine which disk the admin wishes to keep.
34dc7c2f 6024 */
572e2857
BB
6025 if (vd->vdev_ops == &vdev_replacing_ops) {
6026 ASSERT(vd->vdev_children > 1);
6027
6028 newvd = vd->vdev_child[vd->vdev_children - 1];
34dc7c2f 6029 oldvd = vd->vdev_child[0];
34dc7c2f 6030
fb5f0bc8 6031 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 6032 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
fb5f0bc8 6033 !vdev_dtl_required(oldvd))
34dc7c2f 6034 return (oldvd);
34dc7c2f
BB
6035 }
6036
6037 /*
6038 * Check for a completed resilver with the 'unspare' flag set.
6039 */
572e2857
BB
6040 if (vd->vdev_ops == &vdev_spare_ops) {
6041 vdev_t *first = vd->vdev_child[0];
6042 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
6043
6044 if (last->vdev_unspare) {
6045 oldvd = first;
6046 newvd = last;
6047 } else if (first->vdev_unspare) {
6048 oldvd = last;
6049 newvd = first;
6050 } else {
6051 oldvd = NULL;
6052 }
34dc7c2f 6053
572e2857 6054 if (oldvd != NULL &&
fb5f0bc8 6055 vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 6056 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
572e2857 6057 !vdev_dtl_required(oldvd))
34dc7c2f 6058 return (oldvd);
572e2857
BB
6059
6060 /*
6061 * If there are more than two spares attached to a disk,
6062 * and those spares are not required, then we want to
6063 * attempt to free them up now so that they can be used
6064 * by other pools. Once we're back down to a single
6065 * disk+spare, we stop removing them.
6066 */
6067 if (vd->vdev_children > 2) {
6068 newvd = vd->vdev_child[1];
6069
6070 if (newvd->vdev_isspare && last->vdev_isspare &&
6071 vdev_dtl_empty(last, DTL_MISSING) &&
6072 vdev_dtl_empty(last, DTL_OUTAGE) &&
6073 !vdev_dtl_required(newvd))
6074 return (newvd);
34dc7c2f 6075 }
34dc7c2f
BB
6076 }
6077
6078 return (NULL);
6079}
6080
6081static void
6082spa_vdev_resilver_done(spa_t *spa)
6083{
fb5f0bc8
BB
6084 vdev_t *vd, *pvd, *ppvd;
6085 uint64_t guid, sguid, pguid, ppguid;
34dc7c2f 6086
fb5f0bc8 6087 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
6088
6089 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
fb5f0bc8
BB
6090 pvd = vd->vdev_parent;
6091 ppvd = pvd->vdev_parent;
34dc7c2f 6092 guid = vd->vdev_guid;
fb5f0bc8
BB
6093 pguid = pvd->vdev_guid;
6094 ppguid = ppvd->vdev_guid;
6095 sguid = 0;
34dc7c2f
BB
6096 /*
6097 * If we have just finished replacing a hot spared device, then
6098 * we need to detach the parent's first child (the original hot
6099 * spare) as well.
6100 */
572e2857
BB
6101 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
6102 ppvd->vdev_children == 2) {
34dc7c2f 6103 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
fb5f0bc8 6104 sguid = ppvd->vdev_child[1]->vdev_guid;
34dc7c2f 6105 }
5d1f7fb6
GW
6106 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
6107
fb5f0bc8
BB
6108 spa_config_exit(spa, SCL_ALL, FTAG);
6109 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
34dc7c2f 6110 return;
fb5f0bc8 6111 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
34dc7c2f 6112 return;
fb5f0bc8 6113 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
6114 }
6115
fb5f0bc8 6116 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6117}
6118
6119/*
428870ff 6120 * Update the stored path or FRU for this vdev.
34dc7c2f
BB
6121 */
6122int
9babb374
BB
6123spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
6124 boolean_t ispath)
34dc7c2f 6125{
b128c09f 6126 vdev_t *vd;
428870ff 6127 boolean_t sync = B_FALSE;
34dc7c2f 6128
572e2857
BB
6129 ASSERT(spa_writeable(spa));
6130
428870ff 6131 spa_vdev_state_enter(spa, SCL_ALL);
34dc7c2f 6132
9babb374 6133 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
428870ff 6134 return (spa_vdev_state_exit(spa, NULL, ENOENT));
34dc7c2f
BB
6135
6136 if (!vd->vdev_ops->vdev_op_leaf)
428870ff 6137 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
34dc7c2f 6138
9babb374 6139 if (ispath) {
428870ff
BB
6140 if (strcmp(value, vd->vdev_path) != 0) {
6141 spa_strfree(vd->vdev_path);
6142 vd->vdev_path = spa_strdup(value);
6143 sync = B_TRUE;
6144 }
9babb374 6145 } else {
428870ff
BB
6146 if (vd->vdev_fru == NULL) {
6147 vd->vdev_fru = spa_strdup(value);
6148 sync = B_TRUE;
6149 } else if (strcmp(value, vd->vdev_fru) != 0) {
9babb374 6150 spa_strfree(vd->vdev_fru);
428870ff
BB
6151 vd->vdev_fru = spa_strdup(value);
6152 sync = B_TRUE;
6153 }
9babb374 6154 }
34dc7c2f 6155
428870ff 6156 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
34dc7c2f
BB
6157}
6158
9babb374
BB
6159int
6160spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
6161{
6162 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
6163}
6164
6165int
6166spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
6167{
6168 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
6169}
6170
34dc7c2f
BB
6171/*
6172 * ==========================================================================
428870ff 6173 * SPA Scanning
34dc7c2f
BB
6174 * ==========================================================================
6175 */
0ea05c64
AP
6176int
6177spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd)
6178{
6179 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6180
6181 if (dsl_scan_resilvering(spa->spa_dsl_pool))
6182 return (SET_ERROR(EBUSY));
6183
6184 return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd));
6185}
34dc7c2f 6186
34dc7c2f 6187int
428870ff
BB
6188spa_scan_stop(spa_t *spa)
6189{
6190 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6191 if (dsl_scan_resilvering(spa->spa_dsl_pool))
2e528b49 6192 return (SET_ERROR(EBUSY));
428870ff
BB
6193 return (dsl_scan_cancel(spa->spa_dsl_pool));
6194}
6195
6196int
6197spa_scan(spa_t *spa, pool_scan_func_t func)
34dc7c2f 6198{
b128c09f 6199 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
34dc7c2f 6200
428870ff 6201 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
2e528b49 6202 return (SET_ERROR(ENOTSUP));
34dc7c2f 6203
34dc7c2f 6204 /*
b128c09f
BB
6205 * If a resilver was requested, but there is no DTL on a
6206 * writeable leaf device, we have nothing to do.
34dc7c2f 6207 */
428870ff 6208 if (func == POOL_SCAN_RESILVER &&
b128c09f
BB
6209 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
6210 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34dc7c2f
BB
6211 return (0);
6212 }
6213
428870ff 6214 return (dsl_scan(spa->spa_dsl_pool, func));
34dc7c2f
BB
6215}
6216
6217/*
6218 * ==========================================================================
6219 * SPA async task processing
6220 * ==========================================================================
6221 */
6222
6223static void
6224spa_async_remove(spa_t *spa, vdev_t *vd)
6225{
b128c09f 6226 if (vd->vdev_remove_wanted) {
428870ff
BB
6227 vd->vdev_remove_wanted = B_FALSE;
6228 vd->vdev_delayed_close = B_FALSE;
b128c09f 6229 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
428870ff
BB
6230
6231 /*
6232 * We want to clear the stats, but we don't want to do a full
6233 * vdev_clear() as that will cause us to throw away
6234 * degraded/faulted state as well as attempt to reopen the
6235 * device, all of which is a waste.
6236 */
6237 vd->vdev_stat.vs_read_errors = 0;
6238 vd->vdev_stat.vs_write_errors = 0;
6239 vd->vdev_stat.vs_checksum_errors = 0;
6240
b128c09f
BB
6241 vdev_state_dirty(vd->vdev_top);
6242 }
34dc7c2f 6243
1c27024e 6244 for (int c = 0; c < vd->vdev_children; c++)
b128c09f
BB
6245 spa_async_remove(spa, vd->vdev_child[c]);
6246}
6247
6248static void
6249spa_async_probe(spa_t *spa, vdev_t *vd)
6250{
6251 if (vd->vdev_probe_wanted) {
428870ff 6252 vd->vdev_probe_wanted = B_FALSE;
b128c09f 6253 vdev_reopen(vd); /* vdev_open() does the actual probe */
34dc7c2f 6254 }
b128c09f 6255
1c27024e 6256 for (int c = 0; c < vd->vdev_children; c++)
b128c09f 6257 spa_async_probe(spa, vd->vdev_child[c]);
34dc7c2f
BB
6258}
6259
9babb374
BB
6260static void
6261spa_async_autoexpand(spa_t *spa, vdev_t *vd)
6262{
9babb374
BB
6263 if (!spa->spa_autoexpand)
6264 return;
6265
1c27024e 6266 for (int c = 0; c < vd->vdev_children; c++) {
9babb374
BB
6267 vdev_t *cvd = vd->vdev_child[c];
6268 spa_async_autoexpand(spa, cvd);
6269 }
6270
6271 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
6272 return;
6273
12fa0466 6274 spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND);
9babb374
BB
6275}
6276
34dc7c2f 6277static void
c25b8f99 6278spa_async_thread(void *arg)
34dc7c2f 6279{
c25b8f99 6280 spa_t *spa = (spa_t *)arg;
867959b5 6281 int tasks;
34dc7c2f
BB
6282
6283 ASSERT(spa->spa_sync_on);
6284
6285 mutex_enter(&spa->spa_async_lock);
6286 tasks = spa->spa_async_tasks;
6287 spa->spa_async_tasks = 0;
6288 mutex_exit(&spa->spa_async_lock);
6289
6290 /*
6291 * See if the config needs to be updated.
6292 */
6293 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
428870ff 6294 uint64_t old_space, new_space;
9babb374 6295
34dc7c2f 6296 mutex_enter(&spa_namespace_lock);
428870ff 6297 old_space = metaslab_class_get_space(spa_normal_class(spa));
34dc7c2f 6298 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
428870ff 6299 new_space = metaslab_class_get_space(spa_normal_class(spa));
34dc7c2f 6300 mutex_exit(&spa_namespace_lock);
9babb374
BB
6301
6302 /*
6303 * If the pool grew as a result of the config update,
6304 * then log an internal history event.
6305 */
428870ff 6306 if (new_space != old_space) {
6f1ffb06 6307 spa_history_log_internal(spa, "vdev online", NULL,
45d1cae3 6308 "pool '%s' size: %llu(+%llu)",
428870ff 6309 spa_name(spa), new_space, new_space - old_space);
9babb374 6310 }
34dc7c2f
BB
6311 }
6312
6313 /*
6314 * See if any devices need to be marked REMOVED.
34dc7c2f 6315 */
b128c09f 6316 if (tasks & SPA_ASYNC_REMOVE) {
428870ff 6317 spa_vdev_state_enter(spa, SCL_NONE);
34dc7c2f 6318 spa_async_remove(spa, spa->spa_root_vdev);
867959b5 6319 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
b128c09f 6320 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
867959b5 6321 for (int i = 0; i < spa->spa_spares.sav_count; i++)
b128c09f
BB
6322 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6323 (void) spa_vdev_state_exit(spa, NULL, 0);
34dc7c2f
BB
6324 }
6325
9babb374
BB
6326 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6327 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6328 spa_async_autoexpand(spa, spa->spa_root_vdev);
6329 spa_config_exit(spa, SCL_CONFIG, FTAG);
6330 }
6331
34dc7c2f 6332 /*
b128c09f 6333 * See if any devices need to be probed.
34dc7c2f 6334 */
b128c09f 6335 if (tasks & SPA_ASYNC_PROBE) {
428870ff 6336 spa_vdev_state_enter(spa, SCL_NONE);
b128c09f
BB
6337 spa_async_probe(spa, spa->spa_root_vdev);
6338 (void) spa_vdev_state_exit(spa, NULL, 0);
6339 }
34dc7c2f
BB
6340
6341 /*
b128c09f 6342 * If any devices are done replacing, detach them.
34dc7c2f 6343 */
b128c09f
BB
6344 if (tasks & SPA_ASYNC_RESILVER_DONE)
6345 spa_vdev_resilver_done(spa);
34dc7c2f
BB
6346
6347 /*
6348 * Kick off a resilver.
6349 */
b128c09f 6350 if (tasks & SPA_ASYNC_RESILVER)
428870ff 6351 dsl_resilver_restart(spa->spa_dsl_pool, 0);
34dc7c2f
BB
6352
6353 /*
6354 * Let the world know that we're done.
6355 */
6356 mutex_enter(&spa->spa_async_lock);
6357 spa->spa_async_thread = NULL;
6358 cv_broadcast(&spa->spa_async_cv);
6359 mutex_exit(&spa->spa_async_lock);
6360 thread_exit();
6361}
6362
6363void
6364spa_async_suspend(spa_t *spa)
6365{
6366 mutex_enter(&spa->spa_async_lock);
6367 spa->spa_async_suspended++;
9d5b5245 6368 while (spa->spa_async_thread != NULL)
34dc7c2f
BB
6369 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6370 mutex_exit(&spa->spa_async_lock);
a1d477c2
MA
6371
6372 spa_vdev_remove_suspend(spa);
9d5b5245
SD
6373
6374 zthr_t *condense_thread = spa->spa_condense_zthr;
6375 if (condense_thread != NULL && zthr_isrunning(condense_thread))
6376 VERIFY0(zthr_cancel(condense_thread));
34dc7c2f
BB
6377}
6378
6379void
6380spa_async_resume(spa_t *spa)
6381{
6382 mutex_enter(&spa->spa_async_lock);
6383 ASSERT(spa->spa_async_suspended != 0);
6384 spa->spa_async_suspended--;
6385 mutex_exit(&spa->spa_async_lock);
a1d477c2 6386 spa_restart_removal(spa);
9d5b5245
SD
6387
6388 zthr_t *condense_thread = spa->spa_condense_zthr;
6389 if (condense_thread != NULL && !zthr_isrunning(condense_thread))
6390 zthr_resume(condense_thread);
34dc7c2f
BB
6391}
6392
e6cfd633
WA
6393static boolean_t
6394spa_async_tasks_pending(spa_t *spa)
6395{
6396 uint_t non_config_tasks;
6397 uint_t config_task;
6398 boolean_t config_task_suspended;
6399
6400 non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
6401 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6402 if (spa->spa_ccw_fail_time == 0) {
6403 config_task_suspended = B_FALSE;
6404 } else {
6405 config_task_suspended =
6406 (gethrtime() - spa->spa_ccw_fail_time) <
05852b34 6407 ((hrtime_t)zfs_ccw_retry_interval * NANOSEC);
e6cfd633
WA
6408 }
6409
6410 return (non_config_tasks || (config_task && !config_task_suspended));
6411}
6412
34dc7c2f
BB
6413static void
6414spa_async_dispatch(spa_t *spa)
6415{
6416 mutex_enter(&spa->spa_async_lock);
e6cfd633
WA
6417 if (spa_async_tasks_pending(spa) &&
6418 !spa->spa_async_suspended &&
34dc7c2f 6419 spa->spa_async_thread == NULL &&
e6cfd633 6420 rootdir != NULL)
34dc7c2f
BB
6421 spa->spa_async_thread = thread_create(NULL, 0,
6422 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6423 mutex_exit(&spa->spa_async_lock);
6424}
6425
6426void
6427spa_async_request(spa_t *spa, int task)
6428{
428870ff 6429 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
34dc7c2f
BB
6430 mutex_enter(&spa->spa_async_lock);
6431 spa->spa_async_tasks |= task;
6432 mutex_exit(&spa->spa_async_lock);
6433}
6434
6435/*
6436 * ==========================================================================
6437 * SPA syncing routines
6438 * ==========================================================================
6439 */
6440
428870ff
BB
6441static int
6442bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
34dc7c2f 6443{
428870ff
BB
6444 bpobj_t *bpo = arg;
6445 bpobj_enqueue(bpo, bp, tx);
6446 return (0);
6447}
34dc7c2f 6448
428870ff
BB
6449static int
6450spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6451{
6452 zio_t *zio = arg;
34dc7c2f 6453
428870ff
BB
6454 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6455 zio->io_flags));
6456 return (0);
34dc7c2f
BB
6457}
6458
e8b96c60
MA
6459/*
6460 * Note: this simple function is not inlined to make it easier to dtrace the
6461 * amount of time spent syncing frees.
6462 */
6463static void
6464spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6465{
6466 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6467 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6468 VERIFY(zio_wait(zio) == 0);
6469}
6470
6471/*
6472 * Note: this simple function is not inlined to make it easier to dtrace the
6473 * amount of time spent syncing deferred frees.
6474 */
6475static void
6476spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6477{
6478 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6479 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6480 spa_free_sync_cb, zio, tx), ==, 0);
6481 VERIFY0(zio_wait(zio));
6482}
6483
34dc7c2f
BB
6484static void
6485spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6486{
6487 char *packed = NULL;
b128c09f 6488 size_t bufsize;
34dc7c2f
BB
6489 size_t nvsize = 0;
6490 dmu_buf_t *db;
6491
6492 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6493
b128c09f
BB
6494 /*
6495 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
b0bc7a84 6496 * information. This avoids the dmu_buf_will_dirty() path and
b128c09f
BB
6497 * saves us a pre-read to get data we don't actually care about.
6498 */
9ae529ec 6499 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
79c76d5b 6500 packed = vmem_alloc(bufsize, KM_SLEEP);
34dc7c2f
BB
6501
6502 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
79c76d5b 6503 KM_SLEEP) == 0);
b128c09f 6504 bzero(packed + nvsize, bufsize - nvsize);
34dc7c2f 6505
b128c09f 6506 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
34dc7c2f 6507
00b46022 6508 vmem_free(packed, bufsize);
34dc7c2f
BB
6509
6510 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6511 dmu_buf_will_dirty(db, tx);
6512 *(uint64_t *)db->db_data = nvsize;
6513 dmu_buf_rele(db, FTAG);
6514}
6515
6516static void
6517spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6518 const char *config, const char *entry)
6519{
6520 nvlist_t *nvroot;
6521 nvlist_t **list;
6522 int i;
6523
6524 if (!sav->sav_sync)
6525 return;
6526
6527 /*
6528 * Update the MOS nvlist describing the list of available devices.
6529 * spa_validate_aux() will have already made sure this nvlist is
6530 * valid and the vdevs are labeled appropriately.
6531 */
6532 if (sav->sav_object == 0) {
6533 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6534 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6535 sizeof (uint64_t), tx);
6536 VERIFY(zap_update(spa->spa_meta_objset,
6537 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6538 &sav->sav_object, tx) == 0);
6539 }
6540
79c76d5b 6541 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
6542 if (sav->sav_count == 0) {
6543 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6544 } else {
79c76d5b 6545 list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP);
34dc7c2f
BB
6546 for (i = 0; i < sav->sav_count; i++)
6547 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
428870ff 6548 B_FALSE, VDEV_CONFIG_L2CACHE);
34dc7c2f
BB
6549 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6550 sav->sav_count) == 0);
6551 for (i = 0; i < sav->sav_count; i++)
6552 nvlist_free(list[i]);
6553 kmem_free(list, sav->sav_count * sizeof (void *));
6554 }
6555
6556 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6557 nvlist_free(nvroot);
6558
6559 sav->sav_sync = B_FALSE;
6560}
6561
e0ab3ab5
JS
6562/*
6563 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6564 * The all-vdev ZAP must be empty.
6565 */
6566static void
6567spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
6568{
6569 spa_t *spa = vd->vdev_spa;
e0ab3ab5
JS
6570
6571 if (vd->vdev_top_zap != 0) {
6572 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6573 vd->vdev_top_zap, tx));
6574 }
6575 if (vd->vdev_leaf_zap != 0) {
6576 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6577 vd->vdev_leaf_zap, tx));
6578 }
1c27024e 6579 for (uint64_t i = 0; i < vd->vdev_children; i++) {
e0ab3ab5
JS
6580 spa_avz_build(vd->vdev_child[i], avz, tx);
6581 }
6582}
6583
34dc7c2f
BB
6584static void
6585spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6586{
6587 nvlist_t *config;
6588
e0ab3ab5
JS
6589 /*
6590 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6591 * its config may not be dirty but we still need to build per-vdev ZAPs.
6592 * Similarly, if the pool is being assembled (e.g. after a split), we
6593 * need to rebuild the AVZ although the config may not be dirty.
6594 */
6595 if (list_is_empty(&spa->spa_config_dirty_list) &&
6596 spa->spa_avz_action == AVZ_ACTION_NONE)
34dc7c2f
BB
6597 return;
6598
b128c09f
BB
6599 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6600
e0ab3ab5 6601 ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
38640550 6602 spa->spa_avz_action == AVZ_ACTION_INITIALIZE ||
e0ab3ab5
JS
6603 spa->spa_all_vdev_zaps != 0);
6604
6605 if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
e0ab3ab5
JS
6606 /* Make and build the new AVZ */
6607 uint64_t new_avz = zap_create(spa->spa_meta_objset,
6608 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
6609 spa_avz_build(spa->spa_root_vdev, new_avz, tx);
6610
6611 /* Diff old AVZ with new one */
1c27024e
DB
6612 zap_cursor_t zc;
6613 zap_attribute_t za;
6614
e0ab3ab5
JS
6615 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6616 spa->spa_all_vdev_zaps);
6617 zap_cursor_retrieve(&zc, &za) == 0;
6618 zap_cursor_advance(&zc)) {
6619 uint64_t vdzap = za.za_first_integer;
6620 if (zap_lookup_int(spa->spa_meta_objset, new_avz,
6621 vdzap) == ENOENT) {
6622 /*
6623 * ZAP is listed in old AVZ but not in new one;
6624 * destroy it
6625 */
6626 VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
6627 tx));
6628 }
6629 }
6630
6631 zap_cursor_fini(&zc);
6632
6633 /* Destroy the old AVZ */
6634 VERIFY0(zap_destroy(spa->spa_meta_objset,
6635 spa->spa_all_vdev_zaps, tx));
6636
6637 /* Replace the old AVZ in the dir obj with the new one */
6638 VERIFY0(zap_update(spa->spa_meta_objset,
6639 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
6640 sizeof (new_avz), 1, &new_avz, tx));
6641
6642 spa->spa_all_vdev_zaps = new_avz;
6643 } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
6644 zap_cursor_t zc;
6645 zap_attribute_t za;
6646
6647 /* Walk through the AVZ and destroy all listed ZAPs */
6648 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6649 spa->spa_all_vdev_zaps);
6650 zap_cursor_retrieve(&zc, &za) == 0;
6651 zap_cursor_advance(&zc)) {
6652 uint64_t zap = za.za_first_integer;
6653 VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
6654 }
6655
6656 zap_cursor_fini(&zc);
6657
6658 /* Destroy and unlink the AVZ itself */
6659 VERIFY0(zap_destroy(spa->spa_meta_objset,
6660 spa->spa_all_vdev_zaps, tx));
6661 VERIFY0(zap_remove(spa->spa_meta_objset,
6662 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
6663 spa->spa_all_vdev_zaps = 0;
6664 }
6665
6666 if (spa->spa_all_vdev_zaps == 0) {
6667 spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
6668 DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
6669 DMU_POOL_VDEV_ZAP_MAP, tx);
6670 }
6671 spa->spa_avz_action = AVZ_ACTION_NONE;
6672
6673 /* Create ZAPs for vdevs that don't have them. */
6674 vdev_construct_zaps(spa->spa_root_vdev, tx);
6675
b128c09f
BB
6676 config = spa_config_generate(spa, spa->spa_root_vdev,
6677 dmu_tx_get_txg(tx), B_FALSE);
6678
ea0b2538
GW
6679 /*
6680 * If we're upgrading the spa version then make sure that
6681 * the config object gets updated with the correct version.
6682 */
6683 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6684 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6685 spa->spa_uberblock.ub_version);
6686
b128c09f 6687 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f 6688
8a5fc748 6689 nvlist_free(spa->spa_config_syncing);
34dc7c2f
BB
6690 spa->spa_config_syncing = config;
6691
6692 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6693}
6694
9ae529ec 6695static void
13fe0198 6696spa_sync_version(void *arg, dmu_tx_t *tx)
9ae529ec 6697{
13fe0198
MA
6698 uint64_t *versionp = arg;
6699 uint64_t version = *versionp;
6700 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
9ae529ec
CS
6701
6702 /*
6703 * Setting the version is special cased when first creating the pool.
6704 */
6705 ASSERT(tx->tx_txg != TXG_INITIAL);
6706
8dca0a9a 6707 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
9ae529ec
CS
6708 ASSERT(version >= spa_version(spa));
6709
6710 spa->spa_uberblock.ub_version = version;
6711 vdev_config_dirty(spa->spa_root_vdev);
6f1ffb06 6712 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
9ae529ec
CS
6713}
6714
34dc7c2f
BB
6715/*
6716 * Set zpool properties.
6717 */
6718static void
13fe0198 6719spa_sync_props(void *arg, dmu_tx_t *tx)
34dc7c2f 6720{
13fe0198
MA
6721 nvlist_t *nvp = arg;
6722 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
34dc7c2f 6723 objset_t *mos = spa->spa_meta_objset;
9ae529ec 6724 nvpair_t *elem = NULL;
b128c09f
BB
6725
6726 mutex_enter(&spa->spa_props_lock);
34dc7c2f 6727
34dc7c2f 6728 while ((elem = nvlist_next_nvpair(nvp, elem))) {
9ae529ec
CS
6729 uint64_t intval;
6730 char *strval, *fname;
6731 zpool_prop_t prop;
6732 const char *propname;
6733 zprop_type_t proptype;
fa86b5db 6734 spa_feature_t fid;
9ae529ec 6735
31864e3d
BB
6736 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6737 case ZPOOL_PROP_INVAL:
9ae529ec
CS
6738 /*
6739 * We checked this earlier in spa_prop_validate().
6740 */
6741 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6742
6743 fname = strchr(nvpair_name(elem), '@') + 1;
fa86b5db 6744 VERIFY0(zfeature_lookup_name(fname, &fid));
9ae529ec 6745
fa86b5db 6746 spa_feature_enable(spa, fid, tx);
6f1ffb06
MA
6747 spa_history_log_internal(spa, "set", tx,
6748 "%s=enabled", nvpair_name(elem));
9ae529ec
CS
6749 break;
6750
34dc7c2f 6751 case ZPOOL_PROP_VERSION:
93cf2076 6752 intval = fnvpair_value_uint64(elem);
34dc7c2f 6753 /*
4e33ba4c 6754 * The version is synced separately before other
9ae529ec 6755 * properties and should be correct by now.
34dc7c2f 6756 */
9ae529ec 6757 ASSERT3U(spa_version(spa), >=, intval);
34dc7c2f
BB
6758 break;
6759
6760 case ZPOOL_PROP_ALTROOT:
6761 /*
6762 * 'altroot' is a non-persistent property. It should
6763 * have been set temporarily at creation or import time.
6764 */
6765 ASSERT(spa->spa_root != NULL);
6766 break;
6767
572e2857 6768 case ZPOOL_PROP_READONLY:
34dc7c2f
BB
6769 case ZPOOL_PROP_CACHEFILE:
6770 /*
572e2857
BB
6771 * 'readonly' and 'cachefile' are also non-persisitent
6772 * properties.
34dc7c2f 6773 */
34dc7c2f 6774 break;
d96eb2b1 6775 case ZPOOL_PROP_COMMENT:
93cf2076 6776 strval = fnvpair_value_string(elem);
d96eb2b1
DM
6777 if (spa->spa_comment != NULL)
6778 spa_strfree(spa->spa_comment);
6779 spa->spa_comment = spa_strdup(strval);
6780 /*
6781 * We need to dirty the configuration on all the vdevs
6782 * so that their labels get updated. It's unnecessary
6783 * to do this for pool creation since the vdev's
4e33ba4c 6784 * configuration has already been dirtied.
d96eb2b1
DM
6785 */
6786 if (tx->tx_txg != TXG_INITIAL)
6787 vdev_config_dirty(spa->spa_root_vdev);
6f1ffb06
MA
6788 spa_history_log_internal(spa, "set", tx,
6789 "%s=%s", nvpair_name(elem), strval);
d96eb2b1 6790 break;
34dc7c2f
BB
6791 default:
6792 /*
6793 * Set pool property values in the poolprops mos object.
6794 */
34dc7c2f 6795 if (spa->spa_pool_props_object == 0) {
9ae529ec
CS
6796 spa->spa_pool_props_object =
6797 zap_create_link(mos, DMU_OT_POOL_PROPS,
34dc7c2f 6798 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
9ae529ec 6799 tx);
34dc7c2f 6800 }
34dc7c2f
BB
6801
6802 /* normalize the property name */
6803 propname = zpool_prop_to_name(prop);
6804 proptype = zpool_prop_get_type(prop);
6805
6806 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6807 ASSERT(proptype == PROP_TYPE_STRING);
93cf2076
GW
6808 strval = fnvpair_value_string(elem);
6809 VERIFY0(zap_update(mos,
34dc7c2f 6810 spa->spa_pool_props_object, propname,
93cf2076 6811 1, strlen(strval) + 1, strval, tx));
6f1ffb06
MA
6812 spa_history_log_internal(spa, "set", tx,
6813 "%s=%s", nvpair_name(elem), strval);
34dc7c2f 6814 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
93cf2076 6815 intval = fnvpair_value_uint64(elem);
34dc7c2f
BB
6816
6817 if (proptype == PROP_TYPE_INDEX) {
6818 const char *unused;
93cf2076
GW
6819 VERIFY0(zpool_prop_index_to_string(
6820 prop, intval, &unused));
34dc7c2f 6821 }
93cf2076 6822 VERIFY0(zap_update(mos,
34dc7c2f 6823 spa->spa_pool_props_object, propname,
93cf2076 6824 8, 1, &intval, tx));
6f1ffb06
MA
6825 spa_history_log_internal(spa, "set", tx,
6826 "%s=%lld", nvpair_name(elem), intval);
34dc7c2f
BB
6827 } else {
6828 ASSERT(0); /* not allowed */
6829 }
6830
6831 switch (prop) {
6832 case ZPOOL_PROP_DELEGATION:
6833 spa->spa_delegation = intval;
6834 break;
6835 case ZPOOL_PROP_BOOTFS:
6836 spa->spa_bootfs = intval;
6837 break;
6838 case ZPOOL_PROP_FAILUREMODE:
6839 spa->spa_failmode = intval;
6840 break;
9babb374
BB
6841 case ZPOOL_PROP_AUTOEXPAND:
6842 spa->spa_autoexpand = intval;
428870ff
BB
6843 if (tx->tx_txg != TXG_INITIAL)
6844 spa_async_request(spa,
6845 SPA_ASYNC_AUTOEXPAND);
6846 break;
379ca9cf
OF
6847 case ZPOOL_PROP_MULTIHOST:
6848 spa->spa_multihost = intval;
6849 break;
428870ff
BB
6850 case ZPOOL_PROP_DEDUPDITTO:
6851 spa->spa_dedup_ditto = intval;
9babb374 6852 break;
34dc7c2f
BB
6853 default:
6854 break;
6855 }
6856 }
6857
34dc7c2f 6858 }
b128c09f
BB
6859
6860 mutex_exit(&spa->spa_props_lock);
34dc7c2f
BB
6861}
6862
428870ff
BB
6863/*
6864 * Perform one-time upgrade on-disk changes. spa_version() does not
6865 * reflect the new version this txg, so there must be no changes this
6866 * txg to anything that the upgrade code depends on after it executes.
6867 * Therefore this must be called after dsl_pool_sync() does the sync
6868 * tasks.
6869 */
6870static void
6871spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6872{
6873 dsl_pool_t *dp = spa->spa_dsl_pool;
6874
6875 ASSERT(spa->spa_sync_pass == 1);
6876
13fe0198
MA
6877 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6878
428870ff
BB
6879 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6880 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6881 dsl_pool_create_origin(dp, tx);
6882
6883 /* Keeping the origin open increases spa_minref */
6884 spa->spa_minref += 3;
6885 }
6886
6887 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6888 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6889 dsl_pool_upgrade_clones(dp, tx);
6890 }
6891
6892 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6893 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6894 dsl_pool_upgrade_dir_clones(dp, tx);
6895
6896 /* Keeping the freedir open increases spa_minref */
6897 spa->spa_minref += 3;
6898 }
9ae529ec
CS
6899
6900 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6901 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6902 spa_feature_create_zap_objects(spa, tx);
6903 }
62bdd5eb
DL
6904
6905 /*
6906 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6907 * when possibility to use lz4 compression for metadata was added
6908 * Old pools that have this feature enabled must be upgraded to have
6909 * this feature active
6910 */
6911 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6912 boolean_t lz4_en = spa_feature_is_enabled(spa,
6913 SPA_FEATURE_LZ4_COMPRESS);
6914 boolean_t lz4_ac = spa_feature_is_active(spa,
6915 SPA_FEATURE_LZ4_COMPRESS);
6916
6917 if (lz4_en && !lz4_ac)
6918 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6919 }
3c67d83a
TH
6920
6921 /*
6922 * If we haven't written the salt, do so now. Note that the
6923 * feature may not be activated yet, but that's fine since
6924 * the presence of this ZAP entry is backwards compatible.
6925 */
6926 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6927 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6928 VERIFY0(zap_add(spa->spa_meta_objset,
6929 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6930 sizeof (spa->spa_cksum_salt.zcs_bytes),
6931 spa->spa_cksum_salt.zcs_bytes, tx));
6932 }
6933
13fe0198 6934 rrw_exit(&dp->dp_config_rwlock, FTAG);
428870ff
BB
6935}
6936
a1d477c2
MA
6937static void
6938vdev_indirect_state_sync_verify(vdev_t *vd)
6939{
6940 ASSERTV(vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping);
6941 ASSERTV(vdev_indirect_births_t *vib = vd->vdev_indirect_births);
6942
6943 if (vd->vdev_ops == &vdev_indirect_ops) {
6944 ASSERT(vim != NULL);
6945 ASSERT(vib != NULL);
6946 }
6947
6948 if (vdev_obsolete_sm_object(vd) != 0) {
6949 ASSERT(vd->vdev_obsolete_sm != NULL);
6950 ASSERT(vd->vdev_removing ||
6951 vd->vdev_ops == &vdev_indirect_ops);
6952 ASSERT(vdev_indirect_mapping_num_entries(vim) > 0);
6953 ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0);
6954
6955 ASSERT3U(vdev_obsolete_sm_object(vd), ==,
6956 space_map_object(vd->vdev_obsolete_sm));
6957 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=,
6958 space_map_allocated(vd->vdev_obsolete_sm));
6959 }
6960 ASSERT(vd->vdev_obsolete_segments != NULL);
6961
6962 /*
6963 * Since frees / remaps to an indirect vdev can only
6964 * happen in syncing context, the obsolete segments
6965 * tree must be empty when we start syncing.
6966 */
6967 ASSERT0(range_tree_space(vd->vdev_obsolete_segments));
6968}
6969
34dc7c2f
BB
6970/*
6971 * Sync the specified transaction group. New blocks may be dirtied as
6972 * part of the process, so we iterate until it converges.
6973 */
6974void
6975spa_sync(spa_t *spa, uint64_t txg)
6976{
6977 dsl_pool_t *dp = spa->spa_dsl_pool;
6978 objset_t *mos = spa->spa_meta_objset;
428870ff 6979 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
34dc7c2f
BB
6980 vdev_t *rvd = spa->spa_root_vdev;
6981 vdev_t *vd;
34dc7c2f 6982 dmu_tx_t *tx;
b128c09f 6983 int error;
3dfb57a3
DB
6984 uint32_t max_queue_depth = zfs_vdev_async_write_max_active *
6985 zfs_vdev_queue_depth_pct / 100;
34dc7c2f 6986
572e2857
BB
6987 VERIFY(spa_writeable(spa));
6988
a1d477c2
MA
6989 /*
6990 * Wait for i/os issued in open context that need to complete
6991 * before this txg syncs.
6992 */
6993 VERIFY0(zio_wait(spa->spa_txg_zio[txg & TXG_MASK]));
6994 spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, 0);
6995
34dc7c2f
BB
6996 /*
6997 * Lock out configuration changes.
6998 */
b128c09f 6999 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f
BB
7000
7001 spa->spa_syncing_txg = txg;
7002 spa->spa_sync_pass = 0;
7003
3dfb57a3
DB
7004 mutex_enter(&spa->spa_alloc_lock);
7005 VERIFY0(avl_numnodes(&spa->spa_alloc_tree));
7006 mutex_exit(&spa->spa_alloc_lock);
7007
b128c09f
BB
7008 /*
7009 * If there are any pending vdev state changes, convert them
7010 * into config changes that go out with this transaction group.
7011 */
7012 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
fb5f0bc8
BB
7013 while (list_head(&spa->spa_state_dirty_list) != NULL) {
7014 /*
7015 * We need the write lock here because, for aux vdevs,
7016 * calling vdev_config_dirty() modifies sav_config.
7017 * This is ugly and will become unnecessary when we
7018 * eliminate the aux vdev wart by integrating all vdevs
7019 * into the root vdev tree.
7020 */
7021 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
7022 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
7023 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
7024 vdev_state_clean(vd);
7025 vdev_config_dirty(vd);
7026 }
7027 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
7028 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
b128c09f
BB
7029 }
7030 spa_config_exit(spa, SCL_STATE, FTAG);
7031
34dc7c2f
BB
7032 tx = dmu_tx_create_assigned(dp, txg);
7033
cc92e9d0 7034 spa->spa_sync_starttime = gethrtime();
57ddcda1
CC
7035 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
7036 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
79c76d5b 7037 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
cc92e9d0
GW
7038 NSEC_TO_TICK(spa->spa_deadman_synctime));
7039
34dc7c2f
BB
7040 /*
7041 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
7042 * set spa_deflate if we have no raid-z vdevs.
7043 */
7044 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
7045 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
7046 int i;
7047
7048 for (i = 0; i < rvd->vdev_children; i++) {
7049 vd = rvd->vdev_child[i];
7050 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
7051 break;
7052 }
7053 if (i == rvd->vdev_children) {
7054 spa->spa_deflate = TRUE;
7055 VERIFY(0 == zap_add(spa->spa_meta_objset,
7056 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
7057 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
7058 }
7059 }
7060
3dfb57a3
DB
7061 /*
7062 * Set the top-level vdev's max queue depth. Evaluate each
7063 * top-level's async write queue depth in case it changed.
7064 * The max queue depth will not change in the middle of syncing
7065 * out this txg.
7066 */
1c27024e
DB
7067 uint64_t queue_depth_total = 0;
7068 for (int c = 0; c < rvd->vdev_children; c++) {
3dfb57a3
DB
7069 vdev_t *tvd = rvd->vdev_child[c];
7070 metaslab_group_t *mg = tvd->vdev_mg;
7071
7072 if (mg == NULL || mg->mg_class != spa_normal_class(spa) ||
7073 !metaslab_group_initialized(mg))
7074 continue;
7075
7076 /*
7077 * It is safe to do a lock-free check here because only async
7078 * allocations look at mg_max_alloc_queue_depth, and async
7079 * allocations all happen from spa_sync().
7080 */
7081 ASSERT0(refcount_count(&mg->mg_alloc_queue_depth));
7082 mg->mg_max_alloc_queue_depth = max_queue_depth;
7083 queue_depth_total += mg->mg_max_alloc_queue_depth;
7084 }
1c27024e 7085 metaslab_class_t *mc = spa_normal_class(spa);
3dfb57a3
DB
7086 ASSERT0(refcount_count(&mc->mc_alloc_slots));
7087 mc->mc_alloc_max_slots = queue_depth_total;
7088 mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
7089
7090 ASSERT3U(mc->mc_alloc_max_slots, <=,
7091 max_queue_depth * rvd->vdev_children);
7092
a1d477c2
MA
7093 for (int c = 0; c < rvd->vdev_children; c++) {
7094 vdev_t *vd = rvd->vdev_child[c];
7095 vdev_indirect_state_sync_verify(vd);
7096
7097 if (vdev_indirect_should_condense(vd)) {
7098 spa_condense_indirect_start_sync(vd, tx);
7099 break;
7100 }
7101 }
7102
34dc7c2f
BB
7103 /*
7104 * Iterate to convergence.
7105 */
7106 do {
428870ff 7107 int pass = ++spa->spa_sync_pass;
34dc7c2f
BB
7108
7109 spa_sync_config_object(spa, tx);
7110 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
7111 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
7112 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
7113 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
7114 spa_errlog_sync(spa, txg);
7115 dsl_pool_sync(dp, txg);
7116
55d85d5a 7117 if (pass < zfs_sync_pass_deferred_free) {
e8b96c60 7118 spa_sync_frees(spa, free_bpl, tx);
428870ff 7119 } else {
905edb40
MA
7120 /*
7121 * We can not defer frees in pass 1, because
7122 * we sync the deferred frees later in pass 1.
7123 */
7124 ASSERT3U(pass, >, 1);
428870ff 7125 bplist_iterate(free_bpl, bpobj_enqueue_cb,
e8b96c60 7126 &spa->spa_deferred_bpobj, tx);
34dc7c2f
BB
7127 }
7128
428870ff
BB
7129 ddt_sync(spa, txg);
7130 dsl_scan_sync(dp, tx);
34dc7c2f 7131
a1d477c2
MA
7132 if (spa->spa_vdev_removal != NULL)
7133 svr_sync(spa, tx);
7134
7135 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
7136 != NULL)
428870ff
BB
7137 vdev_sync(vd, txg);
7138
905edb40 7139 if (pass == 1) {
428870ff 7140 spa_sync_upgrades(spa, tx);
905edb40
MA
7141 ASSERT3U(txg, >=,
7142 spa->spa_uberblock.ub_rootbp.blk_birth);
7143 /*
7144 * Note: We need to check if the MOS is dirty
7145 * because we could have marked the MOS dirty
7146 * without updating the uberblock (e.g. if we
7147 * have sync tasks but no dirty user data). We
7148 * need to check the uberblock's rootbp because
7149 * it is updated if we have synced out dirty
7150 * data (though in this case the MOS will most
7151 * likely also be dirty due to second order
7152 * effects, we don't want to rely on that here).
7153 */
7154 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
7155 !dmu_objset_is_dirty(mos, txg)) {
7156 /*
7157 * Nothing changed on the first pass,
7158 * therefore this TXG is a no-op. Avoid
7159 * syncing deferred frees, so that we
7160 * can keep this TXG as a no-op.
7161 */
7162 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
7163 txg));
7164 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
7165 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
7166 break;
7167 }
7168 spa_sync_deferred_frees(spa, tx);
7169 }
34dc7c2f 7170
428870ff 7171 } while (dmu_objset_is_dirty(mos, txg));
34dc7c2f 7172
33cf67cd 7173#ifdef ZFS_DEBUG
e0ab3ab5
JS
7174 if (!list_is_empty(&spa->spa_config_dirty_list)) {
7175 /*
7176 * Make sure that the number of ZAPs for all the vdevs matches
7177 * the number of ZAPs in the per-vdev ZAP list. This only gets
7178 * called if the config is dirty; otherwise there may be
7179 * outstanding AVZ operations that weren't completed in
7180 * spa_sync_config_object.
7181 */
7182 uint64_t all_vdev_zap_entry_count;
7183 ASSERT0(zap_count(spa->spa_meta_objset,
7184 spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
7185 ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
7186 all_vdev_zap_entry_count);
7187 }
33cf67cd 7188#endif
e0ab3ab5 7189
a1d477c2
MA
7190 if (spa->spa_vdev_removal != NULL) {
7191 ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]);
7192 }
7193
34dc7c2f
BB
7194 /*
7195 * Rewrite the vdev configuration (which includes the uberblock)
7196 * to commit the transaction group.
7197 *
7198 * If there are no dirty vdevs, we sync the uberblock to a few
7199 * random top-level vdevs that are known to be visible in the
b128c09f
BB
7200 * config cache (see spa_vdev_add() for a complete description).
7201 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
34dc7c2f 7202 */
b128c09f
BB
7203 for (;;) {
7204 /*
7205 * We hold SCL_STATE to prevent vdev open/close/etc.
7206 * while we're attempting to write the vdev labels.
7207 */
7208 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7209
7210 if (list_is_empty(&spa->spa_config_dirty_list)) {
7211 vdev_t *svd[SPA_DVAS_PER_BP];
7212 int svdcount = 0;
7213 int children = rvd->vdev_children;
7214 int c0 = spa_get_random(children);
b128c09f 7215
1c27024e 7216 for (int c = 0; c < children; c++) {
b128c09f 7217 vd = rvd->vdev_child[(c0 + c) % children];
a1d477c2
MA
7218 if (vd->vdev_ms_array == 0 || vd->vdev_islog ||
7219 !vdev_is_concrete(vd))
b128c09f
BB
7220 continue;
7221 svd[svdcount++] = vd;
7222 if (svdcount == SPA_DVAS_PER_BP)
7223 break;
7224 }
b6fcb792 7225 error = vdev_config_sync(svd, svdcount, txg);
b128c09f
BB
7226 } else {
7227 error = vdev_config_sync(rvd->vdev_child,
b6fcb792 7228 rvd->vdev_children, txg);
34dc7c2f 7229 }
34dc7c2f 7230
3bc7e0fb
GW
7231 if (error == 0)
7232 spa->spa_last_synced_guid = rvd->vdev_guid;
7233
b128c09f
BB
7234 spa_config_exit(spa, SCL_STATE, FTAG);
7235
7236 if (error == 0)
7237 break;
cec3a0a1 7238 zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR);
b128c09f
BB
7239 zio_resume_wait(spa);
7240 }
34dc7c2f
BB
7241 dmu_tx_commit(tx);
7242
57ddcda1 7243 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
cc92e9d0
GW
7244 spa->spa_deadman_tqid = 0;
7245
34dc7c2f
BB
7246 /*
7247 * Clear the dirty config list.
7248 */
b128c09f 7249 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
34dc7c2f
BB
7250 vdev_config_clean(vd);
7251
7252 /*
7253 * Now that the new config has synced transactionally,
7254 * let it become visible to the config cache.
7255 */
7256 if (spa->spa_config_syncing != NULL) {
7257 spa_config_set(spa, spa->spa_config_syncing);
7258 spa->spa_config_txg = txg;
7259 spa->spa_config_syncing = NULL;
7260 }
7261
428870ff 7262 dsl_pool_sync_done(dp, txg);
34dc7c2f 7263
3dfb57a3
DB
7264 mutex_enter(&spa->spa_alloc_lock);
7265 VERIFY0(avl_numnodes(&spa->spa_alloc_tree));
7266 mutex_exit(&spa->spa_alloc_lock);
7267
34dc7c2f
BB
7268 /*
7269 * Update usable space statistics.
7270 */
c65aa5b2 7271 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
34dc7c2f
BB
7272 vdev_sync_done(vd, txg);
7273
428870ff
BB
7274 spa_update_dspace(spa);
7275
34dc7c2f
BB
7276 /*
7277 * It had better be the case that we didn't dirty anything
7278 * since vdev_config_sync().
7279 */
7280 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
7281 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
7282 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
428870ff
BB
7283
7284 spa->spa_sync_pass = 0;
34dc7c2f 7285
55922e73
GW
7286 /*
7287 * Update the last synced uberblock here. We want to do this at
7288 * the end of spa_sync() so that consumers of spa_last_synced_txg()
7289 * will be guaranteed that all the processing associated with
7290 * that txg has been completed.
7291 */
7292 spa->spa_ubsync = spa->spa_uberblock;
b128c09f 7293 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 7294
428870ff
BB
7295 spa_handle_ignored_writes(spa);
7296
34dc7c2f
BB
7297 /*
7298 * If any async tasks have been requested, kick them off.
7299 */
7300 spa_async_dispatch(spa);
7301}
7302
7303/*
7304 * Sync all pools. We don't want to hold the namespace lock across these
7305 * operations, so we take a reference on the spa_t and drop the lock during the
7306 * sync.
7307 */
7308void
7309spa_sync_allpools(void)
7310{
7311 spa_t *spa = NULL;
7312 mutex_enter(&spa_namespace_lock);
7313 while ((spa = spa_next(spa)) != NULL) {
572e2857
BB
7314 if (spa_state(spa) != POOL_STATE_ACTIVE ||
7315 !spa_writeable(spa) || spa_suspended(spa))
34dc7c2f
BB
7316 continue;
7317 spa_open_ref(spa, FTAG);
7318 mutex_exit(&spa_namespace_lock);
7319 txg_wait_synced(spa_get_dsl(spa), 0);
7320 mutex_enter(&spa_namespace_lock);
7321 spa_close(spa, FTAG);
7322 }
7323 mutex_exit(&spa_namespace_lock);
7324}
7325
7326/*
7327 * ==========================================================================
7328 * Miscellaneous routines
7329 * ==========================================================================
7330 */
7331
7332/*
7333 * Remove all pools in the system.
7334 */
7335void
7336spa_evict_all(void)
7337{
7338 spa_t *spa;
7339
7340 /*
7341 * Remove all cached state. All pools should be closed now,
7342 * so every spa in the AVL tree should be unreferenced.
7343 */
7344 mutex_enter(&spa_namespace_lock);
7345 while ((spa = spa_next(NULL)) != NULL) {
7346 /*
7347 * Stop async tasks. The async thread may need to detach
7348 * a device that's been replaced, which requires grabbing
7349 * spa_namespace_lock, so we must drop it here.
7350 */
7351 spa_open_ref(spa, FTAG);
7352 mutex_exit(&spa_namespace_lock);
7353 spa_async_suspend(spa);
7354 mutex_enter(&spa_namespace_lock);
34dc7c2f
BB
7355 spa_close(spa, FTAG);
7356
7357 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
7358 spa_unload(spa);
7359 spa_deactivate(spa);
7360 }
7361 spa_remove(spa);
7362 }
7363 mutex_exit(&spa_namespace_lock);
7364}
7365
7366vdev_t *
9babb374 7367spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
34dc7c2f 7368{
b128c09f
BB
7369 vdev_t *vd;
7370 int i;
7371
7372 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
7373 return (vd);
7374
9babb374 7375 if (aux) {
b128c09f
BB
7376 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
7377 vd = spa->spa_l2cache.sav_vdevs[i];
9babb374
BB
7378 if (vd->vdev_guid == guid)
7379 return (vd);
7380 }
7381
7382 for (i = 0; i < spa->spa_spares.sav_count; i++) {
7383 vd = spa->spa_spares.sav_vdevs[i];
b128c09f
BB
7384 if (vd->vdev_guid == guid)
7385 return (vd);
7386 }
7387 }
7388
7389 return (NULL);
34dc7c2f
BB
7390}
7391
7392void
7393spa_upgrade(spa_t *spa, uint64_t version)
7394{
572e2857
BB
7395 ASSERT(spa_writeable(spa));
7396
b128c09f 7397 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
7398
7399 /*
7400 * This should only be called for a non-faulted pool, and since a
7401 * future version would result in an unopenable pool, this shouldn't be
7402 * possible.
7403 */
8dca0a9a 7404 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
9b67f605 7405 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
34dc7c2f
BB
7406
7407 spa->spa_uberblock.ub_version = version;
7408 vdev_config_dirty(spa->spa_root_vdev);
7409
b128c09f 7410 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
7411
7412 txg_wait_synced(spa_get_dsl(spa), 0);
7413}
7414
7415boolean_t
7416spa_has_spare(spa_t *spa, uint64_t guid)
7417{
7418 int i;
7419 uint64_t spareguid;
7420 spa_aux_vdev_t *sav = &spa->spa_spares;
7421
7422 for (i = 0; i < sav->sav_count; i++)
7423 if (sav->sav_vdevs[i]->vdev_guid == guid)
7424 return (B_TRUE);
7425
7426 for (i = 0; i < sav->sav_npending; i++) {
7427 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
7428 &spareguid) == 0 && spareguid == guid)
7429 return (B_TRUE);
7430 }
7431
7432 return (B_FALSE);
7433}
7434
b128c09f
BB
7435/*
7436 * Check if a pool has an active shared spare device.
7437 * Note: reference count of an active spare is 2, as a spare and as a replace
7438 */
7439static boolean_t
7440spa_has_active_shared_spare(spa_t *spa)
7441{
7442 int i, refcnt;
7443 uint64_t pool;
7444 spa_aux_vdev_t *sav = &spa->spa_spares;
7445
7446 for (i = 0; i < sav->sav_count; i++) {
7447 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7448 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7449 refcnt > 2)
7450 return (B_TRUE);
7451 }
7452
7453 return (B_FALSE);
7454}
7455
a1d477c2 7456sysevent_t *
12fa0466
DE
7457spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
7458{
7459 sysevent_t *ev = NULL;
7460#ifdef _KERNEL
7461 nvlist_t *resource;
7462
7463 resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl);
7464 if (resource) {
7465 ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP);
7466 ev->resource = resource;
7467 }
7468#endif
7469 return (ev);
7470}
7471
a1d477c2 7472void
12fa0466
DE
7473spa_event_post(sysevent_t *ev)
7474{
7475#ifdef _KERNEL
7476 if (ev) {
7477 zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb);
7478 kmem_free(ev, sizeof (*ev));
7479 }
7480#endif
7481}
7482
34dc7c2f 7483/*
fb390aaf
HR
7484 * Post a zevent corresponding to the given sysevent. The 'name' must be one
7485 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
34dc7c2f
BB
7486 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7487 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7488 * or zdb as real changes.
7489 */
7490void
12fa0466 7491spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
34dc7c2f 7492{
12fa0466 7493 spa_event_post(spa_event_create(spa, vd, hist_nvl, name));
34dc7c2f 7494}
c28b2279
BB
7495
7496#if defined(_KERNEL) && defined(HAVE_SPL)
7497/* state manipulation functions */
7498EXPORT_SYMBOL(spa_open);
7499EXPORT_SYMBOL(spa_open_rewind);
7500EXPORT_SYMBOL(spa_get_stats);
7501EXPORT_SYMBOL(spa_create);
c28b2279
BB
7502EXPORT_SYMBOL(spa_import);
7503EXPORT_SYMBOL(spa_tryimport);
7504EXPORT_SYMBOL(spa_destroy);
7505EXPORT_SYMBOL(spa_export);
7506EXPORT_SYMBOL(spa_reset);
7507EXPORT_SYMBOL(spa_async_request);
7508EXPORT_SYMBOL(spa_async_suspend);
7509EXPORT_SYMBOL(spa_async_resume);
7510EXPORT_SYMBOL(spa_inject_addref);
7511EXPORT_SYMBOL(spa_inject_delref);
7512EXPORT_SYMBOL(spa_scan_stat_init);
7513EXPORT_SYMBOL(spa_scan_get_stats);
7514
7515/* device maniion */
7516EXPORT_SYMBOL(spa_vdev_add);
7517EXPORT_SYMBOL(spa_vdev_attach);
7518EXPORT_SYMBOL(spa_vdev_detach);
c28b2279
BB
7519EXPORT_SYMBOL(spa_vdev_setpath);
7520EXPORT_SYMBOL(spa_vdev_setfru);
7521EXPORT_SYMBOL(spa_vdev_split_mirror);
7522
7523/* spare statech is global across all pools) */
7524EXPORT_SYMBOL(spa_spare_add);
7525EXPORT_SYMBOL(spa_spare_remove);
7526EXPORT_SYMBOL(spa_spare_exists);
7527EXPORT_SYMBOL(spa_spare_activate);
7528
7529/* L2ARC statech is global across all pools) */
7530EXPORT_SYMBOL(spa_l2cache_add);
7531EXPORT_SYMBOL(spa_l2cache_remove);
7532EXPORT_SYMBOL(spa_l2cache_exists);
7533EXPORT_SYMBOL(spa_l2cache_activate);
7534EXPORT_SYMBOL(spa_l2cache_drop);
7535
7536/* scanning */
7537EXPORT_SYMBOL(spa_scan);
7538EXPORT_SYMBOL(spa_scan_stop);
7539
7540/* spa syncing */
7541EXPORT_SYMBOL(spa_sync); /* only for DMU use */
7542EXPORT_SYMBOL(spa_sync_allpools);
7543
7544/* properties */
7545EXPORT_SYMBOL(spa_prop_set);
7546EXPORT_SYMBOL(spa_prop_get);
7547EXPORT_SYMBOL(spa_prop_clear_bootfs);
7548
7549/* asynchronous event notification */
7550EXPORT_SYMBOL(spa_event_notify);
7551#endif
dea377c0
MA
7552
7553#if defined(_KERNEL) && defined(HAVE_SPL)
7554module_param(spa_load_verify_maxinflight, int, 0644);
7555MODULE_PARM_DESC(spa_load_verify_maxinflight,
7556 "Max concurrent traversal I/Os while verifying pool during import -X");
7557
7558module_param(spa_load_verify_metadata, int, 0644);
7559MODULE_PARM_DESC(spa_load_verify_metadata,
7560 "Set to traverse metadata on pool import");
7561
7562module_param(spa_load_verify_data, int, 0644);
7563MODULE_PARM_DESC(spa_load_verify_data,
7564 "Set to traverse data on pool import");
dcb6bed1 7565
02730c33 7566/* CSTYLED */
dcb6bed1
D
7567module_param(zio_taskq_batch_pct, uint, 0444);
7568MODULE_PARM_DESC(zio_taskq_batch_pct,
7569 "Percentage of CPUs to run an IO worker thread");
7570
dea377c0 7571#endif