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