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