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