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