]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa.c
spa: Fix FreeBSD sysctl handlers
[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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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.
4f072827 24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
733b5722 25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
0c66c32d 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
3c67d83a 27 * Copyright 2013 Saso Kiselkov. All rights reserved.
e550644f
BB
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
a0bd735a 30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
f65fbee1 31 * Copyright 2018 Joyent, Inc.
3c819a2c 32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
12fa0466 33 * Copyright 2017 Joyent, Inc.
cc99f275 34 * Copyright (c) 2017, Intel Corporation.
658fb802 35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
9d618615 36 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
a38718a6 37 */
34dc7c2f 38
34dc7c2f 39/*
e49f1e20
WA
40 * SPA: Storage Pool Allocator
41 *
34dc7c2f
BB
42 * This file contains all the routines used when modifying on-disk SPA state.
43 * This includes opening, importing, destroying, exporting a pool, and syncing a
44 * pool.
45 */
46
47#include <sys/zfs_context.h>
48#include <sys/fm/fs/zfs.h>
49#include <sys/spa_impl.h>
50#include <sys/zio.h>
51#include <sys/zio_checksum.h>
34dc7c2f
BB
52#include <sys/dmu.h>
53#include <sys/dmu_tx.h>
54#include <sys/zap.h>
55#include <sys/zil.h>
67a1b037 56#include <sys/brt.h>
428870ff 57#include <sys/ddt.h>
34dc7c2f 58#include <sys/vdev_impl.h>
a1d477c2
MA
59#include <sys/vdev_removal.h>
60#include <sys/vdev_indirect_mapping.h>
61#include <sys/vdev_indirect_births.h>
619f0976 62#include <sys/vdev_initialize.h>
9a49d3f3 63#include <sys/vdev_rebuild.h>
1b939560 64#include <sys/vdev_trim.h>
c28b2279 65#include <sys/vdev_disk.h>
5caeef02 66#include <sys/vdev_raidz.h>
b2255edc 67#include <sys/vdev_draid.h>
34dc7c2f 68#include <sys/metaslab.h>
428870ff 69#include <sys/metaslab_impl.h>
379ca9cf 70#include <sys/mmp.h>
34dc7c2f
BB
71#include <sys/uberblock_impl.h>
72#include <sys/txg.h>
73#include <sys/avl.h>
a1d477c2 74#include <sys/bpobj.h>
34dc7c2f
BB
75#include <sys/dmu_traverse.h>
76#include <sys/dmu_objset.h>
77#include <sys/unique.h>
78#include <sys/dsl_pool.h>
79#include <sys/dsl_dataset.h>
80#include <sys/dsl_dir.h>
81#include <sys/dsl_prop.h>
82#include <sys/dsl_synctask.h>
83#include <sys/fs/zfs.h>
84#include <sys/arc.h>
85#include <sys/callb.h>
86#include <sys/systeminfo.h>
9babb374 87#include <sys/zfs_ioctl.h>
428870ff 88#include <sys/dsl_scan.h>
9ae529ec 89#include <sys/zfeature.h>
13fe0198 90#include <sys/dsl_destroy.h>
526af785 91#include <sys/zvol.h>
34dc7c2f 92
d164b209 93#ifdef _KERNEL
12fa0466
DE
94#include <sys/fm/protocol.h>
95#include <sys/fm/util.h>
428870ff 96#include <sys/callb.h>
d164b209 97#include <sys/zone.h>
c8242a96 98#include <sys/vmsystm.h>
d164b209
BB
99#endif /* _KERNEL */
100
34dc7c2f
BB
101#include "zfs_prop.h"
102#include "zfs_comutil.h"
3bd4df38 103#include <cityhash.h>
34dc7c2f 104
b5e60918
RN
105/*
106 * spa_thread() existed on Illumos as a parent thread for the various worker
107 * threads that actually run the pool, as a way to both reference the entire
108 * pool work as a single object, and to share properties like scheduling
109 * options. It has not yet been adapted to Linux or FreeBSD. This define is
110 * used to mark related parts of the code to make things easier for the reader,
111 * and to compile this code out. It can be removed when someone implements it,
112 * moves it to some Illumos-specific place, or removes it entirely.
113 */
114#undef HAVE_SPA_THREAD
115
116/*
117 * The "System Duty Cycle" scheduling class is an Illumos feature to help
118 * prevent CPU-intensive kernel threads from affecting latency on interactive
119 * threads. It doesn't exist on Linux or FreeBSD, so the supporting code is
120 * gated behind a define. On Illumos SDC depends on spa_thread(), but
121 * spa_thread() also has other uses, so this is a separate define.
122 */
123#undef HAVE_SYSDC
124
e6cfd633
WA
125/*
126 * The interval, in seconds, at which failed configuration cache file writes
127 * should be retried.
128 */
a1d477c2 129int zfs_ccw_retry_interval = 300;
e6cfd633 130
428870ff 131typedef enum zti_modes {
7ef5e54e 132 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
7457b024 133 ZTI_MODE_SCALE, /* Taskqs scale with CPUs. */
3bd4df38 134 ZTI_MODE_SYNC, /* sync thread assigned */
7ef5e54e
AL
135 ZTI_MODE_NULL, /* don't create a taskq */
136 ZTI_NMODES
428870ff 137} zti_modes_t;
34dc7c2f 138
7ef5e54e
AL
139#define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
140#define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
7457b024 141#define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
3bd4df38 142#define ZTI_SYNC { ZTI_MODE_SYNC, 0, 1 }
7ef5e54e 143#define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
9babb374 144
7ef5e54e
AL
145#define ZTI_N(n) ZTI_P(n, 1)
146#define ZTI_ONE ZTI_N(1)
9babb374
BB
147
148typedef struct zio_taskq_info {
7ef5e54e 149 zti_modes_t zti_mode;
428870ff 150 uint_t zti_value;
7ef5e54e 151 uint_t zti_count;
9babb374
BB
152} zio_taskq_info_t;
153
154static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
451041db 155 "iss", "iss_h", "int", "int_h"
9babb374
BB
156};
157
428870ff 158/*
7ef5e54e
AL
159 * This table defines the taskq settings for each ZFS I/O type. When
160 * initializing a pool, we use this table to create an appropriately sized
161 * taskq. Some operations are low volume and therefore have a small, static
162 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
3bd4df38 163 * macros. Other operations process a large amount of data; the ZTI_SCALE
7ef5e54e 164 * macro causes us to create a taskq oriented for throughput. Some operations
1b939560 165 * are so high frequency and short-lived that the taskq itself can become a
7ef5e54e
AL
166 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
167 * additional degree of parallelism specified by the number of threads per-
168 * taskq and the number of taskqs; when dispatching an event in this case, the
3bd4df38
EN
169 * particular taskq is chosen at random. ZTI_SCALE uses a number of taskqs
170 * that scales with the number of CPUs.
7ef5e54e
AL
171 *
172 * The different taskq priorities are to handle the different contexts (issue
173 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
174 * need to be handled with minimum delay.
428870ff 175 */
6930ecbb 176static zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
428870ff 177 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
7ef5e54e 178 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
7457b024 179 { ZTI_N(8), ZTI_NULL, ZTI_SCALE, ZTI_NULL }, /* READ */
3bd4df38 180 { ZTI_SYNC, ZTI_N(5), ZTI_SCALE, ZTI_N(5) }, /* WRITE */
7457b024 181 { ZTI_SCALE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
7ef5e54e
AL
182 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
183 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
1b939560 184 { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */
9babb374
BB
185};
186
13fe0198
MA
187static void spa_sync_version(void *arg, dmu_tx_t *tx);
188static void spa_sync_props(void *arg, dmu_tx_t *tx);
b128c09f 189static boolean_t spa_has_active_shared_spare(spa_t *spa);
a926aab9
AZ
190static int spa_load_impl(spa_t *spa, spa_import_type_t type,
191 const char **ereport);
572e2857 192static void spa_vdev_resilver_done(spa_t *spa);
428870ff 193
342357cd
AM
194/*
195 * Percentage of all CPUs that can be used by the metaslab preload taskq.
196 */
197static uint_t metaslab_preload_pct = 50;
198
18168da7
AZ
199static uint_t zio_taskq_batch_pct = 80; /* 1 thread per cpu in pset */
200static uint_t zio_taskq_batch_tpq; /* threads per taskq */
b5e60918
RN
201
202#ifdef HAVE_SYSDC
18168da7
AZ
203static const boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
204static const uint_t zio_taskq_basedc = 80; /* base duty cycle */
b5e60918 205#endif
428870ff 206
b5e60918 207#ifdef HAVE_SPA_THREAD
18168da7 208static const boolean_t spa_create_process = B_TRUE; /* no process => no sysdc */
b5e60918 209#endif
428870ff 210
3bd4df38
EN
211static uint_t zio_taskq_wr_iss_ncpus = 0;
212
afd2f7b7
PZ
213/*
214 * Report any spa_load_verify errors found, but do not fail spa_load.
215 * This is used by zdb to analyze non-idle pools.
216 */
217boolean_t spa_load_verify_dryrun = B_FALSE;
218
e39fe05b
FU
219/*
220 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
221 * This is used by zdb for spacemaps verification.
222 */
223boolean_t spa_mode_readable_spacemaps = B_FALSE;
224
428870ff
BB
225/*
226 * This (illegal) pool name is used when temporarily importing a spa_t in order
227 * to get the vdev stats associated with the imported devices.
228 */
229#define TRYIMPORT_NAME "$import"
34dc7c2f 230
6cb8e530
PZ
231/*
232 * For debugging purposes: print out vdev tree during pool import.
233 */
18168da7 234static int spa_load_print_vdev_tree = B_FALSE;
6cb8e530
PZ
235
236/*
237 * A non-zero value for zfs_max_missing_tvds means that we allow importing
238 * pools with missing top-level vdevs. This is strictly intended for advanced
239 * pool recovery cases since missing data is almost inevitable. Pools with
240 * missing devices can only be imported read-only for safety reasons, and their
241 * fail-mode will be automatically set to "continue".
242 *
243 * With 1 missing vdev we should be able to import the pool and mount all
244 * datasets. User data that was not modified after the missing device has been
245 * added should be recoverable. This means that snapshots created prior to the
246 * addition of that device should be completely intact.
247 *
248 * With 2 missing vdevs, some datasets may fail to mount since there are
249 * dataset statistics that are stored as regular metadata. Some data might be
250 * recoverable if those vdevs were added recently.
251 *
252 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
253 * may be missing entirely. Chances of data recovery are very low. Note that
254 * there are also risks of performing an inadvertent rewind as we might be
255 * missing all the vdevs with the latest uberblocks.
256 */
ab8d9c17 257uint64_t zfs_max_missing_tvds = 0;
6cb8e530
PZ
258
259/*
260 * The parameters below are similar to zfs_max_missing_tvds but are only
261 * intended for a preliminary open of the pool with an untrusted config which
262 * might be incomplete or out-dated.
263 *
264 * We are more tolerant for pools opened from a cachefile since we could have
265 * an out-dated cachefile where a device removal was not registered.
266 * We could have set the limit arbitrarily high but in the case where devices
267 * are really missing we would want to return the proper error codes; we chose
268 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
269 * and we get a chance to retrieve the trusted config.
270 */
271uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1;
d2734cce 272
6cb8e530
PZ
273/*
274 * In the case where config was assembled by scanning device paths (/dev/dsks
275 * by default) we are less tolerant since all the existing devices should have
276 * been detected and we want spa_load to return the right error codes.
277 */
278uint64_t zfs_max_missing_tvds_scan = 0;
279
d2734cce
SD
280/*
281 * Debugging aid that pauses spa_sync() towards the end.
282 */
18168da7 283static const boolean_t zfs_pause_spa_sync = B_FALSE;
d2734cce 284
37f03da8
SH
285/*
286 * Variables to indicate the livelist condense zthr func should wait at certain
287 * points for the livelist to be removed - used to test condense/destroy races
288 */
18168da7
AZ
289static int zfs_livelist_condense_zthr_pause = 0;
290static int zfs_livelist_condense_sync_pause = 0;
37f03da8
SH
291
292/*
293 * Variables to track whether or not condense cancellation has been
294 * triggered in testing.
295 */
18168da7
AZ
296static int zfs_livelist_condense_sync_cancel = 0;
297static int zfs_livelist_condense_zthr_cancel = 0;
37f03da8
SH
298
299/*
300 * Variable to track whether or not extra ALLOC blkptrs were added to a
301 * livelist entry while it was being condensed (caused by the way we track
302 * remapped blkptrs in dbuf_remap_impl)
303 */
18168da7 304static int zfs_livelist_condense_new_alloc = 0;
37f03da8 305
34dc7c2f
BB
306/*
307 * ==========================================================================
308 * SPA properties routines
309 * ==========================================================================
310 */
311
312/*
313 * Add a (source=src, propname=propval) list to an nvlist.
314 */
315static void
a926aab9 316spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, const char *strval,
34dc7c2f
BB
317 uint64_t intval, zprop_source_t src)
318{
319 const char *propname = zpool_prop_to_name(prop);
320 nvlist_t *propval;
321
65ad5d11
AJ
322 propval = fnvlist_alloc();
323 fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
34dc7c2f
BB
324
325 if (strval != NULL)
65ad5d11 326 fnvlist_add_string(propval, ZPROP_VALUE, strval);
34dc7c2f 327 else
65ad5d11 328 fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
34dc7c2f 329
65ad5d11 330 fnvlist_add_nvlist(nvl, propname, propval);
34dc7c2f
BB
331 nvlist_free(propval);
332}
333
8eae2d21
AJ
334/*
335 * Add a user property (source=src, propname=propval) to an nvlist.
336 */
337static void
338spa_prop_add_user(nvlist_t *nvl, const char *propname, char *strval,
339 zprop_source_t src)
340{
341 nvlist_t *propval;
342
343 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
344 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
345 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
346 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
347 nvlist_free(propval);
348}
349
34dc7c2f
BB
350/*
351 * Get property values from the spa configuration.
352 */
353static void
354spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
355{
1bd201e7 356 vdev_t *rvd = spa->spa_root_vdev;
9ae529ec 357 dsl_pool_t *pool = spa->spa_dsl_pool;
f3a7f661 358 uint64_t size, alloc, cap, version;
82ab6848 359 const zprop_source_t src = ZPROP_SRC_NONE;
b128c09f 360 spa_config_dirent_t *dp;
f3a7f661 361 metaslab_class_t *mc = spa_normal_class(spa);
b128c09f
BB
362
363 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
34dc7c2f 364
1bd201e7 365 if (rvd != NULL) {
cc99f275
DB
366 alloc = metaslab_class_get_alloc(mc);
367 alloc += metaslab_class_get_alloc(spa_special_class(spa));
368 alloc += metaslab_class_get_alloc(spa_dedup_class(spa));
aa755b35 369 alloc += metaslab_class_get_alloc(spa_embedded_log_class(spa));
cc99f275
DB
370
371 size = metaslab_class_get_space(mc);
372 size += metaslab_class_get_space(spa_special_class(spa));
373 size += metaslab_class_get_space(spa_dedup_class(spa));
aa755b35 374 size += metaslab_class_get_space(spa_embedded_log_class(spa));
cc99f275 375
d164b209
BB
376 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
377 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
428870ff
BB
378 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
379 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
380 size - alloc, src);
d2734cce
SD
381 spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL,
382 spa->spa_checkpoint_info.sci_dspace, src);
1bd201e7 383
f3a7f661
GW
384 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
385 metaslab_class_fragmentation(mc), src);
386 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
387 metaslab_class_expandable_space(mc), src);
572e2857 388 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
da92d5cb 389 (spa_mode(spa) == SPA_MODE_READ), src);
d164b209 390
428870ff 391 cap = (size == 0) ? 0 : (alloc * 100 / size);
d164b209
BB
392 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
393
428870ff
BB
394 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
395 ddt_get_pool_dedup_ratio(spa), src);
67a1b037
PJD
396 spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONEUSED, NULL,
397 brt_get_used(spa), src);
398 spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONESAVED, NULL,
399 brt_get_saved(spa), src);
400 spa_prop_add_list(*nvp, ZPOOL_PROP_BCLONERATIO, NULL,
401 brt_get_ratio(spa), src);
428870ff 402
d164b209 403 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
1bd201e7 404 rvd->vdev_state, src);
d164b209
BB
405
406 version = spa_version(spa);
82ab6848
HM
407 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) {
408 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
409 version, ZPROP_SRC_DEFAULT);
410 } else {
411 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL,
412 version, ZPROP_SRC_LOCAL);
413 }
a448a255
SD
414 spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID,
415 NULL, spa_load_guid(spa), src);
d164b209 416 }
34dc7c2f 417
9ae529ec 418 if (pool != NULL) {
9ae529ec
CS
419 /*
420 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
421 * when opening pools before this version freedir will be NULL.
422 */
fbeddd60 423 if (pool->dp_free_dir != NULL) {
9ae529ec 424 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
d683ddbb
JG
425 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
426 src);
9ae529ec
CS
427 } else {
428 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
429 NULL, 0, src);
430 }
fbeddd60
MA
431
432 if (pool->dp_leak_dir != NULL) {
433 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
d683ddbb
JG
434 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
435 src);
fbeddd60
MA
436 } else {
437 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
438 NULL, 0, src);
439 }
9ae529ec
CS
440 }
441
34dc7c2f 442 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
34dc7c2f 443
d96eb2b1
DM
444 if (spa->spa_comment != NULL) {
445 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
446 0, ZPROP_SRC_LOCAL);
447 }
448
658fb802
CB
449 if (spa->spa_compatibility != NULL) {
450 spa_prop_add_list(*nvp, ZPOOL_PROP_COMPATIBILITY,
451 spa->spa_compatibility, 0, ZPROP_SRC_LOCAL);
452 }
453
34dc7c2f
BB
454 if (spa->spa_root != NULL)
455 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
456 0, ZPROP_SRC_LOCAL);
457
f1512ee6
MA
458 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
459 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
460 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
461 } else {
462 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
463 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
464 }
465
50c957f7
NB
466 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) {
467 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
468 DNODE_MAX_SIZE, ZPROP_SRC_NONE);
469 } else {
470 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL,
471 DNODE_MIN_SIZE, ZPROP_SRC_NONE);
472 }
473
b128c09f
BB
474 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
475 if (dp->scd_path == NULL) {
34dc7c2f 476 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
b128c09f
BB
477 "none", 0, ZPROP_SRC_LOCAL);
478 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
34dc7c2f 479 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
b128c09f 480 dp->scd_path, 0, ZPROP_SRC_LOCAL);
34dc7c2f
BB
481 }
482 }
483}
484
485/*
486 * Get zpool property values.
487 */
488int
489spa_prop_get(spa_t *spa, nvlist_t **nvp)
490{
428870ff 491 objset_t *mos = spa->spa_meta_objset;
34dc7c2f
BB
492 zap_cursor_t zc;
493 zap_attribute_t za;
1743c737 494 dsl_pool_t *dp;
34dc7c2f
BB
495 int err;
496
79c76d5b 497 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP);
c28b2279 498 if (err)
d1d7e268 499 return (err);
34dc7c2f 500
1743c737
AM
501 dp = spa_get_dsl(spa);
502 dsl_pool_config_enter(dp, FTAG);
b128c09f
BB
503 mutex_enter(&spa->spa_props_lock);
504
34dc7c2f
BB
505 /*
506 * Get properties from the spa config.
507 */
508 spa_prop_get_config(spa, nvp);
509
34dc7c2f 510 /* If no pool property object, no more prop to get. */
1743c737 511 if (mos == NULL || spa->spa_pool_props_object == 0)
c28b2279 512 goto out;
34dc7c2f
BB
513
514 /*
515 * Get properties from the MOS pool property object.
516 */
517 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
518 (err = zap_cursor_retrieve(&zc, &za)) == 0;
519 zap_cursor_advance(&zc)) {
520 uint64_t intval = 0;
521 char *strval = NULL;
522 zprop_source_t src = ZPROP_SRC_DEFAULT;
523 zpool_prop_t prop;
524
8eae2d21
AJ
525 if ((prop = zpool_name_to_prop(za.za_name)) ==
526 ZPOOL_PROP_INVAL && !zfs_prop_user(za.za_name))
34dc7c2f
BB
527 continue;
528
529 switch (za.za_integer_length) {
530 case 8:
531 /* integer property */
532 if (za.za_first_integer !=
533 zpool_prop_default_numeric(prop))
534 src = ZPROP_SRC_LOCAL;
535
536 if (prop == ZPOOL_PROP_BOOTFS) {
34dc7c2f
BB
537 dsl_dataset_t *ds = NULL;
538
619f0976
GW
539 err = dsl_dataset_hold_obj(dp,
540 za.za_first_integer, FTAG, &ds);
1743c737 541 if (err != 0)
34dc7c2f 542 break;
34dc7c2f 543
eca7b760 544 strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
79c76d5b 545 KM_SLEEP);
34dc7c2f 546 dsl_dataset_name(ds, strval);
b128c09f 547 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
548 } else {
549 strval = NULL;
550 intval = za.za_first_integer;
551 }
552
553 spa_prop_add_list(*nvp, prop, strval, intval, src);
554
555 if (strval != NULL)
eca7b760 556 kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
34dc7c2f
BB
557
558 break;
559
560 case 1:
561 /* string property */
79c76d5b 562 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
34dc7c2f
BB
563 err = zap_lookup(mos, spa->spa_pool_props_object,
564 za.za_name, 1, za.za_num_integers, strval);
565 if (err) {
566 kmem_free(strval, za.za_num_integers);
567 break;
568 }
8eae2d21
AJ
569 if (prop != ZPOOL_PROP_INVAL) {
570 spa_prop_add_list(*nvp, prop, strval, 0, src);
571 } else {
572 src = ZPROP_SRC_LOCAL;
573 spa_prop_add_user(*nvp, za.za_name, strval,
574 src);
575 }
34dc7c2f
BB
576 kmem_free(strval, za.za_num_integers);
577 break;
578
579 default:
580 break;
581 }
582 }
583 zap_cursor_fini(&zc);
34dc7c2f 584out:
1743c737
AM
585 mutex_exit(&spa->spa_props_lock);
586 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
587 if (err && err != ENOENT) {
588 nvlist_free(*nvp);
589 *nvp = NULL;
590 return (err);
591 }
592
593 return (0);
594}
595
596/*
597 * Validate the given pool properties nvlist and modify the list
598 * for the property values to be set.
599 */
600static int
601spa_prop_validate(spa_t *spa, nvlist_t *props)
602{
603 nvpair_t *elem;
604 int error = 0, reset_bootfs = 0;
d4ed6673 605 uint64_t objnum = 0;
9ae529ec 606 boolean_t has_feature = B_FALSE;
34dc7c2f
BB
607
608 elem = NULL;
609 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
34dc7c2f 610 uint64_t intval;
d1807f16 611 const char *strval, *slash, *check, *fname;
9ae529ec
CS
612 const char *propname = nvpair_name(elem);
613 zpool_prop_t prop = zpool_name_to_prop(propname);
614
31864e3d
BB
615 switch (prop) {
616 case ZPOOL_PROP_INVAL:
9ae529ec
CS
617 /*
618 * Sanitize the input.
619 */
8eae2d21
AJ
620 if (zfs_prop_user(propname)) {
621 if (strlen(propname) >= ZAP_MAXNAMELEN) {
622 error = SET_ERROR(ENAMETOOLONG);
623 break;
624 }
9ae529ec 625
8eae2d21
AJ
626 if (strlen(fnvpair_value_string(elem)) >=
627 ZAP_MAXVALUELEN) {
628 error = SET_ERROR(E2BIG);
629 break;
630 }
631 } else if (zpool_prop_feature(propname)) {
632 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
633 error = SET_ERROR(EINVAL);
634 break;
635 }
34dc7c2f 636
8eae2d21
AJ
637 if (nvpair_value_uint64(elem, &intval) != 0) {
638 error = SET_ERROR(EINVAL);
639 break;
640 }
641
642 if (intval != 0) {
643 error = SET_ERROR(EINVAL);
644 break;
645 }
646
647 fname = strchr(propname, '@') + 1;
648 if (zfeature_lookup_name(fname, NULL) != 0) {
649 error = SET_ERROR(EINVAL);
650 break;
651 }
34dc7c2f 652
8eae2d21
AJ
653 has_feature = B_TRUE;
654 } else {
2e528b49 655 error = SET_ERROR(EINVAL);
9ae529ec
CS
656 break;
657 }
9ae529ec 658 break;
34dc7c2f 659
34dc7c2f
BB
660 case ZPOOL_PROP_VERSION:
661 error = nvpair_value_uint64(elem, &intval);
662 if (!error &&
9ae529ec
CS
663 (intval < spa_version(spa) ||
664 intval > SPA_VERSION_BEFORE_FEATURES ||
665 has_feature))
2e528b49 666 error = SET_ERROR(EINVAL);
34dc7c2f
BB
667 break;
668
669 case ZPOOL_PROP_DELEGATION:
670 case ZPOOL_PROP_AUTOREPLACE:
b128c09f 671 case ZPOOL_PROP_LISTSNAPS:
9babb374 672 case ZPOOL_PROP_AUTOEXPAND:
1b939560 673 case ZPOOL_PROP_AUTOTRIM:
34dc7c2f
BB
674 error = nvpair_value_uint64(elem, &intval);
675 if (!error && intval > 1)
2e528b49 676 error = SET_ERROR(EINVAL);
34dc7c2f
BB
677 break;
678
379ca9cf
OF
679 case ZPOOL_PROP_MULTIHOST:
680 error = nvpair_value_uint64(elem, &intval);
681 if (!error && intval > 1)
682 error = SET_ERROR(EINVAL);
683
25f06d67
BB
684 if (!error) {
685 uint32_t hostid = zone_get_hostid(NULL);
686 if (hostid)
687 spa->spa_hostid = hostid;
688 else
689 error = SET_ERROR(ENOTSUP);
690 }
379ca9cf
OF
691
692 break;
693
34dc7c2f 694 case ZPOOL_PROP_BOOTFS:
9babb374
BB
695 /*
696 * If the pool version is less than SPA_VERSION_BOOTFS,
697 * or the pool is still being created (version == 0),
698 * the bootfs property cannot be set.
699 */
34dc7c2f 700 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
2e528b49 701 error = SET_ERROR(ENOTSUP);
34dc7c2f
BB
702 break;
703 }
704
705 /*
b128c09f 706 * Make sure the vdev config is bootable
34dc7c2f 707 */
b128c09f 708 if (!vdev_is_bootable(spa->spa_root_vdev)) {
2e528b49 709 error = SET_ERROR(ENOTSUP);
34dc7c2f
BB
710 break;
711 }
712
713 reset_bootfs = 1;
714
715 error = nvpair_value_string(elem, &strval);
716
717 if (!error) {
9ae529ec 718 objset_t *os;
b128c09f 719
34dc7c2f
BB
720 if (strval == NULL || strval[0] == '\0') {
721 objnum = zpool_prop_default_numeric(
722 ZPOOL_PROP_BOOTFS);
723 break;
724 }
725
d1d7e268 726 error = dmu_objset_hold(strval, FTAG, &os);
619f0976 727 if (error != 0)
34dc7c2f 728 break;
b128c09f 729
eaa25f1a 730 /* Must be ZPL. */
428870ff 731 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2e528b49 732 error = SET_ERROR(ENOTSUP);
b128c09f
BB
733 } else {
734 objnum = dmu_objset_id(os);
735 }
428870ff 736 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
737 }
738 break;
b128c09f 739
34dc7c2f
BB
740 case ZPOOL_PROP_FAILUREMODE:
741 error = nvpair_value_uint64(elem, &intval);
3bfd95d5 742 if (!error && intval > ZIO_FAILURE_MODE_PANIC)
2e528b49 743 error = SET_ERROR(EINVAL);
34dc7c2f
BB
744
745 /*
746 * This is a special case which only occurs when
747 * the pool has completely failed. This allows
748 * the user to change the in-core failmode property
749 * without syncing it out to disk (I/Os might
750 * currently be blocked). We do this by returning
751 * EIO to the caller (spa_prop_set) to trick it
752 * into thinking we encountered a property validation
753 * error.
754 */
b128c09f 755 if (!error && spa_suspended(spa)) {
34dc7c2f 756 spa->spa_failmode = intval;
2e528b49 757 error = SET_ERROR(EIO);
34dc7c2f
BB
758 }
759 break;
760
761 case ZPOOL_PROP_CACHEFILE:
762 if ((error = nvpair_value_string(elem, &strval)) != 0)
763 break;
764
765 if (strval[0] == '\0')
766 break;
767
768 if (strcmp(strval, "none") == 0)
769 break;
770
771 if (strval[0] != '/') {
2e528b49 772 error = SET_ERROR(EINVAL);
34dc7c2f
BB
773 break;
774 }
775
776 slash = strrchr(strval, '/');
777 ASSERT(slash != NULL);
778
779 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
780 strcmp(slash, "/..") == 0)
2e528b49 781 error = SET_ERROR(EINVAL);
34dc7c2f 782 break;
428870ff 783
d96eb2b1
DM
784 case ZPOOL_PROP_COMMENT:
785 if ((error = nvpair_value_string(elem, &strval)) != 0)
786 break;
787 for (check = strval; *check != '\0'; check++) {
788 if (!isprint(*check)) {
2e528b49 789 error = SET_ERROR(EINVAL);
d96eb2b1
DM
790 break;
791 }
d96eb2b1
DM
792 }
793 if (strlen(strval) > ZPROP_MAX_COMMENT)
2e528b49 794 error = SET_ERROR(E2BIG);
d96eb2b1
DM
795 break;
796
e75c13c3
BB
797 default:
798 break;
34dc7c2f
BB
799 }
800
801 if (error)
802 break;
803 }
804
050d720c
MA
805 (void) nvlist_remove_all(props,
806 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO));
807
34dc7c2f
BB
808 if (!error && reset_bootfs) {
809 error = nvlist_remove(props,
810 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
811
812 if (!error) {
813 error = nvlist_add_uint64(props,
814 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
815 }
816 }
817
818 return (error);
819}
820
d164b209
BB
821void
822spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
823{
d1807f16 824 const char *cachefile;
d164b209
BB
825 spa_config_dirent_t *dp;
826
827 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
828 &cachefile) != 0)
829 return;
830
831 dp = kmem_alloc(sizeof (spa_config_dirent_t),
79c76d5b 832 KM_SLEEP);
d164b209
BB
833
834 if (cachefile[0] == '\0')
835 dp->scd_path = spa_strdup(spa_config_path);
836 else if (strcmp(cachefile, "none") == 0)
837 dp->scd_path = NULL;
838 else
839 dp->scd_path = spa_strdup(cachefile);
840
841 list_insert_head(&spa->spa_config_list, dp);
842 if (need_sync)
843 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
844}
845
34dc7c2f
BB
846int
847spa_prop_set(spa_t *spa, nvlist_t *nvp)
848{
849 int error;
9ae529ec 850 nvpair_t *elem = NULL;
d164b209 851 boolean_t need_sync = B_FALSE;
34dc7c2f
BB
852
853 if ((error = spa_prop_validate(spa, nvp)) != 0)
854 return (error);
855
d164b209 856 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
9ae529ec 857 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
d164b209 858
572e2857
BB
859 if (prop == ZPOOL_PROP_CACHEFILE ||
860 prop == ZPOOL_PROP_ALTROOT ||
861 prop == ZPOOL_PROP_READONLY)
d164b209
BB
862 continue;
863
8eae2d21
AJ
864 if (prop == ZPOOL_PROP_INVAL &&
865 zfs_prop_user(nvpair_name(elem))) {
866 need_sync = B_TRUE;
867 break;
868 }
869
31864e3d 870 if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) {
2a673e76 871 uint64_t ver = 0;
9ae529ec
CS
872
873 if (prop == ZPOOL_PROP_VERSION) {
874 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
875 } else {
876 ASSERT(zpool_prop_feature(nvpair_name(elem)));
877 ver = SPA_VERSION_FEATURES;
878 need_sync = B_TRUE;
879 }
880
881 /* Save time if the version is already set. */
882 if (ver == spa_version(spa))
883 continue;
884
885 /*
886 * In addition to the pool directory object, we might
887 * create the pool properties object, the features for
888 * read object, the features for write object, or the
889 * feature descriptions object.
890 */
13fe0198 891 error = dsl_sync_task(spa->spa_name, NULL,
3d45fdd6
MA
892 spa_sync_version, &ver,
893 6, ZFS_SPACE_CHECK_RESERVED);
9ae529ec
CS
894 if (error)
895 return (error);
896 continue;
897 }
898
d164b209
BB
899 need_sync = B_TRUE;
900 break;
901 }
902
9ae529ec 903 if (need_sync) {
13fe0198 904 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
3d45fdd6 905 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
9ae529ec
CS
906 }
907
908 return (0);
34dc7c2f
BB
909}
910
911/*
912 * If the bootfs property value is dsobj, clear it.
913 */
914void
915spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
916{
917 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
918 VERIFY(zap_remove(spa->spa_meta_objset,
919 spa->spa_pool_props_object,
920 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
921 spa->spa_bootfs = 0;
922 }
923}
924
3bc7e0fb 925static int
13fe0198 926spa_change_guid_check(void *arg, dmu_tx_t *tx)
3bc7e0fb 927{
2a8ba608 928 uint64_t *newguid __maybe_unused = arg;
13fe0198 929 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3bc7e0fb
GW
930 vdev_t *rvd = spa->spa_root_vdev;
931 uint64_t vdev_state;
3bc7e0fb 932
d2734cce
SD
933 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
934 int error = (spa_has_checkpoint(spa)) ?
935 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
936 return (SET_ERROR(error));
937 }
938
3bc7e0fb
GW
939 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
940 vdev_state = rvd->vdev_state;
941 spa_config_exit(spa, SCL_STATE, FTAG);
942
943 if (vdev_state != VDEV_STATE_HEALTHY)
2e528b49 944 return (SET_ERROR(ENXIO));
3bc7e0fb
GW
945
946 ASSERT3U(spa_guid(spa), !=, *newguid);
947
948 return (0);
949}
950
951static void
13fe0198 952spa_change_guid_sync(void *arg, dmu_tx_t *tx)
3bc7e0fb 953{
13fe0198
MA
954 uint64_t *newguid = arg;
955 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3bc7e0fb
GW
956 uint64_t oldguid;
957 vdev_t *rvd = spa->spa_root_vdev;
958
959 oldguid = spa_guid(spa);
960
961 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
962 rvd->vdev_guid = *newguid;
963 rvd->vdev_guid_sum += (*newguid - oldguid);
964 vdev_config_dirty(rvd);
965 spa_config_exit(spa, SCL_STATE, FTAG);
966
6f1ffb06 967 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
74756182 968 (u_longlong_t)oldguid, (u_longlong_t)*newguid);
3bc7e0fb
GW
969}
970
3541dc6d
GA
971/*
972 * Change the GUID for the pool. This is done so that we can later
973 * re-import a pool built from a clone of our own vdevs. We will modify
974 * the root vdev's guid, our own pool guid, and then mark all of our
975 * vdevs dirty. Note that we must make sure that all our vdevs are
976 * online when we do this, or else any vdevs that weren't present
977 * would be orphaned from our pool. We are also going to issue a
978 * sysevent to update any watchers.
979 */
980int
981spa_change_guid(spa_t *spa)
982{
3bc7e0fb
GW
983 int error;
984 uint64_t guid;
3541dc6d 985
621dd7bb 986 mutex_enter(&spa->spa_vdev_top_lock);
3bc7e0fb
GW
987 mutex_enter(&spa_namespace_lock);
988 guid = spa_generate_guid(NULL);
3541dc6d 989
13fe0198 990 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
3d45fdd6 991 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
3541dc6d 992
3bc7e0fb 993 if (error == 0) {
55c12724
AH
994 /*
995 * Clear the kobj flag from all the vdevs to allow
996 * vdev_cache_process_kobj_evt() to post events to all the
997 * vdevs since GUID is updated.
998 */
999 vdev_clear_kobj_evt(spa->spa_root_vdev);
1000 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
1001 vdev_clear_kobj_evt(spa->spa_l2cache.sav_vdevs[i]);
1002
1003 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
12fa0466 1004 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID);
3bc7e0fb 1005 }
3541dc6d 1006
3bc7e0fb 1007 mutex_exit(&spa_namespace_lock);
621dd7bb 1008 mutex_exit(&spa->spa_vdev_top_lock);
3541dc6d 1009
3bc7e0fb 1010 return (error);
3541dc6d
GA
1011}
1012
34dc7c2f
BB
1013/*
1014 * ==========================================================================
1015 * SPA state manipulation (open/create/destroy/import/export)
1016 * ==========================================================================
1017 */
1018
1019static int
1020spa_error_entry_compare(const void *a, const void *b)
1021{
ee36c709
GN
1022 const spa_error_entry_t *sa = (const spa_error_entry_t *)a;
1023 const spa_error_entry_t *sb = (const spa_error_entry_t *)b;
34dc7c2f
BB
1024 int ret;
1025
ee36c709 1026 ret = memcmp(&sa->se_bookmark, &sb->se_bookmark,
5dbd68a3 1027 sizeof (zbookmark_phys_t));
34dc7c2f 1028
ca577779 1029 return (TREE_ISIGN(ret));
34dc7c2f
BB
1030}
1031
1032/*
1033 * Utility function which retrieves copies of the current logs and
1034 * re-initializes them in the process.
1035 */
1036void
1037spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
1038{
1039 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
1040
861166b0
AZ
1041 memcpy(last, &spa->spa_errlist_last, sizeof (avl_tree_t));
1042 memcpy(scrub, &spa->spa_errlist_scrub, sizeof (avl_tree_t));
34dc7c2f
BB
1043
1044 avl_create(&spa->spa_errlist_scrub,
1045 spa_error_entry_compare, sizeof (spa_error_entry_t),
1046 offsetof(spa_error_entry_t, se_avl));
1047 avl_create(&spa->spa_errlist_last,
1048 spa_error_entry_compare, sizeof (spa_error_entry_t),
1049 offsetof(spa_error_entry_t, se_avl));
1050}
1051
7ef5e54e
AL
1052static void
1053spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
34dc7c2f 1054{
7ef5e54e
AL
1055 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
1056 enum zti_modes mode = ztip->zti_mode;
1057 uint_t value = ztip->zti_value;
1058 uint_t count = ztip->zti_count;
1059 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
7457b024 1060 uint_t cpus, flags = TASKQ_DYNAMIC;
34dc7c2f 1061
e8b96c60
MA
1062 switch (mode) {
1063 case ZTI_MODE_FIXED:
7457b024 1064 ASSERT3U(value, >, 0);
e8b96c60 1065 break;
7ef5e54e 1066
3bd4df38
EN
1067 case ZTI_MODE_SYNC:
1068
1069 /*
1070 * Create one wr_iss taskq for every 'zio_taskq_wr_iss_ncpus',
1071 * not to exceed the number of spa allocators.
1072 */
1073 if (zio_taskq_wr_iss_ncpus == 0) {
1074 count = MAX(boot_ncpus / spa->spa_alloc_count, 1);
1075 } else {
1076 count = MAX(1,
1077 boot_ncpus / MAX(1, zio_taskq_wr_iss_ncpus));
1078 }
1079 count = MAX(count, (zio_taskq_batch_pct + 99) / 100);
1080 count = MIN(count, spa->spa_alloc_count);
1081
1082 /*
1083 * zio_taskq_batch_pct is unbounded and may exceed 100%, but no
1084 * single taskq may have more threads than 100% of online cpus.
1085 */
1086 value = (zio_taskq_batch_pct + count / 2) / count;
1087 value = MIN(value, 100);
e8b96c60 1088 flags |= TASKQ_THREADS_CPU_PCT;
e8b96c60 1089 break;
7ef5e54e 1090
7457b024
AM
1091 case ZTI_MODE_SCALE:
1092 flags |= TASKQ_THREADS_CPU_PCT;
1093 /*
1094 * We want more taskqs to reduce lock contention, but we want
1095 * less for better request ordering and CPU utilization.
1096 */
1097 cpus = MAX(1, boot_ncpus * zio_taskq_batch_pct / 100);
1098 if (zio_taskq_batch_tpq > 0) {
1099 count = MAX(1, (cpus + zio_taskq_batch_tpq / 2) /
1100 zio_taskq_batch_tpq);
1101 } else {
1102 /*
1103 * Prefer 6 threads per taskq, but no more taskqs
1104 * than threads in them on large systems. For 80%:
1105 *
1106 * taskq taskq total
1107 * cpus taskqs percent threads threads
1108 * ------- ------- ------- ------- -------
1109 * 1 1 80% 1 1
1110 * 2 1 80% 1 1
1111 * 4 1 80% 3 3
1112 * 8 2 40% 3 6
1113 * 16 3 27% 4 12
1114 * 32 5 16% 5 25
1115 * 64 7 11% 7 49
1116 * 128 10 8% 10 100
1117 * 256 14 6% 15 210
1118 */
1119 count = 1 + cpus / 6;
1120 while (count * count > cpus)
1121 count--;
1122 }
1123 /* Limit each taskq within 100% to not trigger assertion. */
1124 count = MAX(count, (zio_taskq_batch_pct + 99) / 100);
1125 value = (zio_taskq_batch_pct + count / 2) / count;
1126 break;
1127
1128 case ZTI_MODE_NULL:
1129 tqs->stqs_count = 0;
1130 tqs->stqs_taskq = NULL;
1131 return;
1132
e8b96c60
MA
1133 default:
1134 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
3bd4df38 1135 "spa_taskqs_init()",
e8b96c60
MA
1136 zio_type_name[t], zio_taskq_types[q], mode, value);
1137 break;
1138 }
7ef5e54e 1139
7457b024
AM
1140 ASSERT3U(count, >, 0);
1141 tqs->stqs_count = count;
1142 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
1143
1c27024e 1144 for (uint_t i = 0; i < count; i++) {
e8b96c60 1145 taskq_t *tq;
af430294 1146 char name[32];
7ef5e54e 1147
7457b024
AM
1148 if (count > 1)
1149 (void) snprintf(name, sizeof (name), "%s_%s_%u",
1150 zio_type_name[t], zio_taskq_types[q], i);
1151 else
1152 (void) snprintf(name, sizeof (name), "%s_%s",
1153 zio_type_name[t], zio_taskq_types[q]);
7ef5e54e 1154
b5e60918 1155#ifdef HAVE_SYSDC
7ef5e54e 1156 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
18168da7 1157 (void) zio_taskq_basedc;
7ef5e54e
AL
1158 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
1159 spa->spa_proc, zio_taskq_basedc, flags);
1160 } else {
b5e60918 1161#endif
e8b96c60
MA
1162 pri_t pri = maxclsyspri;
1163 /*
1164 * The write issue taskq can be extremely CPU
1229323d 1165 * intensive. Run it at slightly less important
7432d297
MM
1166 * priority than the other taskqs.
1167 *
1168 * Under Linux and FreeBSD this means incrementing
1169 * the priority value as opposed to platforms like
1170 * illumos where it should be decremented.
1171 *
1172 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1173 * are equal then a difference between them is
1174 * insignificant.
e8b96c60 1175 */
7432d297
MM
1176 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) {
1177#if defined(__linux__)
1229323d 1178 pri++;
7432d297
MM
1179#elif defined(__FreeBSD__)
1180 pri += 4;
1181#else
1182#error "unknown OS"
1183#endif
1184 }
e8b96c60 1185 tq = taskq_create_proc(name, value, pri, 50,
7ef5e54e 1186 INT_MAX, spa->spa_proc, flags);
b5e60918 1187#ifdef HAVE_SYSDC
7ef5e54e 1188 }
b5e60918 1189#endif
7ef5e54e
AL
1190
1191 tqs->stqs_taskq[i] = tq;
1192 }
1193}
1194
1195static void
1196spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
1197{
1198 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
7ef5e54e
AL
1199
1200 if (tqs->stqs_taskq == NULL) {
1201 ASSERT3U(tqs->stqs_count, ==, 0);
1202 return;
1203 }
1204
1c27024e 1205 for (uint_t i = 0; i < tqs->stqs_count; i++) {
7ef5e54e
AL
1206 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
1207 taskq_destroy(tqs->stqs_taskq[i]);
428870ff 1208 }
34dc7c2f 1209
7ef5e54e
AL
1210 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
1211 tqs->stqs_taskq = NULL;
1212}
34dc7c2f 1213
6930ecbb
RN
1214#ifdef _KERNEL
1215/*
1216 * The READ and WRITE rows of zio_taskqs are configurable at module load time
1217 * by setting zio_taskq_read or zio_taskq_write.
1218 *
1219 * Example (the defaults for READ and WRITE)
1220 * zio_taskq_read='fixed,1,8 null scale null'
1221 * zio_taskq_write='sync fixed,1,5 scale fixed,1,5'
1222 *
1223 * Each sets the entire row at a time.
1224 *
1225 * 'fixed' is parameterised: fixed,Q,T where Q is number of taskqs, T is number
1226 * of threads per taskq.
1227 *
1228 * 'null' can only be set on the high-priority queues (queue selection for
1229 * high-priority queues will fall back to the regular queue if the high-pri
1230 * is NULL.
1231 */
1232static const char *const modes[ZTI_NMODES] = {
1233 "fixed", "scale", "sync", "null"
1234};
1235
1236/* Parse the incoming config string. Modifies cfg */
1237static int
1238spa_taskq_param_set(zio_type_t t, char *cfg)
1239{
1240 int err = 0;
1241
1242 zio_taskq_info_t row[ZIO_TASKQ_TYPES] = {{0}};
1243
1244 char *next = cfg, *tok, *c;
1245
1246 /*
1247 * Parse out each element from the string and fill `row`. The entire
1248 * row has to be set at once, so any errors are flagged by just
1249 * breaking out of this loop early.
1250 */
1251 uint_t q;
1252 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
1253 /* `next` is the start of the config */
1254 if (next == NULL)
1255 break;
1256
1257 /* Eat up leading space */
1258 while (isspace(*next))
1259 next++;
1260 if (*next == '\0')
1261 break;
1262
1263 /* Mode ends at space or end of string */
1264 tok = next;
1265 next = strchr(tok, ' ');
1266 if (next != NULL) *next++ = '\0';
1267
1268 /* Parameters start after a comma */
1269 c = strchr(tok, ',');
1270 if (c != NULL) *c++ = '\0';
1271
1272 /* Match mode string */
1273 uint_t mode;
1274 for (mode = 0; mode < ZTI_NMODES; mode++)
1275 if (strcmp(tok, modes[mode]) == 0)
1276 break;
1277 if (mode == ZTI_NMODES)
1278 break;
1279
1280 /* Invalid canary */
1281 row[q].zti_mode = ZTI_NMODES;
1282
1283 /* Per-mode setup */
1284 switch (mode) {
1285
1286 /*
1287 * FIXED is parameterised: number of queues, and number of
1288 * threads per queue.
1289 */
1290 case ZTI_MODE_FIXED: {
1291 /* No parameters? */
1292 if (c == NULL || *c == '\0')
1293 break;
1294
1295 /* Find next parameter */
1296 tok = c;
1297 c = strchr(tok, ',');
1298 if (c == NULL)
1299 break;
1300
1301 /* Take digits and convert */
1302 unsigned long long nq;
1303 if (!(isdigit(*tok)))
1304 break;
1305 err = ddi_strtoull(tok, &tok, 10, &nq);
1306 /* Must succeed and also end at the next param sep */
1307 if (err != 0 || tok != c)
1308 break;
1309
1310 /* Move past the comma */
1311 tok++;
1312 /* Need another number */
1313 if (!(isdigit(*tok)))
1314 break;
1315 /* Remember start to make sure we moved */
1316 c = tok;
1317
1318 /* Take digits */
1319 unsigned long long ntpq;
1320 err = ddi_strtoull(tok, &tok, 10, &ntpq);
1321 /* Must succeed, and moved forward */
1322 if (err != 0 || tok == c || *tok != '\0')
1323 break;
1324
1325 /*
1326 * sanity; zero queues/threads make no sense, and
1327 * 16K is almost certainly more than anyone will ever
1328 * need and avoids silly numbers like UINT32_MAX
1329 */
1330 if (nq == 0 || nq >= 16384 ||
1331 ntpq == 0 || ntpq >= 16384)
1332 break;
1333
1334 const zio_taskq_info_t zti = ZTI_P(ntpq, nq);
1335 row[q] = zti;
1336 break;
1337 }
1338
1339 case ZTI_MODE_SCALE: {
1340 const zio_taskq_info_t zti = ZTI_SCALE;
1341 row[q] = zti;
1342 break;
1343 }
1344
1345 case ZTI_MODE_SYNC: {
1346 const zio_taskq_info_t zti = ZTI_SYNC;
1347 row[q] = zti;
1348 break;
1349 }
1350
1351 case ZTI_MODE_NULL: {
1352 /*
1353 * Can only null the high-priority queues; the general-
1354 * purpose ones have to exist.
1355 */
1356 if (q != ZIO_TASKQ_ISSUE_HIGH &&
1357 q != ZIO_TASKQ_INTERRUPT_HIGH)
1358 break;
1359
1360 const zio_taskq_info_t zti = ZTI_NULL;
1361 row[q] = zti;
1362 break;
1363 }
1364
1365 default:
1366 break;
1367 }
1368
1369 /* Ensure we set a mode */
1370 if (row[q].zti_mode == ZTI_NMODES)
1371 break;
1372 }
1373
1374 /* Didn't get a full row, fail */
1375 if (q < ZIO_TASKQ_TYPES)
1376 return (SET_ERROR(EINVAL));
1377
1378 /* Eat trailing space */
1379 if (next != NULL)
1380 while (isspace(*next))
1381 next++;
1382
1383 /* If there's anything left over then fail */
1384 if (next != NULL && *next != '\0')
1385 return (SET_ERROR(EINVAL));
1386
1387 /* Success! Copy it into the real config */
1388 for (q = 0; q < ZIO_TASKQ_TYPES; q++)
1389 zio_taskqs[t][q] = row[q];
1390
1391 return (0);
1392}
1393
1394static int
1395spa_taskq_param_get(zio_type_t t, char *buf)
1396{
1397 int pos = 0;
1398
1399 /* Build paramater string from live config */
1400 const char *sep = "";
1401 for (uint_t q = 0; q < ZIO_TASKQ_TYPES; q++) {
1402 const zio_taskq_info_t *zti = &zio_taskqs[t][q];
1403 if (zti->zti_mode == ZTI_MODE_FIXED)
1404 pos += sprintf(&buf[pos], "%s%s,%u,%u", sep,
1405 modes[zti->zti_mode], zti->zti_count,
1406 zti->zti_value);
1407 else
1408 pos += sprintf(&buf[pos], "%s%s", sep,
1409 modes[zti->zti_mode]);
1410 sep = " ";
1411 }
1412
1413 buf[pos++] = '\n';
1414 buf[pos] = '\0';
1415
1416 return (pos);
1417}
1418
1419#ifdef __linux__
1420static int
1421spa_taskq_read_param_set(const char *val, zfs_kernel_param_t *kp)
1422{
1423 char *cfg = kmem_strdup(val);
1424 int err = spa_taskq_param_set(ZIO_TYPE_READ, cfg);
1425 kmem_free(cfg, strlen(val)+1);
1426 return (-err);
1427}
1428static int
1429spa_taskq_read_param_get(char *buf, zfs_kernel_param_t *kp)
1430{
1431 return (spa_taskq_param_get(ZIO_TYPE_READ, buf));
1432}
1433
1434static int
1435spa_taskq_write_param_set(const char *val, zfs_kernel_param_t *kp)
1436{
1437 char *cfg = kmem_strdup(val);
1438 int err = spa_taskq_param_set(ZIO_TYPE_WRITE, cfg);
1439 kmem_free(cfg, strlen(val)+1);
1440 return (-err);
1441}
1442static int
1443spa_taskq_write_param_get(char *buf, zfs_kernel_param_t *kp)
1444{
1445 return (spa_taskq_param_get(ZIO_TYPE_WRITE, buf));
1446}
1447#else
6930ecbb
RN
1448/*
1449 * On FreeBSD load-time parameters can be set up before malloc() is available,
1450 * so we have to do all the parsing work on the stack.
1451 */
1452#define SPA_TASKQ_PARAM_MAX (128)
1453
1454static int
1455spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS)
1456{
1457 char buf[SPA_TASKQ_PARAM_MAX];
3bddc4da 1458 int err;
6930ecbb 1459
3bddc4da 1460 (void) spa_taskq_param_get(ZIO_TYPE_READ, buf);
6930ecbb 1461 err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
3bddc4da 1462 if (err || req->newptr == NULL)
6930ecbb
RN
1463 return (err);
1464 return (spa_taskq_param_set(ZIO_TYPE_READ, buf));
1465}
1466
1467static int
1468spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS)
1469{
1470 char buf[SPA_TASKQ_PARAM_MAX];
3bddc4da 1471 int err;
6930ecbb 1472
3bddc4da 1473 (void) spa_taskq_param_get(ZIO_TYPE_WRITE, buf);
6930ecbb 1474 err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
3bddc4da 1475 if (err || req->newptr == NULL)
6930ecbb
RN
1476 return (err);
1477 return (spa_taskq_param_set(ZIO_TYPE_WRITE, buf));
1478}
1479#endif
1480#endif /* _KERNEL */
1481
7ef5e54e
AL
1482/*
1483 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1484 * Note that a type may have multiple discrete taskqs to avoid lock contention
3bd4df38 1485 * on the taskq itself.
7ef5e54e 1486 */
3bd4df38
EN
1487static taskq_t *
1488spa_taskq_dispatch_select(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1489 zio_t *zio)
7ef5e54e
AL
1490{
1491 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1492 taskq_t *tq;
1493
1494 ASSERT3P(tqs->stqs_taskq, !=, NULL);
1495 ASSERT3U(tqs->stqs_count, !=, 0);
1496
3bd4df38
EN
1497 if ((t == ZIO_TYPE_WRITE) && (q == ZIO_TASKQ_ISSUE) &&
1498 (zio != NULL) && (zio->io_wr_iss_tq != NULL)) {
1499 /* dispatch to assigned write issue taskq */
1500 tq = zio->io_wr_iss_tq;
1501 return (tq);
1502 }
1503
7ef5e54e
AL
1504 if (tqs->stqs_count == 1) {
1505 tq = tqs->stqs_taskq[0];
1506 } else {
c12936b1 1507 tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count];
428870ff 1508 }
3bd4df38
EN
1509 return (tq);
1510}
7ef5e54e 1511
3bd4df38
EN
1512void
1513spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1514 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent,
1515 zio_t *zio)
1516{
1517 taskq_t *tq = spa_taskq_dispatch_select(spa, t, q, zio);
7ef5e54e 1518 taskq_dispatch_ent(tq, func, arg, flags, ent);
428870ff
BB
1519}
1520
044baf00
BB
1521/*
1522 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1523 */
1524void
1525spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
1526 task_func_t *func, void *arg, uint_t flags)
1527{
3bd4df38
EN
1528 taskq_t *tq = spa_taskq_dispatch_select(spa, t, q, NULL);
1529 taskqid_t id = taskq_dispatch(tq, func, arg, flags);
044baf00
BB
1530 if (id)
1531 taskq_wait_id(tq, id);
1532}
1533
428870ff
BB
1534static void
1535spa_create_zio_taskqs(spa_t *spa)
1536{
1c27024e
DB
1537 for (int t = 0; t < ZIO_TYPES; t++) {
1538 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
7ef5e54e 1539 spa_taskqs_init(spa, t, q);
428870ff
BB
1540 }
1541 }
1542}
9babb374 1543
7b89a549 1544#if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
428870ff
BB
1545static void
1546spa_thread(void *arg)
1547{
93ce2b4c 1548 psetid_t zio_taskq_psrset_bind = PS_NONE;
428870ff 1549 callb_cpr_t cprinfo;
9babb374 1550
428870ff
BB
1551 spa_t *spa = arg;
1552 user_t *pu = PTOU(curproc);
9babb374 1553
428870ff
BB
1554 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1555 spa->spa_name);
9babb374 1556
428870ff
BB
1557 ASSERT(curproc != &p0);
1558 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1559 "zpool-%s", spa->spa_name);
1560 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1561
1562 /* bind this thread to the requested psrset */
1563 if (zio_taskq_psrset_bind != PS_NONE) {
1564 pool_lock();
1565 mutex_enter(&cpu_lock);
1566 mutex_enter(&pidlock);
1567 mutex_enter(&curproc->p_lock);
1568
1569 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1570 0, NULL, NULL) == 0) {
1571 curthread->t_bind_pset = zio_taskq_psrset_bind;
1572 } else {
1573 cmn_err(CE_WARN,
1574 "Couldn't bind process for zfs pool \"%s\" to "
1575 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1576 }
1577
1578 mutex_exit(&curproc->p_lock);
1579 mutex_exit(&pidlock);
1580 mutex_exit(&cpu_lock);
1581 pool_unlock();
1582 }
1583
b5e60918 1584#ifdef HAVE_SYSDC
428870ff
BB
1585 if (zio_taskq_sysdc) {
1586 sysdc_thread_enter(curthread, 100, 0);
1587 }
b5e60918 1588#endif
428870ff
BB
1589
1590 spa->spa_proc = curproc;
1591 spa->spa_did = curthread->t_did;
1592
1593 spa_create_zio_taskqs(spa);
1594
1595 mutex_enter(&spa->spa_proc_lock);
1596 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1597
1598 spa->spa_proc_state = SPA_PROC_ACTIVE;
1599 cv_broadcast(&spa->spa_proc_cv);
1600
1601 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1602 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1603 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1604 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1605
1606 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1607 spa->spa_proc_state = SPA_PROC_GONE;
1608 spa->spa_proc = &p0;
1609 cv_broadcast(&spa->spa_proc_cv);
1610 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1611
1612 mutex_enter(&curproc->p_lock);
1613 lwp_exit();
1614}
1615#endif
1616
95f71c01
EN
1617extern metaslab_ops_t *metaslab_allocator(spa_t *spa);
1618
428870ff
BB
1619/*
1620 * Activate an uninitialized pool.
1621 */
1622static void
da92d5cb 1623spa_activate(spa_t *spa, spa_mode_t mode)
428870ff 1624{
95f71c01 1625 metaslab_ops_t *msp = metaslab_allocator(spa);
428870ff
BB
1626 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1627
1628 spa->spa_state = POOL_STATE_ACTIVE;
1629 spa->spa_mode = mode;
e39fe05b 1630 spa->spa_read_spacemaps = spa_mode_readable_spacemaps;
428870ff 1631
95f71c01
EN
1632 spa->spa_normal_class = metaslab_class_create(spa, msp);
1633 spa->spa_log_class = metaslab_class_create(spa, msp);
1634 spa->spa_embedded_log_class = metaslab_class_create(spa, msp);
1635 spa->spa_special_class = metaslab_class_create(spa, msp);
1636 spa->spa_dedup_class = metaslab_class_create(spa, msp);
428870ff
BB
1637
1638 /* Try to create a covering process */
1639 mutex_enter(&spa->spa_proc_lock);
1640 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1641 ASSERT(spa->spa_proc == &p0);
1642 spa->spa_did = 0;
1643
7b89a549 1644#ifdef HAVE_SPA_THREAD
428870ff
BB
1645 /* Only create a process if we're going to be around a while. */
1646 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1647 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1648 NULL, 0) == 0) {
1649 spa->spa_proc_state = SPA_PROC_CREATED;
1650 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1651 cv_wait(&spa->spa_proc_cv,
1652 &spa->spa_proc_lock);
9babb374 1653 }
428870ff
BB
1654 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1655 ASSERT(spa->spa_proc != &p0);
1656 ASSERT(spa->spa_did != 0);
1657 } else {
1658#ifdef _KERNEL
1659 cmn_err(CE_WARN,
1660 "Couldn't create process for zfs pool \"%s\"\n",
1661 spa->spa_name);
1662#endif
b128c09f 1663 }
34dc7c2f 1664 }
7b89a549 1665#endif /* HAVE_SPA_THREAD */
428870ff
BB
1666 mutex_exit(&spa->spa_proc_lock);
1667
1668 /* If we didn't create a process, we need to create our taskqs. */
1669 if (spa->spa_proc == &p0) {
1670 spa_create_zio_taskqs(spa);
1671 }
34dc7c2f 1672
619f0976
GW
1673 for (size_t i = 0; i < TXG_SIZE; i++) {
1674 spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL,
1675 ZIO_FLAG_CANFAIL);
1676 }
a1d477c2 1677
b128c09f
BB
1678 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1679 offsetof(vdev_t, vdev_config_dirty_node));
0c66c32d
JG
1680 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1681 offsetof(objset_t, os_evicting_node));
b128c09f
BB
1682 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1683 offsetof(vdev_t, vdev_state_dirty_node));
34dc7c2f 1684
4747a7d3 1685 txg_list_create(&spa->spa_vdev_txg_list, spa,
34dc7c2f
BB
1686 offsetof(struct vdev, vdev_txg_node));
1687
1688 avl_create(&spa->spa_errlist_scrub,
1689 spa_error_entry_compare, sizeof (spa_error_entry_t),
1690 offsetof(spa_error_entry_t, se_avl));
1691 avl_create(&spa->spa_errlist_last,
1692 spa_error_entry_compare, sizeof (spa_error_entry_t),
1693 offsetof(spa_error_entry_t, se_avl));
e8cf3a4f
AP
1694 avl_create(&spa->spa_errlist_healed,
1695 spa_error_entry_compare, sizeof (spa_error_entry_t),
1696 offsetof(spa_error_entry_t, se_avl));
a0bd735a 1697
4759342a
JL
1698 spa_activate_os(spa);
1699
b5256303
TC
1700 spa_keystore_init(&spa->spa_keystore);
1701
a0bd735a
BP
1702 /*
1703 * This taskq is used to perform zvol-minor-related tasks
1704 * asynchronously. This has several advantages, including easy
d0249a4b 1705 * resolution of various deadlocks.
a0bd735a
BP
1706 *
1707 * The taskq must be single threaded to ensure tasks are always
1708 * processed in the order in which they were dispatched.
1709 *
1710 * A taskq per pool allows one to keep the pools independent.
1711 * This way if one pool is suspended, it will not impact another.
1712 *
1713 * The preferred location to dispatch a zvol minor task is a sync
1714 * task. In this context, there is easy access to the spa_t and minimal
1715 * error handling is required because the sync task must succeed.
1716 */
1717 spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri,
1718 1, INT_MAX, 0);
1de321e6 1719
342357cd
AM
1720 /*
1721 * The taskq to preload metaslabs.
1722 */
1723 spa->spa_metaslab_taskq = taskq_create("z_metaslab",
1724 metaslab_preload_pct, maxclsyspri, 1, INT_MAX,
1725 TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
1726
77d8a0f1 1727 /*
1728 * Taskq dedicated to prefetcher threads: this is used to prevent the
1729 * pool traverse code from monopolizing the global (and limited)
1730 * system_taskq by inappropriately scheduling long running tasks on it.
1731 */
60a4c7d2
PD
1732 spa->spa_prefetch_taskq = taskq_create("z_prefetch", 100,
1733 defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
77d8a0f1 1734
1de321e6
JX
1735 /*
1736 * The taskq to upgrade datasets in this pool. Currently used by
9c5167d1 1737 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1de321e6 1738 */
60a4c7d2
PD
1739 spa->spa_upgrade_taskq = taskq_create("z_upgrade", 100,
1740 defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT);
34dc7c2f
BB
1741}
1742
1743/*
1744 * Opposite of spa_activate().
1745 */
1746static void
1747spa_deactivate(spa_t *spa)
1748{
34dc7c2f
BB
1749 ASSERT(spa->spa_sync_on == B_FALSE);
1750 ASSERT(spa->spa_dsl_pool == NULL);
1751 ASSERT(spa->spa_root_vdev == NULL);
9babb374 1752 ASSERT(spa->spa_async_zio_root == NULL);
34dc7c2f
BB
1753 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1754
0c66c32d
JG
1755 spa_evicting_os_wait(spa);
1756
a0bd735a
BP
1757 if (spa->spa_zvol_taskq) {
1758 taskq_destroy(spa->spa_zvol_taskq);
1759 spa->spa_zvol_taskq = NULL;
1760 }
1761
342357cd
AM
1762 if (spa->spa_metaslab_taskq) {
1763 taskq_destroy(spa->spa_metaslab_taskq);
1764 spa->spa_metaslab_taskq = NULL;
1765 }
1766
77d8a0f1 1767 if (spa->spa_prefetch_taskq) {
1768 taskq_destroy(spa->spa_prefetch_taskq);
1769 spa->spa_prefetch_taskq = NULL;
1770 }
1771
1de321e6
JX
1772 if (spa->spa_upgrade_taskq) {
1773 taskq_destroy(spa->spa_upgrade_taskq);
1774 spa->spa_upgrade_taskq = NULL;
1775 }
1776
34dc7c2f
BB
1777 txg_list_destroy(&spa->spa_vdev_txg_list);
1778
b128c09f 1779 list_destroy(&spa->spa_config_dirty_list);
0c66c32d 1780 list_destroy(&spa->spa_evicting_os_list);
b128c09f 1781 list_destroy(&spa->spa_state_dirty_list);
34dc7c2f 1782
57ddcda1 1783 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
cc92e9d0 1784
1c27024e
DB
1785 for (int t = 0; t < ZIO_TYPES; t++) {
1786 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
7ef5e54e 1787 spa_taskqs_fini(spa, t, q);
b128c09f 1788 }
34dc7c2f
BB
1789 }
1790
a1d477c2
MA
1791 for (size_t i = 0; i < TXG_SIZE; i++) {
1792 ASSERT3P(spa->spa_txg_zio[i], !=, NULL);
1793 VERIFY0(zio_wait(spa->spa_txg_zio[i]));
1794 spa->spa_txg_zio[i] = NULL;
1795 }
1796
34dc7c2f
BB
1797 metaslab_class_destroy(spa->spa_normal_class);
1798 spa->spa_normal_class = NULL;
1799
1800 metaslab_class_destroy(spa->spa_log_class);
1801 spa->spa_log_class = NULL;
1802
aa755b35
MA
1803 metaslab_class_destroy(spa->spa_embedded_log_class);
1804 spa->spa_embedded_log_class = NULL;
1805
cc99f275
DB
1806 metaslab_class_destroy(spa->spa_special_class);
1807 spa->spa_special_class = NULL;
1808
1809 metaslab_class_destroy(spa->spa_dedup_class);
1810 spa->spa_dedup_class = NULL;
1811
34dc7c2f
BB
1812 /*
1813 * If this was part of an import or the open otherwise failed, we may
1814 * still have errors left in the queues. Empty them just in case.
1815 */
1816 spa_errlog_drain(spa);
34dc7c2f
BB
1817 avl_destroy(&spa->spa_errlist_scrub);
1818 avl_destroy(&spa->spa_errlist_last);
e8cf3a4f 1819 avl_destroy(&spa->spa_errlist_healed);
34dc7c2f 1820
b5256303
TC
1821 spa_keystore_fini(&spa->spa_keystore);
1822
34dc7c2f 1823 spa->spa_state = POOL_STATE_UNINITIALIZED;
428870ff
BB
1824
1825 mutex_enter(&spa->spa_proc_lock);
1826 if (spa->spa_proc_state != SPA_PROC_NONE) {
1827 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1828 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1829 cv_broadcast(&spa->spa_proc_cv);
1830 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1831 ASSERT(spa->spa_proc != &p0);
1832 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1833 }
1834 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1835 spa->spa_proc_state = SPA_PROC_NONE;
1836 }
1837 ASSERT(spa->spa_proc == &p0);
1838 mutex_exit(&spa->spa_proc_lock);
1839
1840 /*
1841 * We want to make sure spa_thread() has actually exited the ZFS
1842 * module, so that the module can't be unloaded out from underneath
1843 * it.
1844 */
1845 if (spa->spa_did != 0) {
1846 thread_join(spa->spa_did);
1847 spa->spa_did = 0;
1848 }
4759342a
JL
1849
1850 spa_deactivate_os(spa);
1851
34dc7c2f
BB
1852}
1853
1854/*
1855 * Verify a pool configuration, and construct the vdev tree appropriately. This
1856 * will create all the necessary vdevs in the appropriate layout, with each vdev
1857 * in the CLOSED state. This will prep the pool before open/creation/import.
1858 * All vdev validation is done by the vdev_alloc() routine.
1859 */
4a22ba5b 1860int
34dc7c2f
BB
1861spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1862 uint_t id, int atype)
1863{
1864 nvlist_t **child;
9babb374 1865 uint_t children;
34dc7c2f
BB
1866 int error;
1867
1868 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1869 return (error);
1870
1871 if ((*vdp)->vdev_ops->vdev_op_leaf)
1872 return (0);
1873
b128c09f
BB
1874 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1875 &child, &children);
1876
1877 if (error == ENOENT)
1878 return (0);
1879
1880 if (error) {
34dc7c2f
BB
1881 vdev_free(*vdp);
1882 *vdp = NULL;
2e528b49 1883 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1884 }
1885
1c27024e 1886 for (int c = 0; c < children; c++) {
34dc7c2f
BB
1887 vdev_t *vd;
1888 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1889 atype)) != 0) {
1890 vdev_free(*vdp);
1891 *vdp = NULL;
1892 return (error);
1893 }
1894 }
1895
1896 ASSERT(*vdp != NULL);
1897
1898 return (0);
1899}
1900
93e28d66
SD
1901static boolean_t
1902spa_should_flush_logs_on_unload(spa_t *spa)
1903{
1904 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
1905 return (B_FALSE);
1906
1907 if (!spa_writeable(spa))
1908 return (B_FALSE);
1909
1910 if (!spa->spa_sync_on)
1911 return (B_FALSE);
1912
1913 if (spa_state(spa) != POOL_STATE_EXPORTED)
1914 return (B_FALSE);
1915
1916 if (zfs_keep_log_spacemaps_at_export)
1917 return (B_FALSE);
1918
1919 return (B_TRUE);
1920}
1921
1922/*
1923 * Opens a transaction that will set the flag that will instruct
1924 * spa_sync to attempt to flush all the metaslabs for that txg.
1925 */
1926static void
1927spa_unload_log_sm_flush_all(spa_t *spa)
1928{
1929 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
1930 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
1931
1932 ASSERT3U(spa->spa_log_flushall_txg, ==, 0);
1933 spa->spa_log_flushall_txg = dmu_tx_get_txg(tx);
1934
1935 dmu_tx_commit(tx);
1936 txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg);
1937}
1938
1939static void
1940spa_unload_log_sm_metadata(spa_t *spa)
1941{
1942 void *cookie = NULL;
1943 spa_log_sm_t *sls;
b3ad3f48
AM
1944 log_summary_entry_t *e;
1945
93e28d66
SD
1946 while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg,
1947 &cookie)) != NULL) {
1948 VERIFY0(sls->sls_mscount);
1949 kmem_free(sls, sizeof (spa_log_sm_t));
1950 }
1951
b3ad3f48 1952 while ((e = list_remove_head(&spa->spa_log_summary)) != NULL) {
93e28d66 1953 VERIFY0(e->lse_mscount);
93e28d66
SD
1954 kmem_free(e, sizeof (log_summary_entry_t));
1955 }
1956
1957 spa->spa_unflushed_stats.sus_nblocks = 0;
1958 spa->spa_unflushed_stats.sus_memused = 0;
1959 spa->spa_unflushed_stats.sus_blocklimit = 0;
1960}
1961
37f03da8
SH
1962static void
1963spa_destroy_aux_threads(spa_t *spa)
1964{
1965 if (spa->spa_condense_zthr != NULL) {
1966 zthr_destroy(spa->spa_condense_zthr);
1967 spa->spa_condense_zthr = NULL;
1968 }
1969 if (spa->spa_checkpoint_discard_zthr != NULL) {
1970 zthr_destroy(spa->spa_checkpoint_discard_zthr);
1971 spa->spa_checkpoint_discard_zthr = NULL;
1972 }
1973 if (spa->spa_livelist_delete_zthr != NULL) {
1974 zthr_destroy(spa->spa_livelist_delete_zthr);
1975 spa->spa_livelist_delete_zthr = NULL;
1976 }
1977 if (spa->spa_livelist_condense_zthr != NULL) {
1978 zthr_destroy(spa->spa_livelist_condense_zthr);
1979 spa->spa_livelist_condense_zthr = NULL;
1980 }
5caeef02
DB
1981 if (spa->spa_raidz_expand_zthr != NULL) {
1982 zthr_destroy(spa->spa_raidz_expand_zthr);
1983 spa->spa_raidz_expand_zthr = NULL;
1984 }
37f03da8
SH
1985}
1986
34dc7c2f
BB
1987/*
1988 * Opposite of spa_load().
1989 */
1990static void
1991spa_unload(spa_t *spa)
1992{
b128c09f 1993 ASSERT(MUTEX_HELD(&spa_namespace_lock));
93e28d66 1994 ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED);
b128c09f 1995
ca95f70d 1996 spa_import_progress_remove(spa_guid(spa));
4a0ee12a
PZ
1997 spa_load_note(spa, "UNLOADING");
1998
e60e158e
JG
1999 spa_wake_waiters(spa);
2000
93e28d66 2001 /*
2fb52853
GA
2002 * If we have set the spa_final_txg, we have already performed the
2003 * tasks below in spa_export_common(). We should not redo it here since
2004 * we delay the final TXGs beyond what spa_final_txg is set at.
93e28d66 2005 */
2fb52853
GA
2006 if (spa->spa_final_txg == UINT64_MAX) {
2007 /*
2008 * If the log space map feature is enabled and the pool is
2009 * getting exported (but not destroyed), we want to spend some
2010 * time flushing as many metaslabs as we can in an attempt to
2011 * destroy log space maps and save import time.
2012 */
2013 if (spa_should_flush_logs_on_unload(spa))
2014 spa_unload_log_sm_flush_all(spa);
93e28d66 2015
2fb52853
GA
2016 /*
2017 * Stop async tasks.
2018 */
2019 spa_async_suspend(spa);
34dc7c2f 2020
2fb52853
GA
2021 if (spa->spa_root_vdev) {
2022 vdev_t *root_vdev = spa->spa_root_vdev;
2023 vdev_initialize_stop_all(root_vdev,
2024 VDEV_INITIALIZE_ACTIVE);
2025 vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE);
2026 vdev_autotrim_stop_all(spa);
2027 vdev_rebuild_stop_all(spa);
2028 }
619f0976
GW
2029 }
2030
34dc7c2f
BB
2031 /*
2032 * Stop syncing.
2033 */
2034 if (spa->spa_sync_on) {
2035 txg_sync_stop(spa->spa_dsl_pool);
2036 spa->spa_sync_on = B_FALSE;
2037 }
2038
4e21fd06 2039 /*
93e28d66
SD
2040 * This ensures that there is no async metaslab prefetching
2041 * while we attempt to unload the spa.
4e21fd06 2042 */
342357cd 2043 taskq_wait(spa->spa_metaslab_taskq);
4e21fd06 2044
379ca9cf
OF
2045 if (spa->spa_mmp.mmp_thread)
2046 mmp_thread_stop(spa);
2047
34dc7c2f 2048 /*
b128c09f 2049 * Wait for any outstanding async I/O to complete.
34dc7c2f 2050 */
9babb374 2051 if (spa->spa_async_zio_root != NULL) {
1c27024e 2052 for (int i = 0; i < max_ncpus; i++)
e022864d
MA
2053 (void) zio_wait(spa->spa_async_zio_root[i]);
2054 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
9babb374
BB
2055 spa->spa_async_zio_root = NULL;
2056 }
34dc7c2f 2057
a1d477c2
MA
2058 if (spa->spa_vdev_removal != NULL) {
2059 spa_vdev_removal_destroy(spa->spa_vdev_removal);
2060 spa->spa_vdev_removal = NULL;
2061 }
2062
37f03da8 2063 spa_destroy_aux_threads(spa);
d2734cce 2064
a1d477c2
MA
2065 spa_condense_fini(spa);
2066
428870ff
BB
2067 bpobj_close(&spa->spa_deferred_bpobj);
2068
619f0976 2069 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
93cf2076
GW
2070
2071 /*
2072 * Close all vdevs.
2073 */
2074 if (spa->spa_root_vdev)
2075 vdev_free(spa->spa_root_vdev);
2076 ASSERT(spa->spa_root_vdev == NULL);
2077
34dc7c2f
BB
2078 /*
2079 * Close the dsl pool.
2080 */
2081 if (spa->spa_dsl_pool) {
2082 dsl_pool_close(spa->spa_dsl_pool);
2083 spa->spa_dsl_pool = NULL;
428870ff 2084 spa->spa_meta_objset = NULL;
34dc7c2f
BB
2085 }
2086
428870ff 2087 ddt_unload(spa);
67a1b037 2088 brt_unload(spa);
93e28d66 2089 spa_unload_log_sm_metadata(spa);
428870ff 2090
fb5f0bc8
BB
2091 /*
2092 * Drop and purge level 2 cache
2093 */
2094 spa_l2cache_drop(spa);
2095
34dc7c2f 2096 if (spa->spa_spares.sav_vdevs) {
cfb49616
RY
2097 for (int i = 0; i < spa->spa_spares.sav_count; i++)
2098 vdev_free(spa->spa_spares.sav_vdevs[i]);
34dc7c2f
BB
2099 kmem_free(spa->spa_spares.sav_vdevs,
2100 spa->spa_spares.sav_count * sizeof (void *));
2101 spa->spa_spares.sav_vdevs = NULL;
2102 }
2103 if (spa->spa_spares.sav_config) {
2104 nvlist_free(spa->spa_spares.sav_config);
2105 spa->spa_spares.sav_config = NULL;
2106 }
b128c09f 2107 spa->spa_spares.sav_count = 0;
34dc7c2f 2108
34dc7c2f 2109 if (spa->spa_l2cache.sav_vdevs) {
cfb49616
RY
2110 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
2111 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
2112 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
2113 }
34dc7c2f
BB
2114 kmem_free(spa->spa_l2cache.sav_vdevs,
2115 spa->spa_l2cache.sav_count * sizeof (void *));
2116 spa->spa_l2cache.sav_vdevs = NULL;
2117 }
2118 if (spa->spa_l2cache.sav_config) {
2119 nvlist_free(spa->spa_l2cache.sav_config);
2120 spa->spa_l2cache.sav_config = NULL;
2121 }
b128c09f 2122 spa->spa_l2cache.sav_count = 0;
34dc7c2f
BB
2123
2124 spa->spa_async_suspended = 0;
fb5f0bc8 2125
a1d477c2
MA
2126 spa->spa_indirect_vdevs_loaded = B_FALSE;
2127
d96eb2b1
DM
2128 if (spa->spa_comment != NULL) {
2129 spa_strfree(spa->spa_comment);
2130 spa->spa_comment = NULL;
2131 }
658fb802
CB
2132 if (spa->spa_compatibility != NULL) {
2133 spa_strfree(spa->spa_compatibility);
2134 spa->spa_compatibility = NULL;
2135 }
d96eb2b1 2136
5caeef02
DB
2137 spa->spa_raidz_expand = NULL;
2138
619f0976 2139 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f
BB
2140}
2141
2142/*
2143 * Load (or re-load) the current list of vdevs describing the active spares for
2144 * this pool. When this is called, we have some form of basic information in
2145 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
2146 * then re-generate a more complete list including status information.
2147 */
a1d477c2 2148void
34dc7c2f
BB
2149spa_load_spares(spa_t *spa)
2150{
2151 nvlist_t **spares;
2152 uint_t nspares;
2153 int i;
2154 vdev_t *vd, *tvd;
2155
d2734cce
SD
2156#ifndef _KERNEL
2157 /*
2158 * zdb opens both the current state of the pool and the
2159 * checkpointed state (if present), with a different spa_t.
2160 *
2161 * As spare vdevs are shared among open pools, we skip loading
2162 * them when we load the checkpointed state of the pool.
2163 */
2164 if (!spa_writeable(spa))
2165 return;
2166#endif
2167
b128c09f
BB
2168 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2169
34dc7c2f
BB
2170 /*
2171 * First, close and free any existing spare vdevs.
2172 */
cfb49616
RY
2173 if (spa->spa_spares.sav_vdevs) {
2174 for (i = 0; i < spa->spa_spares.sav_count; i++) {
2175 vd = spa->spa_spares.sav_vdevs[i];
2176
2177 /* Undo the call to spa_activate() below */
2178 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
2179 B_FALSE)) != NULL && tvd->vdev_isspare)
2180 spa_spare_remove(tvd);
2181 vdev_close(vd);
2182 vdev_free(vd);
2183 }
34dc7c2f 2184
34dc7c2f
BB
2185 kmem_free(spa->spa_spares.sav_vdevs,
2186 spa->spa_spares.sav_count * sizeof (void *));
cfb49616 2187 }
34dc7c2f
BB
2188
2189 if (spa->spa_spares.sav_config == NULL)
2190 nspares = 0;
2191 else
65ad5d11
AJ
2192 VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2193 ZPOOL_CONFIG_SPARES, &spares, &nspares));
34dc7c2f
BB
2194
2195 spa->spa_spares.sav_count = (int)nspares;
2196 spa->spa_spares.sav_vdevs = NULL;
2197
2198 if (nspares == 0)
2199 return;
2200
2201 /*
2202 * Construct the array of vdevs, opening them to get status in the
2203 * process. For each spare, there is potentially two different vdev_t
2204 * structures associated with it: one in the list of spares (used only
2205 * for basic validation purposes) and one in the active vdev
2206 * configuration (if it's spared in). During this phase we open and
2207 * validate each vdev on the spare list. If the vdev also exists in the
2208 * active configuration, then we also mark this vdev as an active spare.
2209 */
904ea276 2210 spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *),
79c76d5b 2211 KM_SLEEP);
34dc7c2f
BB
2212 for (i = 0; i < spa->spa_spares.sav_count; i++) {
2213 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
2214 VDEV_ALLOC_SPARE) == 0);
2215 ASSERT(vd != NULL);
2216
2217 spa->spa_spares.sav_vdevs[i] = vd;
2218
b128c09f
BB
2219 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
2220 B_FALSE)) != NULL) {
34dc7c2f
BB
2221 if (!tvd->vdev_isspare)
2222 spa_spare_add(tvd);
2223
2224 /*
2225 * We only mark the spare active if we were successfully
2226 * able to load the vdev. Otherwise, importing a pool
2227 * with a bad active spare would result in strange
2228 * behavior, because multiple pool would think the spare
2229 * is actively in use.
2230 *
2231 * There is a vulnerability here to an equally bizarre
2232 * circumstance, where a dead active spare is later
2233 * brought back to life (onlined or otherwise). Given
2234 * the rarity of this scenario, and the extra complexity
2235 * it adds, we ignore the possibility.
2236 */
2237 if (!vdev_is_dead(tvd))
2238 spa_spare_activate(tvd);
2239 }
2240
b128c09f 2241 vd->vdev_top = vd;
9babb374 2242 vd->vdev_aux = &spa->spa_spares;
b128c09f 2243
34dc7c2f
BB
2244 if (vdev_open(vd) != 0)
2245 continue;
2246
34dc7c2f
BB
2247 if (vdev_validate_aux(vd) == 0)
2248 spa_spare_add(vd);
2249 }
2250
2251 /*
2252 * Recompute the stashed list of spares, with status information
2253 * this time.
2254 */
65ad5d11 2255 fnvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES);
34dc7c2f
BB
2256
2257 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
79c76d5b 2258 KM_SLEEP);
34dc7c2f
BB
2259 for (i = 0; i < spa->spa_spares.sav_count; i++)
2260 spares[i] = vdev_config_generate(spa,
428870ff 2261 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
65ad5d11 2262 fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
795075e6
PD
2263 ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
2264 spa->spa_spares.sav_count);
34dc7c2f
BB
2265 for (i = 0; i < spa->spa_spares.sav_count; i++)
2266 nvlist_free(spares[i]);
2267 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
2268}
2269
2270/*
2271 * Load (or re-load) the current list of vdevs describing the active l2cache for
2272 * this pool. When this is called, we have some form of basic information in
2273 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
2274 * then re-generate a more complete list including status information.
2275 * Devices which are already active have their details maintained, and are
2276 * not re-opened.
2277 */
a1d477c2 2278void
34dc7c2f
BB
2279spa_load_l2cache(spa_t *spa)
2280{
460f239e 2281 nvlist_t **l2cache = NULL;
34dc7c2f
BB
2282 uint_t nl2cache;
2283 int i, j, oldnvdevs;
9babb374 2284 uint64_t guid;
a117a6d6 2285 vdev_t *vd, **oldvdevs, **newvdevs;
34dc7c2f
BB
2286 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2287
d2734cce
SD
2288#ifndef _KERNEL
2289 /*
2290 * zdb opens both the current state of the pool and the
2291 * checkpointed state (if present), with a different spa_t.
2292 *
2293 * As L2 caches are part of the ARC which is shared among open
2294 * pools, we skip loading them when we load the checkpointed
2295 * state of the pool.
2296 */
2297 if (!spa_writeable(spa))
2298 return;
2299#endif
2300
b128c09f
BB
2301 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2302
34dc7c2f
BB
2303 oldvdevs = sav->sav_vdevs;
2304 oldnvdevs = sav->sav_count;
2305 sav->sav_vdevs = NULL;
2306 sav->sav_count = 0;
2307
67d60824
NB
2308 if (sav->sav_config == NULL) {
2309 nl2cache = 0;
2310 newvdevs = NULL;
2311 goto out;
2312 }
2313
65ad5d11
AJ
2314 VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config,
2315 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache));
67d60824
NB
2316 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
2317
34dc7c2f
BB
2318 /*
2319 * Process new nvlist of vdevs.
2320 */
2321 for (i = 0; i < nl2cache; i++) {
65ad5d11 2322 guid = fnvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID);
34dc7c2f
BB
2323
2324 newvdevs[i] = NULL;
2325 for (j = 0; j < oldnvdevs; j++) {
2326 vd = oldvdevs[j];
2327 if (vd != NULL && guid == vd->vdev_guid) {
2328 /*
2329 * Retain previous vdev for add/remove ops.
2330 */
2331 newvdevs[i] = vd;
2332 oldvdevs[j] = NULL;
2333 break;
2334 }
2335 }
2336
2337 if (newvdevs[i] == NULL) {
2338 /*
2339 * Create new vdev
2340 */
2341 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
2342 VDEV_ALLOC_L2CACHE) == 0);
2343 ASSERT(vd != NULL);
2344 newvdevs[i] = vd;
2345
2346 /*
2347 * Commit this vdev as an l2cache device,
2348 * even if it fails to open.
2349 */
2350 spa_l2cache_add(vd);
2351
b128c09f
BB
2352 vd->vdev_top = vd;
2353 vd->vdev_aux = sav;
2354
2355 spa_l2cache_activate(vd);
2356
34dc7c2f
BB
2357 if (vdev_open(vd) != 0)
2358 continue;
2359
34dc7c2f
BB
2360 (void) vdev_validate_aux(vd);
2361
9babb374
BB
2362 if (!vdev_is_dead(vd))
2363 l2arc_add_vdev(spa, vd);
b7654bd7
GA
2364
2365 /*
2366 * Upon cache device addition to a pool or pool
2367 * creation with a cache device or if the header
2368 * of the device is invalid we issue an async
2369 * TRIM command for the whole device which will
2370 * execute if l2arc_trim_ahead > 0.
2371 */
2372 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
34dc7c2f
BB
2373 }
2374 }
2375
67d60824
NB
2376 sav->sav_vdevs = newvdevs;
2377 sav->sav_count = (int)nl2cache;
2378
2379 /*
2380 * Recompute the stashed list of l2cache devices, with status
2381 * information this time.
2382 */
65ad5d11 2383 fnvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE);
67d60824 2384
460f239e
D
2385 if (sav->sav_count > 0)
2386 l2cache = kmem_alloc(sav->sav_count * sizeof (void *),
2387 KM_SLEEP);
67d60824
NB
2388 for (i = 0; i < sav->sav_count; i++)
2389 l2cache[i] = vdev_config_generate(spa,
2390 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
795075e6
PD
2391 fnvlist_add_nvlist_array(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
2392 (const nvlist_t * const *)l2cache, sav->sav_count);
67d60824
NB
2393
2394out:
34dc7c2f
BB
2395 /*
2396 * Purge vdevs that were dropped
2397 */
cfb49616
RY
2398 if (oldvdevs) {
2399 for (i = 0; i < oldnvdevs; i++) {
2400 uint64_t pool;
2401
2402 vd = oldvdevs[i];
2403 if (vd != NULL) {
2404 ASSERT(vd->vdev_isl2cache);
2405
2406 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2407 pool != 0ULL && l2arc_vdev_present(vd))
2408 l2arc_remove_vdev(vd);
2409 vdev_clear_stats(vd);
2410 vdev_free(vd);
2411 }
34dc7c2f 2412 }
34dc7c2f 2413
34dc7c2f 2414 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
cfb49616 2415 }
34dc7c2f 2416
34dc7c2f
BB
2417 for (i = 0; i < sav->sav_count; i++)
2418 nvlist_free(l2cache[i]);
2419 if (sav->sav_count)
2420 kmem_free(l2cache, sav->sav_count * sizeof (void *));
2421}
2422
2423static int
2424load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
2425{
2426 dmu_buf_t *db;
2427 char *packed = NULL;
2428 size_t nvsize = 0;
2429 int error;
2430 *value = NULL;
2431
c3275b56
BB
2432 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
2433 if (error)
2434 return (error);
2435
34dc7c2f
BB
2436 nvsize = *(uint64_t *)db->db_data;
2437 dmu_buf_rele(db, FTAG);
2438
77aef6f6 2439 packed = vmem_alloc(nvsize, KM_SLEEP);
9babb374
BB
2440 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
2441 DMU_READ_PREFETCH);
34dc7c2f
BB
2442 if (error == 0)
2443 error = nvlist_unpack(packed, nvsize, value, 0);
77aef6f6 2444 vmem_free(packed, nvsize);
34dc7c2f
BB
2445
2446 return (error);
2447}
2448
6cb8e530
PZ
2449/*
2450 * Concrete top-level vdevs that are not missing and are not logs. At every
2451 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2452 */
2453static uint64_t
2454spa_healthy_core_tvds(spa_t *spa)
2455{
2456 vdev_t *rvd = spa->spa_root_vdev;
2457 uint64_t tvds = 0;
2458
2459 for (uint64_t i = 0; i < rvd->vdev_children; i++) {
2460 vdev_t *vd = rvd->vdev_child[i];
2461 if (vd->vdev_islog)
2462 continue;
2463 if (vdev_is_concrete(vd) && !vdev_is_dead(vd))
2464 tvds++;
2465 }
2466
2467 return (tvds);
2468}
2469
34dc7c2f
BB
2470/*
2471 * Checks to see if the given vdev could not be opened, in which case we post a
2472 * sysevent to notify the autoreplace code that the device has been removed.
2473 */
2474static void
2475spa_check_removed(vdev_t *vd)
2476{
6cb8e530 2477 for (uint64_t c = 0; c < vd->vdev_children; c++)
34dc7c2f
BB
2478 spa_check_removed(vd->vdev_child[c]);
2479
7011fb60 2480 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
a1d477c2 2481 vdev_is_concrete(vd)) {
fb390aaf 2482 zfs_post_autoreplace(vd->vdev_spa, vd);
12fa0466 2483 spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK);
34dc7c2f
BB
2484 }
2485}
2486
6cb8e530
PZ
2487static int
2488spa_check_for_missing_logs(spa_t *spa)
9babb374 2489{
6cb8e530 2490 vdev_t *rvd = spa->spa_root_vdev;
9babb374 2491
428870ff 2492 /*
572e2857 2493 * If we're doing a normal import, then build up any additional
6cb8e530 2494 * diagnostic information about missing log devices.
572e2857 2495 * We'll pass this up to the user for further processing.
428870ff 2496 */
572e2857
BB
2497 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
2498 nvlist_t **child, *nv;
2499 uint64_t idx = 0;
2500
160987b5 2501 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *),
79c76d5b 2502 KM_SLEEP);
65ad5d11 2503 nv = fnvlist_alloc();
572e2857 2504
6cb8e530 2505 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
572e2857 2506 vdev_t *tvd = rvd->vdev_child[c];
572e2857 2507
6cb8e530
PZ
2508 /*
2509 * We consider a device as missing only if it failed
2510 * to open (i.e. offline or faulted is not considered
2511 * as missing).
2512 */
2513 if (tvd->vdev_islog &&
2514 tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
2515 child[idx++] = vdev_config_generate(spa, tvd,
2516 B_FALSE, VDEV_CONFIG_MISSING);
2517 }
572e2857 2518 }
9babb374 2519
6cb8e530 2520 if (idx > 0) {
795075e6
PD
2521 fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2522 (const nvlist_t * const *)child, idx);
6cb8e530
PZ
2523 fnvlist_add_nvlist(spa->spa_load_info,
2524 ZPOOL_CONFIG_MISSING_DEVICES, nv);
572e2857 2525
6cb8e530 2526 for (uint64_t i = 0; i < idx; i++)
572e2857
BB
2527 nvlist_free(child[i]);
2528 }
2529 nvlist_free(nv);
2530 kmem_free(child, rvd->vdev_children * sizeof (char **));
572e2857 2531
6cb8e530
PZ
2532 if (idx > 0) {
2533 spa_load_failed(spa, "some log devices are missing");
db7d07e1 2534 vdev_dbgmsg_print_tree(rvd, 2);
6cb8e530
PZ
2535 return (SET_ERROR(ENXIO));
2536 }
2537 } else {
2538 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2539 vdev_t *tvd = rvd->vdev_child[c];
a1d477c2 2540
6cb8e530
PZ
2541 if (tvd->vdev_islog &&
2542 tvd->vdev_state == VDEV_STATE_CANT_OPEN) {
572e2857 2543 spa_set_log_state(spa, SPA_LOG_CLEAR);
6cb8e530
PZ
2544 spa_load_note(spa, "some log devices are "
2545 "missing, ZIL is dropped.");
db7d07e1 2546 vdev_dbgmsg_print_tree(rvd, 2);
6cb8e530 2547 break;
e0ab3ab5 2548 }
572e2857 2549 }
9babb374 2550 }
e0ab3ab5 2551
6cb8e530 2552 return (0);
9babb374
BB
2553}
2554
b128c09f
BB
2555/*
2556 * Check for missing log devices
2557 */
13fe0198 2558static boolean_t
b128c09f
BB
2559spa_check_logs(spa_t *spa)
2560{
13fe0198 2561 boolean_t rv = B_FALSE;
9c43027b 2562 dsl_pool_t *dp = spa_get_dsl(spa);
13fe0198 2563
b128c09f 2564 switch (spa->spa_log_state) {
e75c13c3
BB
2565 default:
2566 break;
b128c09f
BB
2567 case SPA_LOG_MISSING:
2568 /* need to recheck in case slog has been restored */
2569 case SPA_LOG_UNKNOWN:
9c43027b
AJ
2570 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2571 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
13fe0198 2572 if (rv)
428870ff 2573 spa_set_log_state(spa, SPA_LOG_MISSING);
b128c09f 2574 break;
b128c09f 2575 }
13fe0198 2576 return (rv);
b128c09f
BB
2577}
2578
aa755b35
MA
2579/*
2580 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2581 */
428870ff
BB
2582static boolean_t
2583spa_passivate_log(spa_t *spa)
34dc7c2f 2584{
428870ff
BB
2585 vdev_t *rvd = spa->spa_root_vdev;
2586 boolean_t slog_found = B_FALSE;
b128c09f 2587
428870ff 2588 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
fb5f0bc8 2589
1c27024e 2590 for (int c = 0; c < rvd->vdev_children; c++) {
428870ff 2591 vdev_t *tvd = rvd->vdev_child[c];
34dc7c2f 2592
428870ff 2593 if (tvd->vdev_islog) {
aa755b35
MA
2594 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
2595 metaslab_group_passivate(tvd->vdev_mg);
428870ff
BB
2596 slog_found = B_TRUE;
2597 }
34dc7c2f
BB
2598 }
2599
428870ff
BB
2600 return (slog_found);
2601}
34dc7c2f 2602
aa755b35
MA
2603/*
2604 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2605 */
428870ff
BB
2606static void
2607spa_activate_log(spa_t *spa)
2608{
2609 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 2610
428870ff
BB
2611 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
2612
1c27024e 2613 for (int c = 0; c < rvd->vdev_children; c++) {
428870ff 2614 vdev_t *tvd = rvd->vdev_child[c];
428870ff 2615
aa755b35
MA
2616 if (tvd->vdev_islog) {
2617 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
2618 metaslab_group_activate(tvd->vdev_mg);
2619 }
34dc7c2f 2620 }
428870ff 2621}
34dc7c2f 2622
428870ff 2623int
a1d477c2 2624spa_reset_logs(spa_t *spa)
428870ff 2625{
13fe0198 2626 int error;
9babb374 2627
a1d477c2 2628 error = dmu_objset_find(spa_name(spa), zil_reset,
13fe0198
MA
2629 NULL, DS_FIND_CHILDREN);
2630 if (error == 0) {
428870ff
BB
2631 /*
2632 * We successfully offlined the log device, sync out the
2633 * current txg so that the "stubby" block can be removed
2634 * by zil_sync().
2635 */
2636 txg_wait_synced(spa->spa_dsl_pool, 0);
2637 }
2638 return (error);
2639}
34dc7c2f 2640
428870ff
BB
2641static void
2642spa_aux_check_removed(spa_aux_vdev_t *sav)
2643{
1c27024e 2644 for (int i = 0; i < sav->sav_count; i++)
428870ff
BB
2645 spa_check_removed(sav->sav_vdevs[i]);
2646}
34dc7c2f 2647
428870ff
BB
2648void
2649spa_claim_notify(zio_t *zio)
2650{
2651 spa_t *spa = zio->io_spa;
34dc7c2f 2652
428870ff
BB
2653 if (zio->io_error)
2654 return;
34dc7c2f 2655
428870ff
BB
2656 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
2657 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
2658 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
2659 mutex_exit(&spa->spa_props_lock);
2660}
34dc7c2f 2661
428870ff 2662typedef struct spa_load_error {
f2c5bc15 2663 boolean_t sle_verify_data;
428870ff
BB
2664 uint64_t sle_meta_count;
2665 uint64_t sle_data_count;
2666} spa_load_error_t;
34dc7c2f 2667
428870ff
BB
2668static void
2669spa_load_verify_done(zio_t *zio)
2670{
2671 blkptr_t *bp = zio->io_bp;
2672 spa_load_error_t *sle = zio->io_private;
2673 dmu_object_type_t type = BP_GET_TYPE(bp);
2674 int error = zio->io_error;
dea377c0 2675 spa_t *spa = zio->io_spa;
34dc7c2f 2676
a6255b7f 2677 abd_free(zio->io_abd);
428870ff 2678 if (error) {
9ae529ec 2679 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
428870ff 2680 type != DMU_OT_INTENT_LOG)
bc89ac84 2681 atomic_inc_64(&sle->sle_meta_count);
428870ff 2682 else
bc89ac84 2683 atomic_inc_64(&sle->sle_data_count);
34dc7c2f 2684 }
dea377c0
MA
2685
2686 mutex_enter(&spa->spa_scrub_lock);
c8242a96 2687 spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp);
dea377c0
MA
2688 cv_broadcast(&spa->spa_scrub_io_cv);
2689 mutex_exit(&spa->spa_scrub_lock);
428870ff 2690}
34dc7c2f 2691
dea377c0 2692/*
e1cfd73f 2693 * Maximum number of inflight bytes is the log2 fraction of the arc size.
c8242a96 2694 * By default, we set it to 1/16th of the arc.
dea377c0 2695 */
fdc2d303 2696static uint_t spa_load_verify_shift = 4;
18168da7
AZ
2697static int spa_load_verify_metadata = B_TRUE;
2698static int spa_load_verify_data = B_TRUE;
dea377c0 2699
428870ff
BB
2700static int
2701spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 2702 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
428870ff 2703{
f2c5bc15
AM
2704 zio_t *rio = arg;
2705 spa_load_error_t *sle = rio->io_private;
2706
14e4e3cb
AZ
2707 (void) zilog, (void) dnp;
2708
dea377c0
MA
2709 /*
2710 * Note: normally this routine will not be called if
2711 * spa_load_verify_metadata is not set. However, it may be useful
2712 * to manually set the flag after the traversal has begun.
2713 */
2714 if (!spa_load_verify_metadata)
2715 return (0);
2cd0f98f
BB
2716
2717 /*
2718 * Sanity check the block pointer in order to detect obvious damage
2719 * before using the contents in subsequent checks or in zio_read().
2720 * When damaged consider it to be a metadata error since we cannot
2721 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2722 */
3095ca91 2723 if (!zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) {
2cd0f98f
BB
2724 atomic_inc_64(&sle->sle_meta_count);
2725 return (0);
2726 }
2727
2728 if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) ||
2729 BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp))
2730 return (0);
2731
f2c5bc15
AM
2732 if (!BP_IS_METADATA(bp) &&
2733 (!spa_load_verify_data || !sle->sle_verify_data))
dea377c0
MA
2734 return (0);
2735
1e527162
GW
2736 uint64_t maxinflight_bytes =
2737 arc_target_bytes() >> spa_load_verify_shift;
1c27024e 2738 size_t size = BP_GET_PSIZE(bp);
dea377c0
MA
2739
2740 mutex_enter(&spa->spa_scrub_lock);
c8242a96 2741 while (spa->spa_load_verify_bytes >= maxinflight_bytes)
dea377c0 2742 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
c8242a96 2743 spa->spa_load_verify_bytes += size;
dea377c0
MA
2744 mutex_exit(&spa->spa_scrub_lock);
2745
a6255b7f 2746 zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size,
dea377c0
MA
2747 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
2748 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
2749 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
428870ff
BB
2750 return (0);
2751}
34dc7c2f 2752
65c7cc49 2753static int
d1d19c78
PD
2754verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2755{
14e4e3cb
AZ
2756 (void) dp, (void) arg;
2757
d1d19c78
PD
2758 if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2759 return (SET_ERROR(ENAMETOOLONG));
2760
2761 return (0);
2762}
2763
428870ff
BB
2764static int
2765spa_load_verify(spa_t *spa)
2766{
2767 zio_t *rio;
2768 spa_load_error_t sle = { 0 };
8a393be3 2769 zpool_load_policy_t policy;
428870ff 2770 boolean_t verify_ok = B_FALSE;
dea377c0 2771 int error = 0;
34dc7c2f 2772
8a393be3 2773 zpool_get_load_policy(spa->spa_config, &policy);
34dc7c2f 2774
f2c5bc15
AM
2775 if (policy.zlp_rewind & ZPOOL_NEVER_REWIND ||
2776 policy.zlp_maxmeta == UINT64_MAX)
428870ff 2777 return (0);
34dc7c2f 2778
d1d19c78
PD
2779 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2780 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2781 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2782 DS_FIND_CHILDREN);
2783 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2784 if (error != 0)
2785 return (error);
2786
f2c5bc15
AM
2787 /*
2788 * Verify data only if we are rewinding or error limit was set.
2789 * Otherwise nothing except dbgmsg care about it to waste time.
2790 */
2791 sle.sle_verify_data = (policy.zlp_rewind & ZPOOL_REWIND_MASK) ||
2792 (policy.zlp_maxdata < UINT64_MAX);
2793
428870ff
BB
2794 rio = zio_root(spa, NULL, &sle,
2795 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
34dc7c2f 2796
dea377c0 2797 if (spa_load_verify_metadata) {
4a0ee12a
PZ
2798 if (spa->spa_extreme_rewind) {
2799 spa_load_note(spa, "performing a complete scan of the "
2800 "pool since extreme rewind is on. This may take "
2801 "a very long time.\n (spa_load_verify_data=%u, "
2802 "spa_load_verify_metadata=%u)",
2803 spa_load_verify_data, spa_load_verify_metadata);
2804 }
c8242a96 2805
dea377c0 2806 error = traverse_pool(spa, spa->spa_verify_min_txg,
b5256303
TC
2807 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
2808 TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio);
dea377c0 2809 }
428870ff
BB
2810
2811 (void) zio_wait(rio);
c8242a96 2812 ASSERT0(spa->spa_load_verify_bytes);
428870ff
BB
2813
2814 spa->spa_load_meta_errors = sle.sle_meta_count;
2815 spa->spa_load_data_errors = sle.sle_data_count;
2816
afd2f7b7
PZ
2817 if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) {
2818 spa_load_note(spa, "spa_load_verify found %llu metadata errors "
2819 "and %llu data errors", (u_longlong_t)sle.sle_meta_count,
2820 (u_longlong_t)sle.sle_data_count);
2821 }
2822
2823 if (spa_load_verify_dryrun ||
8a393be3
PZ
2824 (!error && sle.sle_meta_count <= policy.zlp_maxmeta &&
2825 sle.sle_data_count <= policy.zlp_maxdata)) {
572e2857
BB
2826 int64_t loss = 0;
2827
428870ff
BB
2828 verify_ok = B_TRUE;
2829 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2830 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
572e2857
BB
2831
2832 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
65ad5d11
AJ
2833 fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_LOAD_TIME,
2834 spa->spa_load_txg_ts);
2835 fnvlist_add_int64(spa->spa_load_info, ZPOOL_CONFIG_REWIND_TIME,
2836 loss);
f2c5bc15
AM
2837 fnvlist_add_uint64(spa->spa_load_info,
2838 ZPOOL_CONFIG_LOAD_META_ERRORS, sle.sle_meta_count);
65ad5d11
AJ
2839 fnvlist_add_uint64(spa->spa_load_info,
2840 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count);
428870ff
BB
2841 } else {
2842 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2843 }
2844
afd2f7b7
PZ
2845 if (spa_load_verify_dryrun)
2846 return (0);
2847
428870ff
BB
2848 if (error) {
2849 if (error != ENXIO && error != EIO)
2e528b49 2850 error = SET_ERROR(EIO);
428870ff
BB
2851 return (error);
2852 }
2853
2854 return (verify_ok ? 0 : EIO);
2855}
2856
2857/*
2858 * Find a value in the pool props object.
2859 */
2860static void
2861spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2862{
2863 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2864 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2865}
2866
2867/*
2868 * Find a value in the pool directory object.
2869 */
2870static int
4a0ee12a 2871spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent)
428870ff 2872{
4a0ee12a
PZ
2873 int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2874 name, sizeof (uint64_t), 1, val);
2875
2876 if (error != 0 && (error != ENOENT || log_enoent)) {
2877 spa_load_failed(spa, "couldn't get '%s' value in MOS directory "
2878 "[error=%d]", name, error);
2879 }
2880
2881 return (error);
428870ff
BB
2882}
2883
2884static int
2885spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2886{
2887 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
a1d477c2 2888 return (SET_ERROR(err));
428870ff
BB
2889}
2890
37f03da8
SH
2891boolean_t
2892spa_livelist_delete_check(spa_t *spa)
2893{
2894 return (spa->spa_livelists_to_delete != 0);
2895}
2896
37f03da8
SH
2897static boolean_t
2898spa_livelist_delete_cb_check(void *arg, zthr_t *z)
2899{
14e4e3cb 2900 (void) z;
37f03da8
SH
2901 spa_t *spa = arg;
2902 return (spa_livelist_delete_check(spa));
2903}
2904
2905static int
2906delete_blkptr_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2907{
2908 spa_t *spa = arg;
2909 zio_free(spa, tx->tx_txg, bp);
2910 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
2911 -bp_get_dsize_sync(spa, bp),
2912 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
2913 return (0);
2914}
2915
2916static int
2917dsl_get_next_livelist_obj(objset_t *os, uint64_t zap_obj, uint64_t *llp)
2918{
2919 int err;
2920 zap_cursor_t zc;
2921 zap_attribute_t za;
2922 zap_cursor_init(&zc, os, zap_obj);
2923 err = zap_cursor_retrieve(&zc, &za);
2924 zap_cursor_fini(&zc);
2925 if (err == 0)
2926 *llp = za.za_first_integer;
2927 return (err);
2928}
2929
2930/*
2931 * Components of livelist deletion that must be performed in syncing
2932 * context: freeing block pointers and updating the pool-wide data
2933 * structures to indicate how much work is left to do
2934 */
2935typedef struct sublist_delete_arg {
2936 spa_t *spa;
2937 dsl_deadlist_t *ll;
2938 uint64_t key;
2939 bplist_t *to_free;
2940} sublist_delete_arg_t;
2941
2942static void
2943sublist_delete_sync(void *arg, dmu_tx_t *tx)
2944{
2945 sublist_delete_arg_t *sda = arg;
2946 spa_t *spa = sda->spa;
2947 dsl_deadlist_t *ll = sda->ll;
2948 uint64_t key = sda->key;
2949 bplist_t *to_free = sda->to_free;
2950
2951 bplist_iterate(to_free, delete_blkptr_cb, spa, tx);
2952 dsl_deadlist_remove_entry(ll, key, tx);
2953}
2954
2955typedef struct livelist_delete_arg {
2956 spa_t *spa;
2957 uint64_t ll_obj;
2958 uint64_t zap_obj;
2959} livelist_delete_arg_t;
2960
2961static void
2962livelist_delete_sync(void *arg, dmu_tx_t *tx)
2963{
2964 livelist_delete_arg_t *lda = arg;
2965 spa_t *spa = lda->spa;
2966 uint64_t ll_obj = lda->ll_obj;
2967 uint64_t zap_obj = lda->zap_obj;
2968 objset_t *mos = spa->spa_meta_objset;
2969 uint64_t count;
2970
2971 /* free the livelist and decrement the feature count */
2972 VERIFY0(zap_remove_int(mos, zap_obj, ll_obj, tx));
2973 dsl_deadlist_free(mos, ll_obj, tx);
2974 spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);
2975 VERIFY0(zap_count(mos, zap_obj, &count));
2976 if (count == 0) {
2977 /* no more livelists to delete */
2978 VERIFY0(zap_remove(mos, DMU_POOL_DIRECTORY_OBJECT,
2979 DMU_POOL_DELETED_CLONES, tx));
2980 VERIFY0(zap_destroy(mos, zap_obj, tx));
2981 spa->spa_livelists_to_delete = 0;
e60e158e 2982 spa_notify_waiters(spa);
37f03da8
SH
2983 }
2984}
2985
2986/*
2987 * Load in the value for the livelist to be removed and open it. Then,
2988 * load its first sublist and determine which block pointers should actually
2989 * be freed. Then, call a synctask which performs the actual frees and updates
2990 * the pool-wide livelist data.
2991 */
65c7cc49 2992static void
37f03da8
SH
2993spa_livelist_delete_cb(void *arg, zthr_t *z)
2994{
2995 spa_t *spa = arg;
2996 uint64_t ll_obj = 0, count;
2997 objset_t *mos = spa->spa_meta_objset;
2998 uint64_t zap_obj = spa->spa_livelists_to_delete;
2999 /*
3000 * Determine the next livelist to delete. This function should only
3001 * be called if there is at least one deleted clone.
3002 */
3003 VERIFY0(dsl_get_next_livelist_obj(mos, zap_obj, &ll_obj));
3004 VERIFY0(zap_count(mos, ll_obj, &count));
3005 if (count > 0) {
c9562576 3006 dsl_deadlist_t *ll;
37f03da8
SH
3007 dsl_deadlist_entry_t *dle;
3008 bplist_t to_free;
c9562576
PS
3009 ll = kmem_zalloc(sizeof (dsl_deadlist_t), KM_SLEEP);
3010 dsl_deadlist_open(ll, mos, ll_obj);
3011 dle = dsl_deadlist_first(ll);
37f03da8
SH
3012 ASSERT3P(dle, !=, NULL);
3013 bplist_create(&to_free);
3014 int err = dsl_process_sub_livelist(&dle->dle_bpobj, &to_free,
3015 z, NULL);
3016 if (err == 0) {
3017 sublist_delete_arg_t sync_arg = {
3018 .spa = spa,
c9562576 3019 .ll = ll,
37f03da8
SH
3020 .key = dle->dle_mintxg,
3021 .to_free = &to_free
3022 };
3023 zfs_dbgmsg("deleting sublist (id %llu) from"
8e739b2c
RE
3024 " livelist %llu, %lld remaining",
3025 (u_longlong_t)dle->dle_bpobj.bpo_object,
3026 (u_longlong_t)ll_obj, (longlong_t)count - 1);
37f03da8
SH
3027 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3028 sublist_delete_sync, &sync_arg, 0,
3029 ZFS_SPACE_CHECK_DESTROY));
3030 } else {
d87676a9 3031 VERIFY3U(err, ==, EINTR);
37f03da8
SH
3032 }
3033 bplist_clear(&to_free);
3034 bplist_destroy(&to_free);
c9562576
PS
3035 dsl_deadlist_close(ll);
3036 kmem_free(ll, sizeof (dsl_deadlist_t));
37f03da8
SH
3037 } else {
3038 livelist_delete_arg_t sync_arg = {
3039 .spa = spa,
3040 .ll_obj = ll_obj,
3041 .zap_obj = zap_obj
3042 };
8e739b2c
RE
3043 zfs_dbgmsg("deletion of livelist %llu completed",
3044 (u_longlong_t)ll_obj);
37f03da8
SH
3045 VERIFY0(dsl_sync_task(spa_name(spa), NULL, livelist_delete_sync,
3046 &sync_arg, 0, ZFS_SPACE_CHECK_DESTROY));
3047 }
3048}
3049
65c7cc49 3050static void
37f03da8
SH
3051spa_start_livelist_destroy_thread(spa_t *spa)
3052{
3053 ASSERT3P(spa->spa_livelist_delete_zthr, ==, NULL);
843e9ca2
SD
3054 spa->spa_livelist_delete_zthr =
3055 zthr_create("z_livelist_destroy",
6bc61d22
TN
3056 spa_livelist_delete_cb_check, spa_livelist_delete_cb, spa,
3057 minclsyspri);
37f03da8
SH
3058}
3059
3060typedef struct livelist_new_arg {
3061 bplist_t *allocs;
3062 bplist_t *frees;
3063} livelist_new_arg_t;
3064
3065static int
3066livelist_track_new_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
3067 dmu_tx_t *tx)
3068{
3069 ASSERT(tx == NULL);
3070 livelist_new_arg_t *lna = arg;
3071 if (bp_freed) {
3072 bplist_append(lna->frees, bp);
3073 } else {
3074 bplist_append(lna->allocs, bp);
3075 zfs_livelist_condense_new_alloc++;
3076 }
3077 return (0);
3078}
3079
3080typedef struct livelist_condense_arg {
3081 spa_t *spa;
3082 bplist_t to_keep;
3083 uint64_t first_size;
3084 uint64_t next_size;
3085} livelist_condense_arg_t;
3086
3087static void
3088spa_livelist_condense_sync(void *arg, dmu_tx_t *tx)
3089{
3090 livelist_condense_arg_t *lca = arg;
3091 spa_t *spa = lca->spa;
3092 bplist_t new_frees;
3093 dsl_dataset_t *ds = spa->spa_to_condense.ds;
3094
3095 /* Have we been cancelled? */
3096 if (spa->spa_to_condense.cancelled) {
3097 zfs_livelist_condense_sync_cancel++;
3098 goto out;
3099 }
3100
3101 dsl_deadlist_entry_t *first = spa->spa_to_condense.first;
3102 dsl_deadlist_entry_t *next = spa->spa_to_condense.next;
3103 dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist;
3104
3105 /*
3106 * It's possible that the livelist was changed while the zthr was
3107 * running. Therefore, we need to check for new blkptrs in the two
3108 * entries being condensed and continue to track them in the livelist.
3109 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
3110 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
3111 * we need to sort them into two different bplists.
3112 */
3113 uint64_t first_obj = first->dle_bpobj.bpo_object;
3114 uint64_t next_obj = next->dle_bpobj.bpo_object;
3115 uint64_t cur_first_size = first->dle_bpobj.bpo_phys->bpo_num_blkptrs;
3116 uint64_t cur_next_size = next->dle_bpobj.bpo_phys->bpo_num_blkptrs;
3117
3118 bplist_create(&new_frees);
3119 livelist_new_arg_t new_bps = {
3120 .allocs = &lca->to_keep,
3121 .frees = &new_frees,
3122 };
3123
3124 if (cur_first_size > lca->first_size) {
3125 VERIFY0(livelist_bpobj_iterate_from_nofree(&first->dle_bpobj,
3126 livelist_track_new_cb, &new_bps, lca->first_size));
3127 }
3128 if (cur_next_size > lca->next_size) {
3129 VERIFY0(livelist_bpobj_iterate_from_nofree(&next->dle_bpobj,
3130 livelist_track_new_cb, &new_bps, lca->next_size));
3131 }
3132
3133 dsl_deadlist_clear_entry(first, ll, tx);
3134 ASSERT(bpobj_is_empty(&first->dle_bpobj));
3135 dsl_deadlist_remove_entry(ll, next->dle_mintxg, tx);
3136
3137 bplist_iterate(&lca->to_keep, dsl_deadlist_insert_alloc_cb, ll, tx);
3138 bplist_iterate(&new_frees, dsl_deadlist_insert_free_cb, ll, tx);
3139 bplist_destroy(&new_frees);
3140
3141 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3142 dsl_dataset_name(ds, dsname);
3143 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
3144 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
8e739b2c
RE
3145 "(%llu blkptrs)", (u_longlong_t)tx->tx_txg, dsname,
3146 (u_longlong_t)ds->ds_object, (u_longlong_t)first_obj,
3147 (u_longlong_t)cur_first_size, (u_longlong_t)next_obj,
3148 (u_longlong_t)cur_next_size,
3149 (u_longlong_t)first->dle_bpobj.bpo_object,
3150 (u_longlong_t)first->dle_bpobj.bpo_phys->bpo_num_blkptrs);
37f03da8
SH
3151out:
3152 dmu_buf_rele(ds->ds_dbuf, spa);
3153 spa->spa_to_condense.ds = NULL;
3154 bplist_clear(&lca->to_keep);
3155 bplist_destroy(&lca->to_keep);
3156 kmem_free(lca, sizeof (livelist_condense_arg_t));
3157 spa->spa_to_condense.syncing = B_FALSE;
3158}
3159
65c7cc49 3160static void
37f03da8
SH
3161spa_livelist_condense_cb(void *arg, zthr_t *t)
3162{
3163 while (zfs_livelist_condense_zthr_pause &&
3164 !(zthr_has_waiters(t) || zthr_iscancelled(t)))
3165 delay(1);
3166
3167 spa_t *spa = arg;
3168 dsl_deadlist_entry_t *first = spa->spa_to_condense.first;
3169 dsl_deadlist_entry_t *next = spa->spa_to_condense.next;
3170 uint64_t first_size, next_size;
3171
3172 livelist_condense_arg_t *lca =
3173 kmem_alloc(sizeof (livelist_condense_arg_t), KM_SLEEP);
3174 bplist_create(&lca->to_keep);
3175
3176 /*
3177 * Process the livelists (matching FREEs and ALLOCs) in open context
3178 * so we have minimal work in syncing context to condense.
3179 *
3180 * We save bpobj sizes (first_size and next_size) to use later in
3181 * syncing context to determine if entries were added to these sublists
3182 * while in open context. This is possible because the clone is still
3183 * active and open for normal writes and we want to make sure the new,
3184 * unprocessed blockpointers are inserted into the livelist normally.
3185 *
3186 * Note that dsl_process_sub_livelist() both stores the size number of
3187 * blockpointers and iterates over them while the bpobj's lock held, so
3188 * the sizes returned to us are consistent which what was actually
3189 * processed.
3190 */
3191 int err = dsl_process_sub_livelist(&first->dle_bpobj, &lca->to_keep, t,
3192 &first_size);
3193 if (err == 0)
3194 err = dsl_process_sub_livelist(&next->dle_bpobj, &lca->to_keep,
3195 t, &next_size);
3196
3197 if (err == 0) {
3198 while (zfs_livelist_condense_sync_pause &&
3199 !(zthr_has_waiters(t) || zthr_iscancelled(t)))
3200 delay(1);
3201
3202 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
3203 dmu_tx_mark_netfree(tx);
3204 dmu_tx_hold_space(tx, 1);
3205 err = dmu_tx_assign(tx, TXG_NOWAIT | TXG_NOTHROTTLE);
3206 if (err == 0) {
3207 /*
3208 * Prevent the condense zthr restarting before
3209 * the synctask completes.
3210 */
3211 spa->spa_to_condense.syncing = B_TRUE;
3212 lca->spa = spa;
3213 lca->first_size = first_size;
3214 lca->next_size = next_size;
3215 dsl_sync_task_nowait(spa_get_dsl(spa),
38080324 3216 spa_livelist_condense_sync, lca, tx);
37f03da8
SH
3217 dmu_tx_commit(tx);
3218 return;
3219 }
3220 }
3221 /*
3222 * Condensing can not continue: either it was externally stopped or
3223 * we were unable to assign to a tx because the pool has run out of
3224 * space. In the second case, we'll just end up trying to condense
3225 * again in a later txg.
3226 */
3227 ASSERT(err != 0);
3228 bplist_clear(&lca->to_keep);
3229 bplist_destroy(&lca->to_keep);
3230 kmem_free(lca, sizeof (livelist_condense_arg_t));
3231 dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, spa);
3232 spa->spa_to_condense.ds = NULL;
3233 if (err == EINTR)
3234 zfs_livelist_condense_zthr_cancel++;
3235}
3236
37f03da8
SH
3237/*
3238 * Check that there is something to condense but that a condense is not
3239 * already in progress and that condensing has not been cancelled.
3240 */
3241static boolean_t
3242spa_livelist_condense_cb_check(void *arg, zthr_t *z)
3243{
14e4e3cb 3244 (void) z;
37f03da8
SH
3245 spa_t *spa = arg;
3246 if ((spa->spa_to_condense.ds != NULL) &&
3247 (spa->spa_to_condense.syncing == B_FALSE) &&
3248 (spa->spa_to_condense.cancelled == B_FALSE)) {
3249 return (B_TRUE);
3250 }
3251 return (B_FALSE);
3252}
3253
65c7cc49 3254static void
37f03da8
SH
3255spa_start_livelist_condensing_thread(spa_t *spa)
3256{
3257 spa->spa_to_condense.ds = NULL;
3258 spa->spa_to_condense.first = NULL;
3259 spa->spa_to_condense.next = NULL;
3260 spa->spa_to_condense.syncing = B_FALSE;
3261 spa->spa_to_condense.cancelled = B_FALSE;
3262
3263 ASSERT3P(spa->spa_livelist_condense_zthr, ==, NULL);
843e9ca2
SD
3264 spa->spa_livelist_condense_zthr =
3265 zthr_create("z_livelist_condense",
3266 spa_livelist_condense_cb_check,
6bc61d22 3267 spa_livelist_condense_cb, spa, minclsyspri);
37f03da8
SH
3268}
3269
9d5b5245
SD
3270static void
3271spa_spawn_aux_threads(spa_t *spa)
3272{
3273 ASSERT(spa_writeable(spa));
3274
3275 ASSERT(MUTEX_HELD(&spa_namespace_lock));
3276
5caeef02 3277 spa_start_raidz_expansion_thread(spa);
9d5b5245 3278 spa_start_indirect_condensing_thread(spa);
37f03da8
SH
3279 spa_start_livelist_destroy_thread(spa);
3280 spa_start_livelist_condensing_thread(spa);
d2734cce
SD
3281
3282 ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL);
3283 spa->spa_checkpoint_discard_zthr =
843e9ca2
SD
3284 zthr_create("z_checkpoint_discard",
3285 spa_checkpoint_discard_thread_check,
6bc61d22 3286 spa_checkpoint_discard_thread, spa, minclsyspri);
9d5b5245
SD
3287}
3288
428870ff
BB
3289/*
3290 * Fix up config after a partly-completed split. This is done with the
3291 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
3292 * pool have that entry in their config, but only the splitting one contains
3293 * a list of all the guids of the vdevs that are being split off.
3294 *
3295 * This function determines what to do with that list: either rejoin
3296 * all the disks to the pool, or complete the splitting process. To attempt
3297 * the rejoin, each disk that is offlined is marked online again, and
3298 * we do a reopen() call. If the vdev label for every disk that was
3299 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
3300 * then we call vdev_split() on each disk, and complete the split.
3301 *
3302 * Otherwise we leave the config alone, with all the vdevs in place in
3303 * the original pool.
3304 */
3305static void
3306spa_try_repair(spa_t *spa, nvlist_t *config)
3307{
3308 uint_t extracted;
3309 uint64_t *glist;
3310 uint_t i, gcount;
3311 nvlist_t *nvl;
3312 vdev_t **vd;
3313 boolean_t attempt_reopen;
3314
3315 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
3316 return;
3317
3318 /* check that the config is complete */
3319 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
3320 &glist, &gcount) != 0)
3321 return;
3322
79c76d5b 3323 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
428870ff
BB
3324
3325 /* attempt to online all the vdevs & validate */
3326 attempt_reopen = B_TRUE;
3327 for (i = 0; i < gcount; i++) {
3328 if (glist[i] == 0) /* vdev is hole */
3329 continue;
3330
3331 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
3332 if (vd[i] == NULL) {
3333 /*
3334 * Don't bother attempting to reopen the disks;
3335 * just do the split.
3336 */
3337 attempt_reopen = B_FALSE;
3338 } else {
3339 /* attempt to re-online it */
3340 vd[i]->vdev_offline = B_FALSE;
3341 }
3342 }
3343
3344 if (attempt_reopen) {
3345 vdev_reopen(spa->spa_root_vdev);
3346
3347 /* check each device to see what state it's in */
3348 for (extracted = 0, i = 0; i < gcount; i++) {
3349 if (vd[i] != NULL &&
3350 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
3351 break;
3352 ++extracted;
3353 }
3354 }
3355
3356 /*
3357 * If every disk has been moved to the new pool, or if we never
3358 * even attempted to look at them, then we split them off for
3359 * good.
3360 */
3361 if (!attempt_reopen || gcount == extracted) {
3362 for (i = 0; i < gcount; i++)
3363 if (vd[i] != NULL)
3364 vdev_split(vd[i]);
3365 vdev_reopen(spa->spa_root_vdev);
3366 }
3367
3368 kmem_free(vd, gcount * sizeof (vdev_t *));
3369}
3370
3371static int
6cb8e530 3372spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type)
428870ff 3373{
a926aab9 3374 const char *ereport = FM_EREPORT_ZFS_POOL;
428870ff 3375 int error;
428870ff 3376
6cb8e530 3377 spa->spa_load_state = state;
ca95f70d
OF
3378 (void) spa_import_progress_set_state(spa_guid(spa),
3379 spa_load_state(spa));
687e4d7f 3380 spa_import_progress_set_notes(spa, "spa_load()");
9ae529ec 3381
6cb8e530 3382 gethrestime(&spa->spa_loaded_ts);
d2734cce 3383 error = spa_load_impl(spa, type, &ereport);
428870ff 3384
0c66c32d
JG
3385 /*
3386 * Don't count references from objsets that are already closed
3387 * and are making their way through the eviction process.
3388 */
3389 spa_evicting_os_wait(spa);
424fd7c3 3390 spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
572e2857
BB
3391 if (error) {
3392 if (error != EEXIST) {
3393 spa->spa_loaded_ts.tv_sec = 0;
3394 spa->spa_loaded_ts.tv_nsec = 0;
3395 }
3396 if (error != EBADF) {
1144586b 3397 (void) zfs_ereport_post(ereport, spa,
4f072827 3398 NULL, NULL, NULL, 0);
572e2857
BB
3399 }
3400 }
428870ff
BB
3401 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
3402 spa->spa_ena = 0;
3403
ca95f70d
OF
3404 (void) spa_import_progress_set_state(spa_guid(spa),
3405 spa_load_state(spa));
3406
428870ff
BB
3407 return (error);
3408}
3409
33cf67cd 3410#ifdef ZFS_DEBUG
e0ab3ab5
JS
3411/*
3412 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3413 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3414 * spa's per-vdev ZAP list.
3415 */
3416static uint64_t
3417vdev_count_verify_zaps(vdev_t *vd)
3418{
3419 spa_t *spa = vd->vdev_spa;
3420 uint64_t total = 0;
e0ab3ab5 3421
3e4ed421
RW
3422 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2) &&
3423 vd->vdev_root_zap != 0) {
3424 total++;
3425 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3426 spa->spa_all_vdev_zaps, vd->vdev_root_zap));
3427 }
e0ab3ab5
JS
3428 if (vd->vdev_top_zap != 0) {
3429 total++;
3430 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3431 spa->spa_all_vdev_zaps, vd->vdev_top_zap));
3432 }
3433 if (vd->vdev_leaf_zap != 0) {
3434 total++;
3435 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
3436 spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
3437 }
3438
1c27024e 3439 for (uint64_t i = 0; i < vd->vdev_children; i++) {
e0ab3ab5
JS
3440 total += vdev_count_verify_zaps(vd->vdev_child[i]);
3441 }
3442
3443 return (total);
3444}
36542b06
AZ
3445#else
3446#define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
33cf67cd 3447#endif
e0ab3ab5 3448
379ca9cf
OF
3449/*
3450 * Determine whether the activity check is required.
3451 */
3452static boolean_t
bbffb59e
BB
3453spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label,
3454 nvlist_t *config)
379ca9cf
OF
3455{
3456 uint64_t state = 0;
3457 uint64_t hostid = 0;
3458 uint64_t tryconfig_txg = 0;
3459 uint64_t tryconfig_timestamp = 0;
060f0226 3460 uint16_t tryconfig_mmp_seq = 0;
379ca9cf
OF
3461 nvlist_t *nvinfo;
3462
3463 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
3464 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
3465 (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG,
3466 &tryconfig_txg);
3467 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3468 &tryconfig_timestamp);
060f0226
OF
3469 (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ,
3470 &tryconfig_mmp_seq);
379ca9cf
OF
3471 }
3472
3473 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state);
379ca9cf
OF
3474
3475 /*
3476 * Disable the MMP activity check - This is used by zdb which
3477 * is intended to be used on potentially active pools.
3478 */
3479 if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP)
3480 return (B_FALSE);
3481
3482 /*
3483 * Skip the activity check when the MMP feature is disabled.
3484 */
3485 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0)
3486 return (B_FALSE);
ca95f70d 3487
379ca9cf 3488 /*
060f0226
OF
3489 * If the tryconfig_ values are nonzero, they are the results of an
3490 * earlier tryimport. If they all match the uberblock we just found,
3491 * then the pool has not changed and we return false so we do not test
3492 * a second time.
379ca9cf
OF
3493 */
3494 if (tryconfig_txg && tryconfig_txg == ub->ub_txg &&
060f0226
OF
3495 tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp &&
3496 tryconfig_mmp_seq && tryconfig_mmp_seq ==
3497 (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0))
379ca9cf
OF
3498 return (B_FALSE);
3499
3500 /*
3501 * Allow the activity check to be skipped when importing the pool
bbffb59e
BB
3502 * on the same host which last imported it. Since the hostid from
3503 * configuration may be stale use the one read from the label.
379ca9cf 3504 */
bbffb59e
BB
3505 if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID))
3506 hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID);
3507
25f06d67 3508 if (hostid == spa_get_hostid(spa))
379ca9cf
OF
3509 return (B_FALSE);
3510
3511 /*
3512 * Skip the activity test when the pool was cleanly exported.
3513 */
3514 if (state != POOL_STATE_ACTIVE)
3515 return (B_FALSE);
3516
3517 return (B_TRUE);
3518}
3519
060f0226
OF
3520/*
3521 * Nanoseconds the activity check must watch for changes on-disk.
3522 */
3523static uint64_t
3524spa_activity_check_duration(spa_t *spa, uberblock_t *ub)
3525{
3526 uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1);
3527 uint64_t multihost_interval = MSEC2NSEC(
3528 MMP_INTERVAL_OK(zfs_multihost_interval));
3529 uint64_t import_delay = MAX(NANOSEC, import_intervals *
3530 multihost_interval);
3531
3532 /*
3533 * Local tunables determine a minimum duration except for the case
3534 * where we know when the remote host will suspend the pool if MMP
3535 * writes do not land.
3536 *
3537 * See Big Theory comment at the top of mmp.c for the reasoning behind
3538 * these cases and times.
3539 */
3540
3541 ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100);
3542
3543 if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
3544 MMP_FAIL_INT(ub) > 0) {
3545
3546 /* MMP on remote host will suspend pool after failed writes */
3547 import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) *
3548 MMP_IMPORT_SAFETY_FACTOR / 100;
3549
3550 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3551 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
8e739b2c
RE
3552 "import_intervals=%llu", (u_longlong_t)import_delay,
3553 (u_longlong_t)MMP_FAIL_INT(ub),
3554 (u_longlong_t)MMP_INTERVAL(ub),
3555 (u_longlong_t)import_intervals);
060f0226
OF
3556
3557 } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) &&
3558 MMP_FAIL_INT(ub) == 0) {
3559
3560 /* MMP on remote host will never suspend pool */
3561 import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) +
3562 ub->ub_mmp_delay) * import_intervals);
3563
3564 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3565 "mmp_interval=%llu ub_mmp_delay=%llu "
8e739b2c
RE
3566 "import_intervals=%llu", (u_longlong_t)import_delay,
3567 (u_longlong_t)MMP_INTERVAL(ub),
3568 (u_longlong_t)ub->ub_mmp_delay,
3569 (u_longlong_t)import_intervals);
060f0226
OF
3570
3571 } else if (MMP_VALID(ub)) {
3572 /*
e1cfd73f 3573 * zfs-0.7 compatibility case
060f0226
OF
3574 */
3575
3576 import_delay = MAX(import_delay, (multihost_interval +
3577 ub->ub_mmp_delay) * import_intervals);
3578
3579 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
8e739b2c
RE
3580 "import_intervals=%llu leaves=%u",
3581 (u_longlong_t)import_delay,
3582 (u_longlong_t)ub->ub_mmp_delay,
3583 (u_longlong_t)import_intervals,
060f0226
OF
3584 vdev_count_leaves(spa));
3585 } else {
3586 /* Using local tunings is the only reasonable option */
3587 zfs_dbgmsg("pool last imported on non-MMP aware "
3588 "host using import_delay=%llu multihost_interval=%llu "
8e739b2c
RE
3589 "import_intervals=%llu", (u_longlong_t)import_delay,
3590 (u_longlong_t)multihost_interval,
3591 (u_longlong_t)import_intervals);
060f0226
OF
3592 }
3593
3594 return (import_delay);
3595}
3596
379ca9cf
OF
3597/*
3598 * Perform the import activity check. If the user canceled the import or
3599 * we detected activity then fail.
3600 */
3601static int
3602spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config)
3603{
379ca9cf
OF
3604 uint64_t txg = ub->ub_txg;
3605 uint64_t timestamp = ub->ub_timestamp;
060f0226
OF
3606 uint64_t mmp_config = ub->ub_mmp_config;
3607 uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0;
3608 uint64_t import_delay;
687e4d7f 3609 hrtime_t import_expire, now;
379ca9cf
OF
3610 nvlist_t *mmp_label = NULL;
3611 vdev_t *rvd = spa->spa_root_vdev;
3612 kcondvar_t cv;
3613 kmutex_t mtx;
3614 int error = 0;
3615
3616 cv_init(&cv, NULL, CV_DEFAULT, NULL);
3617 mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL);
3618 mutex_enter(&mtx);
3619
3620 /*
3621 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3622 * during the earlier tryimport. If the txg recorded there is 0 then
3623 * the pool is known to be active on another host.
3624 *
060f0226 3625 * Otherwise, the pool might be in use on another host. Check for
379ca9cf
OF
3626 * changes in the uberblocks on disk if necessary.
3627 */
3628 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) {
3629 nvlist_t *nvinfo = fnvlist_lookup_nvlist(config,
3630 ZPOOL_CONFIG_LOAD_INFO);
3631
3632 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) &&
3633 fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) {
3634 vdev_uberblock_load(rvd, ub, &mmp_label);
3635 error = SET_ERROR(EREMOTEIO);
3636 goto out;
3637 }
3638 }
3639
060f0226 3640 import_delay = spa_activity_check_duration(spa, ub);
533ea041 3641
379ca9cf 3642 /* Add a small random factor in case of simultaneous imports (0-25%) */
29274c9f 3643 import_delay += import_delay * random_in_range(250) / 1000;
ca95f70d
OF
3644
3645 import_expire = gethrtime() + import_delay;
379ca9cf 3646
687e4d7f
DB
3647 spa_import_progress_set_notes(spa, "Checking MMP activity, waiting "
3648 "%llu ms", (u_longlong_t)NSEC2MSEC(import_delay));
3649
3650 int interations = 0;
3651 while ((now = gethrtime()) < import_expire) {
3652 if (interations++ % 30 == 0) {
3653 spa_import_progress_set_notes(spa, "Checking MMP "
3654 "activity, %llu ms remaining",
3655 (u_longlong_t)NSEC2MSEC(import_expire - now));
3656 }
3657
ca95f70d
OF
3658 (void) spa_import_progress_set_mmp_check(spa_guid(spa),
3659 NSEC2SEC(import_expire - gethrtime()));
3660
379ca9cf
OF
3661 vdev_uberblock_load(rvd, ub, &mmp_label);
3662
060f0226
OF
3663 if (txg != ub->ub_txg || timestamp != ub->ub_timestamp ||
3664 mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) {
3665 zfs_dbgmsg("multihost activity detected "
3666 "txg %llu ub_txg %llu "
3667 "timestamp %llu ub_timestamp %llu "
3668 "mmp_config %#llx ub_mmp_config %#llx",
8e739b2c
RE
3669 (u_longlong_t)txg, (u_longlong_t)ub->ub_txg,
3670 (u_longlong_t)timestamp,
3671 (u_longlong_t)ub->ub_timestamp,
3672 (u_longlong_t)mmp_config,
3673 (u_longlong_t)ub->ub_mmp_config);
060f0226 3674
379ca9cf
OF
3675 error = SET_ERROR(EREMOTEIO);
3676 break;
3677 }
3678
3679 if (mmp_label) {
3680 nvlist_free(mmp_label);
3681 mmp_label = NULL;
3682 }
3683
3684 error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz);
3685 if (error != -1) {
3686 error = SET_ERROR(EINTR);
3687 break;
3688 }
3689 error = 0;
3690 }
3691
3692out:
3693 mutex_exit(&mtx);
3694 mutex_destroy(&mtx);
3695 cv_destroy(&cv);
3696
3697 /*
3698 * If the pool is determined to be active store the status in the
3699 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3700 * available from configuration read from disk store them as well.
3701 * This allows 'zpool import' to generate a more useful message.
3702 *
3703 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3704 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3705 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3706 */
3707 if (error == EREMOTEIO) {
a926aab9 3708 const char *hostname = "<unknown>";
379ca9cf
OF
3709 uint64_t hostid = 0;
3710
3711 if (mmp_label) {
3712 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) {
3713 hostname = fnvlist_lookup_string(mmp_label,
3714 ZPOOL_CONFIG_HOSTNAME);
3715 fnvlist_add_string(spa->spa_load_info,
3716 ZPOOL_CONFIG_MMP_HOSTNAME, hostname);
3717 }
3718
3719 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) {
3720 hostid = fnvlist_lookup_uint64(mmp_label,
3721 ZPOOL_CONFIG_HOSTID);
3722 fnvlist_add_uint64(spa->spa_load_info,
3723 ZPOOL_CONFIG_MMP_HOSTID, hostid);
3724 }
3725 }
3726
3727 fnvlist_add_uint64(spa->spa_load_info,
3728 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE);
3729 fnvlist_add_uint64(spa->spa_load_info,
3730 ZPOOL_CONFIG_MMP_TXG, 0);
3731
3732 error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO);
3733 }
3734
3735 if (mmp_label)
3736 nvlist_free(mmp_label);
3737
3738 return (error);
3739}
3740
9eb7b46e 3741static int
6cb8e530
PZ
3742spa_verify_host(spa_t *spa, nvlist_t *mos_config)
3743{
3744 uint64_t hostid;
d1807f16 3745 const char *hostname;
6cb8e530
PZ
3746 uint64_t myhostid = 0;
3747
3748 if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config,
3749 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
3750 hostname = fnvlist_lookup_string(mos_config,
3751 ZPOOL_CONFIG_HOSTNAME);
3752
3753 myhostid = zone_get_hostid(NULL);
3754
3755 if (hostid != 0 && myhostid != 0 && hostid != myhostid) {
3756 cmn_err(CE_WARN, "pool '%s' could not be "
3757 "loaded as it was last accessed by "
3758 "another system (host: %s hostid: 0x%llx). "
a2f944a1
RM
3759 "See: https://openzfs.github.io/openzfs-docs/msg/"
3760 "ZFS-8000-EY",
6cb8e530
PZ
3761 spa_name(spa), hostname, (u_longlong_t)hostid);
3762 spa_load_failed(spa, "hostid verification failed: pool "
3763 "last accessed by host: %s (hostid: 0x%llx)",
3764 hostname, (u_longlong_t)hostid);
3765 return (SET_ERROR(EBADF));
3766 }
3767 }
3768
3769 return (0);
3770}
3771
3772static int
3773spa_ld_parse_config(spa_t *spa, spa_import_type_t type)
428870ff
BB
3774{
3775 int error = 0;
6cb8e530 3776 nvlist_t *nvtree, *nvl, *config = spa->spa_config;
1c27024e 3777 int parse;
9eb7b46e 3778 vdev_t *rvd;
6cb8e530 3779 uint64_t pool_guid;
d1807f16
RY
3780 const char *comment;
3781 const char *compatibility;
6cb8e530
PZ
3782
3783 /*
3784 * Versioning wasn't explicitly added to the label until later, so if
3785 * it's not present treat it as the initial version.
3786 */
3787 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3788 &spa->spa_ubsync.ub_version) != 0)
3789 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
3790
3791 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
3792 spa_load_failed(spa, "invalid config provided: '%s' missing",
3793 ZPOOL_CONFIG_POOL_GUID);
3794 return (SET_ERROR(EINVAL));
3795 }
3796
d2734cce
SD
3797 /*
3798 * If we are doing an import, ensure that the pool is not already
3799 * imported by checking if its pool guid already exists in the
3800 * spa namespace.
3801 *
3802 * The only case that we allow an already imported pool to be
3803 * imported again, is when the pool is checkpointed and we want to
3804 * look at its checkpointed state from userland tools like zdb.
3805 */
3806#ifdef _KERNEL
3807 if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
3808 spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
3809 spa_guid_exists(pool_guid, 0)) {
3810#else
3811 if ((spa->spa_load_state == SPA_LOAD_IMPORT ||
3812 spa->spa_load_state == SPA_LOAD_TRYIMPORT) &&
3813 spa_guid_exists(pool_guid, 0) &&
3814 !spa_importing_readonly_checkpoint(spa)) {
3815#endif
6cb8e530
PZ
3816 spa_load_failed(spa, "a pool with guid %llu is already open",
3817 (u_longlong_t)pool_guid);
3818 return (SET_ERROR(EEXIST));
3819 }
3820
3821 spa->spa_config_guid = pool_guid;
3822
3823 nvlist_free(spa->spa_load_info);
3824 spa->spa_load_info = fnvlist_alloc();
3825
3826 ASSERT(spa->spa_comment == NULL);
3827 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
3828 spa->spa_comment = spa_strdup(comment);
3829
658fb802
CB
3830 ASSERT(spa->spa_compatibility == NULL);
3831 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMPATIBILITY,
3832 &compatibility) == 0)
3833 spa->spa_compatibility = spa_strdup(compatibility);
3834
6cb8e530
PZ
3835 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
3836 &spa->spa_config_txg);
3837
3838 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0)
3839 spa->spa_config_splitting = fnvlist_dup(nvl);
428870ff 3840
4a0ee12a
PZ
3841 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) {
3842 spa_load_failed(spa, "invalid config provided: '%s' missing",
3843 ZPOOL_CONFIG_VDEV_TREE);
2e528b49 3844 return (SET_ERROR(EINVAL));
4a0ee12a 3845 }
428870ff 3846
428870ff
BB
3847 /*
3848 * Create "The Godfather" zio to hold all async IOs
3849 */
e022864d
MA
3850 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3851 KM_SLEEP);
1c27024e 3852 for (int i = 0; i < max_ncpus; i++) {
e022864d
MA
3853 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3854 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3855 ZIO_FLAG_GODFATHER);
3856 }
428870ff
BB
3857
3858 /*
3859 * Parse the configuration into a vdev tree. We explicitly set the
3860 * value that will be returned by spa_version() since parsing the
3861 * configuration requires knowing the version number.
3862 */
3863 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6cb8e530
PZ
3864 parse = (type == SPA_IMPORT_EXISTING ?
3865 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
9eb7b46e 3866 error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse);
428870ff
BB
3867 spa_config_exit(spa, SCL_ALL, FTAG);
3868
4a0ee12a
PZ
3869 if (error != 0) {
3870 spa_load_failed(spa, "unable to parse config [error=%d]",
3871 error);
428870ff 3872 return (error);
4a0ee12a 3873 }
428870ff
BB
3874
3875 ASSERT(spa->spa_root_vdev == rvd);
c3520e7f
MA
3876 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
3877 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
428870ff
BB
3878
3879 if (type != SPA_IMPORT_ASSEMBLE) {
3880 ASSERT(spa_guid(spa) == pool_guid);
3881 }
3882
9eb7b46e
PZ
3883 return (0);
3884}
3885
6cb8e530
PZ
3886/*
3887 * Recursively open all vdevs in the vdev tree. This function is called twice:
3888 * first with the untrusted config, then with the trusted config.
3889 */
9eb7b46e
PZ
3890static int
3891spa_ld_open_vdevs(spa_t *spa)
3892{
3893 int error = 0;
3894
6cb8e530
PZ
3895 /*
3896 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3897 * missing/unopenable for the root vdev to be still considered openable.
3898 */
3899 if (spa->spa_trust_config) {
3900 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds;
3901 } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) {
3902 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile;
3903 } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) {
3904 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan;
3905 } else {
3906 spa->spa_missing_tvds_allowed = 0;
3907 }
3908
3909 spa->spa_missing_tvds_allowed =
3910 MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed);
3911
428870ff 3912 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9eb7b46e 3913 error = vdev_open(spa->spa_root_vdev);
428870ff 3914 spa_config_exit(spa, SCL_ALL, FTAG);
6cb8e530
PZ
3915
3916 if (spa->spa_missing_tvds != 0) {
3917 spa_load_note(spa, "vdev tree has %lld missing top-level "
3918 "vdevs.", (u_longlong_t)spa->spa_missing_tvds);
da92d5cb 3919 if (spa->spa_trust_config && (spa->spa_mode & SPA_MODE_WRITE)) {
6cb8e530
PZ
3920 /*
3921 * Although theoretically we could allow users to open
3922 * incomplete pools in RW mode, we'd need to add a lot
3923 * of extra logic (e.g. adjust pool space to account
3924 * for missing vdevs).
3925 * This limitation also prevents users from accidentally
3926 * opening the pool in RW mode during data recovery and
3927 * damaging it further.
3928 */
3929 spa_load_note(spa, "pools with missing top-level "
3930 "vdevs can only be opened in read-only mode.");
3931 error = SET_ERROR(ENXIO);
3932 } else {
3933 spa_load_note(spa, "current settings allow for maximum "
3934 "%lld missing top-level vdevs at this stage.",
3935 (u_longlong_t)spa->spa_missing_tvds_allowed);
3936 }
3937 }
4a0ee12a
PZ
3938 if (error != 0) {
3939 spa_load_failed(spa, "unable to open vdev tree [error=%d]",
3940 error);
3941 }
6cb8e530
PZ
3942 if (spa->spa_missing_tvds != 0 || error != 0)
3943 vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2);
9eb7b46e
PZ
3944
3945 return (error);
3946}
3947
6cb8e530
PZ
3948/*
3949 * We need to validate the vdev labels against the configuration that
3950 * we have in hand. This function is called twice: first with an untrusted
3951 * config, then with a trusted config. The validation is more strict when the
3952 * config is trusted.
3953 */
9eb7b46e 3954static int
6cb8e530 3955spa_ld_validate_vdevs(spa_t *spa)
9eb7b46e
PZ
3956{
3957 int error = 0;
3958 vdev_t *rvd = spa->spa_root_vdev;
428870ff 3959
6cb8e530
PZ
3960 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3961 error = vdev_validate(rvd);
3962 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff 3963
6cb8e530
PZ
3964 if (error != 0) {
3965 spa_load_failed(spa, "vdev_validate failed [error=%d]", error);
3966 return (error);
3967 }
428870ff 3968
6cb8e530
PZ
3969 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
3970 spa_load_failed(spa, "cannot open vdev tree after invalidating "
3971 "some vdevs");
3972 vdev_dbgmsg_print_tree(rvd, 2);
3973 return (SET_ERROR(ENXIO));
428870ff
BB
3974 }
3975
9eb7b46e
PZ
3976 return (0);
3977}
3978
d2734cce
SD
3979static void
3980spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub)
3981{
3982 spa->spa_state = POOL_STATE_ACTIVE;
3983 spa->spa_ubsync = spa->spa_uberblock;
3984 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
3985 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
3986 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
3987 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
3988 spa->spa_claim_max_txg = spa->spa_first_txg;
3989 spa->spa_prev_software_version = ub->ub_software_version;
3990}
3991
9eb7b46e 3992static int
6cb8e530 3993spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type)
9eb7b46e
PZ
3994{
3995 vdev_t *rvd = spa->spa_root_vdev;
3996 nvlist_t *label;
3997 uberblock_t *ub = &spa->spa_uberblock;
9eb7b46e
PZ
3998 boolean_t activity_check = B_FALSE;
3999
d2734cce
SD
4000 /*
4001 * If we are opening the checkpointed state of the pool by
4002 * rewinding to it, at this point we will have written the
4003 * checkpointed uberblock to the vdev labels, so searching
4004 * the labels will find the right uberblock. However, if
4005 * we are opening the checkpointed state read-only, we have
4006 * not modified the labels. Therefore, we must ignore the
4007 * labels and continue using the spa_uberblock that was set
4008 * by spa_ld_checkpoint_rewind.
4009 *
4010 * Note that it would be fine to ignore the labels when
4011 * rewinding (opening writeable) as well. However, if we
4012 * crash just after writing the labels, we will end up
4013 * searching the labels. Doing so in the common case means
4014 * that this code path gets exercised normally, rather than
4015 * just in the edge case.
4016 */
4017 if (ub->ub_checkpoint_txg != 0 &&
4018 spa_importing_readonly_checkpoint(spa)) {
4019 spa_ld_select_uberblock_done(spa, ub);
4020 return (0);
4021 }
4022
428870ff
BB
4023 /*
4024 * Find the best uberblock.
4025 */
9ae529ec 4026 vdev_uberblock_load(rvd, ub, &label);
428870ff
BB
4027
4028 /*
4029 * If we weren't able to find a single valid uberblock, return failure.
4030 */
9ae529ec
CS
4031 if (ub->ub_txg == 0) {
4032 nvlist_free(label);
4a0ee12a 4033 spa_load_failed(spa, "no valid uberblock found");
428870ff 4034 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
9ae529ec 4035 }
428870ff 4036
ca95f70d
OF
4037 if (spa->spa_load_max_txg != UINT64_MAX) {
4038 (void) spa_import_progress_set_max_txg(spa_guid(spa),
4039 (u_longlong_t)spa->spa_load_max_txg);
4040 }
4a0ee12a
PZ
4041 spa_load_note(spa, "using uberblock with txg=%llu",
4042 (u_longlong_t)ub->ub_txg);
5caeef02
DB
4043 if (ub->ub_raidz_reflow_info != 0) {
4044 spa_load_note(spa, "uberblock raidz_reflow_info: "
4045 "state=%u offset=%llu",
4046 (int)RRSS_GET_STATE(ub),
4047 (u_longlong_t)RRSS_GET_OFFSET(ub));
4048 }
4a0ee12a
PZ
4049
4050
379ca9cf
OF
4051 /*
4052 * For pools which have the multihost property on determine if the
4053 * pool is truly inactive and can be safely imported. Prevent
4054 * hosts which don't have a hostid set from importing the pool.
4055 */
6cb8e530
PZ
4056 activity_check = spa_activity_check_required(spa, ub, label,
4057 spa->spa_config);
379ca9cf 4058 if (activity_check) {
379ca9cf 4059 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay &&
25f06d67 4060 spa_get_hostid(spa) == 0) {
379ca9cf
OF
4061 nvlist_free(label);
4062 fnvlist_add_uint64(spa->spa_load_info,
4063 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
4064 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
4065 }
4066
6cb8e530 4067 int error = spa_activity_check(spa, ub, spa->spa_config);
e889f0f5
OF
4068 if (error) {
4069 nvlist_free(label);
4070 return (error);
4071 }
4072
379ca9cf
OF
4073 fnvlist_add_uint64(spa->spa_load_info,
4074 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE);
4075 fnvlist_add_uint64(spa->spa_load_info,
4076 ZPOOL_CONFIG_MMP_TXG, ub->ub_txg);
060f0226
OF
4077 fnvlist_add_uint16(spa->spa_load_info,
4078 ZPOOL_CONFIG_MMP_SEQ,
4079 (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0));
379ca9cf
OF
4080 }
4081
428870ff 4082 /*
9ae529ec 4083 * If the pool has an unsupported version we can't open it.
428870ff 4084 */
9ae529ec
CS
4085 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
4086 nvlist_free(label);
4a0ee12a
PZ
4087 spa_load_failed(spa, "version %llu is not supported",
4088 (u_longlong_t)ub->ub_version);
428870ff 4089 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
9ae529ec
CS
4090 }
4091
4092 if (ub->ub_version >= SPA_VERSION_FEATURES) {
4093 nvlist_t *features;
4094
4095 /*
4096 * If we weren't able to find what's necessary for reading the
4097 * MOS in the label, return failure.
4098 */
4a0ee12a
PZ
4099 if (label == NULL) {
4100 spa_load_failed(spa, "label config unavailable");
4101 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
4102 ENXIO));
4103 }
4104
4105 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ,
4106 &features) != 0) {
9ae529ec 4107 nvlist_free(label);
4a0ee12a
PZ
4108 spa_load_failed(spa, "invalid label: '%s' missing",
4109 ZPOOL_CONFIG_FEATURES_FOR_READ);
9ae529ec
CS
4110 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
4111 ENXIO));
4112 }
4113
4114 /*
4115 * Update our in-core representation with the definitive values
4116 * from the label.
4117 */
4118 nvlist_free(spa->spa_label_features);
65ad5d11 4119 spa->spa_label_features = fnvlist_dup(features);
9ae529ec
CS
4120 }
4121
4122 nvlist_free(label);
4123
4124 /*
4125 * Look through entries in the label nvlist's features_for_read. If
4126 * there is a feature listed there which we don't understand then we
4127 * cannot open a pool.
4128 */
4129 if (ub->ub_version >= SPA_VERSION_FEATURES) {
4130 nvlist_t *unsup_feat;
9ae529ec 4131
65ad5d11 4132 unsup_feat = fnvlist_alloc();
9ae529ec 4133
1c27024e
DB
4134 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
4135 NULL); nvp != NULL;
9ae529ec
CS
4136 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
4137 if (!zfeature_is_supported(nvpair_name(nvp))) {
65ad5d11
AJ
4138 fnvlist_add_string(unsup_feat,
4139 nvpair_name(nvp), "");
9ae529ec
CS
4140 }
4141 }
4142
4143 if (!nvlist_empty(unsup_feat)) {
65ad5d11
AJ
4144 fnvlist_add_nvlist(spa->spa_load_info,
4145 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
9ae529ec 4146 nvlist_free(unsup_feat);
4a0ee12a 4147 spa_load_failed(spa, "some features are unsupported");
9ae529ec
CS
4148 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
4149 ENOTSUP));
4150 }
4151
4152 nvlist_free(unsup_feat);
4153 }
428870ff 4154
428870ff
BB
4155 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
4156 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6cb8e530 4157 spa_try_repair(spa, spa->spa_config);
428870ff
BB
4158 spa_config_exit(spa, SCL_ALL, FTAG);
4159 nvlist_free(spa->spa_config_splitting);
4160 spa->spa_config_splitting = NULL;
4161 }
4162
4163 /*
4164 * Initialize internal SPA structures.
4165 */
d2734cce 4166 spa_ld_select_uberblock_done(spa, ub);
428870ff 4167
9eb7b46e
PZ
4168 return (0);
4169}
4170
4171static int
4172spa_ld_open_rootbp(spa_t *spa)
4173{
4174 int error = 0;
4175 vdev_t *rvd = spa->spa_root_vdev;
a1d477c2 4176
9ae529ec 4177 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
4a0ee12a
PZ
4178 if (error != 0) {
4179 spa_load_failed(spa, "unable to open rootbp in dsl_pool_init "
4180 "[error=%d]", error);
428870ff 4181 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4182 }
428870ff
BB
4183 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
4184
9eb7b46e
PZ
4185 return (0);
4186}
4187
4188static int
d2734cce 4189spa_ld_trusted_config(spa_t *spa, spa_import_type_t type,
6cb8e530 4190 boolean_t reloading)
9eb7b46e 4191{
6cb8e530
PZ
4192 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
4193 nvlist_t *nv, *mos_config, *policy;
4194 int error = 0, copy_error;
4195 uint64_t healthy_tvds, healthy_tvds_mos;
4196 uint64_t mos_config_txg;
9eb7b46e 4197
4a0ee12a
PZ
4198 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE)
4199 != 0)
428870ff
BB
4200 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4201
a1d477c2 4202 /*
6cb8e530
PZ
4203 * If we're assembling a pool from a split, the config provided is
4204 * already trusted so there is nothing to do.
a1d477c2 4205 */
6cb8e530
PZ
4206 if (type == SPA_IMPORT_ASSEMBLE)
4207 return (0);
4208
4209 healthy_tvds = spa_healthy_core_tvds(spa);
a1d477c2 4210
6cb8e530
PZ
4211 if (load_nvlist(spa, spa->spa_config_object, &mos_config)
4212 != 0) {
4213 spa_load_failed(spa, "unable to retrieve MOS config");
4214 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4215 }
4216
4217 /*
4218 * If we are doing an open, pool owner wasn't verified yet, thus do
4219 * the verification here.
4220 */
4221 if (spa->spa_load_state == SPA_LOAD_OPEN) {
4222 error = spa_verify_host(spa, mos_config);
4223 if (error != 0) {
a1d477c2 4224 nvlist_free(mos_config);
6cb8e530 4225 return (error);
a1d477c2 4226 }
6cb8e530
PZ
4227 }
4228
4229 nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE);
a1d477c2 4230
6cb8e530
PZ
4231 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4232
4233 /*
4234 * Build a new vdev tree from the trusted config
4235 */
b2255edc
BB
4236 error = spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD);
4237 if (error != 0) {
4238 nvlist_free(mos_config);
4239 spa_config_exit(spa, SCL_ALL, FTAG);
4240 spa_load_failed(spa, "spa_config_parse failed [error=%d]",
4241 error);
4242 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4243 }
6cb8e530
PZ
4244
4245 /*
4246 * Vdev paths in the MOS may be obsolete. If the untrusted config was
4247 * obtained by scanning /dev/dsk, then it will have the right vdev
4248 * paths. We update the trusted MOS config with this information.
4249 * We first try to copy the paths with vdev_copy_path_strict, which
4250 * succeeds only when both configs have exactly the same vdev tree.
4251 * If that fails, we fall back to a more flexible method that has a
4252 * best effort policy.
4253 */
4254 copy_error = vdev_copy_path_strict(rvd, mrvd);
4255 if (copy_error != 0 || spa_load_print_vdev_tree) {
4256 spa_load_note(spa, "provided vdev tree:");
4257 vdev_dbgmsg_print_tree(rvd, 2);
4258 spa_load_note(spa, "MOS vdev tree:");
4259 vdev_dbgmsg_print_tree(mrvd, 2);
4260 }
4261 if (copy_error != 0) {
4262 spa_load_note(spa, "vdev_copy_path_strict failed, falling "
4263 "back to vdev_copy_path_relaxed");
4264 vdev_copy_path_relaxed(rvd, mrvd);
4265 }
4266
4267 vdev_close(rvd);
4268 vdev_free(rvd);
4269 spa->spa_root_vdev = mrvd;
4270 rvd = mrvd;
4271 spa_config_exit(spa, SCL_ALL, FTAG);
4272
54b1b1d8
RN
4273 /*
4274 * If 'zpool import' used a cached config, then the on-disk hostid and
4275 * hostname may be different to the cached config in ways that should
4276 * prevent import. Userspace can't discover this without a scan, but
4277 * we know, so we add these values to LOAD_INFO so the caller can know
4278 * the difference.
4279 *
4280 * Note that we have to do this before the config is regenerated,
4281 * because the new config will have the hostid and hostname for this
4282 * host, in readiness for import.
4283 */
4284 if (nvlist_exists(mos_config, ZPOOL_CONFIG_HOSTID))
4285 fnvlist_add_uint64(spa->spa_load_info, ZPOOL_CONFIG_HOSTID,
4286 fnvlist_lookup_uint64(mos_config, ZPOOL_CONFIG_HOSTID));
4287 if (nvlist_exists(mos_config, ZPOOL_CONFIG_HOSTNAME))
4288 fnvlist_add_string(spa->spa_load_info, ZPOOL_CONFIG_HOSTNAME,
4289 fnvlist_lookup_string(mos_config, ZPOOL_CONFIG_HOSTNAME));
4290
6cb8e530
PZ
4291 /*
4292 * We will use spa_config if we decide to reload the spa or if spa_load
4293 * fails and we rewind. We must thus regenerate the config using the
8a393be3
PZ
4294 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
4295 * pass settings on how to load the pool and is not stored in the MOS.
4296 * We copy it over to our new, trusted config.
6cb8e530
PZ
4297 */
4298 mos_config_txg = fnvlist_lookup_uint64(mos_config,
4299 ZPOOL_CONFIG_POOL_TXG);
4300 nvlist_free(mos_config);
4301 mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE);
8a393be3 4302 if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY,
6cb8e530 4303 &policy) == 0)
8a393be3 4304 fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy);
6cb8e530
PZ
4305 spa_config_set(spa, mos_config);
4306 spa->spa_config_source = SPA_CONFIG_SRC_MOS;
4307
4308 /*
4309 * Now that we got the config from the MOS, we should be more strict
4310 * in checking blkptrs and can make assumptions about the consistency
4311 * of the vdev tree. spa_trust_config must be set to true before opening
4312 * vdevs in order for them to be writeable.
4313 */
4314 spa->spa_trust_config = B_TRUE;
4315
4316 /*
4317 * Open and validate the new vdev tree
4318 */
4319 error = spa_ld_open_vdevs(spa);
4320 if (error != 0)
4321 return (error);
4322
4323 error = spa_ld_validate_vdevs(spa);
4324 if (error != 0)
4325 return (error);
4326
4327 if (copy_error != 0 || spa_load_print_vdev_tree) {
4328 spa_load_note(spa, "final vdev tree:");
4329 vdev_dbgmsg_print_tree(rvd, 2);
4330 }
4331
4332 if (spa->spa_load_state != SPA_LOAD_TRYIMPORT &&
4333 !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) {
a1d477c2 4334 /*
6cb8e530
PZ
4335 * Sanity check to make sure that we are indeed loading the
4336 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4337 * in the config provided and they happened to be the only ones
4338 * to have the latest uberblock, we could involuntarily perform
4339 * an extreme rewind.
a1d477c2 4340 */
6cb8e530
PZ
4341 healthy_tvds_mos = spa_healthy_core_tvds(spa);
4342 if (healthy_tvds_mos - healthy_tvds >=
4343 SPA_SYNC_MIN_VDEVS) {
4344 spa_load_note(spa, "config provided misses too many "
4345 "top-level vdevs compared to MOS (%lld vs %lld). ",
4346 (u_longlong_t)healthy_tvds,
4347 (u_longlong_t)healthy_tvds_mos);
4348 spa_load_note(spa, "vdev tree:");
4349 vdev_dbgmsg_print_tree(rvd, 2);
4350 if (reloading) {
4351 spa_load_failed(spa, "config was already "
4352 "provided from MOS. Aborting.");
4353 return (spa_vdev_err(rvd,
4354 VDEV_AUX_CORRUPT_DATA, EIO));
4355 }
4356 spa_load_note(spa, "spa must be reloaded using MOS "
4357 "config");
4358 return (SET_ERROR(EAGAIN));
4a0ee12a 4359 }
a1d477c2
MA
4360 }
4361
6cb8e530
PZ
4362 error = spa_check_for_missing_logs(spa);
4363 if (error != 0)
4364 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
4365
4366 if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) {
4367 spa_load_failed(spa, "uberblock guid sum doesn't match MOS "
4368 "guid sum (%llu != %llu)",
4369 (u_longlong_t)spa->spa_uberblock.ub_guid_sum,
4370 (u_longlong_t)rvd->vdev_guid_sum);
4371 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
4372 ENXIO));
4373 }
4374
9eb7b46e
PZ
4375 return (0);
4376}
4377
4378static int
4379spa_ld_open_indirect_vdev_metadata(spa_t *spa)
4380{
4381 int error = 0;
4382 vdev_t *rvd = spa->spa_root_vdev;
4383
a1d477c2
MA
4384 /*
4385 * Everything that we read before spa_remove_init() must be stored
4386 * on concreted vdevs. Therefore we do this as early as possible.
4387 */
4a0ee12a
PZ
4388 error = spa_remove_init(spa);
4389 if (error != 0) {
4390 spa_load_failed(spa, "spa_remove_init failed [error=%d]",
4391 error);
a1d477c2 4392 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4393 }
a1d477c2 4394
9eb7b46e
PZ
4395 /*
4396 * Retrieve information needed to condense indirect vdev mappings.
4397 */
4398 error = spa_condense_init(spa);
4399 if (error != 0) {
4a0ee12a
PZ
4400 spa_load_failed(spa, "spa_condense_init failed [error=%d]",
4401 error);
9eb7b46e
PZ
4402 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4403 }
4404
4405 return (0);
4406}
4407
4408static int
4a0ee12a 4409spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep)
9eb7b46e
PZ
4410{
4411 int error = 0;
4412 vdev_t *rvd = spa->spa_root_vdev;
4413
9ae529ec
CS
4414 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
4415 boolean_t missing_feat_read = B_FALSE;
b9b24bb4 4416 nvlist_t *unsup_feat, *enabled_feat;
9ae529ec
CS
4417
4418 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
4a0ee12a 4419 &spa->spa_feat_for_read_obj, B_TRUE) != 0) {
9ae529ec
CS
4420 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4421 }
4422
4423 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
4a0ee12a 4424 &spa->spa_feat_for_write_obj, B_TRUE) != 0) {
9ae529ec
CS
4425 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4426 }
4427
4428 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
4a0ee12a 4429 &spa->spa_feat_desc_obj, B_TRUE) != 0) {
9ae529ec
CS
4430 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4431 }
4432
b9b24bb4
CS
4433 enabled_feat = fnvlist_alloc();
4434 unsup_feat = fnvlist_alloc();
9ae529ec 4435
fa86b5db 4436 if (!spa_features_check(spa, B_FALSE,
b9b24bb4 4437 unsup_feat, enabled_feat))
9ae529ec
CS
4438 missing_feat_read = B_TRUE;
4439
4a0ee12a
PZ
4440 if (spa_writeable(spa) ||
4441 spa->spa_load_state == SPA_LOAD_TRYIMPORT) {
fa86b5db 4442 if (!spa_features_check(spa, B_TRUE,
b9b24bb4 4443 unsup_feat, enabled_feat)) {
9eb7b46e 4444 *missing_feat_writep = B_TRUE;
b9b24bb4 4445 }
9ae529ec
CS
4446 }
4447
b9b24bb4
CS
4448 fnvlist_add_nvlist(spa->spa_load_info,
4449 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
4450
9ae529ec 4451 if (!nvlist_empty(unsup_feat)) {
b9b24bb4
CS
4452 fnvlist_add_nvlist(spa->spa_load_info,
4453 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
9ae529ec
CS
4454 }
4455
b9b24bb4
CS
4456 fnvlist_free(enabled_feat);
4457 fnvlist_free(unsup_feat);
9ae529ec
CS
4458
4459 if (!missing_feat_read) {
4460 fnvlist_add_boolean(spa->spa_load_info,
4461 ZPOOL_CONFIG_CAN_RDONLY);
4462 }
4463
4464 /*
4465 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4466 * twofold: to determine whether the pool is available for
4467 * import in read-write mode and (if it is not) whether the
4468 * pool is available for import in read-only mode. If the pool
4469 * is available for import in read-write mode, it is displayed
4470 * as available in userland; if it is not available for import
4471 * in read-only mode, it is displayed as unavailable in
4472 * userland. If the pool is available for import in read-only
4473 * mode but not read-write mode, it is displayed as unavailable
4474 * in userland with a special note that the pool is actually
4475 * available for open in read-only mode.
4476 *
4477 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4478 * missing a feature for write, we must first determine whether
4479 * the pool can be opened read-only before returning to
4480 * userland in order to know whether to display the
4481 * abovementioned note.
4482 */
9eb7b46e 4483 if (missing_feat_read || (*missing_feat_writep &&
9ae529ec 4484 spa_writeable(spa))) {
4a0ee12a 4485 spa_load_failed(spa, "pool uses unsupported features");
9ae529ec
CS
4486 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
4487 ENOTSUP));
4488 }
b0bc7a84
MG
4489
4490 /*
4491 * Load refcounts for ZFS features from disk into an in-memory
4492 * cache during SPA initialization.
4493 */
1c27024e 4494 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
b0bc7a84
MG
4495 uint64_t refcount;
4496
4497 error = feature_get_refcount_from_disk(spa,
4498 &spa_feature_table[i], &refcount);
4499 if (error == 0) {
4500 spa->spa_feat_refcount_cache[i] = refcount;
4501 } else if (error == ENOTSUP) {
4502 spa->spa_feat_refcount_cache[i] =
4503 SPA_FEATURE_DISABLED;
4504 } else {
4a0ee12a
PZ
4505 spa_load_failed(spa, "error getting refcount "
4506 "for feature %s [error=%d]",
4507 spa_feature_table[i].fi_guid, error);
b0bc7a84
MG
4508 return (spa_vdev_err(rvd,
4509 VDEV_AUX_CORRUPT_DATA, EIO));
4510 }
4511 }
4512 }
4513
4514 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
4515 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
4a0ee12a 4516 &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0)
b0bc7a84 4517 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
9ae529ec
CS
4518 }
4519
f00ab3f2
TC
4520 /*
4521 * Encryption was added before bookmark_v2, even though bookmark_v2
4522 * is now a dependency. If this pool has encryption enabled without
4523 * bookmark_v2, trigger an errata message.
4524 */
4525 if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) &&
4526 !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) {
4527 spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
4528 }
4529
9eb7b46e
PZ
4530 return (0);
4531}
4532
4533static int
4534spa_ld_load_special_directories(spa_t *spa)
4535{
4536 int error = 0;
4537 vdev_t *rvd = spa->spa_root_vdev;
4538
9ae529ec
CS
4539 spa->spa_is_initializing = B_TRUE;
4540 error = dsl_pool_open(spa->spa_dsl_pool);
4541 spa->spa_is_initializing = B_FALSE;
4a0ee12a
PZ
4542 if (error != 0) {
4543 spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error);
9ae529ec 4544 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4545 }
9ae529ec 4546
9eb7b46e
PZ
4547 return (0);
4548}
428870ff 4549
9eb7b46e
PZ
4550static int
4551spa_ld_get_props(spa_t *spa)
4552{
4553 int error = 0;
4554 uint64_t obj;
4555 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 4556
3c67d83a
TH
4557 /* Grab the checksum salt from the MOS. */
4558 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4559 DMU_POOL_CHECKSUM_SALT, 1,
4560 sizeof (spa->spa_cksum_salt.zcs_bytes),
4561 spa->spa_cksum_salt.zcs_bytes);
4562 if (error == ENOENT) {
4563 /* Generate a new salt for subsequent use */
4564 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
4565 sizeof (spa->spa_cksum_salt.zcs_bytes));
4566 } else if (error != 0) {
4a0ee12a
PZ
4567 spa_load_failed(spa, "unable to retrieve checksum salt from "
4568 "MOS [error=%d]", error);
3c67d83a
TH
4569 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4570 }
4571
4a0ee12a 4572 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0)
428870ff
BB
4573 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4574 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
4a0ee12a
PZ
4575 if (error != 0) {
4576 spa_load_failed(spa, "error opening deferred-frees bpobj "
4577 "[error=%d]", error);
428870ff 4578 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4579 }
34dc7c2f
BB
4580
4581 /*
4582 * Load the bit that tells us to use the new accounting function
4583 * (raid-z deflation). If we have an older pool, this will not
4584 * be present.
4585 */
4a0ee12a 4586 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE);
428870ff
BB
4587 if (error != 0 && error != ENOENT)
4588 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4589
4590 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
4a0ee12a 4591 &spa->spa_creation_version, B_FALSE);
428870ff
BB
4592 if (error != 0 && error != ENOENT)
4593 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
4594
4595 /*
4596 * Load the persistent error log. If we have an older pool, this will
4597 * not be present.
4598 */
4a0ee12a
PZ
4599 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last,
4600 B_FALSE);
428870ff
BB
4601 if (error != 0 && error != ENOENT)
4602 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 4603
428870ff 4604 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
4a0ee12a 4605 &spa->spa_errlog_scrub, B_FALSE);
428870ff
BB
4606 if (error != 0 && error != ENOENT)
4607 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 4608
37f03da8
SH
4609 /*
4610 * Load the livelist deletion field. If a livelist is queued for
4611 * deletion, indicate that in the spa
4612 */
4613 error = spa_dir_prop(spa, DMU_POOL_DELETED_CLONES,
4614 &spa->spa_livelists_to_delete, B_FALSE);
4615 if (error != 0 && error != ENOENT)
4616 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4617
34dc7c2f
BB
4618 /*
4619 * Load the history object. If we have an older pool, this
4620 * will not be present.
4621 */
4a0ee12a 4622 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE);
428870ff
BB
4623 if (error != 0 && error != ENOENT)
4624 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4625
e0ab3ab5
JS
4626 /*
4627 * Load the per-vdev ZAP map. If we have an older pool, this will not
4628 * be present; in this case, defer its creation to a later time to
4629 * avoid dirtying the MOS this early / out of sync context. See
4630 * spa_sync_config_object.
4631 */
4632
4633 /* The sentinel is only available in the MOS config. */
1c27024e 4634 nvlist_t *mos_config;
4a0ee12a
PZ
4635 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) {
4636 spa_load_failed(spa, "unable to retrieve MOS config");
e0ab3ab5 4637 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4638 }
e0ab3ab5
JS
4639
4640 error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
4a0ee12a 4641 &spa->spa_all_vdev_zaps, B_FALSE);
e0ab3ab5 4642
38640550
DB
4643 if (error == ENOENT) {
4644 VERIFY(!nvlist_exists(mos_config,
4645 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
4646 spa->spa_avz_action = AVZ_ACTION_INITIALIZE;
4647 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
4648 } else if (error != 0) {
cb01da68 4649 nvlist_free(mos_config);
e0ab3ab5 4650 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
38640550 4651 } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
e0ab3ab5
JS
4652 /*
4653 * An older version of ZFS overwrote the sentinel value, so
4654 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4655 * destruction to later; see spa_sync_config_object.
4656 */
4657 spa->spa_avz_action = AVZ_ACTION_DESTROY;
4658 /*
4659 * We're assuming that no vdevs have had their ZAPs created
4660 * before this. Better be sure of it.
4661 */
4662 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
4663 }
4664 nvlist_free(mos_config);
4665
9eb7b46e
PZ
4666 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
4667
4a0ee12a
PZ
4668 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object,
4669 B_FALSE);
9eb7b46e
PZ
4670 if (error && error != ENOENT)
4671 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4672
4673 if (error == 0) {
da27b8bc 4674 uint64_t autoreplace = 0;
9eb7b46e
PZ
4675
4676 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
4677 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
4678 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
4679 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
4680 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
c02c1bec 4681 spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost);
1b939560 4682 spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim);
9eb7b46e
PZ
4683 spa->spa_autoreplace = (autoreplace != 0);
4684 }
4685
6cb8e530
PZ
4686 /*
4687 * If we are importing a pool with missing top-level vdevs,
4688 * we enforce that the pool doesn't panic or get suspended on
4689 * error since the likelihood of missing data is extremely high.
4690 */
4691 if (spa->spa_missing_tvds > 0 &&
4692 spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE &&
4693 spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
4694 spa_load_note(spa, "forcing failmode to 'continue' "
4695 "as some top level vdevs are missing");
4696 spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE;
4697 }
4698
9eb7b46e
PZ
4699 return (0);
4700}
4701
4702static int
4703spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type)
4704{
4705 int error = 0;
4706 vdev_t *rvd = spa->spa_root_vdev;
4707
428870ff
BB
4708 /*
4709 * If we're assembling the pool from the split-off vdevs of
4710 * an existing pool, we don't want to attach the spares & cache
4711 * devices.
4712 */
34dc7c2f
BB
4713
4714 /*
4715 * Load any hot spares for this pool.
4716 */
4a0ee12a
PZ
4717 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object,
4718 B_FALSE);
428870ff
BB
4719 if (error != 0 && error != ENOENT)
4720 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4721 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
4722 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
4723 if (load_nvlist(spa, spa->spa_spares.sav_object,
4a0ee12a
PZ
4724 &spa->spa_spares.sav_config) != 0) {
4725 spa_load_failed(spa, "error loading spares nvlist");
428870ff 4726 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4727 }
34dc7c2f 4728
b128c09f 4729 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4730 spa_load_spares(spa);
b128c09f 4731 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
4732 } else if (error == 0) {
4733 spa->spa_spares.sav_sync = B_TRUE;
34dc7c2f
BB
4734 }
4735
4736 /*
4737 * Load any level 2 ARC devices for this pool.
4738 */
428870ff 4739 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
4a0ee12a 4740 &spa->spa_l2cache.sav_object, B_FALSE);
428870ff
BB
4741 if (error != 0 && error != ENOENT)
4742 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4743 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
4744 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
4745 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
4a0ee12a
PZ
4746 &spa->spa_l2cache.sav_config) != 0) {
4747 spa_load_failed(spa, "error loading l2cache nvlist");
428870ff 4748 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4749 }
34dc7c2f 4750
b128c09f 4751 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4752 spa_load_l2cache(spa);
b128c09f 4753 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
4754 } else if (error == 0) {
4755 spa->spa_l2cache.sav_sync = B_TRUE;
b128c09f
BB
4756 }
4757
9eb7b46e
PZ
4758 return (0);
4759}
428870ff 4760
9eb7b46e 4761static int
4a0ee12a 4762spa_ld_load_vdev_metadata(spa_t *spa)
9eb7b46e
PZ
4763{
4764 int error = 0;
4765 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f 4766
379ca9cf
OF
4767 /*
4768 * If the 'multihost' property is set, then never allow a pool to
4769 * be imported when the system hostid is zero. The exception to
4770 * this rule is zdb which is always allowed to access pools.
4771 */
25f06d67 4772 if (spa_multihost(spa) && spa_get_hostid(spa) == 0 &&
379ca9cf
OF
4773 (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) {
4774 fnvlist_add_uint64(spa->spa_load_info,
4775 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID);
4776 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO));
4777 }
4778
34dc7c2f
BB
4779 /*
4780 * If the 'autoreplace' property is set, then post a resource notifying
4781 * the ZFS DE that it should not issue any faults for unopenable
4782 * devices. We also iterate over the vdevs, and post a sysevent for any
4783 * unopenable vdevs so that the normal autoreplace handler can take
4784 * over.
4785 */
4a0ee12a 4786 if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
34dc7c2f 4787 spa_check_removed(spa->spa_root_vdev);
428870ff
BB
4788 /*
4789 * For the import case, this is done in spa_import(), because
4790 * at this point we're using the spare definitions from
4791 * the MOS config, not necessarily from the userland config.
4792 */
4a0ee12a 4793 if (spa->spa_load_state != SPA_LOAD_IMPORT) {
428870ff
BB
4794 spa_aux_check_removed(&spa->spa_spares);
4795 spa_aux_check_removed(&spa->spa_l2cache);
4796 }
4797 }
34dc7c2f
BB
4798
4799 /*
9eb7b46e 4800 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
34dc7c2f 4801 */
a1d477c2
MA
4802 error = vdev_load(rvd);
4803 if (error != 0) {
4a0ee12a 4804 spa_load_failed(spa, "vdev_load failed [error=%d]", error);
a1d477c2
MA
4805 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4806 }
4807
93e28d66
SD
4808 error = spa_ld_log_spacemaps(spa);
4809 if (error != 0) {
600a02b8 4810 spa_load_failed(spa, "spa_ld_log_spacemaps failed [error=%d]",
93e28d66
SD
4811 error);
4812 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error));
4813 }
4814
34dc7c2f 4815 /*
9eb7b46e 4816 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
34dc7c2f 4817 */
b128c09f 4818 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
9a49d3f3 4819 vdev_dtl_reassess(rvd, 0, 0, B_FALSE, B_FALSE);
b128c09f 4820 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 4821
9eb7b46e
PZ
4822 return (0);
4823}
4824
4825static int
4826spa_ld_load_dedup_tables(spa_t *spa)
4827{
4828 int error = 0;
4829 vdev_t *rvd = spa->spa_root_vdev;
4830
428870ff 4831 error = ddt_load(spa);
4a0ee12a
PZ
4832 if (error != 0) {
4833 spa_load_failed(spa, "ddt_load failed [error=%d]", error);
428870ff 4834 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4a0ee12a 4835 }
428870ff 4836
9eb7b46e
PZ
4837 return (0);
4838}
4839
67a1b037
PJD
4840static int
4841spa_ld_load_brt(spa_t *spa)
4842{
4843 int error = 0;
4844 vdev_t *rvd = spa->spa_root_vdev;
4845
4846 error = brt_load(spa);
4847 if (error != 0) {
4848 spa_load_failed(spa, "brt_load failed [error=%d]", error);
4849 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
4850 }
4851
4852 return (0);
4853}
4854
9eb7b46e 4855static int
a926aab9 4856spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, const char **ereport)
9eb7b46e
PZ
4857{
4858 vdev_t *rvd = spa->spa_root_vdev;
428870ff 4859
4a0ee12a
PZ
4860 if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) {
4861 boolean_t missing = spa_check_logs(spa);
4862 if (missing) {
6cb8e530
PZ
4863 if (spa->spa_missing_tvds != 0) {
4864 spa_load_note(spa, "spa_check_logs failed "
4865 "so dropping the logs");
4866 } else {
4867 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
4868 spa_load_failed(spa, "spa_check_logs failed");
4869 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG,
4870 ENXIO));
4871 }
4a0ee12a 4872 }
428870ff
BB
4873 }
4874
9eb7b46e
PZ
4875 return (0);
4876}
4877
4878static int
4a0ee12a 4879spa_ld_verify_pool_data(spa_t *spa)
9eb7b46e
PZ
4880{
4881 int error = 0;
4882 vdev_t *rvd = spa->spa_root_vdev;
4883
4884 /*
4885 * We've successfully opened the pool, verify that we're ready
4886 * to start pushing transactions.
4887 */
4a0ee12a 4888 if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) {
9eb7b46e
PZ
4889 error = spa_load_verify(spa);
4890 if (error != 0) {
4a0ee12a
PZ
4891 spa_load_failed(spa, "spa_load_verify failed "
4892 "[error=%d]", error);
9eb7b46e
PZ
4893 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
4894 error));
4895 }
4896 }
4897
4898 return (0);
4899}
4900
4901static void
4902spa_ld_claim_log_blocks(spa_t *spa)
4903{
4904 dmu_tx_t *tx;
4905 dsl_pool_t *dp = spa_get_dsl(spa);
4906
4907 /*
4908 * Claim log blocks that haven't been committed yet.
4909 * This must all happen in a single txg.
4910 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4911 * invoked from zil_claim_log_block()'s i/o done callback.
4912 * Price of rollback is that we abandon the log.
4913 */
4914 spa->spa_claiming = B_TRUE;
4915
4916 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
4917 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
4918 zil_claim, tx, DS_FIND_CHILDREN);
4919 dmu_tx_commit(tx);
4920
4921 spa->spa_claiming = B_FALSE;
4922
4923 spa_set_log_state(spa, SPA_LOG_GOOD);
4924}
4925
4926static void
6cb8e530 4927spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg,
d2734cce 4928 boolean_t update_config_cache)
9eb7b46e
PZ
4929{
4930 vdev_t *rvd = spa->spa_root_vdev;
4931 int need_update = B_FALSE;
4932
4933 /*
4934 * If the config cache is stale, or we have uninitialized
4935 * metaslabs (see spa_vdev_add()), then update the config.
4936 *
4937 * If this is a verbatim import, trust the current
4938 * in-core spa_config and update the disk labels.
4939 */
d2734cce 4940 if (update_config_cache || config_cache_txg != spa->spa_config_txg ||
4a0ee12a
PZ
4941 spa->spa_load_state == SPA_LOAD_IMPORT ||
4942 spa->spa_load_state == SPA_LOAD_RECOVER ||
9eb7b46e
PZ
4943 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
4944 need_update = B_TRUE;
4945
4946 for (int c = 0; c < rvd->vdev_children; c++)
4947 if (rvd->vdev_child[c]->vdev_ms_array == 0)
4948 need_update = B_TRUE;
4949
4950 /*
e1cfd73f 4951 * Update the config cache asynchronously in case we're the
9eb7b46e
PZ
4952 * root pool, in which case the config cache isn't writable yet.
4953 */
4954 if (need_update)
4955 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4956}
4957
6cb8e530
PZ
4958static void
4959spa_ld_prepare_for_reload(spa_t *spa)
4960{
da92d5cb 4961 spa_mode_t mode = spa->spa_mode;
6cb8e530
PZ
4962 int async_suspended = spa->spa_async_suspended;
4963
4964 spa_unload(spa);
4965 spa_deactivate(spa);
4966 spa_activate(spa, mode);
4967
4968 /*
4969 * We save the value of spa_async_suspended as it gets reset to 0 by
4970 * spa_unload(). We want to restore it back to the original value before
4971 * returning as we might be calling spa_async_resume() later.
4972 */
4973 spa->spa_async_suspended = async_suspended;
4974}
4975
9eb7b46e 4976static int
d2734cce
SD
4977spa_ld_read_checkpoint_txg(spa_t *spa)
4978{
4979 uberblock_t checkpoint;
4980 int error = 0;
4981
4982 ASSERT0(spa->spa_checkpoint_txg);
4983 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4984
4985 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4986 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
4987 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
4988
4989 if (error == ENOENT)
4990 return (0);
4991
4992 if (error != 0)
4993 return (error);
4994
4995 ASSERT3U(checkpoint.ub_txg, !=, 0);
4996 ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0);
4997 ASSERT3U(checkpoint.ub_timestamp, !=, 0);
4998 spa->spa_checkpoint_txg = checkpoint.ub_txg;
4999 spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp;
5000
5001 return (0);
5002}
5003
5004static int
5005spa_ld_mos_init(spa_t *spa, spa_import_type_t type)
9eb7b46e
PZ
5006{
5007 int error = 0;
9eb7b46e 5008
4a0ee12a 5009 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6cb8e530 5010 ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
4a0ee12a 5011
9eb7b46e 5012 /*
6cb8e530
PZ
5013 * Never trust the config that is provided unless we are assembling
5014 * a pool following a split.
5015 * This means don't trust blkptrs and the vdev tree in general. This
5016 * also effectively puts the spa in read-only mode since
5017 * spa_writeable() checks for spa_trust_config to be true.
5018 * We will later load a trusted config from the MOS.
9eb7b46e 5019 */
6cb8e530
PZ
5020 if (type != SPA_IMPORT_ASSEMBLE)
5021 spa->spa_trust_config = B_FALSE;
5022
9eb7b46e
PZ
5023 /*
5024 * Parse the config provided to create a vdev tree.
5025 */
6cb8e530 5026 error = spa_ld_parse_config(spa, type);
9eb7b46e
PZ
5027 if (error != 0)
5028 return (error);
5029
ca95f70d
OF
5030 spa_import_progress_add(spa);
5031
9eb7b46e
PZ
5032 /*
5033 * Now that we have the vdev tree, try to open each vdev. This involves
5034 * opening the underlying physical device, retrieving its geometry and
5035 * probing the vdev with a dummy I/O. The state of each vdev will be set
5036 * based on the success of those operations. After this we'll be ready
5037 * to read from the vdevs.
5038 */
5039 error = spa_ld_open_vdevs(spa);
5040 if (error != 0)
5041 return (error);
5042
5043 /*
5044 * Read the label of each vdev and make sure that the GUIDs stored
5045 * there match the GUIDs in the config provided.
6cb8e530
PZ
5046 * If we're assembling a new pool that's been split off from an
5047 * existing pool, the labels haven't yet been updated so we skip
5048 * validation for now.
9eb7b46e 5049 */
6cb8e530
PZ
5050 if (type != SPA_IMPORT_ASSEMBLE) {
5051 error = spa_ld_validate_vdevs(spa);
5052 if (error != 0)
5053 return (error);
5054 }
9eb7b46e
PZ
5055
5056 /*
d2734cce
SD
5057 * Read all vdev labels to find the best uberblock (i.e. latest,
5058 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
5059 * get the list of features required to read blkptrs in the MOS from
5060 * the vdev label with the best uberblock and verify that our version
5061 * of zfs supports them all.
9eb7b46e 5062 */
6cb8e530 5063 error = spa_ld_select_uberblock(spa, type);
9eb7b46e
PZ
5064 if (error != 0)
5065 return (error);
5066
5067 /*
5068 * Pass that uberblock to the dsl_pool layer which will open the root
5069 * blkptr. This blkptr points to the latest version of the MOS and will
5070 * allow us to read its contents.
5071 */
5072 error = spa_ld_open_rootbp(spa);
5073 if (error != 0)
5074 return (error);
5075
d2734cce
SD
5076 return (0);
5077}
5078
5079static int
5080spa_ld_checkpoint_rewind(spa_t *spa)
5081{
5082 uberblock_t checkpoint;
5083 int error = 0;
5084
5085 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5086 ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
5087
5088 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
5089 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
5090 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
5091
5092 if (error != 0) {
5093 spa_load_failed(spa, "unable to retrieve checkpointed "
5094 "uberblock from the MOS config [error=%d]", error);
5095
5096 if (error == ENOENT)
5097 error = ZFS_ERR_NO_CHECKPOINT;
5098
5099 return (error);
5100 }
5101
5102 ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg);
5103 ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg);
5104
5105 /*
5106 * We need to update the txg and timestamp of the checkpointed
5107 * uberblock to be higher than the latest one. This ensures that
5108 * the checkpointed uberblock is selected if we were to close and
5109 * reopen the pool right after we've written it in the vdev labels.
5110 * (also see block comment in vdev_uberblock_compare)
5111 */
5112 checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1;
5113 checkpoint.ub_timestamp = gethrestime_sec();
5114
5115 /*
5116 * Set current uberblock to be the checkpointed uberblock.
5117 */
5118 spa->spa_uberblock = checkpoint;
5119
5120 /*
5121 * If we are doing a normal rewind, then the pool is open for
5122 * writing and we sync the "updated" checkpointed uberblock to
5123 * disk. Once this is done, we've basically rewound the whole
5124 * pool and there is no way back.
5125 *
5126 * There are cases when we don't want to attempt and sync the
5127 * checkpointed uberblock to disk because we are opening a
5128 * pool as read-only. Specifically, verifying the checkpointed
5129 * state with zdb, and importing the checkpointed state to get
5130 * a "preview" of its content.
5131 */
5132 if (spa_writeable(spa)) {
5133 vdev_t *rvd = spa->spa_root_vdev;
5134
5135 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5136 vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
5137 int svdcount = 0;
5138 int children = rvd->vdev_children;
29274c9f 5139 int c0 = random_in_range(children);
d2734cce
SD
5140
5141 for (int c = 0; c < children; c++) {
5142 vdev_t *vd = rvd->vdev_child[(c0 + c) % children];
5143
5144 /* Stop when revisiting the first vdev */
5145 if (c > 0 && svd[0] == vd)
5146 break;
5147
5148 if (vd->vdev_ms_array == 0 || vd->vdev_islog ||
5149 !vdev_is_concrete(vd))
5150 continue;
5151
5152 svd[svdcount++] = vd;
5153 if (svdcount == SPA_SYNC_MIN_VDEVS)
5154 break;
5155 }
5156 error = vdev_config_sync(svd, svdcount, spa->spa_first_txg);
5157 if (error == 0)
5158 spa->spa_last_synced_guid = rvd->vdev_guid;
5159 spa_config_exit(spa, SCL_ALL, FTAG);
5160
5161 if (error != 0) {
5162 spa_load_failed(spa, "failed to write checkpointed "
5163 "uberblock to the vdev labels [error=%d]", error);
5164 return (error);
5165 }
5166 }
5167
5168 return (0);
5169}
5170
5171static int
5172spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type,
5173 boolean_t *update_config_cache)
5174{
5175 int error;
5176
5177 /*
5178 * Parse the config for pool, open and validate vdevs,
5179 * select an uberblock, and use that uberblock to open
5180 * the MOS.
5181 */
5182 error = spa_ld_mos_init(spa, type);
5183 if (error != 0)
5184 return (error);
5185
9eb7b46e 5186 /*
6cb8e530
PZ
5187 * Retrieve the trusted config stored in the MOS and use it to create
5188 * a new, exact version of the vdev tree, then reopen all vdevs.
9eb7b46e 5189 */
d2734cce 5190 error = spa_ld_trusted_config(spa, type, B_FALSE);
6cb8e530 5191 if (error == EAGAIN) {
d2734cce
SD
5192 if (update_config_cache != NULL)
5193 *update_config_cache = B_TRUE;
5194
6cb8e530
PZ
5195 /*
5196 * Redo the loading process with the trusted config if it is
5197 * too different from the untrusted config.
5198 */
5199 spa_ld_prepare_for_reload(spa);
d2734cce
SD
5200 spa_load_note(spa, "RELOADING");
5201 error = spa_ld_mos_init(spa, type);
5202 if (error != 0)
5203 return (error);
5204
5205 error = spa_ld_trusted_config(spa, type, B_TRUE);
5206 if (error != 0)
5207 return (error);
5208
6cb8e530 5209 } else if (error != 0) {
9eb7b46e 5210 return (error);
6cb8e530 5211 }
9eb7b46e 5212
d2734cce
SD
5213 return (0);
5214}
5215
5216/*
5217 * Load an existing storage pool, using the config provided. This config
5218 * describes which vdevs are part of the pool and is later validated against
5219 * partial configs present in each vdev's label and an entire copy of the
5220 * config stored in the MOS.
5221 */
5222static int
a926aab9 5223spa_load_impl(spa_t *spa, spa_import_type_t type, const char **ereport)
d2734cce
SD
5224{
5225 int error = 0;
5226 boolean_t missing_feat_write = B_FALSE;
5227 boolean_t checkpoint_rewind =
5228 (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
5229 boolean_t update_config_cache = B_FALSE;
5230
5231 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5232 ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE);
5233
5234 spa_load_note(spa, "LOADING");
5235
5236 error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache);
5237 if (error != 0)
5238 return (error);
5239
5240 /*
5241 * If we are rewinding to the checkpoint then we need to repeat
5242 * everything we've done so far in this function but this time
5243 * selecting the checkpointed uberblock and using that to open
5244 * the MOS.
5245 */
5246 if (checkpoint_rewind) {
5247 /*
5248 * If we are rewinding to the checkpoint update config cache
5249 * anyway.
5250 */
5251 update_config_cache = B_TRUE;
5252
5253 /*
5254 * Extract the checkpointed uberblock from the current MOS
5255 * and use this as the pool's uberblock from now on. If the
5256 * pool is imported as writeable we also write the checkpoint
5257 * uberblock to the labels, making the rewind permanent.
5258 */
5259 error = spa_ld_checkpoint_rewind(spa);
5260 if (error != 0)
5261 return (error);
5262
5263 /*
e1cfd73f 5264 * Redo the loading process again with the
d2734cce
SD
5265 * checkpointed uberblock.
5266 */
5267 spa_ld_prepare_for_reload(spa);
5268 spa_load_note(spa, "LOADING checkpointed uberblock");
5269 error = spa_ld_mos_with_trusted_config(spa, type, NULL);
5270 if (error != 0)
5271 return (error);
5272 }
5273
5274 /*
5275 * Retrieve the checkpoint txg if the pool has a checkpoint.
5276 */
687e4d7f 5277 spa_import_progress_set_notes(spa, "Loading checkpoint txg");
d2734cce
SD
5278 error = spa_ld_read_checkpoint_txg(spa);
5279 if (error != 0)
5280 return (error);
5281
9eb7b46e
PZ
5282 /*
5283 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
5284 * from the pool and their contents were re-mapped to other vdevs. Note
5285 * that everything that we read before this step must have been
5286 * rewritten on concrete vdevs after the last device removal was
5287 * initiated. Otherwise we could be reading from indirect vdevs before
5288 * we have loaded their mappings.
5289 */
687e4d7f 5290 spa_import_progress_set_notes(spa, "Loading indirect vdev metadata");
9eb7b46e
PZ
5291 error = spa_ld_open_indirect_vdev_metadata(spa);
5292 if (error != 0)
5293 return (error);
5294
5295 /*
5296 * Retrieve the full list of active features from the MOS and check if
5297 * they are all supported.
5298 */
687e4d7f 5299 spa_import_progress_set_notes(spa, "Checking feature flags");
4a0ee12a 5300 error = spa_ld_check_features(spa, &missing_feat_write);
9eb7b46e
PZ
5301 if (error != 0)
5302 return (error);
5303
5304 /*
5305 * Load several special directories from the MOS needed by the dsl_pool
5306 * layer.
5307 */
687e4d7f 5308 spa_import_progress_set_notes(spa, "Loading special MOS directories");
9eb7b46e
PZ
5309 error = spa_ld_load_special_directories(spa);
5310 if (error != 0)
5311 return (error);
5312
9eb7b46e
PZ
5313 /*
5314 * Retrieve pool properties from the MOS.
5315 */
687e4d7f 5316 spa_import_progress_set_notes(spa, "Loading properties");
9eb7b46e
PZ
5317 error = spa_ld_get_props(spa);
5318 if (error != 0)
5319 return (error);
5320
5321 /*
5322 * Retrieve the list of auxiliary devices - cache devices and spares -
5323 * and open them.
5324 */
687e4d7f 5325 spa_import_progress_set_notes(spa, "Loading AUX vdevs");
9eb7b46e
PZ
5326 error = spa_ld_open_aux_vdevs(spa, type);
5327 if (error != 0)
5328 return (error);
5329
5330 /*
5331 * Load the metadata for all vdevs. Also check if unopenable devices
5332 * should be autoreplaced.
5333 */
687e4d7f 5334 spa_import_progress_set_notes(spa, "Loading vdev metadata");
4a0ee12a 5335 error = spa_ld_load_vdev_metadata(spa);
9eb7b46e
PZ
5336 if (error != 0)
5337 return (error);
5338
687e4d7f 5339 spa_import_progress_set_notes(spa, "Loading dedup tables");
9eb7b46e
PZ
5340 error = spa_ld_load_dedup_tables(spa);
5341 if (error != 0)
5342 return (error);
5343
687e4d7f 5344 spa_import_progress_set_notes(spa, "Loading BRT");
67a1b037
PJD
5345 error = spa_ld_load_brt(spa);
5346 if (error != 0)
5347 return (error);
5348
9eb7b46e
PZ
5349 /*
5350 * Verify the logs now to make sure we don't have any unexpected errors
5351 * when we claim log blocks later.
5352 */
687e4d7f 5353 spa_import_progress_set_notes(spa, "Verifying Log Devices");
9eb7b46e
PZ
5354 error = spa_ld_verify_logs(spa, type, ereport);
5355 if (error != 0)
5356 return (error);
5357
9ae529ec 5358 if (missing_feat_write) {
6cb8e530 5359 ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT);
9ae529ec
CS
5360
5361 /*
5362 * At this point, we know that we can open the pool in
5363 * read-only mode but not read-write mode. We now have enough
5364 * information and can return to userland.
5365 */
9eb7b46e
PZ
5366 return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT,
5367 ENOTSUP));
9ae529ec
CS
5368 }
5369
572e2857 5370 /*
9eb7b46e
PZ
5371 * Traverse the last txgs to make sure the pool was left off in a safe
5372 * state. When performing an extreme rewind, we verify the whole pool,
5373 * which can take a very long time.
572e2857 5374 */
687e4d7f 5375 spa_import_progress_set_notes(spa, "Verifying pool data");
4a0ee12a 5376 error = spa_ld_verify_pool_data(spa);
9eb7b46e
PZ
5377 if (error != 0)
5378 return (error);
572e2857 5379
9eb7b46e
PZ
5380 /*
5381 * Calculate the deflated space for the pool. This must be done before
5382 * we write anything to the pool because we'd need to update the space
5383 * accounting using the deflated sizes.
5384 */
687e4d7f 5385 spa_import_progress_set_notes(spa, "Calculating deflated space");
9eb7b46e
PZ
5386 spa_update_dspace(spa);
5387
5388 /*
5389 * We have now retrieved all the information we needed to open the
5390 * pool. If we are importing the pool in read-write mode, a few
5391 * additional steps must be performed to finish the import.
5392 */
687e4d7f 5393 spa_import_progress_set_notes(spa, "Starting import");
6cb8e530 5394 if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER ||
428870ff 5395 spa->spa_load_max_txg == UINT64_MAX)) {
6cb8e530
PZ
5396 uint64_t config_cache_txg = spa->spa_config_txg;
5397
5398 ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT);
34dc7c2f 5399
5caeef02
DB
5400 /*
5401 * Before we do any zio_write's, complete the raidz expansion
5402 * scratch space copying, if necessary.
5403 */
5404 if (RRSS_GET_STATE(&spa->spa_uberblock) == RRSS_SCRATCH_VALID)
5405 vdev_raidz_reflow_copy_scratch(spa);
5406
d2734cce
SD
5407 /*
5408 * In case of a checkpoint rewind, log the original txg
5409 * of the checkpointed uberblock.
5410 */
5411 if (checkpoint_rewind) {
5412 spa_history_log_internal(spa, "checkpoint rewind",
5413 NULL, "rewound state to txg=%llu",
5414 (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg);
5415 }
5416
687e4d7f 5417 spa_import_progress_set_notes(spa, "Claiming ZIL blocks");
34dc7c2f 5418 /*
9eb7b46e 5419 * Traverse the ZIL and claim all blocks.
34dc7c2f 5420 */
9eb7b46e 5421 spa_ld_claim_log_blocks(spa);
428870ff 5422
9eb7b46e
PZ
5423 /*
5424 * Kick-off the syncing thread.
5425 */
34dc7c2f
BB
5426 spa->spa_sync_on = B_TRUE;
5427 txg_sync_start(spa->spa_dsl_pool);
379ca9cf 5428 mmp_thread_start(spa);
34dc7c2f
BB
5429
5430 /*
428870ff
BB
5431 * Wait for all claims to sync. We sync up to the highest
5432 * claimed log block birth time so that claimed log blocks
5433 * don't appear to be from the future. spa_claim_max_txg
9eb7b46e
PZ
5434 * will have been set for us by ZIL traversal operations
5435 * performed above.
34dc7c2f 5436 */
687e4d7f 5437 spa_import_progress_set_notes(spa, "Syncing ZIL claims");
428870ff 5438 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
34dc7c2f
BB
5439
5440 /*
9eb7b46e
PZ
5441 * Check if we need to request an update of the config. On the
5442 * next sync, we would update the config stored in vdev labels
5443 * and the cachefile (by default /etc/zfs/zpool.cache).
34dc7c2f 5444 */
687e4d7f 5445 spa_import_progress_set_notes(spa, "Updating configs");
6cb8e530 5446 spa_ld_check_for_config_update(spa, config_cache_txg,
d2734cce 5447 update_config_cache);
fb5f0bc8
BB
5448
5449 /*
9a49d3f3
BB
5450 * Check if a rebuild was in progress and if so resume it.
5451 * Then check all DTLs to see if anything needs resilvering.
5452 * The resilver will be deferred if a rebuild was started.
fb5f0bc8 5453 */
687e4d7f 5454 spa_import_progress_set_notes(spa, "Starting resilvers");
9a49d3f3
BB
5455 if (vdev_rebuild_active(spa->spa_root_vdev)) {
5456 vdev_rebuild_restart(spa);
5457 } else if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
5458 vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
fb5f0bc8 5459 spa_async_request(spa, SPA_ASYNC_RESILVER);
9a49d3f3 5460 }
428870ff 5461
6f1ffb06
MA
5462 /*
5463 * Log the fact that we booted up (so that we can detect if
5464 * we rebooted in the middle of an operation).
5465 */
d5e024cb 5466 spa_history_log_version(spa, "open", NULL);
6f1ffb06 5467
687e4d7f
DB
5468 spa_import_progress_set_notes(spa,
5469 "Restarting device removals");
9b2266e3
SD
5470 spa_restart_removal(spa);
5471 spa_spawn_aux_threads(spa);
5472
428870ff
BB
5473 /*
5474 * Delete any inconsistent datasets.
9b2266e3
SD
5475 *
5476 * Note:
5477 * Since we may be issuing deletes for clones here,
5478 * we make sure to do so after we've spawned all the
5479 * auxiliary threads above (from which the livelist
5480 * deletion zthr is part of).
428870ff 5481 */
687e4d7f
DB
5482 spa_import_progress_set_notes(spa,
5483 "Cleaning up inconsistent objsets");
428870ff
BB
5484 (void) dmu_objset_find(spa_name(spa),
5485 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
5486
5487 /*
5488 * Clean up any stale temporary dataset userrefs.
5489 */
687e4d7f
DB
5490 spa_import_progress_set_notes(spa,
5491 "Cleaning up temporary userrefs");
428870ff 5492 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
a1d477c2 5493
619f0976 5494 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
687e4d7f 5495 spa_import_progress_set_notes(spa, "Restarting initialize");
619f0976 5496 vdev_initialize_restart(spa->spa_root_vdev);
687e4d7f 5497 spa_import_progress_set_notes(spa, "Restarting TRIM");
1b939560
BB
5498 vdev_trim_restart(spa->spa_root_vdev);
5499 vdev_autotrim_restart(spa);
619f0976 5500 spa_config_exit(spa, SCL_CONFIG, FTAG);
687e4d7f 5501 spa_import_progress_set_notes(spa, "Finished importing");
34dc7c2f
BB
5502 }
5503
ca95f70d 5504 spa_import_progress_remove(spa_guid(spa));
77f6826b
GA
5505 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
5506
4a0ee12a
PZ
5507 spa_load_note(spa, "LOADED");
5508
428870ff
BB
5509 return (0);
5510}
34dc7c2f 5511
428870ff 5512static int
6cb8e530 5513spa_load_retry(spa_t *spa, spa_load_state_t state)
428870ff 5514{
da92d5cb 5515 spa_mode_t mode = spa->spa_mode;
572e2857 5516
428870ff
BB
5517 spa_unload(spa);
5518 spa_deactivate(spa);
5519
dea377c0 5520 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
428870ff 5521
572e2857 5522 spa_activate(spa, mode);
428870ff
BB
5523 spa_async_suspend(spa);
5524
4a0ee12a
PZ
5525 spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu",
5526 (u_longlong_t)spa->spa_load_max_txg);
5527
6cb8e530 5528 return (spa_load(spa, state, SPA_IMPORT_EXISTING));
428870ff
BB
5529}
5530
9ae529ec
CS
5531/*
5532 * If spa_load() fails this function will try loading prior txg's. If
5533 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5534 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5535 * function will not rewind the pool and will return the same error as
5536 * spa_load().
5537 */
428870ff 5538static int
6cb8e530
PZ
5539spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request,
5540 int rewind_flags)
428870ff 5541{
9ae529ec 5542 nvlist_t *loadinfo = NULL;
428870ff
BB
5543 nvlist_t *config = NULL;
5544 int load_error, rewind_error;
5545 uint64_t safe_rewind_txg;
5546 uint64_t min_txg;
5547
5548 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
5549 spa->spa_load_max_txg = spa->spa_load_txg;
5550 spa_set_log_state(spa, SPA_LOG_CLEAR);
5551 } else {
5552 spa->spa_load_max_txg = max_request;
dea377c0
MA
5553 if (max_request != UINT64_MAX)
5554 spa->spa_extreme_rewind = B_TRUE;
428870ff
BB
5555 }
5556
6cb8e530 5557 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING);
428870ff
BB
5558 if (load_error == 0)
5559 return (0);
d2734cce
SD
5560 if (load_error == ZFS_ERR_NO_CHECKPOINT) {
5561 /*
5562 * When attempting checkpoint-rewind on a pool with no
5563 * checkpoint, we should not attempt to load uberblocks
5564 * from previous txgs when spa_load fails.
5565 */
5566 ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT);
ca95f70d 5567 spa_import_progress_remove(spa_guid(spa));
d2734cce
SD
5568 return (load_error);
5569 }
428870ff
BB
5570
5571 if (spa->spa_root_vdev != NULL)
5572 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
5573
5574 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
5575 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
5576
5577 if (rewind_flags & ZPOOL_NEVER_REWIND) {
5578 nvlist_free(config);
ca95f70d 5579 spa_import_progress_remove(spa_guid(spa));
428870ff
BB
5580 return (load_error);
5581 }
5582
9ae529ec
CS
5583 if (state == SPA_LOAD_RECOVER) {
5584 /* Price of rolling back is discarding txgs, including log */
428870ff 5585 spa_set_log_state(spa, SPA_LOG_CLEAR);
9ae529ec
CS
5586 } else {
5587 /*
5588 * If we aren't rolling back save the load info from our first
5589 * import attempt so that we can restore it after attempting
5590 * to rewind.
5591 */
5592 loadinfo = spa->spa_load_info;
5593 spa->spa_load_info = fnvlist_alloc();
5594 }
428870ff
BB
5595
5596 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
5597 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
5598 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
5599 TXG_INITIAL : safe_rewind_txg;
5600
5601 /*
5602 * Continue as long as we're finding errors, we're still within
5603 * the acceptable rewind range, and we're still finding uberblocks
5604 */
5605 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
5606 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
5607 if (spa->spa_load_max_txg < safe_rewind_txg)
5608 spa->spa_extreme_rewind = B_TRUE;
6cb8e530 5609 rewind_error = spa_load_retry(spa, state);
428870ff
BB
5610 }
5611
428870ff
BB
5612 spa->spa_extreme_rewind = B_FALSE;
5613 spa->spa_load_max_txg = UINT64_MAX;
5614
5615 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
5616 spa_config_set(spa, config);
ee6370a7 5617 else
5618 nvlist_free(config);
428870ff 5619
9ae529ec
CS
5620 if (state == SPA_LOAD_RECOVER) {
5621 ASSERT3P(loadinfo, ==, NULL);
ca95f70d 5622 spa_import_progress_remove(spa_guid(spa));
9ae529ec
CS
5623 return (rewind_error);
5624 } else {
5625 /* Store the rewind info as part of the initial load info */
5626 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
5627 spa->spa_load_info);
5628
5629 /* Restore the initial load info */
5630 fnvlist_free(spa->spa_load_info);
5631 spa->spa_load_info = loadinfo;
5632
ca95f70d 5633 spa_import_progress_remove(spa_guid(spa));
9ae529ec
CS
5634 return (load_error);
5635 }
34dc7c2f
BB
5636}
5637
5638/*
5639 * Pool Open/Import
5640 *
5641 * The import case is identical to an open except that the configuration is sent
5642 * down from userland, instead of grabbed from the configuration cache. For the
5643 * case of an open, the pool configuration will exist in the
5644 * POOL_STATE_UNINITIALIZED state.
5645 *
5646 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5647 * the same time open the pool, without having to keep around the spa_t in some
5648 * ambiguous state.
5649 */
5650static int
a926aab9
AZ
5651spa_open_common(const char *pool, spa_t **spapp, const void *tag,
5652 nvlist_t *nvpolicy, nvlist_t **config)
34dc7c2f
BB
5653{
5654 spa_t *spa;
572e2857 5655 spa_load_state_t state = SPA_LOAD_OPEN;
34dc7c2f 5656 int error;
34dc7c2f 5657 int locked = B_FALSE;
526af785 5658 int firstopen = B_FALSE;
34dc7c2f
BB
5659
5660 *spapp = NULL;
5661
5662 /*
5663 * As disgusting as this is, we need to support recursive calls to this
5664 * function because dsl_dir_open() is called during spa_load(), and ends
5665 * up calling spa_open() again. The real fix is to figure out how to
5666 * avoid dsl_dir_open() calling this in the first place.
5667 */
c25b8f99 5668 if (MUTEX_NOT_HELD(&spa_namespace_lock)) {
34dc7c2f
BB
5669 mutex_enter(&spa_namespace_lock);
5670 locked = B_TRUE;
5671 }
5672
5673 if ((spa = spa_lookup(pool)) == NULL) {
5674 if (locked)
5675 mutex_exit(&spa_namespace_lock);
2e528b49 5676 return (SET_ERROR(ENOENT));
34dc7c2f 5677 }
428870ff 5678
34dc7c2f 5679 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
8a393be3 5680 zpool_load_policy_t policy;
428870ff 5681
526af785
PJD
5682 firstopen = B_TRUE;
5683
8a393be3 5684 zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config,
428870ff 5685 &policy);
8a393be3 5686 if (policy.zlp_rewind & ZPOOL_DO_REWIND)
428870ff 5687 state = SPA_LOAD_RECOVER;
34dc7c2f 5688
fb5f0bc8 5689 spa_activate(spa, spa_mode_global);
34dc7c2f 5690
428870ff
BB
5691 if (state != SPA_LOAD_RECOVER)
5692 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
6cb8e530 5693 spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
428870ff 5694
4a0ee12a 5695 zfs_dbgmsg("spa_open_common: opening %s", pool);
8a393be3
PZ
5696 error = spa_load_best(spa, state, policy.zlp_txg,
5697 policy.zlp_rewind);
34dc7c2f
BB
5698
5699 if (error == EBADF) {
5700 /*
5701 * If vdev_validate() returns failure (indicated by
5702 * EBADF), it indicates that one of the vdevs indicates
5703 * that the pool has been exported or destroyed. If
5704 * this is the case, the config cache is out of sync and
5705 * we should remove the pool from the namespace.
5706 */
34dc7c2f
BB
5707 spa_unload(spa);
5708 spa_deactivate(spa);
55c12724 5709 spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE);
34dc7c2f 5710 spa_remove(spa);
34dc7c2f
BB
5711 if (locked)
5712 mutex_exit(&spa_namespace_lock);
2e528b49 5713 return (SET_ERROR(ENOENT));
34dc7c2f
BB
5714 }
5715
5716 if (error) {
5717 /*
5718 * We can't open the pool, but we still have useful
5719 * information: the state of each vdev after the
5720 * attempted vdev_open(). Return this to the user.
5721 */
572e2857 5722 if (config != NULL && spa->spa_config) {
65ad5d11
AJ
5723 *config = fnvlist_dup(spa->spa_config);
5724 fnvlist_add_nvlist(*config,
572e2857 5725 ZPOOL_CONFIG_LOAD_INFO,
65ad5d11 5726 spa->spa_load_info);
572e2857 5727 }
34dc7c2f
BB
5728 spa_unload(spa);
5729 spa_deactivate(spa);
428870ff 5730 spa->spa_last_open_failed = error;
34dc7c2f
BB
5731 if (locked)
5732 mutex_exit(&spa_namespace_lock);
5733 *spapp = NULL;
5734 return (error);
34dc7c2f 5735 }
34dc7c2f
BB
5736 }
5737
5738 spa_open_ref(spa, tag);
5739
b128c09f 5740 if (config != NULL)
34dc7c2f 5741 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
34dc7c2f 5742
572e2857
BB
5743 /*
5744 * If we've recovered the pool, pass back any information we
5745 * gathered while doing the load.
5746 */
1bd02680 5747 if (state == SPA_LOAD_RECOVER && config != NULL) {
65ad5d11
AJ
5748 fnvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
5749 spa->spa_load_info);
572e2857
BB
5750 }
5751
428870ff
BB
5752 if (locked) {
5753 spa->spa_last_open_failed = 0;
5754 spa->spa_last_ubsync_txg = 0;
5755 spa->spa_load_txg = 0;
5756 mutex_exit(&spa_namespace_lock);
5757 }
5758
526af785 5759 if (firstopen)
ec213971 5760 zvol_create_minors_recursive(spa_name(spa));
526af785 5761
428870ff
BB
5762 *spapp = spa;
5763
34dc7c2f
BB
5764 return (0);
5765}
5766
428870ff 5767int
a926aab9
AZ
5768spa_open_rewind(const char *name, spa_t **spapp, const void *tag,
5769 nvlist_t *policy, nvlist_t **config)
428870ff
BB
5770{
5771 return (spa_open_common(name, spapp, tag, policy, config));
5772}
5773
34dc7c2f 5774int
a926aab9 5775spa_open(const char *name, spa_t **spapp, const void *tag)
34dc7c2f 5776{
428870ff 5777 return (spa_open_common(name, spapp, tag, NULL, NULL));
34dc7c2f
BB
5778}
5779
5780/*
5781 * Lookup the given spa_t, incrementing the inject count in the process,
5782 * preventing it from being exported or destroyed.
5783 */
5784spa_t *
5785spa_inject_addref(char *name)
5786{
5787 spa_t *spa;
5788
5789 mutex_enter(&spa_namespace_lock);
5790 if ((spa = spa_lookup(name)) == NULL) {
5791 mutex_exit(&spa_namespace_lock);
5792 return (NULL);
5793 }
5794 spa->spa_inject_ref++;
5795 mutex_exit(&spa_namespace_lock);
5796
5797 return (spa);
5798}
5799
5800void
5801spa_inject_delref(spa_t *spa)
5802{
5803 mutex_enter(&spa_namespace_lock);
5804 spa->spa_inject_ref--;
5805 mutex_exit(&spa_namespace_lock);
5806}
5807
5808/*
5809 * Add spares device information to the nvlist.
5810 */
5811static void
5812spa_add_spares(spa_t *spa, nvlist_t *config)
5813{
5814 nvlist_t **spares;
5815 uint_t i, nspares;
5816 nvlist_t *nvroot;
5817 uint64_t guid;
5818 vdev_stat_t *vs;
5819 uint_t vsc;
5820 uint64_t pool;
5821
9babb374
BB
5822 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
5823
34dc7c2f
BB
5824 if (spa->spa_spares.sav_count == 0)
5825 return;
5826
65ad5d11
AJ
5827 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
5828 VERIFY0(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5829 ZPOOL_CONFIG_SPARES, &spares, &nspares));
34dc7c2f 5830 if (nspares != 0) {
795075e6
PD
5831 fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5832 (const nvlist_t * const *)spares, nspares);
65ad5d11
AJ
5833 VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
5834 &spares, &nspares));
34dc7c2f
BB
5835
5836 /*
5837 * Go through and find any spares which have since been
5838 * repurposed as an active spare. If this is the case, update
5839 * their status appropriately.
5840 */
5841 for (i = 0; i < nspares; i++) {
65ad5d11
AJ
5842 guid = fnvlist_lookup_uint64(spares[i],
5843 ZPOOL_CONFIG_GUID);
a05263b7
AH
5844 VERIFY0(nvlist_lookup_uint64_array(spares[i],
5845 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc));
b128c09f
BB
5846 if (spa_spare_exists(guid, &pool, NULL) &&
5847 pool != 0ULL) {
34dc7c2f
BB
5848 vs->vs_state = VDEV_STATE_CANT_OPEN;
5849 vs->vs_aux = VDEV_AUX_SPARED;
a05263b7
AH
5850 } else {
5851 vs->vs_state =
5852 spa->spa_spares.sav_vdevs[i]->vdev_state;
34dc7c2f
BB
5853 }
5854 }
5855 }
5856}
5857
5858/*
5859 * Add l2cache device information to the nvlist, including vdev stats.
5860 */
5861static void
5862spa_add_l2cache(spa_t *spa, nvlist_t *config)
5863{
5864 nvlist_t **l2cache;
5865 uint_t i, j, nl2cache;
5866 nvlist_t *nvroot;
5867 uint64_t guid;
5868 vdev_t *vd;
5869 vdev_stat_t *vs;
5870 uint_t vsc;
5871
9babb374
BB
5872 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
5873
34dc7c2f
BB
5874 if (spa->spa_l2cache.sav_count == 0)
5875 return;
5876
65ad5d11
AJ
5877 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
5878 VERIFY0(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5879 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache));
34dc7c2f 5880 if (nl2cache != 0) {
795075e6
PD
5881 fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5882 (const nvlist_t * const *)l2cache, nl2cache);
65ad5d11
AJ
5883 VERIFY0(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
5884 &l2cache, &nl2cache));
34dc7c2f
BB
5885
5886 /*
5887 * Update level 2 cache device stats.
5888 */
5889
5890 for (i = 0; i < nl2cache; i++) {
65ad5d11
AJ
5891 guid = fnvlist_lookup_uint64(l2cache[i],
5892 ZPOOL_CONFIG_GUID);
34dc7c2f
BB
5893
5894 vd = NULL;
5895 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
5896 if (guid ==
5897 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
5898 vd = spa->spa_l2cache.sav_vdevs[j];
5899 break;
5900 }
5901 }
5902 ASSERT(vd != NULL);
5903
65ad5d11
AJ
5904 VERIFY0(nvlist_lookup_uint64_array(l2cache[i],
5905 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc));
34dc7c2f 5906 vdev_get_stats(vd, vs);
193a37cb
TH
5907 vdev_config_generate_stats(vd, l2cache[i]);
5908
34dc7c2f
BB
5909 }
5910 }
34dc7c2f
BB
5911}
5912
9ae529ec 5913static void
417104bd 5914spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features)
9ae529ec 5915{
9ae529ec
CS
5916 zap_cursor_t zc;
5917 zap_attribute_t za;
5918
9ae529ec
CS
5919 if (spa->spa_feat_for_read_obj != 0) {
5920 for (zap_cursor_init(&zc, spa->spa_meta_objset,
5921 spa->spa_feat_for_read_obj);
5922 zap_cursor_retrieve(&zc, &za) == 0;
5923 zap_cursor_advance(&zc)) {
5924 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
5925 za.za_num_integers == 1);
417104bd 5926 VERIFY0(nvlist_add_uint64(features, za.za_name,
9ae529ec
CS
5927 za.za_first_integer));
5928 }
5929 zap_cursor_fini(&zc);
5930 }
5931
5932 if (spa->spa_feat_for_write_obj != 0) {
5933 for (zap_cursor_init(&zc, spa->spa_meta_objset,
5934 spa->spa_feat_for_write_obj);
5935 zap_cursor_retrieve(&zc, &za) == 0;
5936 zap_cursor_advance(&zc)) {
5937 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
5938 za.za_num_integers == 1);
417104bd 5939 VERIFY0(nvlist_add_uint64(features, za.za_name,
9ae529ec
CS
5940 za.za_first_integer));
5941 }
5942 zap_cursor_fini(&zc);
5943 }
417104bd
NB
5944}
5945
5946static void
5947spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features)
5948{
5949 int i;
5950
5951 for (i = 0; i < SPA_FEATURES; i++) {
5952 zfeature_info_t feature = spa_feature_table[i];
5953 uint64_t refcount;
5954
5955 if (feature_get_refcount(spa, &feature, &refcount) != 0)
5956 continue;
5957
5958 VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount));
5959 }
5960}
5961
5962/*
5963 * Store a list of pool features and their reference counts in the
5964 * config.
5965 *
5966 * The first time this is called on a spa, allocate a new nvlist, fetch
5967 * the pool features and reference counts from disk, then save the list
5968 * in the spa. In subsequent calls on the same spa use the saved nvlist
5969 * and refresh its values from the cached reference counts. This
5970 * ensures we don't block here on I/O on a suspended pool so 'zpool
5971 * clear' can resume the pool.
5972 */
5973static void
5974spa_add_feature_stats(spa_t *spa, nvlist_t *config)
5975{
4eb30c68 5976 nvlist_t *features;
417104bd
NB
5977
5978 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
5979
4eb30c68
NB
5980 mutex_enter(&spa->spa_feat_stats_lock);
5981 features = spa->spa_feat_stats;
5982
417104bd
NB
5983 if (features != NULL) {
5984 spa_feature_stats_from_cache(spa, features);
5985 } else {
5986 VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP));
5987 spa->spa_feat_stats = features;
5988 spa_feature_stats_from_disk(spa, features);
5989 }
9ae529ec 5990
417104bd
NB
5991 VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
5992 features));
4eb30c68
NB
5993
5994 mutex_exit(&spa->spa_feat_stats_lock);
9ae529ec
CS
5995}
5996
34dc7c2f 5997int
9ae529ec
CS
5998spa_get_stats(const char *name, nvlist_t **config,
5999 char *altroot, size_t buflen)
34dc7c2f
BB
6000{
6001 int error;
6002 spa_t *spa;
6003
6004 *config = NULL;
428870ff 6005 error = spa_open_common(name, &spa, FTAG, NULL, config);
34dc7c2f 6006
9babb374
BB
6007 if (spa != NULL) {
6008 /*
6009 * This still leaves a window of inconsistency where the spares
6010 * or l2cache devices could change and the config would be
6011 * self-inconsistent.
6012 */
6013 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f 6014
9babb374 6015 if (*config != NULL) {
572e2857
BB
6016 uint64_t loadtimes[2];
6017
6018 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
6019 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
65ad5d11
AJ
6020 fnvlist_add_uint64_array(*config,
6021 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2);
572e2857 6022
65ad5d11 6023 fnvlist_add_uint64(*config,
9babb374 6024 ZPOOL_CONFIG_ERRCOUNT,
018f2604 6025 spa_approx_errlog_size(spa));
9babb374 6026
cec3a0a1 6027 if (spa_suspended(spa)) {
65ad5d11 6028 fnvlist_add_uint64(*config,
9babb374 6029 ZPOOL_CONFIG_SUSPENDED,
65ad5d11
AJ
6030 spa->spa_failmode);
6031 fnvlist_add_uint64(*config,
cec3a0a1 6032 ZPOOL_CONFIG_SUSPENDED_REASON,
65ad5d11 6033 spa->spa_suspended);
cec3a0a1 6034 }
b128c09f 6035
9babb374
BB
6036 spa_add_spares(spa, *config);
6037 spa_add_l2cache(spa, *config);
9ae529ec 6038 spa_add_feature_stats(spa, *config);
9babb374 6039 }
34dc7c2f
BB
6040 }
6041
6042 /*
6043 * We want to get the alternate root even for faulted pools, so we cheat
6044 * and call spa_lookup() directly.
6045 */
6046 if (altroot) {
6047 if (spa == NULL) {
6048 mutex_enter(&spa_namespace_lock);
6049 spa = spa_lookup(name);
6050 if (spa)
6051 spa_altroot(spa, altroot, buflen);
6052 else
6053 altroot[0] = '\0';
6054 spa = NULL;
6055 mutex_exit(&spa_namespace_lock);
6056 } else {
6057 spa_altroot(spa, altroot, buflen);
6058 }
6059 }
6060
9babb374
BB
6061 if (spa != NULL) {
6062 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 6063 spa_close(spa, FTAG);
9babb374 6064 }
34dc7c2f
BB
6065
6066 return (error);
6067}
6068
6069/*
6070 * Validate that the auxiliary device array is well formed. We must have an
6071 * array of nvlists, each which describes a valid leaf vdev. If this is an
6072 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
6073 * specified, as long as they are well-formed.
6074 */
6075static int
6076spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
6077 spa_aux_vdev_t *sav, const char *config, uint64_t version,
6078 vdev_labeltype_t label)
6079{
6080 nvlist_t **dev;
6081 uint_t i, ndev;
6082 vdev_t *vd;
6083 int error;
6084
b128c09f
BB
6085 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6086
34dc7c2f
BB
6087 /*
6088 * It's acceptable to have no devs specified.
6089 */
6090 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
6091 return (0);
6092
6093 if (ndev == 0)
2e528b49 6094 return (SET_ERROR(EINVAL));
34dc7c2f
BB
6095
6096 /*
6097 * Make sure the pool is formatted with a version that supports this
6098 * device type.
6099 */
6100 if (spa_version(spa) < version)
2e528b49 6101 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
6102
6103 /*
6104 * Set the pending device list so we correctly handle device in-use
6105 * checking.
6106 */
6107 sav->sav_pending = dev;
6108 sav->sav_npending = ndev;
6109
6110 for (i = 0; i < ndev; i++) {
6111 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
6112 mode)) != 0)
6113 goto out;
6114
6115 if (!vd->vdev_ops->vdev_op_leaf) {
6116 vdev_free(vd);
2e528b49 6117 error = SET_ERROR(EINVAL);
34dc7c2f
BB
6118 goto out;
6119 }
6120
34dc7c2f
BB
6121 vd->vdev_top = vd;
6122
6123 if ((error = vdev_open(vd)) == 0 &&
6124 (error = vdev_label_init(vd, crtxg, label)) == 0) {
65ad5d11
AJ
6125 fnvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
6126 vd->vdev_guid);
34dc7c2f
BB
6127 }
6128
6129 vdev_free(vd);
6130
6131 if (error &&
6132 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
6133 goto out;
6134 else
6135 error = 0;
6136 }
6137
6138out:
6139 sav->sav_pending = NULL;
6140 sav->sav_npending = 0;
6141 return (error);
6142}
6143
6144static int
6145spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
6146{
6147 int error;
6148
b128c09f
BB
6149 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
6150
34dc7c2f
BB
6151 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
6152 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
6153 VDEV_LABEL_SPARE)) != 0) {
6154 return (error);
6155 }
6156
6157 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
6158 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
6159 VDEV_LABEL_L2CACHE));
6160}
6161
6162static void
6163spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
6164 const char *config)
6165{
6166 int i;
6167
6168 if (sav->sav_config != NULL) {
6169 nvlist_t **olddevs;
6170 uint_t oldndevs;
6171 nvlist_t **newdevs;
6172
6173 /*
4e33ba4c 6174 * Generate new dev list by concatenating with the
34dc7c2f
BB
6175 * current dev list.
6176 */
65ad5d11
AJ
6177 VERIFY0(nvlist_lookup_nvlist_array(sav->sav_config, config,
6178 &olddevs, &oldndevs));
34dc7c2f
BB
6179
6180 newdevs = kmem_alloc(sizeof (void *) *
79c76d5b 6181 (ndevs + oldndevs), KM_SLEEP);
34dc7c2f 6182 for (i = 0; i < oldndevs; i++)
65ad5d11 6183 newdevs[i] = fnvlist_dup(olddevs[i]);
34dc7c2f 6184 for (i = 0; i < ndevs; i++)
65ad5d11 6185 newdevs[i + oldndevs] = fnvlist_dup(devs[i]);
34dc7c2f 6186
65ad5d11 6187 fnvlist_remove(sav->sav_config, config);
34dc7c2f 6188
795075e6
PD
6189 fnvlist_add_nvlist_array(sav->sav_config, config,
6190 (const nvlist_t * const *)newdevs, ndevs + oldndevs);
34dc7c2f
BB
6191 for (i = 0; i < oldndevs + ndevs; i++)
6192 nvlist_free(newdevs[i]);
6193 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
6194 } else {
6195 /*
6196 * Generate a new dev list.
6197 */
65ad5d11 6198 sav->sav_config = fnvlist_alloc();
795075e6
PD
6199 fnvlist_add_nvlist_array(sav->sav_config, config,
6200 (const nvlist_t * const *)devs, ndevs);
34dc7c2f
BB
6201 }
6202}
6203
6204/*
6205 * Stop and drop level 2 ARC devices
6206 */
6207void
6208spa_l2cache_drop(spa_t *spa)
6209{
6210 vdev_t *vd;
6211 int i;
6212 spa_aux_vdev_t *sav = &spa->spa_l2cache;
6213
6214 for (i = 0; i < sav->sav_count; i++) {
6215 uint64_t pool;
6216
6217 vd = sav->sav_vdevs[i];
6218 ASSERT(vd != NULL);
6219
fb5f0bc8
BB
6220 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
6221 pool != 0ULL && l2arc_vdev_present(vd))
34dc7c2f 6222 l2arc_remove_vdev(vd);
34dc7c2f
BB
6223 }
6224}
6225
b5256303
TC
6226/*
6227 * Verify encryption parameters for spa creation. If we are encrypting, we must
6228 * have the encryption feature flag enabled.
6229 */
6230static int
6231spa_create_check_encryption_params(dsl_crypto_params_t *dcp,
6232 boolean_t has_encryption)
6233{
6234 if (dcp->cp_crypt != ZIO_CRYPT_OFF &&
6235 dcp->cp_crypt != ZIO_CRYPT_INHERIT &&
6236 !has_encryption)
6237 return (SET_ERROR(ENOTSUP));
6238
1fff937a 6239 return (dmu_objset_create_crypt_check(NULL, dcp, NULL));
b5256303
TC
6240}
6241
34dc7c2f
BB
6242/*
6243 * Pool Creation
6244 */
6245int
6246spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
b5256303 6247 nvlist_t *zplprops, dsl_crypto_params_t *dcp)
34dc7c2f
BB
6248{
6249 spa_t *spa;
d1807f16 6250 const char *altroot = NULL;
34dc7c2f
BB
6251 vdev_t *rvd;
6252 dsl_pool_t *dp;
6253 dmu_tx_t *tx;
9babb374 6254 int error = 0;
34dc7c2f
BB
6255 uint64_t txg = TXG_INITIAL;
6256 nvlist_t **spares, **l2cache;
6257 uint_t nspares, nl2cache;
b2255edc 6258 uint64_t version, obj, ndraid = 0;
9ae529ec 6259 boolean_t has_features;
b5256303 6260 boolean_t has_encryption;
715c996d 6261 boolean_t has_allocclass;
b5256303 6262 spa_feature_t feat;
d1807f16
RY
6263 const char *feat_name;
6264 const char *poolname;
83e9986f
RY
6265 nvlist_t *nvl;
6266
cc99f275
DB
6267 if (props == NULL ||
6268 nvlist_lookup_string(props, "tname", &poolname) != 0)
83e9986f 6269 poolname = (char *)pool;
34dc7c2f
BB
6270
6271 /*
6272 * If this pool already exists, return failure.
6273 */
6274 mutex_enter(&spa_namespace_lock);
83e9986f 6275 if (spa_lookup(poolname) != NULL) {
34dc7c2f 6276 mutex_exit(&spa_namespace_lock);
2e528b49 6277 return (SET_ERROR(EEXIST));
34dc7c2f
BB
6278 }
6279
6280 /*
6281 * Allocate a new spa_t structure.
6282 */
83e9986f
RY
6283 nvl = fnvlist_alloc();
6284 fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool);
34dc7c2f
BB
6285 (void) nvlist_lookup_string(props,
6286 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
83e9986f
RY
6287 spa = spa_add(poolname, nvl, altroot);
6288 fnvlist_free(nvl);
fb5f0bc8 6289 spa_activate(spa, spa_mode_global);
34dc7c2f 6290
34dc7c2f 6291 if (props && (error = spa_prop_validate(spa, props))) {
34dc7c2f
BB
6292 spa_deactivate(spa);
6293 spa_remove(spa);
b128c09f 6294 mutex_exit(&spa_namespace_lock);
34dc7c2f
BB
6295 return (error);
6296 }
6297
83e9986f
RY
6298 /*
6299 * Temporary pool names should never be written to disk.
6300 */
6301 if (poolname != pool)
6302 spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME;
6303
9ae529ec 6304 has_features = B_FALSE;
b5256303 6305 has_encryption = B_FALSE;
715c996d 6306 has_allocclass = B_FALSE;
1c27024e 6307 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
9ae529ec 6308 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
b5256303 6309 if (zpool_prop_feature(nvpair_name(elem))) {
9ae529ec 6310 has_features = B_TRUE;
b5256303
TC
6311
6312 feat_name = strchr(nvpair_name(elem), '@') + 1;
6313 VERIFY0(zfeature_lookup_name(feat_name, &feat));
6314 if (feat == SPA_FEATURE_ENCRYPTION)
6315 has_encryption = B_TRUE;
715c996d 6316 if (feat == SPA_FEATURE_ALLOCATION_CLASSES)
6317 has_allocclass = B_TRUE;
b5256303
TC
6318 }
6319 }
6320
6321 /* verify encryption params, if they were provided */
6322 if (dcp != NULL) {
6323 error = spa_create_check_encryption_params(dcp, has_encryption);
6324 if (error != 0) {
6325 spa_deactivate(spa);
6326 spa_remove(spa);
6327 mutex_exit(&spa_namespace_lock);
6328 return (error);
6329 }
9ae529ec 6330 }
c24fa4b1 6331 if (!has_allocclass && zfs_special_devs(nvroot, NULL)) {
715c996d 6332 spa_deactivate(spa);
6333 spa_remove(spa);
6334 mutex_exit(&spa_namespace_lock);
6335 return (ENOTSUP);
6336 }
9ae529ec
CS
6337
6338 if (has_features || nvlist_lookup_uint64(props,
6339 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
34dc7c2f 6340 version = SPA_VERSION;
9ae529ec
CS
6341 }
6342 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
428870ff
BB
6343
6344 spa->spa_first_txg = txg;
6345 spa->spa_uberblock.ub_txg = txg - 1;
34dc7c2f
BB
6346 spa->spa_uberblock.ub_version = version;
6347 spa->spa_ubsync = spa->spa_uberblock;
3dfb57a3 6348 spa->spa_load_state = SPA_LOAD_CREATE;
a1d477c2
MA
6349 spa->spa_removing_phys.sr_state = DSS_NONE;
6350 spa->spa_removing_phys.sr_removing_vdev = -1;
6351 spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
944a3724 6352 spa->spa_indirect_vdevs_loaded = B_TRUE;
34dc7c2f 6353
9babb374
BB
6354 /*
6355 * Create "The Godfather" zio to hold all async IOs
6356 */
e022864d
MA
6357 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
6358 KM_SLEEP);
1c27024e 6359 for (int i = 0; i < max_ncpus; i++) {
e022864d
MA
6360 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
6361 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
6362 ZIO_FLAG_GODFATHER);
6363 }
9babb374 6364
34dc7c2f
BB
6365 /*
6366 * Create the root vdev.
6367 */
b128c09f 6368 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
6369
6370 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
6371
6372 ASSERT(error != 0 || rvd != NULL);
6373 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
6374
6375 if (error == 0 && !zfs_allocatable_devs(nvroot))
2e528b49 6376 error = SET_ERROR(EINVAL);
34dc7c2f
BB
6377
6378 if (error == 0 &&
6379 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
b2255edc
BB
6380 (error = vdev_draid_spare_create(nvroot, rvd, &ndraid, 0)) == 0 &&
6381 (error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) == 0) {
cc99f275
DB
6382 /*
6383 * instantiate the metaslab groups (this will dirty the vdevs)
6384 * we can no longer error exit past this point
6385 */
6386 for (int c = 0; error == 0 && c < rvd->vdev_children; c++) {
6387 vdev_t *vd = rvd->vdev_child[c];
6388
6389 vdev_metaslab_set_size(vd);
6390 vdev_expand(vd, txg);
9babb374 6391 }
34dc7c2f
BB
6392 }
6393
b128c09f 6394 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6395
6396 if (error != 0) {
6397 spa_unload(spa);
6398 spa_deactivate(spa);
6399 spa_remove(spa);
6400 mutex_exit(&spa_namespace_lock);
6401 return (error);
6402 }
6403
6404 /*
6405 * Get the list of spares, if specified.
6406 */
6407 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6408 &spares, &nspares) == 0) {
65ad5d11
AJ
6409 spa->spa_spares.sav_config = fnvlist_alloc();
6410 fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
795075e6
PD
6411 ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
6412 nspares);
b128c09f 6413 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6414 spa_load_spares(spa);
b128c09f 6415 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6416 spa->spa_spares.sav_sync = B_TRUE;
6417 }
6418
6419 /*
6420 * Get the list of level 2 cache devices, if specified.
6421 */
6422 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6423 &l2cache, &nl2cache) == 0) {
795075e6
PD
6424 VERIFY0(nvlist_alloc(&spa->spa_l2cache.sav_config,
6425 NV_UNIQUE_NAME, KM_SLEEP));
65ad5d11 6426 fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
795075e6
PD
6427 ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache,
6428 nl2cache);
b128c09f 6429 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6430 spa_load_l2cache(spa);
b128c09f 6431 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6432 spa->spa_l2cache.sav_sync = B_TRUE;
6433 }
6434
9ae529ec 6435 spa->spa_is_initializing = B_TRUE;
b5256303 6436 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg);
9ae529ec 6437 spa->spa_is_initializing = B_FALSE;
34dc7c2f 6438
428870ff
BB
6439 /*
6440 * Create DDTs (dedup tables).
6441 */
6442 ddt_create(spa);
67a1b037
PJD
6443 /*
6444 * Create BRT table and BRT table object.
6445 */
6446 brt_create(spa);
428870ff
BB
6447
6448 spa_update_dspace(spa);
6449
34dc7c2f
BB
6450 tx = dmu_tx_create_assigned(dp, txg);
6451
d5e024cb
BB
6452 /*
6453 * Create the pool's history object.
6454 */
6455 if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history)
6456 spa_history_create_obj(spa, tx);
6457
6458 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE);
6459 spa_history_log_version(spa, "create", tx);
6460
34dc7c2f
BB
6461 /*
6462 * Create the pool config object.
6463 */
6464 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
b128c09f 6465 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
34dc7c2f
BB
6466 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
6467
6468 if (zap_add(spa->spa_meta_objset,
6469 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
6470 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
6471 cmn_err(CE_PANIC, "failed to add pool config");
6472 }
6473
428870ff
BB
6474 if (zap_add(spa->spa_meta_objset,
6475 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
6476 sizeof (uint64_t), 1, &version, tx) != 0) {
6477 cmn_err(CE_PANIC, "failed to add pool version");
6478 }
6479
34dc7c2f
BB
6480 /* Newly created pools with the right version are always deflated. */
6481 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
6482 spa->spa_deflate = TRUE;
6483 if (zap_add(spa->spa_meta_objset,
6484 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6485 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
6486 cmn_err(CE_PANIC, "failed to add deflate");
6487 }
6488 }
6489
6490 /*
428870ff 6491 * Create the deferred-free bpobj. Turn off compression
34dc7c2f
BB
6492 * because sync-to-convergence takes longer if the blocksize
6493 * keeps changing.
6494 */
428870ff
BB
6495 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
6496 dmu_object_set_compress(spa->spa_meta_objset, obj,
34dc7c2f 6497 ZIO_COMPRESS_OFF, tx);
34dc7c2f 6498 if (zap_add(spa->spa_meta_objset,
428870ff
BB
6499 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
6500 sizeof (uint64_t), 1, &obj, tx) != 0) {
6501 cmn_err(CE_PANIC, "failed to add bpobj");
34dc7c2f 6502 }
428870ff
BB
6503 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
6504 spa->spa_meta_objset, obj));
34dc7c2f 6505
3c67d83a
TH
6506 /*
6507 * Generate some random noise for salted checksums to operate on.
6508 */
6509 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
6510 sizeof (spa->spa_cksum_salt.zcs_bytes));
6511
34dc7c2f
BB
6512 /*
6513 * Set pool properties.
6514 */
6515 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
6516 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
6517 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
9babb374 6518 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
379ca9cf 6519 spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST);
1b939560 6520 spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM);
428870ff 6521
d164b209
BB
6522 if (props != NULL) {
6523 spa_configfile_set(spa, props, B_FALSE);
13fe0198 6524 spa_sync_props(props, tx);
d164b209 6525 }
34dc7c2f 6526
b2255edc
BB
6527 for (int i = 0; i < ndraid; i++)
6528 spa_feature_incr(spa, SPA_FEATURE_DRAID, tx);
6529
34dc7c2f
BB
6530 dmu_tx_commit(tx);
6531
6532 spa->spa_sync_on = B_TRUE;
b5256303 6533 txg_sync_start(dp);
379ca9cf 6534 mmp_thread_start(spa);
b5256303 6535 txg_wait_synced(dp, txg);
34dc7c2f 6536
9d5b5245
SD
6537 spa_spawn_aux_threads(spa);
6538
55c12724 6539 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
34dc7c2f 6540
0c66c32d
JG
6541 /*
6542 * Don't count references from objsets that are already closed
6543 * and are making their way through the eviction process.
6544 */
6545 spa_evicting_os_wait(spa);
424fd7c3 6546 spa->spa_minref = zfs_refcount_count(&spa->spa_refcount);
3dfb57a3 6547 spa->spa_load_state = SPA_LOAD_NONE;
b128c09f 6548
4759342a
JL
6549 spa_import_os(spa);
6550
d164b209
BB
6551 mutex_exit(&spa_namespace_lock);
6552
34dc7c2f
BB
6553 return (0);
6554}
6555
9babb374
BB
6556/*
6557 * Import a non-root pool into the system.
6558 */
6559int
13fe0198 6560spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
34dc7c2f
BB
6561{
6562 spa_t *spa;
d1807f16 6563 const char *altroot = NULL;
428870ff 6564 spa_load_state_t state = SPA_LOAD_IMPORT;
8a393be3 6565 zpool_load_policy_t policy;
da92d5cb 6566 spa_mode_t mode = spa_mode_global;
572e2857 6567 uint64_t readonly = B_FALSE;
9babb374 6568 int error;
34dc7c2f
BB
6569 nvlist_t *nvroot;
6570 nvlist_t **spares, **l2cache;
6571 uint_t nspares, nl2cache;
34dc7c2f
BB
6572
6573 /*
6574 * If a pool with this name exists, return failure.
6575 */
6576 mutex_enter(&spa_namespace_lock);
428870ff 6577 if (spa_lookup(pool) != NULL) {
9babb374 6578 mutex_exit(&spa_namespace_lock);
2e528b49 6579 return (SET_ERROR(EEXIST));
34dc7c2f
BB
6580 }
6581
6582 /*
6583 * Create and initialize the spa structure.
6584 */
6585 (void) nvlist_lookup_string(props,
6586 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
572e2857
BB
6587 (void) nvlist_lookup_uint64(props,
6588 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
6589 if (readonly)
da92d5cb 6590 mode = SPA_MODE_READ;
428870ff 6591 spa = spa_add(pool, config, altroot);
572e2857
BB
6592 spa->spa_import_flags = flags;
6593
6594 /*
6595 * Verbatim import - Take a pool and insert it into the namespace
6596 * as if it had been loaded at boot.
6597 */
6598 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
6599 if (props != NULL)
6600 spa_configfile_set(spa, props, B_FALSE);
6601
55c12724 6602 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
12fa0466 6603 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
4a0ee12a 6604 zfs_dbgmsg("spa_import: verbatim import of %s", pool);
572e2857 6605 mutex_exit(&spa_namespace_lock);
572e2857
BB
6606 return (0);
6607 }
6608
6609 spa_activate(spa, mode);
34dc7c2f 6610
9babb374
BB
6611 /*
6612 * Don't start async tasks until we know everything is healthy.
6613 */
6614 spa_async_suspend(spa);
b128c09f 6615
8a393be3
PZ
6616 zpool_get_load_policy(config, &policy);
6617 if (policy.zlp_rewind & ZPOOL_DO_REWIND)
572e2857
BB
6618 state = SPA_LOAD_RECOVER;
6619
6cb8e530 6620 spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT;
572e2857 6621
6cb8e530
PZ
6622 if (state != SPA_LOAD_RECOVER) {
6623 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
6624 zfs_dbgmsg("spa_import: importing %s", pool);
6625 } else {
6626 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
8a393be3 6627 "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg);
6cb8e530 6628 }
8a393be3 6629 error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind);
428870ff
BB
6630
6631 /*
572e2857
BB
6632 * Propagate anything learned while loading the pool and pass it
6633 * back to caller (i.e. rewind info, missing devices, etc).
428870ff 6634 */
65ad5d11 6635 fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, spa->spa_load_info);
34dc7c2f 6636
b128c09f 6637 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6638 /*
9babb374
BB
6639 * Toss any existing sparelist, as it doesn't have any validity
6640 * anymore, and conflicts with spa_has_spare().
34dc7c2f 6641 */
9babb374 6642 if (spa->spa_spares.sav_config) {
34dc7c2f
BB
6643 nvlist_free(spa->spa_spares.sav_config);
6644 spa->spa_spares.sav_config = NULL;
6645 spa_load_spares(spa);
6646 }
9babb374 6647 if (spa->spa_l2cache.sav_config) {
34dc7c2f
BB
6648 nvlist_free(spa->spa_l2cache.sav_config);
6649 spa->spa_l2cache.sav_config = NULL;
6650 spa_load_l2cache(spa);
6651 }
6652
65ad5d11 6653 nvroot = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
b128c09f 6654 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 6655
d164b209
BB
6656 if (props != NULL)
6657 spa_configfile_set(spa, props, B_FALSE);
6658
fb5f0bc8
BB
6659 if (error != 0 || (props && spa_writeable(spa) &&
6660 (error = spa_prop_set(spa, props)))) {
9babb374
BB
6661 spa_unload(spa);
6662 spa_deactivate(spa);
6663 spa_remove(spa);
34dc7c2f
BB
6664 mutex_exit(&spa_namespace_lock);
6665 return (error);
6666 }
6667
572e2857
BB
6668 spa_async_resume(spa);
6669
34dc7c2f
BB
6670 /*
6671 * Override any spares and level 2 cache devices as specified by
6672 * the user, as these may have correct device names/devids, etc.
6673 */
6674 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
6675 &spares, &nspares) == 0) {
6676 if (spa->spa_spares.sav_config)
65ad5d11
AJ
6677 fnvlist_remove(spa->spa_spares.sav_config,
6678 ZPOOL_CONFIG_SPARES);
34dc7c2f 6679 else
65ad5d11
AJ
6680 spa->spa_spares.sav_config = fnvlist_alloc();
6681 fnvlist_add_nvlist_array(spa->spa_spares.sav_config,
795075e6
PD
6682 ZPOOL_CONFIG_SPARES, (const nvlist_t * const *)spares,
6683 nspares);
b128c09f 6684 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6685 spa_load_spares(spa);
b128c09f 6686 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6687 spa->spa_spares.sav_sync = B_TRUE;
6688 }
6689 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
6690 &l2cache, &nl2cache) == 0) {
6691 if (spa->spa_l2cache.sav_config)
65ad5d11
AJ
6692 fnvlist_remove(spa->spa_l2cache.sav_config,
6693 ZPOOL_CONFIG_L2CACHE);
34dc7c2f 6694 else
65ad5d11
AJ
6695 spa->spa_l2cache.sav_config = fnvlist_alloc();
6696 fnvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
795075e6
PD
6697 ZPOOL_CONFIG_L2CACHE, (const nvlist_t * const *)l2cache,
6698 nl2cache);
b128c09f 6699 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6700 spa_load_l2cache(spa);
b128c09f 6701 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6702 spa->spa_l2cache.sav_sync = B_TRUE;
6703 }
6704
428870ff
BB
6705 /*
6706 * Check for any removed devices.
6707 */
6708 if (spa->spa_autoreplace) {
6709 spa_aux_check_removed(&spa->spa_spares);
6710 spa_aux_check_removed(&spa->spa_l2cache);
6711 }
6712
fb5f0bc8 6713 if (spa_writeable(spa)) {
b128c09f
BB
6714 /*
6715 * Update the config cache to include the newly-imported pool.
6716 */
45d1cae3 6717 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
b128c09f 6718 }
34dc7c2f 6719
34dc7c2f 6720 /*
9babb374
BB
6721 * It's possible that the pool was expanded while it was exported.
6722 * We kick off an async task to handle this for us.
34dc7c2f 6723 */
9babb374 6724 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
b128c09f 6725
d5e024cb 6726 spa_history_log_version(spa, "import", NULL);
fb390aaf 6727
12fa0466 6728 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT);
fb390aaf 6729
fb390aaf
HR
6730 mutex_exit(&spa_namespace_lock);
6731
ec213971 6732 zvol_create_minors_recursive(pool);
4a22ba5b 6733
4759342a
JL
6734 spa_import_os(spa);
6735
b128c09f
BB
6736 return (0);
6737}
6738
34dc7c2f
BB
6739nvlist_t *
6740spa_tryimport(nvlist_t *tryconfig)
6741{
6742 nvlist_t *config = NULL;
d1807f16 6743 const char *poolname, *cachefile;
34dc7c2f
BB
6744 spa_t *spa;
6745 uint64_t state;
d164b209 6746 int error;
8a393be3 6747 zpool_load_policy_t policy;
34dc7c2f
BB
6748
6749 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
6750 return (NULL);
6751
6752 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
6753 return (NULL);
6754
6755 /*
6756 * Create and initialize the spa structure.
6757 */
6758 mutex_enter(&spa_namespace_lock);
428870ff 6759 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
da92d5cb 6760 spa_activate(spa, SPA_MODE_READ);
34dc7c2f
BB
6761
6762 /*
8a393be3 6763 * Rewind pool if a max txg was provided.
34dc7c2f 6764 */
8a393be3
PZ
6765 zpool_get_load_policy(spa->spa_config, &policy);
6766 if (policy.zlp_txg != UINT64_MAX) {
6767 spa->spa_load_max_txg = policy.zlp_txg;
6cb8e530
PZ
6768 spa->spa_extreme_rewind = B_TRUE;
6769 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
8a393be3 6770 poolname, (longlong_t)policy.zlp_txg);
6cb8e530
PZ
6771 } else {
6772 zfs_dbgmsg("spa_tryimport: importing %s", poolname);
6773 }
6774
6775 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile)
6776 == 0) {
6777 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile);
6778 spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE;
6779 } else {
6780 spa->spa_config_source = SPA_CONFIG_SRC_SCAN;
6781 }
6782
82ac409a
AH
6783 /*
6784 * spa_import() relies on a pool config fetched by spa_try_import()
6785 * for spare/cache devices. Import flags are not passed to
6786 * spa_tryimport(), which makes it return early due to a missing log
6787 * device and missing retrieving the cache device and spare eventually.
6788 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6789 * the correct configuration regardless of the missing log device.
6790 */
6791 spa->spa_import_flags |= ZFS_IMPORT_MISSING_LOG;
6792
6cb8e530 6793 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING);
34dc7c2f
BB
6794
6795 /*
6796 * If 'tryconfig' was at least parsable, return the current config.
6797 */
6798 if (spa->spa_root_vdev != NULL) {
34dc7c2f 6799 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
65ad5d11
AJ
6800 fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, poolname);
6801 fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, state);
6802 fnvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
6803 spa->spa_uberblock.ub_timestamp);
6804 fnvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
6805 spa->spa_load_info);
6806 fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA,
6807 spa->spa_errata);
34dc7c2f
BB
6808
6809 /*
6810 * If the bootfs property exists on this pool then we
6811 * copy it out so that external consumers can tell which
6812 * pools are bootable.
6813 */
d164b209 6814 if ((!error || error == EEXIST) && spa->spa_bootfs) {
79c76d5b 6815 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
34dc7c2f
BB
6816
6817 /*
6818 * We have to play games with the name since the
6819 * pool was opened as TRYIMPORT_NAME.
6820 */
b128c09f 6821 if (dsl_dsobj_to_dsname(spa_name(spa),
34dc7c2f
BB
6822 spa->spa_bootfs, tmpname) == 0) {
6823 char *cp;
d1d7e268
MK
6824 char *dsname;
6825
79c76d5b 6826 dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
34dc7c2f
BB
6827
6828 cp = strchr(tmpname, '/');
6829 if (cp == NULL) {
6830 (void) strlcpy(dsname, tmpname,
6831 MAXPATHLEN);
6832 } else {
6833 (void) snprintf(dsname, MAXPATHLEN,
6834 "%s/%s", poolname, ++cp);
6835 }
65ad5d11
AJ
6836 fnvlist_add_string(config, ZPOOL_CONFIG_BOOTFS,
6837 dsname);
34dc7c2f
BB
6838 kmem_free(dsname, MAXPATHLEN);
6839 }
6840 kmem_free(tmpname, MAXPATHLEN);
6841 }
6842
6843 /*
6844 * Add the list of hot spares and level 2 cache devices.
6845 */
9babb374 6846 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f
BB
6847 spa_add_spares(spa, config);
6848 spa_add_l2cache(spa, config);
9babb374 6849 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f
BB
6850 }
6851
6852 spa_unload(spa);
6853 spa_deactivate(spa);
6854 spa_remove(spa);
6855 mutex_exit(&spa_namespace_lock);
6856
6857 return (config);
6858}
6859
6860/*
6861 * Pool export/destroy
6862 *
6863 * The act of destroying or exporting a pool is very simple. We make sure there
6864 * is no more pending I/O and any references to the pool are gone. Then, we
6865 * update the pool state and sync all the labels to disk, removing the
fb5f0bc8
BB
6866 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6867 * we don't sync the labels or remove the configuration cache.
34dc7c2f
BB
6868 */
6869static int
4d55ea81 6870spa_export_common(const char *pool, int new_state, nvlist_t **oldconfig,
fb5f0bc8 6871 boolean_t force, boolean_t hardforce)
34dc7c2f 6872{
f4f50a70 6873 int error;
34dc7c2f
BB
6874 spa_t *spa;
6875
6876 if (oldconfig)
6877 *oldconfig = NULL;
6878
da92d5cb 6879 if (!(spa_mode_global & SPA_MODE_WRITE))
2e528b49 6880 return (SET_ERROR(EROFS));
34dc7c2f
BB
6881
6882 mutex_enter(&spa_namespace_lock);
6883 if ((spa = spa_lookup(pool)) == NULL) {
6884 mutex_exit(&spa_namespace_lock);
2e528b49 6885 return (SET_ERROR(ENOENT));
34dc7c2f
BB
6886 }
6887
43a85362
SD
6888 if (spa->spa_is_exporting) {
6889 /* the pool is being exported by another thread */
6890 mutex_exit(&spa_namespace_lock);
6891 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS));
6892 }
6893 spa->spa_is_exporting = B_TRUE;
6894
34dc7c2f
BB
6895 /*
6896 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6897 * reacquire the namespace lock, and see if we can export.
6898 */
6899 spa_open_ref(spa, FTAG);
6900 mutex_exit(&spa_namespace_lock);
6901 spa_async_suspend(spa);
a0bd735a
BP
6902 if (spa->spa_zvol_taskq) {
6903 zvol_remove_minors(spa, spa_name(spa), B_TRUE);
6904 taskq_wait(spa->spa_zvol_taskq);
6905 }
34dc7c2f
BB
6906 mutex_enter(&spa_namespace_lock);
6907 spa_close(spa, FTAG);
6908
d14cfd83
IH
6909 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
6910 goto export_spa;
34dc7c2f 6911 /*
d14cfd83
IH
6912 * The pool will be in core if it's openable, in which case we can
6913 * modify its state. Objsets may be open only because they're dirty,
6914 * so we have to force it to sync before checking spa_refcnt.
34dc7c2f 6915 */
0c66c32d 6916 if (spa->spa_sync_on) {
34dc7c2f 6917 txg_wait_synced(spa->spa_dsl_pool, 0);
0c66c32d
JG
6918 spa_evicting_os_wait(spa);
6919 }
34dc7c2f 6920
d14cfd83
IH
6921 /*
6922 * A pool cannot be exported or destroyed if there are active
6923 * references. If we are resetting a pool, allow references by
6924 * fault injection handlers.
6925 */
f4f50a70
WA
6926 if (!spa_refcount_zero(spa) || (spa->spa_inject_ref != 0)) {
6927 error = SET_ERROR(EBUSY);
6928 goto fail;
d14cfd83 6929 }
34dc7c2f 6930
d14cfd83 6931 if (spa->spa_sync_on) {
88b199c2 6932 vdev_t *rvd = spa->spa_root_vdev;
b128c09f
BB
6933 /*
6934 * A pool cannot be exported if it has an active shared spare.
6935 * This is to prevent other pools stealing the active spare
6936 * from an exported pool. At user's own will, such pool can
6937 * be forcedly exported.
6938 */
6939 if (!force && new_state == POOL_STATE_EXPORTED &&
6940 spa_has_active_shared_spare(spa)) {
f4f50a70
WA
6941 error = SET_ERROR(EXDEV);
6942 goto fail;
b128c09f 6943 }
34dc7c2f 6944
619f0976
GW
6945 /*
6946 * We're about to export or destroy this pool. Make sure
1b939560
BB
6947 * we stop all initialization and trim activity here before
6948 * we set the spa_final_txg. This will ensure that all
619f0976
GW
6949 * dirty data resulting from the initialization is
6950 * committed to disk before we unload the pool.
6951 */
88b199c2
RY
6952 vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE);
6953 vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE);
6954 vdev_autotrim_stop_all(spa);
6955 vdev_rebuild_stop_all(spa);
619f0976 6956
34dc7c2f
BB
6957 /*
6958 * We want this to be reflected on every label,
6959 * so mark them all dirty. spa_unload() will do the
6960 * final sync that pushes these changes out.
6961 */
fb5f0bc8 6962 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
b128c09f 6963 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 6964 spa->spa_state = new_state;
88b199c2 6965 vdev_config_dirty(rvd);
2fb52853
GA
6966 spa_config_exit(spa, SCL_ALL, FTAG);
6967 }
6968
6969 /*
6970 * If the log space map feature is enabled and the pool is
6971 * getting exported (but not destroyed), we want to spend some
6972 * time flushing as many metaslabs as we can in an attempt to
6973 * destroy log space maps and save import time. This has to be
6974 * done before we set the spa_final_txg, otherwise
6975 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6976 * spa_should_flush_logs_on_unload() should be called after
6977 * spa_state has been set to the new_state.
6978 */
6979 if (spa_should_flush_logs_on_unload(spa))
6980 spa_unload_log_sm_flush_all(spa);
6981
6982 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
6983 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
428870ff
BB
6984 spa->spa_final_txg = spa_last_synced_txg(spa) +
6985 TXG_DEFER_SIZE + 1;
b128c09f 6986 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6987 }
6988 }
6989
d14cfd83 6990export_spa:
4759342a
JL
6991 spa_export_os(spa);
6992
d5e024cb
BB
6993 if (new_state == POOL_STATE_DESTROYED)
6994 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY);
6995 else if (new_state == POOL_STATE_EXPORTED)
6996 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT);
34dc7c2f
BB
6997
6998 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6999 spa_unload(spa);
7000 spa_deactivate(spa);
7001 }
7002
7003 if (oldconfig && spa->spa_config)
65ad5d11 7004 *oldconfig = fnvlist_dup(spa->spa_config);
34dc7c2f
BB
7005
7006 if (new_state != POOL_STATE_UNINITIALIZED) {
fb5f0bc8 7007 if (!hardforce)
55c12724 7008 spa_write_cachefile(spa, B_TRUE, B_TRUE, B_FALSE);
34dc7c2f 7009 spa_remove(spa);
43a85362
SD
7010 } else {
7011 /*
7012 * If spa_remove() is not called for this spa_t and
7013 * there is any possibility that it can be reused,
7014 * we make sure to reset the exporting flag.
7015 */
7016 spa->spa_is_exporting = B_FALSE;
34dc7c2f 7017 }
34dc7c2f 7018
43a85362 7019 mutex_exit(&spa_namespace_lock);
34dc7c2f 7020 return (0);
f4f50a70
WA
7021
7022fail:
7023 spa->spa_is_exporting = B_FALSE;
7024 spa_async_resume(spa);
7025 mutex_exit(&spa_namespace_lock);
7026 return (error);
34dc7c2f
BB
7027}
7028
7029/*
7030 * Destroy a storage pool.
7031 */
7032int
4d55ea81 7033spa_destroy(const char *pool)
34dc7c2f 7034{
fb5f0bc8
BB
7035 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
7036 B_FALSE, B_FALSE));
34dc7c2f
BB
7037}
7038
7039/*
7040 * Export a storage pool.
7041 */
7042int
4d55ea81 7043spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force,
fb5f0bc8 7044 boolean_t hardforce)
34dc7c2f 7045{
fb5f0bc8
BB
7046 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
7047 force, hardforce));
34dc7c2f
BB
7048}
7049
7050/*
7051 * Similar to spa_export(), this unloads the spa_t without actually removing it
7052 * from the namespace in any way.
7053 */
7054int
4d55ea81 7055spa_reset(const char *pool)
34dc7c2f 7056{
b128c09f 7057 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
fb5f0bc8 7058 B_FALSE, B_FALSE));
34dc7c2f
BB
7059}
7060
34dc7c2f
BB
7061/*
7062 * ==========================================================================
7063 * Device manipulation
7064 * ==========================================================================
7065 */
7066
b2255edc
BB
7067/*
7068 * This is called as a synctask to increment the draid feature flag
7069 */
7070static void
7071spa_draid_feature_incr(void *arg, dmu_tx_t *tx)
7072{
7073 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7074 int draid = (int)(uintptr_t)arg;
7075
7076 for (int c = 0; c < draid; c++)
7077 spa_feature_incr(spa, SPA_FEATURE_DRAID, tx);
7078}
7079
34dc7c2f
BB
7080/*
7081 * Add a device to a storage pool.
7082 */
7083int
7084spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
7085{
b2255edc 7086 uint64_t txg, ndraid = 0;
fb5f0bc8 7087 int error;
34dc7c2f
BB
7088 vdev_t *rvd = spa->spa_root_vdev;
7089 vdev_t *vd, *tvd;
7090 nvlist_t **spares, **l2cache;
7091 uint_t nspares, nl2cache;
7092
572e2857
BB
7093 ASSERT(spa_writeable(spa));
7094
34dc7c2f
BB
7095 txg = spa_vdev_enter(spa);
7096
7097 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
7098 VDEV_ALLOC_ADD)) != 0)
7099 return (spa_vdev_exit(spa, NULL, txg, error));
7100
b128c09f 7101 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
34dc7c2f
BB
7102
7103 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
7104 &nspares) != 0)
7105 nspares = 0;
7106
7107 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
7108 &nl2cache) != 0)
7109 nl2cache = 0;
7110
b128c09f 7111 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
34dc7c2f 7112 return (spa_vdev_exit(spa, vd, txg, EINVAL));
34dc7c2f 7113
b128c09f 7114 if (vd->vdev_children != 0 &&
b2255edc 7115 (error = vdev_create(vd, txg, B_FALSE)) != 0) {
b128c09f 7116 return (spa_vdev_exit(spa, vd, txg, error));
b2255edc
BB
7117 }
7118
7119 /*
7120 * The virtual dRAID spares must be added after vdev tree is created
bf169e9f 7121 * and the vdev guids are generated. The guid of their associated
b2255edc
BB
7122 * dRAID is stored in the config and used when opening the spare.
7123 */
7124 if ((error = vdev_draid_spare_create(nvroot, vd, &ndraid,
7125 rvd->vdev_children)) == 0) {
7126 if (ndraid > 0 && nvlist_lookup_nvlist_array(nvroot,
7127 ZPOOL_CONFIG_SPARES, &spares, &nspares) != 0)
7128 nspares = 0;
7129 } else {
7130 return (spa_vdev_exit(spa, vd, txg, error));
7131 }
34dc7c2f
BB
7132
7133 /*
7134 * We must validate the spares and l2cache devices after checking the
7135 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
7136 */
b128c09f 7137 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
34dc7c2f 7138 return (spa_vdev_exit(spa, vd, txg, error));
34dc7c2f
BB
7139
7140 /*
a1d477c2
MA
7141 * If we are in the middle of a device removal, we can only add
7142 * devices which match the existing devices in the pool.
7143 * If we are in the middle of a removal, or have some indirect
b2255edc 7144 * vdevs, we can not add raidz or dRAID top levels.
34dc7c2f 7145 */
a1d477c2
MA
7146 if (spa->spa_vdev_removal != NULL ||
7147 spa->spa_removing_phys.sr_prev_indirect_vdev != -1) {
7148 for (int c = 0; c < vd->vdev_children; c++) {
7149 tvd = vd->vdev_child[c];
7150 if (spa->spa_vdev_removal != NULL &&
9e052db4 7151 tvd->vdev_ashift != spa->spa_max_ashift) {
a1d477c2
MA
7152 return (spa_vdev_exit(spa, vd, txg, EINVAL));
7153 }
b2255edc
BB
7154 /* Fail if top level vdev is raidz or a dRAID */
7155 if (vdev_get_nparity(tvd) != 0)
a1d477c2 7156 return (spa_vdev_exit(spa, vd, txg, EINVAL));
b2255edc 7157
a1d477c2
MA
7158 /*
7159 * Need the top level mirror to be
7160 * a mirror of leaf vdevs only
7161 */
7162 if (tvd->vdev_ops == &vdev_mirror_ops) {
7163 for (uint64_t cid = 0;
7164 cid < tvd->vdev_children; cid++) {
7165 vdev_t *cvd = tvd->vdev_child[cid];
7166 if (!cvd->vdev_ops->vdev_op_leaf) {
7167 return (spa_vdev_exit(spa, vd,
7168 txg, EINVAL));
7169 }
7170 }
7171 }
7172 }
7173 }
7174
1c27024e 7175 for (int c = 0; c < vd->vdev_children; c++) {
34dc7c2f
BB
7176 tvd = vd->vdev_child[c];
7177 vdev_remove_child(vd, tvd);
93e28d66 7178 tvd->vdev_id = rvd->vdev_children;
34dc7c2f
BB
7179 vdev_add_child(rvd, tvd);
7180 vdev_config_dirty(tvd);
7181 }
7182
7183 if (nspares != 0) {
7184 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
7185 ZPOOL_CONFIG_SPARES);
7186 spa_load_spares(spa);
7187 spa->spa_spares.sav_sync = B_TRUE;
7188 }
7189
7190 if (nl2cache != 0) {
7191 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
7192 ZPOOL_CONFIG_L2CACHE);
7193 spa_load_l2cache(spa);
7194 spa->spa_l2cache.sav_sync = B_TRUE;
7195 }
7196
b2255edc
BB
7197 /*
7198 * We can't increment a feature while holding spa_vdev so we
7199 * have to do it in a synctask.
7200 */
7201 if (ndraid != 0) {
7202 dmu_tx_t *tx;
7203
7204 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
7205 dsl_sync_task_nowait(spa->spa_dsl_pool, spa_draid_feature_incr,
7206 (void *)(uintptr_t)ndraid, tx);
7207 dmu_tx_commit(tx);
7208 }
7209
34dc7c2f
BB
7210 /*
7211 * We have to be careful when adding new vdevs to an existing pool.
7212 * If other threads start allocating from these vdevs before we
7213 * sync the config cache, and we lose power, then upon reboot we may
7214 * fail to open the pool because there are DVAs that the config cache
7215 * can't translate. Therefore, we first add the vdevs without
7216 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
7217 * and then let spa_config_update() initialize the new metaslabs.
7218 *
7219 * spa_load() checks for added-but-not-initialized vdevs, so that
7220 * if we lose power at any point in this sequence, the remaining
7221 * steps will be completed the next time we load the pool.
7222 */
7223 (void) spa_vdev_exit(spa, vd, txg, 0);
7224
7225 mutex_enter(&spa_namespace_lock);
7226 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
12fa0466 7227 spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD);
34dc7c2f
BB
7228 mutex_exit(&spa_namespace_lock);
7229
7230 return (0);
7231}
7232
7233/*
5caeef02
DB
7234 * Attach a device to a vdev specified by its guid. The vdev type can be
7235 * a mirror, a raidz, or a leaf device that is also a top-level (e.g. a
7236 * single device). When the vdev is a single device, a mirror vdev will be
7237 * automatically inserted.
34dc7c2f
BB
7238 *
7239 * If 'replacing' is specified, the new device is intended to replace the
7240 * existing device; in this case the two devices are made into their own
7241 * mirror using the 'replacing' vdev, which is functionally identical to
7242 * the mirror vdev (it actually reuses all the same ops) but has a few
7243 * extra rules: you can't attach to it after it's been created, and upon
7244 * completion of resilvering, the first disk (the one being replaced)
7245 * is automatically detached.
9a49d3f3
BB
7246 *
7247 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
7248 * should be performed instead of traditional healing reconstruction. From
7249 * an administrators perspective these are both resilver operations.
34dc7c2f
BB
7250 */
7251int
9a49d3f3
BB
7252spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing,
7253 int rebuild)
34dc7c2f 7254{
428870ff 7255 uint64_t txg, dtl_max_txg;
9a49d3f3 7256 vdev_t *rvd = spa->spa_root_vdev;
34dc7c2f
BB
7257 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
7258 vdev_ops_t *pvops;
b128c09f 7259 char *oldvdpath, *newvdpath;
5caeef02 7260 int newvd_isspare = B_FALSE;
b128c09f 7261 int error;
34dc7c2f 7262
572e2857
BB
7263 ASSERT(spa_writeable(spa));
7264
34dc7c2f
BB
7265 txg = spa_vdev_enter(spa);
7266
b128c09f 7267 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f 7268
d2734cce
SD
7269 ASSERT(MUTEX_HELD(&spa_namespace_lock));
7270 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
7271 error = (spa_has_checkpoint(spa)) ?
7272 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
7273 return (spa_vdev_exit(spa, NULL, txg, error));
7274 }
7275
9a49d3f3
BB
7276 if (rebuild) {
7277 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD))
7278 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7279
9d618615
A
7280 if (dsl_scan_resilvering(spa_get_dsl(spa)) ||
7281 dsl_scan_resilver_scheduled(spa_get_dsl(spa))) {
9a49d3f3
BB
7282 return (spa_vdev_exit(spa, NULL, txg,
7283 ZFS_ERR_RESILVER_IN_PROGRESS));
9d618615 7284 }
9a49d3f3
BB
7285 } else {
7286 if (vdev_rebuild_active(rvd))
7287 return (spa_vdev_exit(spa, NULL, txg,
7288 ZFS_ERR_REBUILD_IN_PROGRESS));
7289 }
7290
5caeef02
DB
7291 if (spa->spa_vdev_removal != NULL) {
7292 return (spa_vdev_exit(spa, NULL, txg,
7293 ZFS_ERR_DEVRM_IN_PROGRESS));
7294 }
a1d477c2 7295
34dc7c2f
BB
7296 if (oldvd == NULL)
7297 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
7298
5caeef02
DB
7299 boolean_t raidz = oldvd->vdev_ops == &vdev_raidz_ops;
7300
7301 if (raidz) {
7302 if (!spa_feature_is_enabled(spa, SPA_FEATURE_RAIDZ_EXPANSION))
7303 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7304
7305 /*
7306 * Can't expand a raidz while prior expand is in progress.
7307 */
7308 if (spa->spa_raidz_expand != NULL) {
7309 return (spa_vdev_exit(spa, NULL, txg,
7310 ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS));
7311 }
7312 } else if (!oldvd->vdev_ops->vdev_op_leaf) {
34dc7c2f 7313 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5caeef02 7314 }
34dc7c2f 7315
5caeef02
DB
7316 if (raidz)
7317 pvd = oldvd;
7318 else
7319 pvd = oldvd->vdev_parent;
34dc7c2f 7320
6a42939f
RY
7321 if (spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
7322 VDEV_ALLOC_ATTACH) != 0)
34dc7c2f
BB
7323 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
7324
7325 if (newrootvd->vdev_children != 1)
7326 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
7327
7328 newvd = newrootvd->vdev_child[0];
7329
7330 if (!newvd->vdev_ops->vdev_op_leaf)
7331 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
7332
7333 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
7334 return (spa_vdev_exit(spa, newrootvd, txg, error));
7335
7336 /*
c23738c7 7337 * log, dedup and special vdevs should not be replaced by spares.
34dc7c2f 7338 */
c23738c7
AH
7339 if ((oldvd->vdev_top->vdev_alloc_bias != VDEV_BIAS_NONE ||
7340 oldvd->vdev_top->vdev_islog) && newvd->vdev_isspare) {
34dc7c2f 7341 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
c23738c7 7342 }
34dc7c2f 7343
b2255edc
BB
7344 /*
7345 * A dRAID spare can only replace a child of its parent dRAID vdev.
7346 */
7347 if (newvd->vdev_ops == &vdev_draid_spare_ops &&
7348 oldvd->vdev_top != vdev_draid_spare_get_parent(newvd)) {
7349 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7350 }
7351
9a49d3f3
BB
7352 if (rebuild) {
7353 /*
b2255edc 7354 * For rebuilds, the top vdev must support reconstruction
9a49d3f3 7355 * using only space maps. This means the only allowable
b2255edc 7356 * vdevs types are the root vdev, a mirror, or dRAID.
9a49d3f3 7357 */
b2255edc
BB
7358 tvd = pvd;
7359 if (pvd->vdev_top != NULL)
7360 tvd = pvd->vdev_top;
7361
7362 if (tvd->vdev_ops != &vdev_mirror_ops &&
7363 tvd->vdev_ops != &vdev_root_ops &&
7364 tvd->vdev_ops != &vdev_draid_ops) {
9a49d3f3
BB
7365 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7366 }
7367 }
7368
34dc7c2f
BB
7369 if (!replacing) {
7370 /*
7bbd42ef
DB
7371 * For attach, the only allowable parent is a mirror or
7372 * the root vdev. A raidz vdev can be attached to, but
7373 * you cannot attach to a raidz child.
34dc7c2f
BB
7374 */
7375 if (pvd->vdev_ops != &vdev_mirror_ops &&
7bbd42ef
DB
7376 pvd->vdev_ops != &vdev_root_ops &&
7377 !raidz)
34dc7c2f
BB
7378 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7379
7380 pvops = &vdev_mirror_ops;
7381 } else {
7382 /*
7383 * Active hot spares can only be replaced by inactive hot
7384 * spares.
7385 */
7386 if (pvd->vdev_ops == &vdev_spare_ops &&
572e2857 7387 oldvd->vdev_isspare &&
34dc7c2f
BB
7388 !spa_has_spare(spa, newvd->vdev_guid))
7389 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7390
7391 /*
7392 * If the source is a hot spare, and the parent isn't already a
7393 * spare, then we want to create a new hot spare. Otherwise, we
7394 * want to create a replacing vdev. The user is not allowed to
7395 * attach to a spared vdev child unless the 'isspare' state is
7396 * the same (spare replaces spare, non-spare replaces
7397 * non-spare).
7398 */
572e2857
BB
7399 if (pvd->vdev_ops == &vdev_replacing_ops &&
7400 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
34dc7c2f 7401 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
7402 } else if (pvd->vdev_ops == &vdev_spare_ops &&
7403 newvd->vdev_isspare != oldvd->vdev_isspare) {
34dc7c2f 7404 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
7405 }
7406
7407 if (newvd->vdev_isspare)
34dc7c2f
BB
7408 pvops = &vdev_spare_ops;
7409 else
7410 pvops = &vdev_replacing_ops;
7411 }
7412
7413 /*
9babb374 7414 * Make sure the new device is big enough.
34dc7c2f 7415 */
5caeef02
DB
7416 vdev_t *min_vdev = raidz ? oldvd->vdev_child[0] : oldvd;
7417 if (newvd->vdev_asize < vdev_get_min_asize(min_vdev))
34dc7c2f
BB
7418 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
7419
7420 /*
7421 * The new device cannot have a higher alignment requirement
7422 * than the top-level vdev.
7423 */
7424 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
9a49d3f3 7425 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
34dc7c2f 7426
5caeef02
DB
7427 /*
7428 * RAIDZ-expansion-specific checks.
7429 */
7430 if (raidz) {
7431 if (vdev_raidz_attach_check(newvd) != 0)
7432 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
7433
7434 /*
7435 * Fail early if a child is not healthy or being replaced
7436 */
7437 for (int i = 0; i < oldvd->vdev_children; i++) {
7438 if (vdev_is_dead(oldvd->vdev_child[i]) ||
7439 !oldvd->vdev_child[i]->vdev_ops->vdev_op_leaf) {
7440 return (spa_vdev_exit(spa, newrootvd, txg,
7441 ENXIO));
7442 }
7443 /* Also fail if reserved boot area is in-use */
7444 if (vdev_check_boot_reserve(spa, oldvd->vdev_child[i])
7445 != 0) {
7446 return (spa_vdev_exit(spa, newrootvd, txg,
7447 EADDRINUSE));
7448 }
7449 }
7450 }
7451
7452 if (raidz) {
7453 /*
7454 * Note: oldvdpath is freed by spa_strfree(), but
7455 * kmem_asprintf() is freed by kmem_strfree(), so we have to
7456 * move it to a spa_strdup-ed string.
7457 */
7458 char *tmp = kmem_asprintf("raidz%u-%u",
7459 (uint_t)vdev_get_nparity(oldvd), (uint_t)oldvd->vdev_id);
7460 oldvdpath = spa_strdup(tmp);
7461 kmem_strfree(tmp);
7462 } else {
7463 oldvdpath = spa_strdup(oldvd->vdev_path);
7464 }
7465 newvdpath = spa_strdup(newvd->vdev_path);
7466
34dc7c2f
BB
7467 /*
7468 * If this is an in-place replacement, update oldvd's path and devid
7469 * to make it distinguishable from newvd, and unopenable from now on.
7470 */
5caeef02 7471 if (strcmp(oldvdpath, newvdpath) == 0) {
34dc7c2f 7472 spa_strfree(oldvd->vdev_path);
5caeef02 7473 oldvd->vdev_path = kmem_alloc(strlen(newvdpath) + 5,
79c76d5b 7474 KM_SLEEP);
5caeef02
DB
7475 (void) sprintf(oldvd->vdev_path, "%s/old",
7476 newvdpath);
34dc7c2f
BB
7477 if (oldvd->vdev_devid != NULL) {
7478 spa_strfree(oldvd->vdev_devid);
7479 oldvd->vdev_devid = NULL;
7480 }
5caeef02
DB
7481 spa_strfree(oldvdpath);
7482 oldvdpath = spa_strdup(oldvd->vdev_path);
34dc7c2f
BB
7483 }
7484
7485 /*
7486 * If the parent is not a mirror, or if we're replacing, insert the new
7487 * mirror/replacing/spare vdev above oldvd.
7488 */
5caeef02 7489 if (!raidz && pvd->vdev_ops != pvops) {
34dc7c2f 7490 pvd = vdev_add_parent(oldvd, pvops);
5caeef02
DB
7491 ASSERT(pvd->vdev_ops == pvops);
7492 ASSERT(oldvd->vdev_parent == pvd);
7493 }
34dc7c2f
BB
7494
7495 ASSERT(pvd->vdev_top->vdev_parent == rvd);
34dc7c2f
BB
7496
7497 /*
7498 * Extract the new device from its root and add it to pvd.
7499 */
7500 vdev_remove_child(newrootvd, newvd);
7501 newvd->vdev_id = pvd->vdev_children;
428870ff 7502 newvd->vdev_crtxg = oldvd->vdev_crtxg;
34dc7c2f
BB
7503 vdev_add_child(pvd, newvd);
7504
6d82f98c
IH
7505 /*
7506 * Reevaluate the parent vdev state.
7507 */
7508 vdev_propagate_state(pvd);
7509
34dc7c2f
BB
7510 tvd = newvd->vdev_top;
7511 ASSERT(pvd->vdev_top == tvd);
7512 ASSERT(tvd->vdev_parent == rvd);
7513
7514 vdev_config_dirty(tvd);
7515
7516 /*
428870ff
BB
7517 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7518 * for any dmu_sync-ed blocks. It will propagate upward when
7519 * spa_vdev_exit() calls vdev_dtl_reassess().
34dc7c2f 7520 */
428870ff 7521 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
34dc7c2f 7522
5caeef02
DB
7523 if (raidz) {
7524 /*
7525 * Wait for the youngest allocations and frees to sync,
7526 * and then wait for the deferral of those frees to finish.
7527 */
7528 spa_vdev_config_exit(spa, NULL,
7529 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
34dc7c2f 7530
5caeef02
DB
7531 vdev_initialize_stop_all(tvd, VDEV_INITIALIZE_ACTIVE);
7532 vdev_trim_stop_all(tvd, VDEV_TRIM_ACTIVE);
7533 vdev_autotrim_stop_wait(tvd);
9babb374 7534
5caeef02 7535 dtl_max_txg = spa_vdev_config_enter(spa);
34dc7c2f 7536
5caeef02 7537 tvd->vdev_rz_expanding = B_TRUE;
34dc7c2f 7538
5caeef02
DB
7539 vdev_dirty_leaves(tvd, VDD_DTL, dtl_max_txg);
7540 vdev_config_dirty(tvd);
9a49d3f3 7541
5caeef02
DB
7542 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool,
7543 dtl_max_txg);
7544 dsl_sync_task_nowait(spa->spa_dsl_pool, vdev_raidz_attach_sync,
7545 newvd, tx);
7546 dmu_tx_commit(tx);
9a49d3f3 7547 } else {
5caeef02
DB
7548 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
7549 dtl_max_txg - TXG_INITIAL);
7550
7551 if (newvd->vdev_isspare) {
7552 spa_spare_activate(newvd);
7553 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE);
7554 }
7555
7556 newvd_isspare = newvd->vdev_isspare;
7557
7558 /*
7559 * Mark newvd's DTL dirty in this txg.
7560 */
7561 vdev_dirty(tvd, VDD_DTL, newvd, txg);
9a49d3f3 7562
5caeef02
DB
7563 /*
7564 * Schedule the resilver or rebuild to restart in the future.
7565 * We do this to ensure that dmu_sync-ed blocks have been
7566 * stitched into the respective datasets.
7567 */
7568 if (rebuild) {
7569 newvd->vdev_rebuild_txg = txg;
7570
7571 vdev_rebuild(tvd);
9a49d3f3 7572 } else {
5caeef02
DB
7573 newvd->vdev_resilver_txg = txg;
7574
7575 if (dsl_scan_resilvering(spa_get_dsl(spa)) &&
7576 spa_feature_is_enabled(spa,
7577 SPA_FEATURE_RESILVER_DEFER)) {
7578 vdev_defer_resilver(newvd);
7579 } else {
7580 dsl_scan_restart_resilver(spa->spa_dsl_pool,
7581 dtl_max_txg);
7582 }
9a49d3f3
BB
7583 }
7584 }
428870ff 7585
fb390aaf 7586 if (spa->spa_bootfs)
12fa0466 7587 spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH);
fb390aaf 7588
12fa0466 7589 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH);
fb390aaf 7590
428870ff
BB
7591 /*
7592 * Commit the config
7593 */
7594 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
34dc7c2f 7595
6f1ffb06 7596 spa_history_log_internal(spa, "vdev attach", NULL,
428870ff 7597 "%s vdev=%s %s vdev=%s",
45d1cae3
BB
7598 replacing && newvd_isspare ? "spare in" :
7599 replacing ? "replace" : "attach", newvdpath,
7600 replacing ? "for" : "to", oldvdpath);
b128c09f
BB
7601
7602 spa_strfree(oldvdpath);
7603 spa_strfree(newvdpath);
7604
34dc7c2f
BB
7605 return (0);
7606}
7607
7608/*
7609 * Detach a device from a mirror or replacing vdev.
d3cc8b15 7610 *
34dc7c2f 7611 * If 'replace_done' is specified, only detach if the parent
719534ca 7612 * is a replacing or a spare vdev.
34dc7c2f
BB
7613 */
7614int
fb5f0bc8 7615spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
34dc7c2f
BB
7616{
7617 uint64_t txg;
fb5f0bc8 7618 int error;
2a8ba608 7619 vdev_t *rvd __maybe_unused = spa->spa_root_vdev;
34dc7c2f
BB
7620 vdev_t *vd, *pvd, *cvd, *tvd;
7621 boolean_t unspare = B_FALSE;
d4ed6673 7622 uint64_t unspare_guid = 0;
428870ff 7623 char *vdpath;
1c27024e 7624
572e2857
BB
7625 ASSERT(spa_writeable(spa));
7626
9a49d3f3 7627 txg = spa_vdev_detach_enter(spa, guid);
34dc7c2f 7628
b128c09f 7629 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f 7630
d2734cce
SD
7631 /*
7632 * Besides being called directly from the userland through the
7633 * ioctl interface, spa_vdev_detach() can be potentially called
7634 * at the end of spa_vdev_resilver_done().
7635 *
7636 * In the regular case, when we have a checkpoint this shouldn't
7637 * happen as we never empty the DTLs of a vdev during the scrub
7638 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7639 * should never get here when we have a checkpoint.
7640 *
7641 * That said, even in a case when we checkpoint the pool exactly
7642 * as spa_vdev_resilver_done() calls this function everything
7643 * should be fine as the resilver will return right away.
7644 */
7645 ASSERT(MUTEX_HELD(&spa_namespace_lock));
7646 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
7647 error = (spa_has_checkpoint(spa)) ?
7648 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
7649 return (spa_vdev_exit(spa, NULL, txg, error));
7650 }
7651
34dc7c2f
BB
7652 if (vd == NULL)
7653 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
7654
7655 if (!vd->vdev_ops->vdev_op_leaf)
7656 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7657
7658 pvd = vd->vdev_parent;
7659
fb5f0bc8
BB
7660 /*
7661 * If the parent/child relationship is not as expected, don't do it.
7662 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7663 * vdev that's replacing B with C. The user's intent in replacing
7664 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7665 * the replace by detaching C, the expected behavior is to end up
7666 * M(A,B). But suppose that right after deciding to detach C,
7667 * the replacement of B completes. We would have M(A,C), and then
7668 * ask to detach C, which would leave us with just A -- not what
7669 * the user wanted. To prevent this, we make sure that the
7670 * parent/child relationship hasn't changed -- in this example,
7671 * that C's parent is still the replacing vdev R.
7672 */
7673 if (pvd->vdev_guid != pguid && pguid != 0)
7674 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
7675
34dc7c2f 7676 /*
572e2857 7677 * Only 'replacing' or 'spare' vdevs can be replaced.
34dc7c2f 7678 */
572e2857
BB
7679 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
7680 pvd->vdev_ops != &vdev_spare_ops)
7681 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34dc7c2f
BB
7682
7683 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
7684 spa_version(spa) >= SPA_VERSION_SPARES);
7685
7686 /*
7687 * Only mirror, replacing, and spare vdevs support detach.
7688 */
7689 if (pvd->vdev_ops != &vdev_replacing_ops &&
7690 pvd->vdev_ops != &vdev_mirror_ops &&
7691 pvd->vdev_ops != &vdev_spare_ops)
7692 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
7693
7694 /*
fb5f0bc8
BB
7695 * If this device has the only valid copy of some data,
7696 * we cannot safely detach it.
34dc7c2f 7697 */
fb5f0bc8 7698 if (vdev_dtl_required(vd))
34dc7c2f
BB
7699 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
7700
fb5f0bc8 7701 ASSERT(pvd->vdev_children >= 2);
34dc7c2f 7702
b128c09f
BB
7703 /*
7704 * If we are detaching the second disk from a replacing vdev, then
7705 * check to see if we changed the original vdev's path to have "/old"
7706 * at the end in spa_vdev_attach(). If so, undo that change now.
7707 */
572e2857
BB
7708 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
7709 vd->vdev_path != NULL) {
7710 size_t len = strlen(vd->vdev_path);
7711
1c27024e 7712 for (int c = 0; c < pvd->vdev_children; c++) {
572e2857
BB
7713 cvd = pvd->vdev_child[c];
7714
7715 if (cvd == vd || cvd->vdev_path == NULL)
7716 continue;
7717
7718 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
7719 strcmp(cvd->vdev_path + len, "/old") == 0) {
7720 spa_strfree(cvd->vdev_path);
7721 cvd->vdev_path = spa_strdup(vd->vdev_path);
7722 break;
7723 }
b128c09f
BB
7724 }
7725 }
7726
34dc7c2f 7727 /*
b2255edc
BB
7728 * If we are detaching the original disk from a normal spare, then it
7729 * implies that the spare should become a real disk, and be removed
7730 * from the active spare list for the pool. dRAID spares on the
7731 * other hand are coupled to the pool and thus should never be removed
7732 * from the spares list.
34dc7c2f 7733 */
b2255edc
BB
7734 if (pvd->vdev_ops == &vdev_spare_ops && vd->vdev_id == 0) {
7735 vdev_t *last_cvd = pvd->vdev_child[pvd->vdev_children - 1];
7736
7737 if (last_cvd->vdev_isspare &&
7738 last_cvd->vdev_ops != &vdev_draid_spare_ops) {
7739 unspare = B_TRUE;
7740 }
7741 }
34dc7c2f
BB
7742
7743 /*
7744 * Erase the disk labels so the disk can be used for other things.
7745 * This must be done after all other error cases are handled,
7746 * but before we disembowel vd (so we can still do I/O to it).
7747 * But if we can't do it, don't treat the error as fatal --
7748 * it may be that the unwritability of the disk is the reason
7749 * it's being detached!
7750 */
6a42939f 7751 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
34dc7c2f
BB
7752
7753 /*
7754 * Remove vd from its parent and compact the parent's children.
7755 */
7756 vdev_remove_child(pvd, vd);
7757 vdev_compact_children(pvd);
7758
7759 /*
7760 * Remember one of the remaining children so we can get tvd below.
7761 */
572e2857 7762 cvd = pvd->vdev_child[pvd->vdev_children - 1];
34dc7c2f
BB
7763
7764 /*
7765 * If we need to remove the remaining child from the list of hot spares,
fb5f0bc8
BB
7766 * do it now, marking the vdev as no longer a spare in the process.
7767 * We must do this before vdev_remove_parent(), because that can
7768 * change the GUID if it creates a new toplevel GUID. For a similar
7769 * reason, we must remove the spare now, in the same txg as the detach;
7770 * otherwise someone could attach a new sibling, change the GUID, and
7771 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
34dc7c2f
BB
7772 */
7773 if (unspare) {
7774 ASSERT(cvd->vdev_isspare);
7775 spa_spare_remove(cvd);
7776 unspare_guid = cvd->vdev_guid;
fb5f0bc8 7777 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
572e2857 7778 cvd->vdev_unspare = B_TRUE;
34dc7c2f
BB
7779 }
7780
428870ff
BB
7781 /*
7782 * If the parent mirror/replacing vdev only has one child,
7783 * the parent is no longer needed. Remove it from the tree.
7784 */
572e2857
BB
7785 if (pvd->vdev_children == 1) {
7786 if (pvd->vdev_ops == &vdev_spare_ops)
7787 cvd->vdev_unspare = B_FALSE;
428870ff 7788 vdev_remove_parent(cvd);
572e2857
BB
7789 }
7790
428870ff
BB
7791 /*
7792 * We don't set tvd until now because the parent we just removed
7793 * may have been the previous top-level vdev.
7794 */
7795 tvd = cvd->vdev_top;
7796 ASSERT(tvd->vdev_parent == rvd);
7797
7798 /*
7799 * Reevaluate the parent vdev state.
7800 */
7801 vdev_propagate_state(cvd);
7802
7803 /*
7804 * If the 'autoexpand' property is set on the pool then automatically
7805 * try to expand the size of the pool. For example if the device we
7806 * just detached was smaller than the others, it may be possible to
7807 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7808 * first so that we can obtain the updated sizes of the leaf vdevs.
7809 */
7810 if (spa->spa_autoexpand) {
7811 vdev_reopen(tvd);
7812 vdev_expand(tvd, txg);
7813 }
7814
7815 vdev_config_dirty(tvd);
7816
7817 /*
7818 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7819 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7820 * But first make sure we're not on any *other* txg's DTL list, to
7821 * prevent vd from being accessed after it's freed.
7822 */
b6ca6193 7823 vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none");
1c27024e 7824 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
7825 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
7826 vd->vdev_detached = B_TRUE;
7827 vdev_dirty(tvd, VDD_DTL, vd, txg);
7828
12fa0466 7829 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE);
e60e158e 7830 spa_notify_waiters(spa);
428870ff 7831
572e2857
BB
7832 /* hang on to the spa before we release the lock */
7833 spa_open_ref(spa, FTAG);
7834
428870ff
BB
7835 error = spa_vdev_exit(spa, vd, txg, 0);
7836
6f1ffb06 7837 spa_history_log_internal(spa, "detach", NULL,
428870ff
BB
7838 "vdev=%s", vdpath);
7839 spa_strfree(vdpath);
7840
7841 /*
7842 * If this was the removal of the original device in a hot spare vdev,
7843 * then we want to go through and remove the device from the hot spare
7844 * list of every other pool.
7845 */
7846 if (unspare) {
572e2857
BB
7847 spa_t *altspa = NULL;
7848
428870ff 7849 mutex_enter(&spa_namespace_lock);
572e2857
BB
7850 while ((altspa = spa_next(altspa)) != NULL) {
7851 if (altspa->spa_state != POOL_STATE_ACTIVE ||
7852 altspa == spa)
428870ff 7853 continue;
572e2857
BB
7854
7855 spa_open_ref(altspa, FTAG);
428870ff 7856 mutex_exit(&spa_namespace_lock);
572e2857 7857 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
428870ff 7858 mutex_enter(&spa_namespace_lock);
572e2857 7859 spa_close(altspa, FTAG);
428870ff
BB
7860 }
7861 mutex_exit(&spa_namespace_lock);
572e2857
BB
7862
7863 /* search the rest of the vdevs for spares to remove */
7864 spa_vdev_resilver_done(spa);
428870ff
BB
7865 }
7866
572e2857
BB
7867 /* all done with the spa; OK to release */
7868 mutex_enter(&spa_namespace_lock);
7869 spa_close(spa, FTAG);
7870 mutex_exit(&spa_namespace_lock);
7871
428870ff
BB
7872 return (error);
7873}
7874
c10d37dd
GW
7875static int
7876spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type,
7877 list_t *vd_list)
619f0976 7878{
c10d37dd
GW
7879 ASSERT(MUTEX_HELD(&spa_namespace_lock));
7880
619f0976
GW
7881 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
7882
7883 /* Look up vdev and ensure it's a leaf. */
7884 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
7885 if (vd == NULL || vd->vdev_detached) {
7886 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
619f0976
GW
7887 return (SET_ERROR(ENODEV));
7888 } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) {
7889 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
619f0976
GW
7890 return (SET_ERROR(EINVAL));
7891 } else if (!vdev_writeable(vd)) {
7892 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
619f0976
GW
7893 return (SET_ERROR(EROFS));
7894 }
7895 mutex_enter(&vd->vdev_initialize_lock);
7896 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
7897
7898 /*
7899 * When we activate an initialize action we check to see
7900 * if the vdev_initialize_thread is NULL. We do this instead
7901 * of using the vdev_initialize_state since there might be
7902 * a previous initialization process which has completed but
7903 * the thread is not exited.
7904 */
1b939560 7905 if (cmd_type == POOL_INITIALIZE_START &&
619f0976 7906 (vd->vdev_initialize_thread != NULL ||
5caeef02 7907 vd->vdev_top->vdev_removing || vd->vdev_top->vdev_rz_expanding)) {
619f0976 7908 mutex_exit(&vd->vdev_initialize_lock);
619f0976
GW
7909 return (SET_ERROR(EBUSY));
7910 } else if (cmd_type == POOL_INITIALIZE_CANCEL &&
7911 (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE &&
7912 vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) {
7913 mutex_exit(&vd->vdev_initialize_lock);
619f0976
GW
7914 return (SET_ERROR(ESRCH));
7915 } else if (cmd_type == POOL_INITIALIZE_SUSPEND &&
7916 vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) {
7917 mutex_exit(&vd->vdev_initialize_lock);
619f0976 7918 return (SET_ERROR(ESRCH));
e34e15ed
BB
7919 } else if (cmd_type == POOL_INITIALIZE_UNINIT &&
7920 vd->vdev_initialize_thread != NULL) {
7921 mutex_exit(&vd->vdev_initialize_lock);
7922 return (SET_ERROR(EBUSY));
619f0976
GW
7923 }
7924
7925 switch (cmd_type) {
1b939560 7926 case POOL_INITIALIZE_START:
619f0976
GW
7927 vdev_initialize(vd);
7928 break;
7929 case POOL_INITIALIZE_CANCEL:
c10d37dd 7930 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list);
619f0976
GW
7931 break;
7932 case POOL_INITIALIZE_SUSPEND:
c10d37dd 7933 vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list);
619f0976 7934 break;
e34e15ed
BB
7935 case POOL_INITIALIZE_UNINIT:
7936 vdev_uninitialize(vd);
7937 break;
619f0976
GW
7938 default:
7939 panic("invalid cmd_type %llu", (unsigned long long)cmd_type);
7940 }
7941 mutex_exit(&vd->vdev_initialize_lock);
7942
c10d37dd
GW
7943 return (0);
7944}
7945
7946int
7947spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
7948 nvlist_t *vdev_errlist)
7949{
7950 int total_errors = 0;
7951 list_t vd_list;
7952
7953 list_create(&vd_list, sizeof (vdev_t),
7954 offsetof(vdev_t, vdev_initialize_node));
7955
7956 /*
7957 * We hold the namespace lock through the whole function
7958 * to prevent any changes to the pool while we're starting or
7959 * stopping initialization. The config and state locks are held so that
7960 * we can properly assess the vdev state before we commit to
7961 * the initializing operation.
7962 */
7963 mutex_enter(&spa_namespace_lock);
7964
7965 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
7966 pair != NULL; pair = nvlist_next_nvpair(nv, pair)) {
7967 uint64_t vdev_guid = fnvpair_value_uint64(pair);
7968
7969 int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type,
7970 &vd_list);
7971 if (error != 0) {
7972 char guid_as_str[MAXNAMELEN];
7973
7974 (void) snprintf(guid_as_str, sizeof (guid_as_str),
7975 "%llu", (unsigned long long)vdev_guid);
7976 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
7977 total_errors++;
7978 }
7979 }
7980
7981 /* Wait for all initialize threads to stop. */
7982 vdev_initialize_stop_wait(spa, &vd_list);
7983
619f0976
GW
7984 /* Sync out the initializing state */
7985 txg_wait_synced(spa->spa_dsl_pool, 0);
7986 mutex_exit(&spa_namespace_lock);
7987
c10d37dd 7988 list_destroy(&vd_list);
619f0976 7989
c10d37dd
GW
7990 return (total_errors);
7991}
619f0976 7992
1b939560
BB
7993static int
7994spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type,
7995 uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list)
7996{
7997 ASSERT(MUTEX_HELD(&spa_namespace_lock));
7998
7999 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
8000
8001 /* Look up vdev and ensure it's a leaf. */
8002 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
8003 if (vd == NULL || vd->vdev_detached) {
8004 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8005 return (SET_ERROR(ENODEV));
8006 } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) {
8007 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8008 return (SET_ERROR(EINVAL));
8009 } else if (!vdev_writeable(vd)) {
8010 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8011 return (SET_ERROR(EROFS));
8012 } else if (!vd->vdev_has_trim) {
8013 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8014 return (SET_ERROR(EOPNOTSUPP));
8015 } else if (secure && !vd->vdev_has_securetrim) {
8016 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8017 return (SET_ERROR(EOPNOTSUPP));
8018 }
8019 mutex_enter(&vd->vdev_trim_lock);
8020 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
8021
8022 /*
8023 * When we activate a TRIM action we check to see if the
8024 * vdev_trim_thread is NULL. We do this instead of using the
8025 * vdev_trim_state since there might be a previous TRIM process
8026 * which has completed but the thread is not exited.
8027 */
8028 if (cmd_type == POOL_TRIM_START &&
5caeef02
DB
8029 (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing ||
8030 vd->vdev_top->vdev_rz_expanding)) {
1b939560
BB
8031 mutex_exit(&vd->vdev_trim_lock);
8032 return (SET_ERROR(EBUSY));
8033 } else if (cmd_type == POOL_TRIM_CANCEL &&
8034 (vd->vdev_trim_state != VDEV_TRIM_ACTIVE &&
8035 vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) {
8036 mutex_exit(&vd->vdev_trim_lock);
8037 return (SET_ERROR(ESRCH));
8038 } else if (cmd_type == POOL_TRIM_SUSPEND &&
8039 vd->vdev_trim_state != VDEV_TRIM_ACTIVE) {
8040 mutex_exit(&vd->vdev_trim_lock);
8041 return (SET_ERROR(ESRCH));
8042 }
8043
8044 switch (cmd_type) {
8045 case POOL_TRIM_START:
8046 vdev_trim(vd, rate, partial, secure);
8047 break;
8048 case POOL_TRIM_CANCEL:
8049 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list);
8050 break;
8051 case POOL_TRIM_SUSPEND:
8052 vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list);
8053 break;
8054 default:
8055 panic("invalid cmd_type %llu", (unsigned long long)cmd_type);
8056 }
8057 mutex_exit(&vd->vdev_trim_lock);
8058
8059 return (0);
8060}
8061
8062/*
8063 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
8064 * TRIM threads for each child vdev. These threads pass over all of the free
8065 * space in the vdev's metaslabs and issues TRIM commands for that space.
8066 */
8067int
8068spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate,
8069 boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist)
8070{
8071 int total_errors = 0;
8072 list_t vd_list;
8073
8074 list_create(&vd_list, sizeof (vdev_t),
8075 offsetof(vdev_t, vdev_trim_node));
8076
8077 /*
8078 * We hold the namespace lock through the whole function
8079 * to prevent any changes to the pool while we're starting or
8080 * stopping TRIM. The config and state locks are held so that
8081 * we can properly assess the vdev state before we commit to
8082 * the TRIM operation.
8083 */
8084 mutex_enter(&spa_namespace_lock);
8085
8086 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
8087 pair != NULL; pair = nvlist_next_nvpair(nv, pair)) {
8088 uint64_t vdev_guid = fnvpair_value_uint64(pair);
8089
8090 int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type,
8091 rate, partial, secure, &vd_list);
8092 if (error != 0) {
8093 char guid_as_str[MAXNAMELEN];
8094
8095 (void) snprintf(guid_as_str, sizeof (guid_as_str),
8096 "%llu", (unsigned long long)vdev_guid);
8097 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
8098 total_errors++;
8099 }
8100 }
8101
8102 /* Wait for all TRIM threads to stop. */
8103 vdev_trim_stop_wait(spa, &vd_list);
8104
8105 /* Sync out the TRIM state */
8106 txg_wait_synced(spa->spa_dsl_pool, 0);
8107 mutex_exit(&spa_namespace_lock);
8108
8109 list_destroy(&vd_list);
8110
8111 return (total_errors);
8112}
8113
428870ff
BB
8114/*
8115 * Split a set of devices from their mirrors, and create a new pool from them.
8116 */
8117int
a926aab9 8118spa_vdev_split_mirror(spa_t *spa, const char *newname, nvlist_t *config,
428870ff
BB
8119 nvlist_t *props, boolean_t exp)
8120{
8121 int error = 0;
8122 uint64_t txg, *glist;
8123 spa_t *newspa;
8124 uint_t c, children, lastlog;
8125 nvlist_t **child, *nvl, *tmp;
8126 dmu_tx_t *tx;
d1807f16 8127 const char *altroot = NULL;
428870ff
BB
8128 vdev_t *rvd, **vml = NULL; /* vdev modify list */
8129 boolean_t activate_slog;
8130
572e2857 8131 ASSERT(spa_writeable(spa));
428870ff
BB
8132
8133 txg = spa_vdev_enter(spa);
8134
d2734cce
SD
8135 ASSERT(MUTEX_HELD(&spa_namespace_lock));
8136 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
8137 error = (spa_has_checkpoint(spa)) ?
8138 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
8139 return (spa_vdev_exit(spa, NULL, txg, error));
8140 }
8141
428870ff
BB
8142 /* clear the log and flush everything up to now */
8143 activate_slog = spa_passivate_log(spa);
8144 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
a1d477c2 8145 error = spa_reset_logs(spa);
428870ff
BB
8146 txg = spa_vdev_config_enter(spa);
8147
8148 if (activate_slog)
8149 spa_activate_log(spa);
8150
8151 if (error != 0)
8152 return (spa_vdev_exit(spa, NULL, txg, error));
8153
8154 /* check new spa name before going any further */
8155 if (spa_lookup(newname) != NULL)
8156 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
8157
8158 /*
8159 * scan through all the children to ensure they're all mirrors
8160 */
8161 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
8162 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
8163 &children) != 0)
8164 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8165
8166 /* first, check to ensure we've got the right child count */
8167 rvd = spa->spa_root_vdev;
8168 lastlog = 0;
8169 for (c = 0; c < rvd->vdev_children; c++) {
8170 vdev_t *vd = rvd->vdev_child[c];
8171
8172 /* don't count the holes & logs as children */
1b664952
GA
8173 if (vd->vdev_islog || (vd->vdev_ops != &vdev_indirect_ops &&
8174 !vdev_is_concrete(vd))) {
428870ff
BB
8175 if (lastlog == 0)
8176 lastlog = c;
8177 continue;
8178 }
8179
8180 lastlog = 0;
8181 }
8182 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
8183 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8184
8185 /* next, ensure no spare or cache devices are part of the split */
8186 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
8187 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
8188 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
8189
79c76d5b
BB
8190 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
8191 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
428870ff
BB
8192
8193 /* then, loop over each vdev and validate it */
8194 for (c = 0; c < children; c++) {
8195 uint64_t is_hole = 0;
8196
8197 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
8198 &is_hole);
8199
8200 if (is_hole != 0) {
8201 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
8202 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
8203 continue;
8204 } else {
2e528b49 8205 error = SET_ERROR(EINVAL);
428870ff
BB
8206 break;
8207 }
8208 }
8209
1b664952
GA
8210 /* deal with indirect vdevs */
8211 if (spa->spa_root_vdev->vdev_child[c]->vdev_ops ==
8212 &vdev_indirect_ops)
8213 continue;
8214
428870ff
BB
8215 /* which disk is going to be split? */
8216 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
8217 &glist[c]) != 0) {
2e528b49 8218 error = SET_ERROR(EINVAL);
428870ff
BB
8219 break;
8220 }
8221
8222 /* look it up in the spa */
8223 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
8224 if (vml[c] == NULL) {
2e528b49 8225 error = SET_ERROR(ENODEV);
428870ff
BB
8226 break;
8227 }
8228
8229 /* make sure there's nothing stopping the split */
8230 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
8231 vml[c]->vdev_islog ||
a1d477c2 8232 !vdev_is_concrete(vml[c]) ||
428870ff
BB
8233 vml[c]->vdev_isspare ||
8234 vml[c]->vdev_isl2cache ||
8235 !vdev_writeable(vml[c]) ||
8236 vml[c]->vdev_children != 0 ||
8237 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
8238 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
2e528b49 8239 error = SET_ERROR(EINVAL);
428870ff
BB
8240 break;
8241 }
8242
733b5722
RS
8243 if (vdev_dtl_required(vml[c]) ||
8244 vdev_resilver_needed(vml[c], NULL, NULL)) {
2e528b49 8245 error = SET_ERROR(EBUSY);
428870ff
BB
8246 break;
8247 }
8248
8249 /* we need certain info from the top level */
65ad5d11
AJ
8250 fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
8251 vml[c]->vdev_top->vdev_ms_array);
8252 fnvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
8253 vml[c]->vdev_top->vdev_ms_shift);
8254 fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
8255 vml[c]->vdev_top->vdev_asize);
8256 fnvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
8257 vml[c]->vdev_top->vdev_ashift);
e0ab3ab5
JS
8258
8259 /* transfer per-vdev ZAPs */
8260 ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
8261 VERIFY0(nvlist_add_uint64(child[c],
8262 ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
8263
8264 ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
8265 VERIFY0(nvlist_add_uint64(child[c],
8266 ZPOOL_CONFIG_VDEV_TOP_ZAP,
8267 vml[c]->vdev_parent->vdev_top_zap));
428870ff
BB
8268 }
8269
8270 if (error != 0) {
8271 kmem_free(vml, children * sizeof (vdev_t *));
8272 kmem_free(glist, children * sizeof (uint64_t));
8273 return (spa_vdev_exit(spa, NULL, txg, error));
8274 }
8275
8276 /* stop writers from using the disks */
8277 for (c = 0; c < children; c++) {
8278 if (vml[c] != NULL)
8279 vml[c]->vdev_offline = B_TRUE;
8280 }
8281 vdev_reopen(spa->spa_root_vdev);
34dc7c2f
BB
8282
8283 /*
428870ff
BB
8284 * Temporarily record the splitting vdevs in the spa config. This
8285 * will disappear once the config is regenerated.
34dc7c2f 8286 */
65ad5d11
AJ
8287 nvl = fnvlist_alloc();
8288 fnvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, glist, children);
428870ff 8289 kmem_free(glist, children * sizeof (uint64_t));
34dc7c2f 8290
428870ff 8291 mutex_enter(&spa->spa_props_lock);
65ad5d11 8292 fnvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, nvl);
428870ff
BB
8293 mutex_exit(&spa->spa_props_lock);
8294 spa->spa_config_splitting = nvl;
8295 vdev_config_dirty(spa->spa_root_vdev);
8296
8297 /* configure and create the new pool */
65ad5d11
AJ
8298 fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname);
8299 fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
8300 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE);
8301 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
8302 fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, spa->spa_config_txg);
8303 fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
8304 spa_generate_guid(NULL));
e0ab3ab5 8305 VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
428870ff
BB
8306 (void) nvlist_lookup_string(props,
8307 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
34dc7c2f 8308
428870ff
BB
8309 /* add the new pool to the namespace */
8310 newspa = spa_add(newname, config, altroot);
e0ab3ab5 8311 newspa->spa_avz_action = AVZ_ACTION_REBUILD;
428870ff
BB
8312 newspa->spa_config_txg = spa->spa_config_txg;
8313 spa_set_log_state(newspa, SPA_LOG_CLEAR);
8314
8315 /* release the spa config lock, retaining the namespace lock */
8316 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
8317
8318 if (zio_injection_enabled)
8319 zio_handle_panic_injection(spa, FTAG, 1);
8320
8321 spa_activate(newspa, spa_mode_global);
8322 spa_async_suspend(newspa);
8323
c10d37dd 8324 /*
1b939560
BB
8325 * Temporarily stop the initializing and TRIM activity. We set the
8326 * state to ACTIVE so that we know to resume initializing or TRIM
8327 * once the split has completed.
c10d37dd 8328 */
1b939560
BB
8329 list_t vd_initialize_list;
8330 list_create(&vd_initialize_list, sizeof (vdev_t),
c10d37dd
GW
8331 offsetof(vdev_t, vdev_initialize_node));
8332
1b939560
BB
8333 list_t vd_trim_list;
8334 list_create(&vd_trim_list, sizeof (vdev_t),
8335 offsetof(vdev_t, vdev_trim_node));
8336
619f0976 8337 for (c = 0; c < children; c++) {
1b664952 8338 if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) {
619f0976 8339 mutex_enter(&vml[c]->vdev_initialize_lock);
1b939560
BB
8340 vdev_initialize_stop(vml[c],
8341 VDEV_INITIALIZE_ACTIVE, &vd_initialize_list);
619f0976 8342 mutex_exit(&vml[c]->vdev_initialize_lock);
1b939560
BB
8343
8344 mutex_enter(&vml[c]->vdev_trim_lock);
8345 vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list);
8346 mutex_exit(&vml[c]->vdev_trim_lock);
619f0976
GW
8347 }
8348 }
1b939560
BB
8349
8350 vdev_initialize_stop_wait(spa, &vd_initialize_list);
8351 vdev_trim_stop_wait(spa, &vd_trim_list);
8352
8353 list_destroy(&vd_initialize_list);
8354 list_destroy(&vd_trim_list);
619f0976 8355
6cb8e530 8356 newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT;
8b27e08e 8357 newspa->spa_is_splitting = B_TRUE;
6cb8e530 8358
428870ff 8359 /* create the new pool from the disks of the original pool */
6cb8e530 8360 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE);
428870ff
BB
8361 if (error)
8362 goto out;
8363
8364 /* if that worked, generate a real config for the new pool */
8365 if (newspa->spa_root_vdev != NULL) {
65ad5d11
AJ
8366 newspa->spa_config_splitting = fnvlist_alloc();
8367 fnvlist_add_uint64(newspa->spa_config_splitting,
8368 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa));
428870ff
BB
8369 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
8370 B_TRUE));
9babb374 8371 }
34dc7c2f 8372
428870ff
BB
8373 /* set the props */
8374 if (props != NULL) {
8375 spa_configfile_set(newspa, props, B_FALSE);
8376 error = spa_prop_set(newspa, props);
8377 if (error)
8378 goto out;
8379 }
34dc7c2f 8380
428870ff
BB
8381 /* flush everything */
8382 txg = spa_vdev_config_enter(newspa);
8383 vdev_config_dirty(newspa->spa_root_vdev);
8384 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
34dc7c2f 8385
428870ff
BB
8386 if (zio_injection_enabled)
8387 zio_handle_panic_injection(spa, FTAG, 2);
34dc7c2f 8388
428870ff 8389 spa_async_resume(newspa);
34dc7c2f 8390
428870ff
BB
8391 /* finally, update the original pool's config */
8392 txg = spa_vdev_config_enter(spa);
8393 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
8394 error = dmu_tx_assign(tx, TXG_WAIT);
8395 if (error != 0)
8396 dmu_tx_abort(tx);
8397 for (c = 0; c < children; c++) {
1b664952 8398 if (vml[c] != NULL && vml[c]->vdev_ops != &vdev_indirect_ops) {
234234ca
RS
8399 vdev_t *tvd = vml[c]->vdev_top;
8400
8401 /*
8402 * Need to be sure the detachable VDEV is not
8403 * on any *other* txg's DTL list to prevent it
8404 * from being accessed after it's freed.
8405 */
8406 for (int t = 0; t < TXG_SIZE; t++) {
8407 (void) txg_list_remove_this(
8408 &tvd->vdev_dtl_list, vml[c], t);
8409 }
8410
428870ff
BB
8411 vdev_split(vml[c]);
8412 if (error == 0)
6f1ffb06
MA
8413 spa_history_log_internal(spa, "detach", tx,
8414 "vdev=%s", vml[c]->vdev_path);
e0ab3ab5 8415
428870ff 8416 vdev_free(vml[c]);
34dc7c2f 8417 }
34dc7c2f 8418 }
e0ab3ab5 8419 spa->spa_avz_action = AVZ_ACTION_REBUILD;
428870ff
BB
8420 vdev_config_dirty(spa->spa_root_vdev);
8421 spa->spa_config_splitting = NULL;
8422 nvlist_free(nvl);
8423 if (error == 0)
8424 dmu_tx_commit(tx);
8425 (void) spa_vdev_exit(spa, NULL, txg, 0);
8426
8427 if (zio_injection_enabled)
8428 zio_handle_panic_injection(spa, FTAG, 3);
8429
8430 /* split is complete; log a history record */
6f1ffb06
MA
8431 spa_history_log_internal(newspa, "split", NULL,
8432 "from pool %s", spa_name(spa));
428870ff 8433
8b27e08e 8434 newspa->spa_is_splitting = B_FALSE;
428870ff
BB
8435 kmem_free(vml, children * sizeof (vdev_t *));
8436
8437 /* if we're not going to mount the filesystems in userland, export */
8438 if (exp)
8439 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
8440 B_FALSE, B_FALSE);
8441
8442 return (error);
8443
8444out:
8445 spa_unload(newspa);
8446 spa_deactivate(newspa);
8447 spa_remove(newspa);
8448
8449 txg = spa_vdev_config_enter(spa);
8450
8451 /* re-online all offlined disks */
8452 for (c = 0; c < children; c++) {
8453 if (vml[c] != NULL)
8454 vml[c]->vdev_offline = B_FALSE;
8455 }
619f0976 8456
1b939560 8457 /* restart initializing or trimming disks as necessary */
619f0976 8458 spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
1b939560
BB
8459 spa_async_request(spa, SPA_ASYNC_TRIM_RESTART);
8460 spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART);
619f0976 8461
428870ff
BB
8462 vdev_reopen(spa->spa_root_vdev);
8463
8464 nvlist_free(spa->spa_config_splitting);
8465 spa->spa_config_splitting = NULL;
8466 (void) spa_vdev_exit(spa, NULL, txg, error);
34dc7c2f 8467
428870ff 8468 kmem_free(vml, children * sizeof (vdev_t *));
34dc7c2f
BB
8469 return (error);
8470}
8471
34dc7c2f
BB
8472/*
8473 * Find any device that's done replacing, or a vdev marked 'unspare' that's
d3cc8b15 8474 * currently spared, so we can detach it.
34dc7c2f
BB
8475 */
8476static vdev_t *
8477spa_vdev_resilver_done_hunt(vdev_t *vd)
8478{
8479 vdev_t *newvd, *oldvd;
34dc7c2f 8480
1c27024e 8481 for (int c = 0; c < vd->vdev_children; c++) {
34dc7c2f
BB
8482 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
8483 if (oldvd != NULL)
8484 return (oldvd);
8485 }
8486
8487 /*
572e2857
BB
8488 * Check for a completed replacement. We always consider the first
8489 * vdev in the list to be the oldest vdev, and the last one to be
8490 * the newest (see spa_vdev_attach() for how that works). In
8491 * the case where the newest vdev is faulted, we will not automatically
8492 * remove it after a resilver completes. This is OK as it will require
8493 * user intervention to determine which disk the admin wishes to keep.
34dc7c2f 8494 */
572e2857
BB
8495 if (vd->vdev_ops == &vdev_replacing_ops) {
8496 ASSERT(vd->vdev_children > 1);
8497
8498 newvd = vd->vdev_child[vd->vdev_children - 1];
34dc7c2f 8499 oldvd = vd->vdev_child[0];
34dc7c2f 8500
fb5f0bc8 8501 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 8502 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
fb5f0bc8 8503 !vdev_dtl_required(oldvd))
34dc7c2f 8504 return (oldvd);
34dc7c2f
BB
8505 }
8506
8507 /*
8508 * Check for a completed resilver with the 'unspare' flag set.
f65fbee1 8509 * Also potentially update faulted state.
34dc7c2f 8510 */
572e2857
BB
8511 if (vd->vdev_ops == &vdev_spare_ops) {
8512 vdev_t *first = vd->vdev_child[0];
8513 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
8514
8515 if (last->vdev_unspare) {
8516 oldvd = first;
8517 newvd = last;
8518 } else if (first->vdev_unspare) {
8519 oldvd = last;
8520 newvd = first;
8521 } else {
8522 oldvd = NULL;
8523 }
34dc7c2f 8524
572e2857 8525 if (oldvd != NULL &&
fb5f0bc8 8526 vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 8527 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
572e2857 8528 !vdev_dtl_required(oldvd))
34dc7c2f 8529 return (oldvd);
572e2857 8530
f65fbee1
JJ
8531 vdev_propagate_state(vd);
8532
572e2857
BB
8533 /*
8534 * If there are more than two spares attached to a disk,
8535 * and those spares are not required, then we want to
8536 * attempt to free them up now so that they can be used
8537 * by other pools. Once we're back down to a single
8538 * disk+spare, we stop removing them.
8539 */
8540 if (vd->vdev_children > 2) {
8541 newvd = vd->vdev_child[1];
8542
8543 if (newvd->vdev_isspare && last->vdev_isspare &&
8544 vdev_dtl_empty(last, DTL_MISSING) &&
8545 vdev_dtl_empty(last, DTL_OUTAGE) &&
8546 !vdev_dtl_required(newvd))
8547 return (newvd);
34dc7c2f 8548 }
34dc7c2f
BB
8549 }
8550
8551 return (NULL);
8552}
8553
8554static void
8555spa_vdev_resilver_done(spa_t *spa)
8556{
fb5f0bc8
BB
8557 vdev_t *vd, *pvd, *ppvd;
8558 uint64_t guid, sguid, pguid, ppguid;
34dc7c2f 8559
fb5f0bc8 8560 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
8561
8562 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
fb5f0bc8
BB
8563 pvd = vd->vdev_parent;
8564 ppvd = pvd->vdev_parent;
34dc7c2f 8565 guid = vd->vdev_guid;
fb5f0bc8
BB
8566 pguid = pvd->vdev_guid;
8567 ppguid = ppvd->vdev_guid;
8568 sguid = 0;
34dc7c2f
BB
8569 /*
8570 * If we have just finished replacing a hot spared device, then
8571 * we need to detach the parent's first child (the original hot
8572 * spare) as well.
8573 */
572e2857
BB
8574 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
8575 ppvd->vdev_children == 2) {
34dc7c2f 8576 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
fb5f0bc8 8577 sguid = ppvd->vdev_child[1]->vdev_guid;
34dc7c2f 8578 }
5d1f7fb6
GW
8579 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
8580
fb5f0bc8
BB
8581 spa_config_exit(spa, SCL_ALL, FTAG);
8582 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
34dc7c2f 8583 return;
fb5f0bc8 8584 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
34dc7c2f 8585 return;
fb5f0bc8 8586 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
8587 }
8588
fb5f0bc8 8589 spa_config_exit(spa, SCL_ALL, FTAG);
9a49d3f3
BB
8590
8591 /*
8592 * If a detach was not performed above replace waiters will not have
8593 * been notified. In which case we must do so now.
8594 */
8595 spa_notify_waiters(spa);
34dc7c2f
BB
8596}
8597
8598/*
428870ff 8599 * Update the stored path or FRU for this vdev.
34dc7c2f 8600 */
65c7cc49 8601static int
9babb374
BB
8602spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
8603 boolean_t ispath)
34dc7c2f 8604{
b128c09f 8605 vdev_t *vd;
428870ff 8606 boolean_t sync = B_FALSE;
34dc7c2f 8607
572e2857
BB
8608 ASSERT(spa_writeable(spa));
8609
428870ff 8610 spa_vdev_state_enter(spa, SCL_ALL);
34dc7c2f 8611
9babb374 8612 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
428870ff 8613 return (spa_vdev_state_exit(spa, NULL, ENOENT));
34dc7c2f
BB
8614
8615 if (!vd->vdev_ops->vdev_op_leaf)
428870ff 8616 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
34dc7c2f 8617
9babb374 8618 if (ispath) {
428870ff
BB
8619 if (strcmp(value, vd->vdev_path) != 0) {
8620 spa_strfree(vd->vdev_path);
8621 vd->vdev_path = spa_strdup(value);
8622 sync = B_TRUE;
8623 }
9babb374 8624 } else {
428870ff
BB
8625 if (vd->vdev_fru == NULL) {
8626 vd->vdev_fru = spa_strdup(value);
8627 sync = B_TRUE;
8628 } else if (strcmp(value, vd->vdev_fru) != 0) {
9babb374 8629 spa_strfree(vd->vdev_fru);
428870ff
BB
8630 vd->vdev_fru = spa_strdup(value);
8631 sync = B_TRUE;
8632 }
9babb374 8633 }
34dc7c2f 8634
428870ff 8635 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
34dc7c2f
BB
8636}
8637
9babb374
BB
8638int
8639spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
8640{
8641 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
8642}
8643
8644int
8645spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
8646{
8647 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
8648}
8649
34dc7c2f
BB
8650/*
8651 * ==========================================================================
428870ff 8652 * SPA Scanning
34dc7c2f
BB
8653 * ==========================================================================
8654 */
0ea05c64
AP
8655int
8656spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd)
8657{
8658 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
8659
8660 if (dsl_scan_resilvering(spa->spa_dsl_pool))
8661 return (SET_ERROR(EBUSY));
8662
8663 return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd));
8664}
34dc7c2f 8665
34dc7c2f 8666int
428870ff
BB
8667spa_scan_stop(spa_t *spa)
8668{
8669 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
8670 if (dsl_scan_resilvering(spa->spa_dsl_pool))
2e528b49 8671 return (SET_ERROR(EBUSY));
482eeef8 8672
428870ff
BB
8673 return (dsl_scan_cancel(spa->spa_dsl_pool));
8674}
8675
8676int
8677spa_scan(spa_t *spa, pool_scan_func_t func)
34dc7c2f 8678{
b128c09f 8679 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
34dc7c2f 8680
428870ff 8681 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
2e528b49 8682 return (SET_ERROR(ENOTSUP));
34dc7c2f 8683
fa241660
TC
8684 if (func == POOL_SCAN_RESILVER &&
8685 !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER))
8686 return (SET_ERROR(ENOTSUP));
8687
34dc7c2f 8688 /*
b128c09f
BB
8689 * If a resilver was requested, but there is no DTL on a
8690 * writeable leaf device, we have nothing to do.
34dc7c2f 8691 */
428870ff 8692 if (func == POOL_SCAN_RESILVER &&
b128c09f
BB
8693 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
8694 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34dc7c2f
BB
8695 return (0);
8696 }
8697
482eeef8
GA
8698 if (func == POOL_SCAN_ERRORSCRUB &&
8699 !spa_feature_is_enabled(spa, SPA_FEATURE_HEAD_ERRLOG))
8700 return (SET_ERROR(ENOTSUP));
8701
428870ff 8702 return (dsl_scan(spa->spa_dsl_pool, func));
34dc7c2f
BB
8703}
8704
8705/*
8706 * ==========================================================================
8707 * SPA async task processing
8708 * ==========================================================================
8709 */
8710
8711static void
8712spa_async_remove(spa_t *spa, vdev_t *vd)
8713{
b128c09f 8714 if (vd->vdev_remove_wanted) {
428870ff
BB
8715 vd->vdev_remove_wanted = B_FALSE;
8716 vd->vdev_delayed_close = B_FALSE;
b128c09f 8717 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
428870ff
BB
8718
8719 /*
8720 * We want to clear the stats, but we don't want to do a full
8721 * vdev_clear() as that will cause us to throw away
8722 * degraded/faulted state as well as attempt to reopen the
8723 * device, all of which is a waste.
8724 */
8725 vd->vdev_stat.vs_read_errors = 0;
8726 vd->vdev_stat.vs_write_errors = 0;
8727 vd->vdev_stat.vs_checksum_errors = 0;
8728
b128c09f 8729 vdev_state_dirty(vd->vdev_top);
0aacde2e
RM
8730
8731 /* Tell userspace that the vdev is gone. */
8732 zfs_post_remove(spa, vd);
b128c09f 8733 }
34dc7c2f 8734
1c27024e 8735 for (int c = 0; c < vd->vdev_children; c++)
b128c09f
BB
8736 spa_async_remove(spa, vd->vdev_child[c]);
8737}
8738
8739static void
8740spa_async_probe(spa_t *spa, vdev_t *vd)
8741{
8742 if (vd->vdev_probe_wanted) {
428870ff 8743 vd->vdev_probe_wanted = B_FALSE;
b128c09f 8744 vdev_reopen(vd); /* vdev_open() does the actual probe */
34dc7c2f 8745 }
b128c09f 8746
1c27024e 8747 for (int c = 0; c < vd->vdev_children; c++)
b128c09f 8748 spa_async_probe(spa, vd->vdev_child[c]);
34dc7c2f
BB
8749}
8750
9babb374
BB
8751static void
8752spa_async_autoexpand(spa_t *spa, vdev_t *vd)
8753{
9babb374
BB
8754 if (!spa->spa_autoexpand)
8755 return;
8756
1c27024e 8757 for (int c = 0; c < vd->vdev_children; c++) {
9babb374
BB
8758 vdev_t *cvd = vd->vdev_child[c];
8759 spa_async_autoexpand(spa, cvd);
8760 }
8761
8762 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
8763 return;
8764
12fa0466 8765 spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND);
9babb374
BB
8766}
8767
460748d4 8768static __attribute__((noreturn)) void
c25b8f99 8769spa_async_thread(void *arg)
34dc7c2f 8770{
c25b8f99 8771 spa_t *spa = (spa_t *)arg;
80a91e74 8772 dsl_pool_t *dp = spa->spa_dsl_pool;
867959b5 8773 int tasks;
34dc7c2f
BB
8774
8775 ASSERT(spa->spa_sync_on);
8776
8777 mutex_enter(&spa->spa_async_lock);
8778 tasks = spa->spa_async_tasks;
8779 spa->spa_async_tasks = 0;
8780 mutex_exit(&spa->spa_async_lock);
8781
8782 /*
8783 * See if the config needs to be updated.
8784 */
8785 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
428870ff 8786 uint64_t old_space, new_space;
9babb374 8787
34dc7c2f 8788 mutex_enter(&spa_namespace_lock);
428870ff 8789 old_space = metaslab_class_get_space(spa_normal_class(spa));
cc99f275
DB
8790 old_space += metaslab_class_get_space(spa_special_class(spa));
8791 old_space += metaslab_class_get_space(spa_dedup_class(spa));
aa755b35
MA
8792 old_space += metaslab_class_get_space(
8793 spa_embedded_log_class(spa));
cc99f275 8794
34dc7c2f 8795 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
cc99f275 8796
428870ff 8797 new_space = metaslab_class_get_space(spa_normal_class(spa));
cc99f275
DB
8798 new_space += metaslab_class_get_space(spa_special_class(spa));
8799 new_space += metaslab_class_get_space(spa_dedup_class(spa));
aa755b35
MA
8800 new_space += metaslab_class_get_space(
8801 spa_embedded_log_class(spa));
34dc7c2f 8802 mutex_exit(&spa_namespace_lock);
9babb374
BB
8803
8804 /*
8805 * If the pool grew as a result of the config update,
8806 * then log an internal history event.
8807 */
428870ff 8808 if (new_space != old_space) {
6f1ffb06 8809 spa_history_log_internal(spa, "vdev online", NULL,
45d1cae3 8810 "pool '%s' size: %llu(+%llu)",
74756182
MM
8811 spa_name(spa), (u_longlong_t)new_space,
8812 (u_longlong_t)(new_space - old_space));
9babb374 8813 }
34dc7c2f
BB
8814 }
8815
8816 /*
8817 * See if any devices need to be marked REMOVED.
34dc7c2f 8818 */
b128c09f 8819 if (tasks & SPA_ASYNC_REMOVE) {
428870ff 8820 spa_vdev_state_enter(spa, SCL_NONE);
34dc7c2f 8821 spa_async_remove(spa, spa->spa_root_vdev);
867959b5 8822 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
b128c09f 8823 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
867959b5 8824 for (int i = 0; i < spa->spa_spares.sav_count; i++)
b128c09f
BB
8825 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
8826 (void) spa_vdev_state_exit(spa, NULL, 0);
34dc7c2f
BB
8827 }
8828
9babb374
BB
8829 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
8830 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8831 spa_async_autoexpand(spa, spa->spa_root_vdev);
8832 spa_config_exit(spa, SCL_CONFIG, FTAG);
8833 }
8834
34dc7c2f 8835 /*
b128c09f 8836 * See if any devices need to be probed.
34dc7c2f 8837 */
b128c09f 8838 if (tasks & SPA_ASYNC_PROBE) {
428870ff 8839 spa_vdev_state_enter(spa, SCL_NONE);
b128c09f
BB
8840 spa_async_probe(spa, spa->spa_root_vdev);
8841 (void) spa_vdev_state_exit(spa, NULL, 0);
8842 }
34dc7c2f
BB
8843
8844 /*
b128c09f 8845 * If any devices are done replacing, detach them.
34dc7c2f 8846 */
b2255edc 8847 if (tasks & SPA_ASYNC_RESILVER_DONE ||
719534ca
AH
8848 tasks & SPA_ASYNC_REBUILD_DONE ||
8849 tasks & SPA_ASYNC_DETACH_SPARE) {
b128c09f 8850 spa_vdev_resilver_done(spa);
9a49d3f3
BB
8851 }
8852
34dc7c2f
BB
8853 /*
8854 * Kick off a resilver.
8855 */
80a91e74 8856 if (tasks & SPA_ASYNC_RESILVER &&
9a49d3f3 8857 !vdev_rebuild_active(spa->spa_root_vdev) &&
80a91e74
TC
8858 (!dsl_scan_resilvering(dp) ||
8859 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER)))
3c819a2c 8860 dsl_scan_restart_resilver(dp, 0);
34dc7c2f 8861
619f0976
GW
8862 if (tasks & SPA_ASYNC_INITIALIZE_RESTART) {
8863 mutex_enter(&spa_namespace_lock);
8864 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8865 vdev_initialize_restart(spa->spa_root_vdev);
8866 spa_config_exit(spa, SCL_CONFIG, FTAG);
8867 mutex_exit(&spa_namespace_lock);
8868 }
8869
1b939560
BB
8870 if (tasks & SPA_ASYNC_TRIM_RESTART) {
8871 mutex_enter(&spa_namespace_lock);
8872 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8873 vdev_trim_restart(spa->spa_root_vdev);
8874 spa_config_exit(spa, SCL_CONFIG, FTAG);
8875 mutex_exit(&spa_namespace_lock);
8876 }
8877
8878 if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) {
8879 mutex_enter(&spa_namespace_lock);
8880 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8881 vdev_autotrim_restart(spa);
8882 spa_config_exit(spa, SCL_CONFIG, FTAG);
8883 mutex_exit(&spa_namespace_lock);
8884 }
8885
b7654bd7
GA
8886 /*
8887 * Kick off L2 cache whole device TRIM.
8888 */
8889 if (tasks & SPA_ASYNC_L2CACHE_TRIM) {
8890 mutex_enter(&spa_namespace_lock);
8891 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
8892 vdev_trim_l2arc(spa);
8893 spa_config_exit(spa, SCL_CONFIG, FTAG);
8894 mutex_exit(&spa_namespace_lock);
8895 }
8896
77f6826b
GA
8897 /*
8898 * Kick off L2 cache rebuilding.
8899 */
8900 if (tasks & SPA_ASYNC_L2CACHE_REBUILD) {
8901 mutex_enter(&spa_namespace_lock);
8902 spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER);
8903 l2arc_spa_rebuild_start(spa);
8904 spa_config_exit(spa, SCL_L2ARC, FTAG);
8905 mutex_exit(&spa_namespace_lock);
8906 }
8907
34dc7c2f
BB
8908 /*
8909 * Let the world know that we're done.
8910 */
8911 mutex_enter(&spa->spa_async_lock);
8912 spa->spa_async_thread = NULL;
8913 cv_broadcast(&spa->spa_async_cv);
8914 mutex_exit(&spa->spa_async_lock);
8915 thread_exit();
8916}
8917
8918void
8919spa_async_suspend(spa_t *spa)
8920{
8921 mutex_enter(&spa->spa_async_lock);
8922 spa->spa_async_suspended++;
9d5b5245 8923 while (spa->spa_async_thread != NULL)
34dc7c2f
BB
8924 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
8925 mutex_exit(&spa->spa_async_lock);
a1d477c2
MA
8926
8927 spa_vdev_remove_suspend(spa);
9d5b5245
SD
8928
8929 zthr_t *condense_thread = spa->spa_condense_zthr;
61c3391a
SD
8930 if (condense_thread != NULL)
8931 zthr_cancel(condense_thread);
d2734cce 8932
5caeef02
DB
8933 zthr_t *raidz_expand_thread = spa->spa_raidz_expand_zthr;
8934 if (raidz_expand_thread != NULL)
8935 zthr_cancel(raidz_expand_thread);
8936
d2734cce 8937 zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
61c3391a
SD
8938 if (discard_thread != NULL)
8939 zthr_cancel(discard_thread);
37f03da8
SH
8940
8941 zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr;
8942 if (ll_delete_thread != NULL)
8943 zthr_cancel(ll_delete_thread);
8944
8945 zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
8946 if (ll_condense_thread != NULL)
8947 zthr_cancel(ll_condense_thread);
34dc7c2f
BB
8948}
8949
8950void
8951spa_async_resume(spa_t *spa)
8952{
8953 mutex_enter(&spa->spa_async_lock);
8954 ASSERT(spa->spa_async_suspended != 0);
8955 spa->spa_async_suspended--;
8956 mutex_exit(&spa->spa_async_lock);
a1d477c2 8957 spa_restart_removal(spa);
9d5b5245
SD
8958
8959 zthr_t *condense_thread = spa->spa_condense_zthr;
61c3391a 8960 if (condense_thread != NULL)
9d5b5245 8961 zthr_resume(condense_thread);
d2734cce 8962
5caeef02
DB
8963 zthr_t *raidz_expand_thread = spa->spa_raidz_expand_zthr;
8964 if (raidz_expand_thread != NULL)
8965 zthr_resume(raidz_expand_thread);
8966
d2734cce 8967 zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr;
61c3391a 8968 if (discard_thread != NULL)
d2734cce 8969 zthr_resume(discard_thread);
37f03da8
SH
8970
8971 zthr_t *ll_delete_thread = spa->spa_livelist_delete_zthr;
8972 if (ll_delete_thread != NULL)
8973 zthr_resume(ll_delete_thread);
8974
8975 zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
8976 if (ll_condense_thread != NULL)
8977 zthr_resume(ll_condense_thread);
34dc7c2f
BB
8978}
8979
e6cfd633
WA
8980static boolean_t
8981spa_async_tasks_pending(spa_t *spa)
8982{
8983 uint_t non_config_tasks;
8984 uint_t config_task;
8985 boolean_t config_task_suspended;
8986
8987 non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
8988 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
8989 if (spa->spa_ccw_fail_time == 0) {
8990 config_task_suspended = B_FALSE;
8991 } else {
8992 config_task_suspended =
8993 (gethrtime() - spa->spa_ccw_fail_time) <
05852b34 8994 ((hrtime_t)zfs_ccw_retry_interval * NANOSEC);
e6cfd633
WA
8995 }
8996
8997 return (non_config_tasks || (config_task && !config_task_suspended));
8998}
8999
34dc7c2f
BB
9000static void
9001spa_async_dispatch(spa_t *spa)
9002{
9003 mutex_enter(&spa->spa_async_lock);
e6cfd633
WA
9004 if (spa_async_tasks_pending(spa) &&
9005 !spa->spa_async_suspended &&
da92d5cb 9006 spa->spa_async_thread == NULL)
34dc7c2f
BB
9007 spa->spa_async_thread = thread_create(NULL, 0,
9008 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
9009 mutex_exit(&spa->spa_async_lock);
9010}
9011
9012void
9013spa_async_request(spa_t *spa, int task)
9014{
428870ff 9015 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
34dc7c2f
BB
9016 mutex_enter(&spa->spa_async_lock);
9017 spa->spa_async_tasks |= task;
9018 mutex_exit(&spa->spa_async_lock);
9019}
9020
3c819a2c
JP
9021int
9022spa_async_tasks(spa_t *spa)
9023{
9024 return (spa->spa_async_tasks);
9025}
9026
34dc7c2f
BB
9027/*
9028 * ==========================================================================
9029 * SPA syncing routines
9030 * ==========================================================================
9031 */
9032
37f03da8 9033
428870ff 9034static int
37f03da8
SH
9035bpobj_enqueue_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
9036 dmu_tx_t *tx)
34dc7c2f 9037{
428870ff 9038 bpobj_t *bpo = arg;
37f03da8 9039 bpobj_enqueue(bpo, bp, bp_freed, tx);
428870ff
BB
9040 return (0);
9041}
34dc7c2f 9042
37f03da8
SH
9043int
9044bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9045{
9046 return (bpobj_enqueue_cb(arg, bp, B_FALSE, tx));
9047}
9048
9049int
9050bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9051{
9052 return (bpobj_enqueue_cb(arg, bp, B_TRUE, tx));
9053}
9054
428870ff
BB
9055static int
9056spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
9057{
9cdf7b1f 9058 zio_t *pio = arg;
34dc7c2f 9059
9cdf7b1f
MA
9060 zio_nowait(zio_free_sync(pio, pio->io_spa, dmu_tx_get_txg(tx), bp,
9061 pio->io_flags));
428870ff 9062 return (0);
34dc7c2f
BB
9063}
9064
37f03da8
SH
9065static int
9066bpobj_spa_free_sync_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed,
9067 dmu_tx_t *tx)
9068{
9069 ASSERT(!bp_freed);
9070 return (spa_free_sync_cb(arg, bp, tx));
9071}
9072
e8b96c60
MA
9073/*
9074 * Note: this simple function is not inlined to make it easier to dtrace the
9075 * amount of time spent syncing frees.
9076 */
9077static void
9078spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
9079{
9080 zio_t *zio = zio_root(spa, NULL, NULL, 0);
9081 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
9082 VERIFY(zio_wait(zio) == 0);
9083}
9084
9085/*
9086 * Note: this simple function is not inlined to make it easier to dtrace the
9087 * amount of time spent syncing deferred frees.
9088 */
9089static void
9090spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
9091{
8dc2197b
SD
9092 if (spa_sync_pass(spa) != 1)
9093 return;
9094
93e28d66
SD
9095 /*
9096 * Note:
9097 * If the log space map feature is active, we stop deferring
9098 * frees to the next TXG and therefore running this function
9099 * would be considered a no-op as spa_deferred_bpobj should
9100 * not have any entries.
9101 *
9102 * That said we run this function anyway (instead of returning
9103 * immediately) for the edge-case scenario where we just
9104 * activated the log space map feature in this TXG but we have
9105 * deferred frees from the previous TXG.
9106 */
e8b96c60
MA
9107 zio_t *zio = zio_root(spa, NULL, NULL, 0);
9108 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
37f03da8 9109 bpobj_spa_free_sync_cb, zio, tx), ==, 0);
e8b96c60
MA
9110 VERIFY0(zio_wait(zio));
9111}
9112
34dc7c2f
BB
9113static void
9114spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
9115{
9116 char *packed = NULL;
b128c09f 9117 size_t bufsize;
34dc7c2f
BB
9118 size_t nvsize = 0;
9119 dmu_buf_t *db;
9120
9121 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
9122
b128c09f
BB
9123 /*
9124 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
b0bc7a84 9125 * information. This avoids the dmu_buf_will_dirty() path and
b128c09f
BB
9126 * saves us a pre-read to get data we don't actually care about.
9127 */
9ae529ec 9128 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
79c76d5b 9129 packed = vmem_alloc(bufsize, KM_SLEEP);
34dc7c2f
BB
9130
9131 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
79c76d5b 9132 KM_SLEEP) == 0);
861166b0 9133 memset(packed + nvsize, 0, bufsize - nvsize);
34dc7c2f 9134
b128c09f 9135 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
34dc7c2f 9136
00b46022 9137 vmem_free(packed, bufsize);
34dc7c2f
BB
9138
9139 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
9140 dmu_buf_will_dirty(db, tx);
9141 *(uint64_t *)db->db_data = nvsize;
9142 dmu_buf_rele(db, FTAG);
9143}
9144
9145static void
9146spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
9147 const char *config, const char *entry)
9148{
9149 nvlist_t *nvroot;
9150 nvlist_t **list;
9151 int i;
9152
9153 if (!sav->sav_sync)
9154 return;
9155
9156 /*
9157 * Update the MOS nvlist describing the list of available devices.
9158 * spa_validate_aux() will have already made sure this nvlist is
9159 * valid and the vdevs are labeled appropriately.
9160 */
9161 if (sav->sav_object == 0) {
9162 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
9163 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
9164 sizeof (uint64_t), tx);
9165 VERIFY(zap_update(spa->spa_meta_objset,
9166 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
9167 &sav->sav_object, tx) == 0);
9168 }
9169
65ad5d11 9170 nvroot = fnvlist_alloc();
34dc7c2f 9171 if (sav->sav_count == 0) {
795075e6
PD
9172 fnvlist_add_nvlist_array(nvroot, config,
9173 (const nvlist_t * const *)NULL, 0);
34dc7c2f 9174 } else {
79c76d5b 9175 list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP);
34dc7c2f
BB
9176 for (i = 0; i < sav->sav_count; i++)
9177 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
428870ff 9178 B_FALSE, VDEV_CONFIG_L2CACHE);
795075e6
PD
9179 fnvlist_add_nvlist_array(nvroot, config,
9180 (const nvlist_t * const *)list, sav->sav_count);
34dc7c2f
BB
9181 for (i = 0; i < sav->sav_count; i++)
9182 nvlist_free(list[i]);
9183 kmem_free(list, sav->sav_count * sizeof (void *));
9184 }
9185
9186 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
9187 nvlist_free(nvroot);
9188
9189 sav->sav_sync = B_FALSE;
9190}
9191
e0ab3ab5
JS
9192/*
9193 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
9194 * The all-vdev ZAP must be empty.
9195 */
9196static void
9197spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
9198{
9199 spa_t *spa = vd->vdev_spa;
e0ab3ab5 9200
3e4ed421
RW
9201 if (vd->vdev_root_zap != 0 &&
9202 spa_feature_is_active(spa, SPA_FEATURE_AVZ_V2)) {
9203 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9204 vd->vdev_root_zap, tx));
9205 }
e0ab3ab5
JS
9206 if (vd->vdev_top_zap != 0) {
9207 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9208 vd->vdev_top_zap, tx));
9209 }
9210 if (vd->vdev_leaf_zap != 0) {
9211 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
9212 vd->vdev_leaf_zap, tx));
9213 }
1c27024e 9214 for (uint64_t i = 0; i < vd->vdev_children; i++) {
e0ab3ab5
JS
9215 spa_avz_build(vd->vdev_child[i], avz, tx);
9216 }
9217}
9218
34dc7c2f
BB
9219static void
9220spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
9221{
9222 nvlist_t *config;
9223
e0ab3ab5
JS
9224 /*
9225 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
9226 * its config may not be dirty but we still need to build per-vdev ZAPs.
9227 * Similarly, if the pool is being assembled (e.g. after a split), we
9228 * need to rebuild the AVZ although the config may not be dirty.
9229 */
9230 if (list_is_empty(&spa->spa_config_dirty_list) &&
9231 spa->spa_avz_action == AVZ_ACTION_NONE)
34dc7c2f
BB
9232 return;
9233
b128c09f
BB
9234 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
9235
e0ab3ab5 9236 ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
38640550 9237 spa->spa_avz_action == AVZ_ACTION_INITIALIZE ||
e0ab3ab5
JS
9238 spa->spa_all_vdev_zaps != 0);
9239
9240 if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
e0ab3ab5
JS
9241 /* Make and build the new AVZ */
9242 uint64_t new_avz = zap_create(spa->spa_meta_objset,
9243 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
9244 spa_avz_build(spa->spa_root_vdev, new_avz, tx);
9245
9246 /* Diff old AVZ with new one */
1c27024e
DB
9247 zap_cursor_t zc;
9248 zap_attribute_t za;
9249
e0ab3ab5
JS
9250 for (zap_cursor_init(&zc, spa->spa_meta_objset,
9251 spa->spa_all_vdev_zaps);
9252 zap_cursor_retrieve(&zc, &za) == 0;
9253 zap_cursor_advance(&zc)) {
9254 uint64_t vdzap = za.za_first_integer;
9255 if (zap_lookup_int(spa->spa_meta_objset, new_avz,
9256 vdzap) == ENOENT) {
9257 /*
9258 * ZAP is listed in old AVZ but not in new one;
9259 * destroy it
9260 */
9261 VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
9262 tx));
9263 }
9264 }
9265
9266 zap_cursor_fini(&zc);
9267
9268 /* Destroy the old AVZ */
9269 VERIFY0(zap_destroy(spa->spa_meta_objset,
9270 spa->spa_all_vdev_zaps, tx));
9271
9272 /* Replace the old AVZ in the dir obj with the new one */
9273 VERIFY0(zap_update(spa->spa_meta_objset,
9274 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
9275 sizeof (new_avz), 1, &new_avz, tx));
9276
9277 spa->spa_all_vdev_zaps = new_avz;
9278 } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
9279 zap_cursor_t zc;
9280 zap_attribute_t za;
9281
9282 /* Walk through the AVZ and destroy all listed ZAPs */
9283 for (zap_cursor_init(&zc, spa->spa_meta_objset,
9284 spa->spa_all_vdev_zaps);
9285 zap_cursor_retrieve(&zc, &za) == 0;
9286 zap_cursor_advance(&zc)) {
9287 uint64_t zap = za.za_first_integer;
9288 VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
9289 }
9290
9291 zap_cursor_fini(&zc);
9292
9293 /* Destroy and unlink the AVZ itself */
9294 VERIFY0(zap_destroy(spa->spa_meta_objset,
9295 spa->spa_all_vdev_zaps, tx));
9296 VERIFY0(zap_remove(spa->spa_meta_objset,
9297 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
9298 spa->spa_all_vdev_zaps = 0;
9299 }
9300
9301 if (spa->spa_all_vdev_zaps == 0) {
9302 spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
9303 DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
9304 DMU_POOL_VDEV_ZAP_MAP, tx);
9305 }
9306 spa->spa_avz_action = AVZ_ACTION_NONE;
9307
9308 /* Create ZAPs for vdevs that don't have them. */
9309 vdev_construct_zaps(spa->spa_root_vdev, tx);
9310
b128c09f
BB
9311 config = spa_config_generate(spa, spa->spa_root_vdev,
9312 dmu_tx_get_txg(tx), B_FALSE);
9313
ea0b2538
GW
9314 /*
9315 * If we're upgrading the spa version then make sure that
9316 * the config object gets updated with the correct version.
9317 */
9318 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
9319 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
9320 spa->spa_uberblock.ub_version);
9321
b128c09f 9322 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f 9323
8a5fc748 9324 nvlist_free(spa->spa_config_syncing);
34dc7c2f
BB
9325 spa->spa_config_syncing = config;
9326
9327 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
9328}
9329
9ae529ec 9330static void
13fe0198 9331spa_sync_version(void *arg, dmu_tx_t *tx)
9ae529ec 9332{
13fe0198
MA
9333 uint64_t *versionp = arg;
9334 uint64_t version = *versionp;
9335 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
9ae529ec
CS
9336
9337 /*
9338 * Setting the version is special cased when first creating the pool.
9339 */
9340 ASSERT(tx->tx_txg != TXG_INITIAL);
9341
8dca0a9a 9342 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
9ae529ec
CS
9343 ASSERT(version >= spa_version(spa));
9344
9345 spa->spa_uberblock.ub_version = version;
9346 vdev_config_dirty(spa->spa_root_vdev);
74756182
MM
9347 spa_history_log_internal(spa, "set", tx, "version=%lld",
9348 (longlong_t)version);
9ae529ec
CS
9349}
9350
34dc7c2f
BB
9351/*
9352 * Set zpool properties.
9353 */
9354static void
13fe0198 9355spa_sync_props(void *arg, dmu_tx_t *tx)
34dc7c2f 9356{
13fe0198
MA
9357 nvlist_t *nvp = arg;
9358 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
34dc7c2f 9359 objset_t *mos = spa->spa_meta_objset;
9ae529ec 9360 nvpair_t *elem = NULL;
b128c09f
BB
9361
9362 mutex_enter(&spa->spa_props_lock);
34dc7c2f 9363
34dc7c2f 9364 while ((elem = nvlist_next_nvpair(nvp, elem))) {
9ae529ec 9365 uint64_t intval;
d1807f16 9366 const char *strval, *fname;
9ae529ec
CS
9367 zpool_prop_t prop;
9368 const char *propname;
8eae2d21 9369 const char *elemname = nvpair_name(elem);
9ae529ec 9370 zprop_type_t proptype;
fa86b5db 9371 spa_feature_t fid;
9ae529ec 9372
8eae2d21 9373 switch (prop = zpool_name_to_prop(elemname)) {
34dc7c2f 9374 case ZPOOL_PROP_VERSION:
93cf2076 9375 intval = fnvpair_value_uint64(elem);
34dc7c2f 9376 /*
4e33ba4c 9377 * The version is synced separately before other
9ae529ec 9378 * properties and should be correct by now.
34dc7c2f 9379 */
9ae529ec 9380 ASSERT3U(spa_version(spa), >=, intval);
34dc7c2f
BB
9381 break;
9382
9383 case ZPOOL_PROP_ALTROOT:
9384 /*
9385 * 'altroot' is a non-persistent property. It should
9386 * have been set temporarily at creation or import time.
9387 */
9388 ASSERT(spa->spa_root != NULL);
9389 break;
9390
572e2857 9391 case ZPOOL_PROP_READONLY:
34dc7c2f
BB
9392 case ZPOOL_PROP_CACHEFILE:
9393 /*
e1cfd73f 9394 * 'readonly' and 'cachefile' are also non-persistent
572e2857 9395 * properties.
34dc7c2f 9396 */
34dc7c2f 9397 break;
d96eb2b1 9398 case ZPOOL_PROP_COMMENT:
93cf2076 9399 strval = fnvpair_value_string(elem);
d96eb2b1
DM
9400 if (spa->spa_comment != NULL)
9401 spa_strfree(spa->spa_comment);
9402 spa->spa_comment = spa_strdup(strval);
9403 /*
9404 * We need to dirty the configuration on all the vdevs
88a48330
BB
9405 * so that their labels get updated. We also need to
9406 * update the cache file to keep it in sync with the
9407 * MOS version. It's unnecessary to do this for pool
9408 * creation since the vdev's configuration has already
9409 * been dirtied.
d96eb2b1 9410 */
88a48330 9411 if (tx->tx_txg != TXG_INITIAL) {
d96eb2b1 9412 vdev_config_dirty(spa->spa_root_vdev);
88a48330
BB
9413 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
9414 }
6f1ffb06 9415 spa_history_log_internal(spa, "set", tx,
8eae2d21 9416 "%s=%s", elemname, strval);
d96eb2b1 9417 break;
658fb802
CB
9418 case ZPOOL_PROP_COMPATIBILITY:
9419 strval = fnvpair_value_string(elem);
9420 if (spa->spa_compatibility != NULL)
9421 spa_strfree(spa->spa_compatibility);
9422 spa->spa_compatibility = spa_strdup(strval);
9423 /*
9424 * Dirty the configuration on vdevs as above.
9425 */
88a48330 9426 if (tx->tx_txg != TXG_INITIAL) {
658fb802 9427 vdev_config_dirty(spa->spa_root_vdev);
88a48330
BB
9428 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
9429 }
9430
658fb802
CB
9431 spa_history_log_internal(spa, "set", tx,
9432 "%s=%s", nvpair_name(elem), strval);
9433 break;
9434
8eae2d21
AJ
9435 case ZPOOL_PROP_INVAL:
9436 if (zpool_prop_feature(elemname)) {
9437 fname = strchr(elemname, '@') + 1;
9438 VERIFY0(zfeature_lookup_name(fname, &fid));
9439
9440 spa_feature_enable(spa, fid, tx);
9441 spa_history_log_internal(spa, "set", tx,
9442 "%s=enabled", elemname);
9443 break;
9444 } else if (!zfs_prop_user(elemname)) {
9445 ASSERT(zpool_prop_feature(elemname));
9446 break;
9447 }
9448 zfs_fallthrough;
34dc7c2f
BB
9449 default:
9450 /*
9451 * Set pool property values in the poolprops mos object.
9452 */
34dc7c2f 9453 if (spa->spa_pool_props_object == 0) {
9ae529ec
CS
9454 spa->spa_pool_props_object =
9455 zap_create_link(mos, DMU_OT_POOL_PROPS,
34dc7c2f 9456 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
9ae529ec 9457 tx);
34dc7c2f 9458 }
34dc7c2f
BB
9459
9460 /* normalize the property name */
ee7b71db 9461 if (prop == ZPOOL_PROP_INVAL) {
8eae2d21
AJ
9462 propname = elemname;
9463 proptype = PROP_TYPE_STRING;
ee7b71db
RY
9464 } else {
9465 propname = zpool_prop_to_name(prop);
9466 proptype = zpool_prop_get_type(prop);
8eae2d21 9467 }
34dc7c2f
BB
9468
9469 if (nvpair_type(elem) == DATA_TYPE_STRING) {
9470 ASSERT(proptype == PROP_TYPE_STRING);
93cf2076
GW
9471 strval = fnvpair_value_string(elem);
9472 VERIFY0(zap_update(mos,
34dc7c2f 9473 spa->spa_pool_props_object, propname,
93cf2076 9474 1, strlen(strval) + 1, strval, tx));
6f1ffb06 9475 spa_history_log_internal(spa, "set", tx,
8eae2d21 9476 "%s=%s", elemname, strval);
34dc7c2f 9477 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
93cf2076 9478 intval = fnvpair_value_uint64(elem);
34dc7c2f
BB
9479
9480 if (proptype == PROP_TYPE_INDEX) {
9481 const char *unused;
93cf2076
GW
9482 VERIFY0(zpool_prop_index_to_string(
9483 prop, intval, &unused));
34dc7c2f 9484 }
93cf2076 9485 VERIFY0(zap_update(mos,
34dc7c2f 9486 spa->spa_pool_props_object, propname,
93cf2076 9487 8, 1, &intval, tx));
6f1ffb06 9488 spa_history_log_internal(spa, "set", tx,
8eae2d21 9489 "%s=%lld", elemname,
74756182 9490 (longlong_t)intval);
34dc7c2f 9491
44f71818
RY
9492 switch (prop) {
9493 case ZPOOL_PROP_DELEGATION:
9494 spa->spa_delegation = intval;
9495 break;
9496 case ZPOOL_PROP_BOOTFS:
9497 spa->spa_bootfs = intval;
9498 break;
9499 case ZPOOL_PROP_FAILUREMODE:
9500 spa->spa_failmode = intval;
9501 break;
9502 case ZPOOL_PROP_AUTOTRIM:
9503 spa->spa_autotrim = intval;
428870ff 9504 spa_async_request(spa,
44f71818
RY
9505 SPA_ASYNC_AUTOTRIM_RESTART);
9506 break;
9507 case ZPOOL_PROP_AUTOEXPAND:
9508 spa->spa_autoexpand = intval;
9509 if (tx->tx_txg != TXG_INITIAL)
9510 spa_async_request(spa,
9511 SPA_ASYNC_AUTOEXPAND);
9512 break;
9513 case ZPOOL_PROP_MULTIHOST:
9514 spa->spa_multihost = intval;
9515 break;
9516 default:
9517 break;
9518 }
9519 } else {
9520 ASSERT(0); /* not allowed */
34dc7c2f
BB
9521 }
9522 }
9523
34dc7c2f 9524 }
b128c09f
BB
9525
9526 mutex_exit(&spa->spa_props_lock);
34dc7c2f
BB
9527}
9528
428870ff
BB
9529/*
9530 * Perform one-time upgrade on-disk changes. spa_version() does not
9531 * reflect the new version this txg, so there must be no changes this
9532 * txg to anything that the upgrade code depends on after it executes.
9533 * Therefore this must be called after dsl_pool_sync() does the sync
9534 * tasks.
9535 */
9536static void
9537spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
9538{
8dc2197b
SD
9539 if (spa_sync_pass(spa) != 1)
9540 return;
428870ff 9541
8dc2197b 9542 dsl_pool_t *dp = spa->spa_dsl_pool;
13fe0198
MA
9543 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
9544
428870ff
BB
9545 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
9546 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
9547 dsl_pool_create_origin(dp, tx);
9548
9549 /* Keeping the origin open increases spa_minref */
9550 spa->spa_minref += 3;
9551 }
9552
9553 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
9554 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
9555 dsl_pool_upgrade_clones(dp, tx);
9556 }
9557
9558 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
9559 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
9560 dsl_pool_upgrade_dir_clones(dp, tx);
9561
9562 /* Keeping the freedir open increases spa_minref */
9563 spa->spa_minref += 3;
9564 }
9ae529ec
CS
9565
9566 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
9567 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
9568 spa_feature_create_zap_objects(spa, tx);
9569 }
62bdd5eb
DL
9570
9571 /*
9572 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9573 * when possibility to use lz4 compression for metadata was added
9574 * Old pools that have this feature enabled must be upgraded to have
9575 * this feature active
9576 */
9577 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
9578 boolean_t lz4_en = spa_feature_is_enabled(spa,
9579 SPA_FEATURE_LZ4_COMPRESS);
9580 boolean_t lz4_ac = spa_feature_is_active(spa,
9581 SPA_FEATURE_LZ4_COMPRESS);
9582
9583 if (lz4_en && !lz4_ac)
9584 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
9585 }
3c67d83a
TH
9586
9587 /*
9588 * If we haven't written the salt, do so now. Note that the
9589 * feature may not be activated yet, but that's fine since
9590 * the presence of this ZAP entry is backwards compatible.
9591 */
9592 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
9593 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
9594 VERIFY0(zap_add(spa->spa_meta_objset,
9595 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
9596 sizeof (spa->spa_cksum_salt.zcs_bytes),
9597 spa->spa_cksum_salt.zcs_bytes, tx));
9598 }
9599
13fe0198 9600 rrw_exit(&dp->dp_config_rwlock, FTAG);
428870ff
BB
9601}
9602
a1d477c2
MA
9603static void
9604vdev_indirect_state_sync_verify(vdev_t *vd)
9605{
2a8ba608
MM
9606 vdev_indirect_mapping_t *vim __maybe_unused = vd->vdev_indirect_mapping;
9607 vdev_indirect_births_t *vib __maybe_unused = vd->vdev_indirect_births;
a1d477c2
MA
9608
9609 if (vd->vdev_ops == &vdev_indirect_ops) {
9610 ASSERT(vim != NULL);
9611 ASSERT(vib != NULL);
9612 }
9613
27f80e85
BB
9614 uint64_t obsolete_sm_object = 0;
9615 ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object));
9616 if (obsolete_sm_object != 0) {
a1d477c2
MA
9617 ASSERT(vd->vdev_obsolete_sm != NULL);
9618 ASSERT(vd->vdev_removing ||
9619 vd->vdev_ops == &vdev_indirect_ops);
9620 ASSERT(vdev_indirect_mapping_num_entries(vim) > 0);
9621 ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0);
27f80e85 9622 ASSERT3U(obsolete_sm_object, ==,
a1d477c2
MA
9623 space_map_object(vd->vdev_obsolete_sm));
9624 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=,
9625 space_map_allocated(vd->vdev_obsolete_sm));
9626 }
9627 ASSERT(vd->vdev_obsolete_segments != NULL);
9628
9629 /*
9630 * Since frees / remaps to an indirect vdev can only
9631 * happen in syncing context, the obsolete segments
9632 * tree must be empty when we start syncing.
9633 */
9634 ASSERT0(range_tree_space(vd->vdev_obsolete_segments));
9635}
9636
34dc7c2f 9637/*
8dc2197b
SD
9638 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9639 * async write queue depth in case it changed. The max queue depth will
9640 * not change in the middle of syncing out this txg.
34dc7c2f 9641 */
8dc2197b
SD
9642static void
9643spa_sync_adjust_vdev_max_queue_depth(spa_t *spa)
34dc7c2f 9644{
8dc2197b
SD
9645 ASSERT(spa_writeable(spa));
9646
34dc7c2f 9647 vdev_t *rvd = spa->spa_root_vdev;
3dfb57a3
DB
9648 uint32_t max_queue_depth = zfs_vdev_async_write_max_active *
9649 zfs_vdev_queue_depth_pct / 100;
8dc2197b
SD
9650 metaslab_class_t *normal = spa_normal_class(spa);
9651 metaslab_class_t *special = spa_special_class(spa);
9652 metaslab_class_t *dedup = spa_dedup_class(spa);
34dc7c2f 9653
492f64e9 9654 uint64_t slots_per_allocator = 0;
1c27024e 9655 for (int c = 0; c < rvd->vdev_children; c++) {
3dfb57a3 9656 vdev_t *tvd = rvd->vdev_child[c];
cc99f275 9657
8dc2197b 9658 metaslab_group_t *mg = tvd->vdev_mg;
cc99f275
DB
9659 if (mg == NULL || !metaslab_group_initialized(mg))
9660 continue;
3dfb57a3 9661
8dc2197b 9662 metaslab_class_t *mc = mg->mg_class;
cc99f275 9663 if (mc != normal && mc != special && mc != dedup)
3dfb57a3
DB
9664 continue;
9665
9666 /*
9667 * It is safe to do a lock-free check here because only async
9668 * allocations look at mg_max_alloc_queue_depth, and async
9669 * allocations all happen from spa_sync().
9670 */
32d805c3 9671 for (int i = 0; i < mg->mg_allocators; i++) {
424fd7c3 9672 ASSERT0(zfs_refcount_count(
32d805c3
MA
9673 &(mg->mg_allocator[i].mga_alloc_queue_depth)));
9674 }
3dfb57a3 9675 mg->mg_max_alloc_queue_depth = max_queue_depth;
492f64e9 9676
32d805c3
MA
9677 for (int i = 0; i < mg->mg_allocators; i++) {
9678 mg->mg_allocator[i].mga_cur_max_alloc_queue_depth =
492f64e9
PD
9679 zfs_vdev_def_queue_depth;
9680 }
9681 slots_per_allocator += zfs_vdev_def_queue_depth;
3dfb57a3 9682 }
cc99f275 9683
492f64e9 9684 for (int i = 0; i < spa->spa_alloc_count; i++) {
f8020c93
AM
9685 ASSERT0(zfs_refcount_count(&normal->mc_allocator[i].
9686 mca_alloc_slots));
9687 ASSERT0(zfs_refcount_count(&special->mc_allocator[i].
9688 mca_alloc_slots));
9689 ASSERT0(zfs_refcount_count(&dedup->mc_allocator[i].
9690 mca_alloc_slots));
9691 normal->mc_allocator[i].mca_alloc_max_slots =
9692 slots_per_allocator;
9693 special->mc_allocator[i].mca_alloc_max_slots =
9694 slots_per_allocator;
9695 dedup->mc_allocator[i].mca_alloc_max_slots =
9696 slots_per_allocator;
cc99f275
DB
9697 }
9698 normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
9699 special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
9700 dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
8dc2197b
SD
9701}
9702
9703static void
9704spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx)
9705{
9706 ASSERT(spa_writeable(spa));
3dfb57a3 9707
8dc2197b 9708 vdev_t *rvd = spa->spa_root_vdev;
a1d477c2
MA
9709 for (int c = 0; c < rvd->vdev_children; c++) {
9710 vdev_t *vd = rvd->vdev_child[c];
9711 vdev_indirect_state_sync_verify(vd);
9712
9713 if (vdev_indirect_should_condense(vd)) {
9714 spa_condense_indirect_start_sync(vd, tx);
9715 break;
9716 }
9717 }
8dc2197b
SD
9718}
9719
9720static void
9721spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx)
9722{
9723 objset_t *mos = spa->spa_meta_objset;
9724 dsl_pool_t *dp = spa->spa_dsl_pool;
9725 uint64_t txg = tx->tx_txg;
9726 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
a1d477c2 9727
34dc7c2f 9728 do {
428870ff 9729 int pass = ++spa->spa_sync_pass;
34dc7c2f
BB
9730
9731 spa_sync_config_object(spa, tx);
9732 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
9733 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
9734 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
9735 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
9736 spa_errlog_sync(spa, txg);
9737 dsl_pool_sync(dp, txg);
9738
93e28d66
SD
9739 if (pass < zfs_sync_pass_deferred_free ||
9740 spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
9741 /*
9742 * If the log space map feature is active we don't
9743 * care about deferred frees and the deferred bpobj
9744 * as the log space map should effectively have the
9745 * same results (i.e. appending only to one object).
9746 */
e8b96c60 9747 spa_sync_frees(spa, free_bpl, tx);
428870ff 9748 } else {
905edb40
MA
9749 /*
9750 * We can not defer frees in pass 1, because
9751 * we sync the deferred frees later in pass 1.
9752 */
9753 ASSERT3U(pass, >, 1);
37f03da8 9754 bplist_iterate(free_bpl, bpobj_enqueue_alloc_cb,
e8b96c60 9755 &spa->spa_deferred_bpobj, tx);
34dc7c2f
BB
9756 }
9757
67a1b037 9758 brt_sync(spa, txg);
428870ff
BB
9759 ddt_sync(spa, txg);
9760 dsl_scan_sync(dp, tx);
482eeef8 9761 dsl_errorscrub_sync(dp, tx);
8dc2197b
SD
9762 svr_sync(spa, tx);
9763 spa_sync_upgrades(spa, tx);
34dc7c2f 9764
93e28d66
SD
9765 spa_flush_metaslabs(spa, tx);
9766
8dc2197b 9767 vdev_t *vd = NULL;
a1d477c2
MA
9768 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
9769 != NULL)
428870ff
BB
9770 vdev_sync(vd, txg);
9771
5caeef02
DB
9772 if (pass == 1) {
9773 /*
9774 * dsl_pool_sync() -> dp_sync_tasks may have dirtied
9775 * the config. If that happens, this txg should not
9776 * be a no-op. So we must sync the config to the MOS
9777 * before checking for no-op.
9778 *
9779 * Note that when the config is dirty, it will
9780 * be written to the MOS (i.e. the MOS will be
9781 * dirtied) every time we call spa_sync_config_object()
9782 * in this txg. Therefore we can't call this after
9783 * dsl_pool_sync() every pass, because it would
9784 * prevent us from converging, since we'd dirty
9785 * the MOS every pass.
9786 *
9787 * Sync tasks can only be processed in pass 1, so
9788 * there's no need to do this in later passes.
9789 */
9790 spa_sync_config_object(spa, tx);
9791 }
9792
8dc2197b
SD
9793 /*
9794 * Note: We need to check if the MOS is dirty because we could
9795 * have marked the MOS dirty without updating the uberblock
9796 * (e.g. if we have sync tasks but no dirty user data). We need
9797 * to check the uberblock's rootbp because it is updated if we
9798 * have synced out dirty data (though in this case the MOS will
9799 * most likely also be dirty due to second order effects, we
9800 * don't want to rely on that here).
9801 */
9802 if (pass == 1 &&
9803 spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
9804 !dmu_objset_is_dirty(mos, txg)) {
905edb40 9805 /*
8dc2197b
SD
9806 * Nothing changed on the first pass, therefore this
9807 * TXG is a no-op. Avoid syncing deferred frees, so
9808 * that we can keep this TXG as a no-op.
905edb40 9809 */
8dc2197b
SD
9810 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
9811 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
9812 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
9813 ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg));
9814 break;
905edb40 9815 }
34dc7c2f 9816
8dc2197b 9817 spa_sync_deferred_frees(spa, tx);
428870ff 9818 } while (dmu_objset_is_dirty(mos, txg));
8dc2197b 9819}
34dc7c2f 9820
8dc2197b
SD
9821/*
9822 * Rewrite the vdev configuration (which includes the uberblock) to
9823 * commit the transaction group.
9824 *
9825 * If there are no dirty vdevs, we sync the uberblock to a few random
9826 * top-level vdevs that are known to be visible in the config cache
9827 * (see spa_vdev_add() for a complete description). If there *are* dirty
9828 * vdevs, sync the uberblock to all vdevs.
9829 */
9830static void
9831spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx)
9832{
9833 vdev_t *rvd = spa->spa_root_vdev;
9834 uint64_t txg = tx->tx_txg;
a1d477c2 9835
b128c09f 9836 for (;;) {
8dc2197b
SD
9837 int error = 0;
9838
b128c09f
BB
9839 /*
9840 * We hold SCL_STATE to prevent vdev open/close/etc.
9841 * while we're attempting to write the vdev labels.
9842 */
9843 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
9844
9845 if (list_is_empty(&spa->spa_config_dirty_list)) {
d2734cce 9846 vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL };
b128c09f
BB
9847 int svdcount = 0;
9848 int children = rvd->vdev_children;
29274c9f 9849 int c0 = random_in_range(children);
b128c09f 9850
1c27024e 9851 for (int c = 0; c < children; c++) {
8dc2197b
SD
9852 vdev_t *vd =
9853 rvd->vdev_child[(c0 + c) % children];
d2734cce
SD
9854
9855 /* Stop when revisiting the first vdev */
9856 if (c > 0 && svd[0] == vd)
9857 break;
9858
8dc2197b
SD
9859 if (vd->vdev_ms_array == 0 ||
9860 vd->vdev_islog ||
a1d477c2 9861 !vdev_is_concrete(vd))
b128c09f 9862 continue;
d2734cce 9863
b128c09f 9864 svd[svdcount++] = vd;
6cb8e530 9865 if (svdcount == SPA_SYNC_MIN_VDEVS)
b128c09f
BB
9866 break;
9867 }
b6fcb792 9868 error = vdev_config_sync(svd, svdcount, txg);
b128c09f
BB
9869 } else {
9870 error = vdev_config_sync(rvd->vdev_child,
b6fcb792 9871 rvd->vdev_children, txg);
34dc7c2f 9872 }
34dc7c2f 9873
3bc7e0fb
GW
9874 if (error == 0)
9875 spa->spa_last_synced_guid = rvd->vdev_guid;
9876
b128c09f
BB
9877 spa_config_exit(spa, SCL_STATE, FTAG);
9878
9879 if (error == 0)
9880 break;
cec3a0a1 9881 zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR);
b128c09f
BB
9882 zio_resume_wait(spa);
9883 }
8dc2197b
SD
9884}
9885
9886/*
9887 * Sync the specified transaction group. New blocks may be dirtied as
9888 * part of the process, so we iterate until it converges.
9889 */
9890void
9891spa_sync(spa_t *spa, uint64_t txg)
9892{
9893 vdev_t *vd = NULL;
9894
9895 VERIFY(spa_writeable(spa));
9896
9897 /*
9898 * Wait for i/os issued in open context that need to complete
9899 * before this txg syncs.
9900 */
9901 (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]);
9902 spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL,
9903 ZIO_FLAG_CANFAIL);
9904
67a1b037
PJD
9905 /*
9906 * Now that there can be no more cloning in this transaction group,
9907 * but we are still before issuing frees, we can process pending BRT
9908 * updates.
9909 */
9910 brt_pending_apply(spa, txg);
9911
8dc2197b
SD
9912 /*
9913 * Lock out configuration changes.
9914 */
9915 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
9916
9917 spa->spa_syncing_txg = txg;
9918 spa->spa_sync_pass = 0;
9919
9920 for (int i = 0; i < spa->spa_alloc_count; i++) {
1b50749c
AM
9921 mutex_enter(&spa->spa_allocs[i].spaa_lock);
9922 VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree));
9923 mutex_exit(&spa->spa_allocs[i].spaa_lock);
8dc2197b
SD
9924 }
9925
9926 /*
9927 * If there are any pending vdev state changes, convert them
9928 * into config changes that go out with this transaction group.
9929 */
9930 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
9f08b6e3
RY
9931 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
9932 /* Avoid holding the write lock unless actually necessary */
9933 if (vd->vdev_aux == NULL) {
9934 vdev_state_clean(vd);
9935 vdev_config_dirty(vd);
9936 continue;
9937 }
8dc2197b
SD
9938 /*
9939 * We need the write lock here because, for aux vdevs,
9940 * calling vdev_config_dirty() modifies sav_config.
9941 * This is ugly and will become unnecessary when we
9942 * eliminate the aux vdev wart by integrating all vdevs
9943 * into the root vdev tree.
9944 */
9945 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
9946 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
9947 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
9948 vdev_state_clean(vd);
9949 vdev_config_dirty(vd);
9950 }
9951 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
9952 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
9953 }
9954 spa_config_exit(spa, SCL_STATE, FTAG);
9955
9956 dsl_pool_t *dp = spa->spa_dsl_pool;
9957 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
9958
9959 spa->spa_sync_starttime = gethrtime();
9960 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
9961 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
9962 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
9963 NSEC_TO_TICK(spa->spa_deadman_synctime));
9964
9965 /*
9966 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9967 * set spa_deflate if we have no raid-z vdevs.
9968 */
9969 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
9970 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
9971 vdev_t *rvd = spa->spa_root_vdev;
9972
9973 int i;
9974 for (i = 0; i < rvd->vdev_children; i++) {
9975 vd = rvd->vdev_child[i];
9976 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
9977 break;
9978 }
9979 if (i == rvd->vdev_children) {
9980 spa->spa_deflate = TRUE;
9981 VERIFY0(zap_add(spa->spa_meta_objset,
9982 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
9983 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
9984 }
9985 }
9986
9987 spa_sync_adjust_vdev_max_queue_depth(spa);
9988
9989 spa_sync_condense_indirect(spa, tx);
9990
9991 spa_sync_iterate_to_convergence(spa, tx);
9992
9993#ifdef ZFS_DEBUG
9994 if (!list_is_empty(&spa->spa_config_dirty_list)) {
9995 /*
9996 * Make sure that the number of ZAPs for all the vdevs matches
9997 * the number of ZAPs in the per-vdev ZAP list. This only gets
9998 * called if the config is dirty; otherwise there may be
9999 * outstanding AVZ operations that weren't completed in
10000 * spa_sync_config_object.
10001 */
10002 uint64_t all_vdev_zap_entry_count;
10003 ASSERT0(zap_count(spa->spa_meta_objset,
10004 spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
10005 ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
10006 all_vdev_zap_entry_count);
10007 }
10008#endif
10009
10010 if (spa->spa_vdev_removal != NULL) {
10011 ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]);
10012 }
10013
10014 spa_sync_rewrite_vdev_config(spa, tx);
34dc7c2f
BB
10015 dmu_tx_commit(tx);
10016
57ddcda1 10017 taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid);
cc92e9d0
GW
10018 spa->spa_deadman_tqid = 0;
10019
34dc7c2f
BB
10020 /*
10021 * Clear the dirty config list.
10022 */
b128c09f 10023 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
34dc7c2f
BB
10024 vdev_config_clean(vd);
10025
10026 /*
10027 * Now that the new config has synced transactionally,
10028 * let it become visible to the config cache.
10029 */
10030 if (spa->spa_config_syncing != NULL) {
10031 spa_config_set(spa, spa->spa_config_syncing);
10032 spa->spa_config_txg = txg;
10033 spa->spa_config_syncing = NULL;
10034 }
10035
428870ff 10036 dsl_pool_sync_done(dp, txg);
34dc7c2f 10037
492f64e9 10038 for (int i = 0; i < spa->spa_alloc_count; i++) {
1b50749c
AM
10039 mutex_enter(&spa->spa_allocs[i].spaa_lock);
10040 VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree));
10041 mutex_exit(&spa->spa_allocs[i].spaa_lock);
492f64e9 10042 }
3dfb57a3 10043
34dc7c2f
BB
10044 /*
10045 * Update usable space statistics.
10046 */
619f0976
GW
10047 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
10048 != NULL)
34dc7c2f 10049 vdev_sync_done(vd, txg);
f09fda50
PD
10050
10051 metaslab_class_evict_old(spa->spa_normal_class, txg);
10052 metaslab_class_evict_old(spa->spa_log_class, txg);
10053
93e28d66 10054 spa_sync_close_syncing_log_sm(spa);
34dc7c2f 10055
428870ff
BB
10056 spa_update_dspace(spa);
10057
65d10bd8
KJ
10058 if (spa_get_autotrim(spa) == SPA_AUTOTRIM_ON)
10059 vdev_autotrim_kick(spa);
10060
34dc7c2f
BB
10061 /*
10062 * It had better be the case that we didn't dirty anything
10063 * since vdev_config_sync().
10064 */
10065 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
10066 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
10067 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
428870ff 10068
d2734cce
SD
10069 while (zfs_pause_spa_sync)
10070 delay(1);
10071
428870ff 10072 spa->spa_sync_pass = 0;
34dc7c2f 10073
55922e73
GW
10074 /*
10075 * Update the last synced uberblock here. We want to do this at
10076 * the end of spa_sync() so that consumers of spa_last_synced_txg()
10077 * will be guaranteed that all the processing associated with
10078 * that txg has been completed.
10079 */
10080 spa->spa_ubsync = spa->spa_uberblock;
b128c09f 10081 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 10082
428870ff
BB
10083 spa_handle_ignored_writes(spa);
10084
34dc7c2f
BB
10085 /*
10086 * If any async tasks have been requested, kick them off.
10087 */
10088 spa_async_dispatch(spa);
10089}
10090
10091/*
10092 * Sync all pools. We don't want to hold the namespace lock across these
10093 * operations, so we take a reference on the spa_t and drop the lock during the
10094 * sync.
10095 */
10096void
10097spa_sync_allpools(void)
10098{
10099 spa_t *spa = NULL;
10100 mutex_enter(&spa_namespace_lock);
10101 while ((spa = spa_next(spa)) != NULL) {
572e2857
BB
10102 if (spa_state(spa) != POOL_STATE_ACTIVE ||
10103 !spa_writeable(spa) || spa_suspended(spa))
34dc7c2f
BB
10104 continue;
10105 spa_open_ref(spa, FTAG);
10106 mutex_exit(&spa_namespace_lock);
10107 txg_wait_synced(spa_get_dsl(spa), 0);
10108 mutex_enter(&spa_namespace_lock);
10109 spa_close(spa, FTAG);
10110 }
10111 mutex_exit(&spa_namespace_lock);
10112}
10113
3bd4df38
EN
10114taskq_t *
10115spa_sync_tq_create(spa_t *spa, const char *name)
10116{
10117 kthread_t **kthreads;
10118
10119 ASSERT(spa->spa_sync_tq == NULL);
10120 ASSERT3S(spa->spa_alloc_count, <=, boot_ncpus);
10121
10122 /*
10123 * - do not allow more allocators than cpus.
10124 * - there may be more cpus than allocators.
10125 * - do not allow more sync taskq threads than allocators or cpus.
10126 */
10127 int nthreads = spa->spa_alloc_count;
10128 spa->spa_syncthreads = kmem_zalloc(sizeof (spa_syncthread_info_t) *
10129 nthreads, KM_SLEEP);
10130
10131 spa->spa_sync_tq = taskq_create_synced(name, nthreads, minclsyspri,
10132 nthreads, INT_MAX, TASKQ_PREPOPULATE, &kthreads);
10133 VERIFY(spa->spa_sync_tq != NULL);
10134 VERIFY(kthreads != NULL);
10135
10136 spa_taskqs_t *tqs =
10137 &spa->spa_zio_taskq[ZIO_TYPE_WRITE][ZIO_TASKQ_ISSUE];
10138
10139 spa_syncthread_info_t *ti = spa->spa_syncthreads;
10140 for (int i = 0, w = 0; i < nthreads; i++, w++, ti++) {
10141 ti->sti_thread = kthreads[i];
10142 if (w == tqs->stqs_count) {
10143 w = 0;
10144 }
10145 ti->sti_wr_iss_tq = tqs->stqs_taskq[w];
10146 }
10147
10148 kmem_free(kthreads, sizeof (*kthreads) * nthreads);
10149 return (spa->spa_sync_tq);
10150}
10151
10152void
10153spa_sync_tq_destroy(spa_t *spa)
10154{
10155 ASSERT(spa->spa_sync_tq != NULL);
10156
10157 taskq_wait(spa->spa_sync_tq);
10158 taskq_destroy(spa->spa_sync_tq);
10159 kmem_free(spa->spa_syncthreads,
10160 sizeof (spa_syncthread_info_t) * spa->spa_alloc_count);
10161 spa->spa_sync_tq = NULL;
10162}
10163
10164void
10165spa_select_allocator(zio_t *zio)
10166{
10167 zbookmark_phys_t *bm = &zio->io_bookmark;
10168 spa_t *spa = zio->io_spa;
10169
10170 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
10171
10172 /*
10173 * A gang block (for example) may have inherited its parent's
10174 * allocator, in which case there is nothing further to do here.
10175 */
10176 if (ZIO_HAS_ALLOCATOR(zio))
10177 return;
10178
10179 ASSERT(spa != NULL);
10180 ASSERT(bm != NULL);
10181
10182 /*
10183 * First try to use an allocator assigned to the syncthread, and set
10184 * the corresponding write issue taskq for the allocator.
10185 * Note, we must have an open pool to do this.
10186 */
10187 if (spa->spa_sync_tq != NULL) {
10188 spa_syncthread_info_t *ti = spa->spa_syncthreads;
10189 for (int i = 0; i < spa->spa_alloc_count; i++, ti++) {
10190 if (ti->sti_thread == curthread) {
10191 zio->io_allocator = i;
10192 zio->io_wr_iss_tq = ti->sti_wr_iss_tq;
10193 return;
10194 }
10195 }
10196 }
10197
10198 /*
10199 * We want to try to use as many allocators as possible to help improve
10200 * performance, but we also want logically adjacent IOs to be physically
10201 * adjacent to improve sequential read performance. We chunk each object
10202 * into 2^20 block regions, and then hash based on the objset, object,
10203 * level, and region to accomplish both of these goals.
10204 */
10205 uint64_t hv = cityhash4(bm->zb_objset, bm->zb_object, bm->zb_level,
10206 bm->zb_blkid >> 20);
10207
10208 zio->io_allocator = (uint_t)hv % spa->spa_alloc_count;
10209 zio->io_wr_iss_tq = NULL;
10210}
10211
34dc7c2f
BB
10212/*
10213 * ==========================================================================
10214 * Miscellaneous routines
10215 * ==========================================================================
10216 */
10217
10218/*
10219 * Remove all pools in the system.
10220 */
10221void
10222spa_evict_all(void)
10223{
10224 spa_t *spa;
10225
10226 /*
10227 * Remove all cached state. All pools should be closed now,
10228 * so every spa in the AVL tree should be unreferenced.
10229 */
10230 mutex_enter(&spa_namespace_lock);
10231 while ((spa = spa_next(NULL)) != NULL) {
10232 /*
10233 * Stop async tasks. The async thread may need to detach
10234 * a device that's been replaced, which requires grabbing
10235 * spa_namespace_lock, so we must drop it here.
10236 */
10237 spa_open_ref(spa, FTAG);
10238 mutex_exit(&spa_namespace_lock);
10239 spa_async_suspend(spa);
10240 mutex_enter(&spa_namespace_lock);
34dc7c2f
BB
10241 spa_close(spa, FTAG);
10242
10243 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
10244 spa_unload(spa);
10245 spa_deactivate(spa);
10246 }
10247 spa_remove(spa);
10248 }
10249 mutex_exit(&spa_namespace_lock);
10250}
10251
10252vdev_t *
9babb374 10253spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
34dc7c2f 10254{
b128c09f
BB
10255 vdev_t *vd;
10256 int i;
10257
10258 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
10259 return (vd);
10260
9babb374 10261 if (aux) {
b128c09f
BB
10262 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
10263 vd = spa->spa_l2cache.sav_vdevs[i];
9babb374
BB
10264 if (vd->vdev_guid == guid)
10265 return (vd);
10266 }
10267
10268 for (i = 0; i < spa->spa_spares.sav_count; i++) {
10269 vd = spa->spa_spares.sav_vdevs[i];
b128c09f
BB
10270 if (vd->vdev_guid == guid)
10271 return (vd);
10272 }
10273 }
10274
10275 return (NULL);
34dc7c2f
BB
10276}
10277
10278void
10279spa_upgrade(spa_t *spa, uint64_t version)
10280{
572e2857
BB
10281 ASSERT(spa_writeable(spa));
10282
b128c09f 10283 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
10284
10285 /*
10286 * This should only be called for a non-faulted pool, and since a
10287 * future version would result in an unopenable pool, this shouldn't be
10288 * possible.
10289 */
8dca0a9a 10290 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
9b67f605 10291 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
34dc7c2f
BB
10292
10293 spa->spa_uberblock.ub_version = version;
10294 vdev_config_dirty(spa->spa_root_vdev);
10295
b128c09f 10296 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
10297
10298 txg_wait_synced(spa_get_dsl(spa), 0);
10299}
10300
49d42425
FU
10301static boolean_t
10302spa_has_aux_vdev(spa_t *spa, uint64_t guid, spa_aux_vdev_t *sav)
34dc7c2f 10303{
14e4e3cb 10304 (void) spa;
34dc7c2f 10305 int i;
49d42425 10306 uint64_t vdev_guid;
34dc7c2f
BB
10307
10308 for (i = 0; i < sav->sav_count; i++)
10309 if (sav->sav_vdevs[i]->vdev_guid == guid)
10310 return (B_TRUE);
10311
10312 for (i = 0; i < sav->sav_npending; i++) {
10313 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
49d42425 10314 &vdev_guid) == 0 && vdev_guid == guid)
34dc7c2f
BB
10315 return (B_TRUE);
10316 }
10317
10318 return (B_FALSE);
10319}
10320
49d42425
FU
10321boolean_t
10322spa_has_l2cache(spa_t *spa, uint64_t guid)
10323{
10324 return (spa_has_aux_vdev(spa, guid, &spa->spa_l2cache));
10325}
10326
10327boolean_t
10328spa_has_spare(spa_t *spa, uint64_t guid)
10329{
10330 return (spa_has_aux_vdev(spa, guid, &spa->spa_spares));
10331}
10332
b128c09f
BB
10333/*
10334 * Check if a pool has an active shared spare device.
10335 * Note: reference count of an active spare is 2, as a spare and as a replace
10336 */
10337static boolean_t
10338spa_has_active_shared_spare(spa_t *spa)
10339{
10340 int i, refcnt;
10341 uint64_t pool;
10342 spa_aux_vdev_t *sav = &spa->spa_spares;
10343
10344 for (i = 0; i < sav->sav_count; i++) {
10345 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
10346 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
10347 refcnt > 2)
10348 return (B_TRUE);
10349 }
10350
10351 return (B_FALSE);
10352}
10353
93e28d66
SD
10354uint64_t
10355spa_total_metaslabs(spa_t *spa)
10356{
10357 vdev_t *rvd = spa->spa_root_vdev;
10358
10359 uint64_t m = 0;
10360 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
10361 vdev_t *vd = rvd->vdev_child[c];
10362 if (!vdev_is_concrete(vd))
10363 continue;
10364 m += vd->vdev_ms_count;
10365 }
10366 return (m);
10367}
10368
e60e158e
JG
10369/*
10370 * Notify any waiting threads that some activity has switched from being in-
10371 * progress to not-in-progress so that the thread can wake up and determine
10372 * whether it is finished waiting.
10373 */
10374void
10375spa_notify_waiters(spa_t *spa)
10376{
10377 /*
10378 * Acquiring spa_activities_lock here prevents the cv_broadcast from
10379 * happening between the waiting thread's check and cv_wait.
10380 */
10381 mutex_enter(&spa->spa_activities_lock);
10382 cv_broadcast(&spa->spa_activities_cv);
10383 mutex_exit(&spa->spa_activities_lock);
10384}
10385
10386/*
10387 * Notify any waiting threads that the pool is exporting, and then block until
10388 * they are finished using the spa_t.
10389 */
10390void
10391spa_wake_waiters(spa_t *spa)
10392{
10393 mutex_enter(&spa->spa_activities_lock);
10394 spa->spa_waiters_cancel = B_TRUE;
10395 cv_broadcast(&spa->spa_activities_cv);
10396 while (spa->spa_waiters != 0)
10397 cv_wait(&spa->spa_waiters_cv, &spa->spa_activities_lock);
10398 spa->spa_waiters_cancel = B_FALSE;
10399 mutex_exit(&spa->spa_activities_lock);
10400}
10401
2288d419 10402/* Whether the vdev or any of its descendants are being initialized/trimmed. */
e60e158e 10403static boolean_t
2288d419 10404spa_vdev_activity_in_progress_impl(vdev_t *vd, zpool_wait_activity_t activity)
e60e158e
JG
10405{
10406 spa_t *spa = vd->vdev_spa;
e60e158e
JG
10407
10408 ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER));
10409 ASSERT(MUTEX_HELD(&spa->spa_activities_lock));
2288d419
BB
10410 ASSERT(activity == ZPOOL_WAIT_INITIALIZE ||
10411 activity == ZPOOL_WAIT_TRIM);
10412
10413 kmutex_t *lock = activity == ZPOOL_WAIT_INITIALIZE ?
10414 &vd->vdev_initialize_lock : &vd->vdev_trim_lock;
e60e158e
JG
10415
10416 mutex_exit(&spa->spa_activities_lock);
2288d419 10417 mutex_enter(lock);
e60e158e
JG
10418 mutex_enter(&spa->spa_activities_lock);
10419
2288d419
BB
10420 boolean_t in_progress = (activity == ZPOOL_WAIT_INITIALIZE) ?
10421 (vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) :
10422 (vd->vdev_trim_state == VDEV_TRIM_ACTIVE);
10423 mutex_exit(lock);
e60e158e 10424
2288d419 10425 if (in_progress)
e60e158e
JG
10426 return (B_TRUE);
10427
10428 for (int i = 0; i < vd->vdev_children; i++) {
2288d419
BB
10429 if (spa_vdev_activity_in_progress_impl(vd->vdev_child[i],
10430 activity))
e60e158e
JG
10431 return (B_TRUE);
10432 }
10433
10434 return (B_FALSE);
10435}
10436
10437/*
10438 * If use_guid is true, this checks whether the vdev specified by guid is
2288d419
BB
10439 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
10440 * is being initialized/trimmed. The caller must hold the config lock and
10441 * spa_activities_lock.
e60e158e
JG
10442 */
10443static int
2288d419
BB
10444spa_vdev_activity_in_progress(spa_t *spa, boolean_t use_guid, uint64_t guid,
10445 zpool_wait_activity_t activity, boolean_t *in_progress)
e60e158e
JG
10446{
10447 mutex_exit(&spa->spa_activities_lock);
10448 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
10449 mutex_enter(&spa->spa_activities_lock);
10450
10451 vdev_t *vd;
10452 if (use_guid) {
10453 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
10454 if (vd == NULL || !vd->vdev_ops->vdev_op_leaf) {
10455 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10456 return (EINVAL);
10457 }
10458 } else {
10459 vd = spa->spa_root_vdev;
10460 }
10461
2288d419 10462 *in_progress = spa_vdev_activity_in_progress_impl(vd, activity);
e60e158e
JG
10463
10464 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10465 return (0);
10466}
10467
10468/*
10469 * Locking for waiting threads
10470 * ---------------------------
10471 *
10472 * Waiting threads need a way to check whether a given activity is in progress,
10473 * and then, if it is, wait for it to complete. Each activity will have some
10474 * in-memory representation of the relevant on-disk state which can be used to
10475 * determine whether or not the activity is in progress. The in-memory state and
10476 * the locking used to protect it will be different for each activity, and may
10477 * not be suitable for use with a cvar (e.g., some state is protected by the
10478 * config lock). To allow waiting threads to wait without any races, another
10479 * lock, spa_activities_lock, is used.
10480 *
10481 * When the state is checked, both the activity-specific lock (if there is one)
10482 * and spa_activities_lock are held. In some cases, the activity-specific lock
10483 * is acquired explicitly (e.g. the config lock). In others, the locking is
10484 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
10485 * thread releases the activity-specific lock and, if the activity is in
10486 * progress, then cv_waits using spa_activities_lock.
10487 *
10488 * The waiting thread is woken when another thread, one completing some
10489 * activity, updates the state of the activity and then calls
10490 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
10491 * needs to hold its activity-specific lock when updating the state, and this
10492 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
10493 *
10494 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
10495 * and because it is held when the waiting thread checks the state of the
10496 * activity, it can never be the case that the completing thread both updates
10497 * the activity state and cv_broadcasts in between the waiting thread's check
10498 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
10499 *
10500 * In order to prevent deadlock, when the waiting thread does its check, in some
10501 * cases it will temporarily drop spa_activities_lock in order to acquire the
10502 * activity-specific lock. The order in which spa_activities_lock and the
10503 * activity specific lock are acquired in the waiting thread is determined by
10504 * the order in which they are acquired in the completing thread; if the
10505 * completing thread calls spa_notify_waiters with the activity-specific lock
10506 * held, then the waiting thread must also acquire the activity-specific lock
10507 * first.
10508 */
10509
10510static int
10511spa_activity_in_progress(spa_t *spa, zpool_wait_activity_t activity,
10512 boolean_t use_tag, uint64_t tag, boolean_t *in_progress)
10513{
10514 int error = 0;
10515
10516 ASSERT(MUTEX_HELD(&spa->spa_activities_lock));
10517
10518 switch (activity) {
10519 case ZPOOL_WAIT_CKPT_DISCARD:
10520 *in_progress =
10521 (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT) &&
10522 zap_contains(spa_meta_objset(spa),
10523 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ZPOOL_CHECKPOINT) ==
10524 ENOENT);
10525 break;
10526 case ZPOOL_WAIT_FREE:
10527 *in_progress = ((spa_version(spa) >= SPA_VERSION_DEADLISTS &&
10528 !bpobj_is_empty(&spa->spa_dsl_pool->dp_free_bpobj)) ||
10529 spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY) ||
10530 spa_livelist_delete_check(spa));
10531 break;
10532 case ZPOOL_WAIT_INITIALIZE:
2288d419
BB
10533 case ZPOOL_WAIT_TRIM:
10534 error = spa_vdev_activity_in_progress(spa, use_tag, tag,
10535 activity, in_progress);
e60e158e
JG
10536 break;
10537 case ZPOOL_WAIT_REPLACE:
10538 mutex_exit(&spa->spa_activities_lock);
10539 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
10540 mutex_enter(&spa->spa_activities_lock);
10541
10542 *in_progress = vdev_replace_in_progress(spa->spa_root_vdev);
10543 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
10544 break;
10545 case ZPOOL_WAIT_REMOVE:
10546 *in_progress = (spa->spa_removing_phys.sr_state ==
10547 DSS_SCANNING);
10548 break;
10549 case ZPOOL_WAIT_RESILVER:
5caeef02
DB
10550 *in_progress = vdev_rebuild_active(spa->spa_root_vdev);
10551 if (*in_progress)
9a49d3f3 10552 break;
9a70e97f 10553 zfs_fallthrough;
e60e158e
JG
10554 case ZPOOL_WAIT_SCRUB:
10555 {
10556 boolean_t scanning, paused, is_scrub;
10557 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
10558
10559 is_scrub = (scn->scn_phys.scn_func == POOL_SCAN_SCRUB);
10560 scanning = (scn->scn_phys.scn_state == DSS_SCANNING);
10561 paused = dsl_scan_is_paused_scrub(scn);
10562 *in_progress = (scanning && !paused &&
10563 is_scrub == (activity == ZPOOL_WAIT_SCRUB));
10564 break;
10565 }
5caeef02
DB
10566 case ZPOOL_WAIT_RAIDZ_EXPAND:
10567 {
10568 vdev_raidz_expand_t *vre = spa->spa_raidz_expand;
10569 *in_progress = (vre != NULL && vre->vre_state == DSS_SCANNING);
10570 break;
10571 }
e60e158e
JG
10572 default:
10573 panic("unrecognized value for activity %d", activity);
10574 }
10575
10576 return (error);
10577}
10578
10579static int
10580spa_wait_common(const char *pool, zpool_wait_activity_t activity,
10581 boolean_t use_tag, uint64_t tag, boolean_t *waited)
10582{
10583 /*
10584 * The tag is used to distinguish between instances of an activity.
2288d419
BB
10585 * 'initialize' and 'trim' are the only activities that we use this for.
10586 * The other activities can only have a single instance in progress in a
10587 * pool at one time, making the tag unnecessary.
e60e158e
JG
10588 *
10589 * There can be multiple devices being replaced at once, but since they
10590 * all finish once resilvering finishes, we don't bother keeping track
10591 * of them individually, we just wait for them all to finish.
10592 */
2288d419
BB
10593 if (use_tag && activity != ZPOOL_WAIT_INITIALIZE &&
10594 activity != ZPOOL_WAIT_TRIM)
e60e158e
JG
10595 return (EINVAL);
10596
10597 if (activity < 0 || activity >= ZPOOL_WAIT_NUM_ACTIVITIES)
10598 return (EINVAL);
10599
10600 spa_t *spa;
10601 int error = spa_open(pool, &spa, FTAG);
10602 if (error != 0)
10603 return (error);
10604
10605 /*
10606 * Increment the spa's waiter count so that we can call spa_close and
10607 * still ensure that the spa_t doesn't get freed before this thread is
10608 * finished with it when the pool is exported. We want to call spa_close
10609 * before we start waiting because otherwise the additional ref would
10610 * prevent the pool from being exported or destroyed throughout the
10611 * potentially long wait.
10612 */
10613 mutex_enter(&spa->spa_activities_lock);
10614 spa->spa_waiters++;
10615 spa_close(spa, FTAG);
10616
10617 *waited = B_FALSE;
10618 for (;;) {
10619 boolean_t in_progress;
10620 error = spa_activity_in_progress(spa, activity, use_tag, tag,
10621 &in_progress);
10622
b24771a8 10623 if (error || !in_progress || spa->spa_waiters_cancel)
e60e158e
JG
10624 break;
10625
10626 *waited = B_TRUE;
10627
10628 if (cv_wait_sig(&spa->spa_activities_cv,
10629 &spa->spa_activities_lock) == 0) {
10630 error = EINTR;
10631 break;
10632 }
10633 }
10634
10635 spa->spa_waiters--;
10636 cv_signal(&spa->spa_waiters_cv);
10637 mutex_exit(&spa->spa_activities_lock);
10638
10639 return (error);
10640}
10641
10642/*
10643 * Wait for a particular instance of the specified activity to complete, where
10644 * the instance is identified by 'tag'
10645 */
10646int
10647spa_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag,
10648 boolean_t *waited)
10649{
10650 return (spa_wait_common(pool, activity, B_TRUE, tag, waited));
10651}
10652
10653/*
10654 * Wait for all instances of the specified activity complete
10655 */
10656int
10657spa_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited)
10658{
10659
10660 return (spa_wait_common(pool, activity, B_FALSE, 0, waited));
10661}
10662
a1d477c2 10663sysevent_t *
12fa0466
DE
10664spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
10665{
10666 sysevent_t *ev = NULL;
10667#ifdef _KERNEL
10668 nvlist_t *resource;
10669
10670 resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl);
10671 if (resource) {
10672 ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP);
10673 ev->resource = resource;
10674 }
14e4e3cb
AZ
10675#else
10676 (void) spa, (void) vd, (void) hist_nvl, (void) name;
12fa0466
DE
10677#endif
10678 return (ev);
10679}
10680
a1d477c2 10681void
12fa0466
DE
10682spa_event_post(sysevent_t *ev)
10683{
10684#ifdef _KERNEL
10685 if (ev) {
10686 zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb);
10687 kmem_free(ev, sizeof (*ev));
10688 }
14e4e3cb
AZ
10689#else
10690 (void) ev;
12fa0466
DE
10691#endif
10692}
10693
34dc7c2f 10694/*
fb390aaf
HR
10695 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10696 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
34dc7c2f
BB
10697 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10698 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10699 * or zdb as real changes.
10700 */
10701void
12fa0466 10702spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name)
34dc7c2f 10703{
12fa0466 10704 spa_event_post(spa_event_create(spa, vd, hist_nvl, name));
34dc7c2f 10705}
c28b2279 10706
c28b2279
BB
10707/* state manipulation functions */
10708EXPORT_SYMBOL(spa_open);
10709EXPORT_SYMBOL(spa_open_rewind);
10710EXPORT_SYMBOL(spa_get_stats);
10711EXPORT_SYMBOL(spa_create);
c28b2279
BB
10712EXPORT_SYMBOL(spa_import);
10713EXPORT_SYMBOL(spa_tryimport);
10714EXPORT_SYMBOL(spa_destroy);
10715EXPORT_SYMBOL(spa_export);
10716EXPORT_SYMBOL(spa_reset);
10717EXPORT_SYMBOL(spa_async_request);
10718EXPORT_SYMBOL(spa_async_suspend);
10719EXPORT_SYMBOL(spa_async_resume);
10720EXPORT_SYMBOL(spa_inject_addref);
10721EXPORT_SYMBOL(spa_inject_delref);
10722EXPORT_SYMBOL(spa_scan_stat_init);
10723EXPORT_SYMBOL(spa_scan_get_stats);
10724
e1cfd73f 10725/* device manipulation */
c28b2279
BB
10726EXPORT_SYMBOL(spa_vdev_add);
10727EXPORT_SYMBOL(spa_vdev_attach);
10728EXPORT_SYMBOL(spa_vdev_detach);
c28b2279
BB
10729EXPORT_SYMBOL(spa_vdev_setpath);
10730EXPORT_SYMBOL(spa_vdev_setfru);
10731EXPORT_SYMBOL(spa_vdev_split_mirror);
10732
10733/* spare statech is global across all pools) */
10734EXPORT_SYMBOL(spa_spare_add);
10735EXPORT_SYMBOL(spa_spare_remove);
10736EXPORT_SYMBOL(spa_spare_exists);
10737EXPORT_SYMBOL(spa_spare_activate);
10738
10739/* L2ARC statech is global across all pools) */
10740EXPORT_SYMBOL(spa_l2cache_add);
10741EXPORT_SYMBOL(spa_l2cache_remove);
10742EXPORT_SYMBOL(spa_l2cache_exists);
10743EXPORT_SYMBOL(spa_l2cache_activate);
10744EXPORT_SYMBOL(spa_l2cache_drop);
10745
10746/* scanning */
10747EXPORT_SYMBOL(spa_scan);
10748EXPORT_SYMBOL(spa_scan_stop);
10749
10750/* spa syncing */
10751EXPORT_SYMBOL(spa_sync); /* only for DMU use */
10752EXPORT_SYMBOL(spa_sync_allpools);
10753
10754/* properties */
10755EXPORT_SYMBOL(spa_prop_set);
10756EXPORT_SYMBOL(spa_prop_get);
10757EXPORT_SYMBOL(spa_prop_clear_bootfs);
10758
10759/* asynchronous event notification */
10760EXPORT_SYMBOL(spa_event_notify);
dea377c0 10761
342357cd
AM
10762ZFS_MODULE_PARAM(zfs_metaslab, metaslab_, preload_pct, UINT, ZMOD_RW,
10763 "Percentage of CPUs to run a metaslab preload taskq");
10764
c8242a96 10765/* BEGIN CSTYLED */
fdc2d303 10766ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_shift, UINT, ZMOD_RW,
458f8231 10767 "log2 fraction of arc that can be used by inflight I/Os when "
03fdcb9a 10768 "verifying pool during import");
7ada752a 10769/* END CSTYLED */
dea377c0 10770
03fdcb9a 10771ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_metadata, INT, ZMOD_RW,
dea377c0
MA
10772 "Set to traverse metadata on pool import");
10773
03fdcb9a 10774ZFS_MODULE_PARAM(zfs_spa, spa_, load_verify_data, INT, ZMOD_RW,
dea377c0 10775 "Set to traverse data on pool import");
dcb6bed1 10776
03fdcb9a 10777ZFS_MODULE_PARAM(zfs_spa, spa_, load_print_vdev_tree, INT, ZMOD_RW,
6cb8e530
PZ
10778 "Print vdev tree to zfs_dbgmsg during pool import");
10779
03fdcb9a 10780ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_pct, UINT, ZMOD_RD,
dcb6bed1
D
10781 "Percentage of CPUs to run an IO worker thread");
10782
7457b024
AM
10783ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_batch_tpq, UINT, ZMOD_RD,
10784 "Number of threads per IO worker taskqueue");
10785
7ada752a 10786/* BEGIN CSTYLED */
ab8d9c17 10787ZFS_MODULE_PARAM(zfs, zfs_, max_missing_tvds, U64, ZMOD_RW,
03fdcb9a
MM
10788 "Allow importing pool with up to this number of missing top-level "
10789 "vdevs (in read-only mode)");
7ada752a 10790/* END CSTYLED */
6cb8e530 10791
7ada752a
AZ
10792ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_pause, INT,
10793 ZMOD_RW, "Set the livelist condense zthr to pause");
03fdcb9a 10794
7ada752a
AZ
10795ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_pause, INT,
10796 ZMOD_RW, "Set the livelist condense synctask to pause");
37f03da8 10797
7ada752a
AZ
10798/* BEGIN CSTYLED */
10799ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, sync_cancel,
10800 INT, ZMOD_RW,
37f03da8 10801 "Whether livelist condensing was canceled in the synctask");
03fdcb9a 10802
7ada752a
AZ
10803ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, zthr_cancel,
10804 INT, ZMOD_RW,
37f03da8
SH
10805 "Whether livelist condensing was canceled in the zthr function");
10806
7ada752a
AZ
10807ZFS_MODULE_PARAM(zfs_livelist_condense, zfs_livelist_condense_, new_alloc, INT,
10808 ZMOD_RW,
03fdcb9a
MM
10809 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10810 "was being condensed");
6930ecbb
RN
10811
10812#ifdef _KERNEL
10813ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio, zio_, taskq_read,
10814 spa_taskq_read_param_set, spa_taskq_read_param_get, ZMOD_RD,
10815 "Configure IO queues for read IO");
10816ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio, zio_, taskq_write,
10817 spa_taskq_write_param_set, spa_taskq_write_param_get, ZMOD_RD,
10818 "Configure IO queues for write IO");
10819#endif
37f03da8 10820/* END CSTYLED */
3bd4df38
EN
10821
10822ZFS_MODULE_PARAM(zfs_zio, zio_, taskq_wr_iss_ncpus, UINT, ZMOD_RW,
10823 "Number of CPUs to run write issue taskqs");