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