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