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