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