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