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