spa.c revision 77e3a39cc409dd0a5c2e86f25e89031be10dee7b
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 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
30 * pool.
31 */
32
33#include <sys/zfs_context.h>
34#include <sys/fm/fs/zfs.h>
35#include <sys/spa_impl.h>
36#include <sys/zio.h>
37#include <sys/zio_checksum.h>
38#include <sys/zio_compress.h>
39#include <sys/dmu.h>
40#include <sys/dmu_tx.h>
41#include <sys/zap.h>
42#include <sys/zil.h>
43#include <sys/vdev_impl.h>
44#include <sys/metaslab.h>
45#include <sys/uberblock_impl.h>
46#include <sys/txg.h>
47#include <sys/avl.h>
48#include <sys/dmu_traverse.h>
49#include <sys/dmu_objset.h>
50#include <sys/unique.h>
51#include <sys/dsl_pool.h>
52#include <sys/dsl_dataset.h>
53#include <sys/dsl_dir.h>
54#include <sys/dsl_prop.h>
55#include <sys/dsl_synctask.h>
56#include <sys/fs/zfs.h>
57#include <sys/arc.h>
58#include <sys/callb.h>
59#include <sys/systeminfo.h>
60#include <sys/sunddi.h>
61#include <sys/spa_boot.h>
62
63#ifdef	_KERNEL
64#include <sys/zone.h>
65#endif	/* _KERNEL */
66
67#include "zfs_prop.h"
68#include "zfs_comutil.h"
69
70int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
71	/*	ISSUE	INTR					*/
72	{	1,	1	},	/* ZIO_TYPE_NULL	*/
73	{	8,	8	},	/* ZIO_TYPE_READ	*/
74	{	8,	8	},	/* ZIO_TYPE_WRITE	*/
75	{	1,	1	},	/* ZIO_TYPE_FREE	*/
76	{	1,	1	},	/* ZIO_TYPE_CLAIM	*/
77	{	1,	1	},	/* ZIO_TYPE_IOCTL	*/
78};
79
80static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
81static boolean_t spa_has_active_shared_spare(spa_t *spa);
82
83/*
84 * ==========================================================================
85 * SPA properties routines
86 * ==========================================================================
87 */
88
89/*
90 * Add a (source=src, propname=propval) list to an nvlist.
91 */
92static void
93spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
94    uint64_t intval, zprop_source_t src)
95{
96	const char *propname = zpool_prop_to_name(prop);
97	nvlist_t *propval;
98
99	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
100	VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
101
102	if (strval != NULL)
103		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
104	else
105		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
106
107	VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
108	nvlist_free(propval);
109}
110
111/*
112 * Get property values from the spa configuration.
113 */
114static void
115spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
116{
117	uint64_t size;
118	uint64_t used;
119	uint64_t cap, version;
120	zprop_source_t src = ZPROP_SRC_NONE;
121	spa_config_dirent_t *dp;
122
123	ASSERT(MUTEX_HELD(&spa->spa_props_lock));
124
125	if (spa->spa_root_vdev != NULL) {
126		size = spa_get_space(spa);
127		used = spa_get_alloc(spa);
128		spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
129		spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
130		spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
131		spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL,
132		    size - used, src);
133
134		cap = (size == 0) ? 0 : (used * 100 / size);
135		spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
136
137		spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
138		    spa->spa_root_vdev->vdev_state, src);
139
140		version = spa_version(spa);
141		if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
142			src = ZPROP_SRC_DEFAULT;
143		else
144			src = ZPROP_SRC_LOCAL;
145		spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
146	}
147
148	spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
149
150	if (spa->spa_root != NULL)
151		spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
152		    0, ZPROP_SRC_LOCAL);
153
154	if ((dp = list_head(&spa->spa_config_list)) != NULL) {
155		if (dp->scd_path == NULL) {
156			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
157			    "none", 0, ZPROP_SRC_LOCAL);
158		} else if (strcmp(dp->scd_path, spa_config_path) != 0) {
159			spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
160			    dp->scd_path, 0, ZPROP_SRC_LOCAL);
161		}
162	}
163}
164
165/*
166 * Get zpool property values.
167 */
168int
169spa_prop_get(spa_t *spa, nvlist_t **nvp)
170{
171	zap_cursor_t zc;
172	zap_attribute_t za;
173	objset_t *mos = spa->spa_meta_objset;
174	int err;
175
176	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
177
178	mutex_enter(&spa->spa_props_lock);
179
180	/*
181	 * Get properties from the spa config.
182	 */
183	spa_prop_get_config(spa, nvp);
184
185	/* If no pool property object, no more prop to get. */
186	if (spa->spa_pool_props_object == 0) {
187		mutex_exit(&spa->spa_props_lock);
188		return (0);
189	}
190
191	/*
192	 * Get properties from the MOS pool property object.
193	 */
194	for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
195	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
196	    zap_cursor_advance(&zc)) {
197		uint64_t intval = 0;
198		char *strval = NULL;
199		zprop_source_t src = ZPROP_SRC_DEFAULT;
200		zpool_prop_t prop;
201
202		if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
203			continue;
204
205		switch (za.za_integer_length) {
206		case 8:
207			/* integer property */
208			if (za.za_first_integer !=
209			    zpool_prop_default_numeric(prop))
210				src = ZPROP_SRC_LOCAL;
211
212			if (prop == ZPOOL_PROP_BOOTFS) {
213				dsl_pool_t *dp;
214				dsl_dataset_t *ds = NULL;
215
216				dp = spa_get_dsl(spa);
217				rw_enter(&dp->dp_config_rwlock, RW_READER);
218				if (err = dsl_dataset_hold_obj(dp,
219				    za.za_first_integer, FTAG, &ds)) {
220					rw_exit(&dp->dp_config_rwlock);
221					break;
222				}
223
224				strval = kmem_alloc(
225				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
226				    KM_SLEEP);
227				dsl_dataset_name(ds, strval);
228				dsl_dataset_rele(ds, FTAG);
229				rw_exit(&dp->dp_config_rwlock);
230			} else {
231				strval = NULL;
232				intval = za.za_first_integer;
233			}
234
235			spa_prop_add_list(*nvp, prop, strval, intval, src);
236
237			if (strval != NULL)
238				kmem_free(strval,
239				    MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
240
241			break;
242
243		case 1:
244			/* string property */
245			strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
246			err = zap_lookup(mos, spa->spa_pool_props_object,
247			    za.za_name, 1, za.za_num_integers, strval);
248			if (err) {
249				kmem_free(strval, za.za_num_integers);
250				break;
251			}
252			spa_prop_add_list(*nvp, prop, strval, 0, src);
253			kmem_free(strval, za.za_num_integers);
254			break;
255
256		default:
257			break;
258		}
259	}
260	zap_cursor_fini(&zc);
261	mutex_exit(&spa->spa_props_lock);
262out:
263	if (err && err != ENOENT) {
264		nvlist_free(*nvp);
265		*nvp = NULL;
266		return (err);
267	}
268
269	return (0);
270}
271
272/*
273 * Validate the given pool properties nvlist and modify the list
274 * for the property values to be set.
275 */
276static int
277spa_prop_validate(spa_t *spa, nvlist_t *props)
278{
279	nvpair_t *elem;
280	int error = 0, reset_bootfs = 0;
281	uint64_t objnum;
282
283	elem = NULL;
284	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
285		zpool_prop_t prop;
286		char *propname, *strval;
287		uint64_t intval;
288		objset_t *os;
289		char *slash;
290
291		propname = nvpair_name(elem);
292
293		if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
294			return (EINVAL);
295
296		switch (prop) {
297		case ZPOOL_PROP_VERSION:
298			error = nvpair_value_uint64(elem, &intval);
299			if (!error &&
300			    (intval < spa_version(spa) || intval > SPA_VERSION))
301				error = EINVAL;
302			break;
303
304		case ZPOOL_PROP_DELEGATION:
305		case ZPOOL_PROP_AUTOREPLACE:
306		case ZPOOL_PROP_LISTSNAPS:
307			error = nvpair_value_uint64(elem, &intval);
308			if (!error && intval > 1)
309				error = EINVAL;
310			break;
311
312		case ZPOOL_PROP_BOOTFS:
313			if (spa_version(spa) < SPA_VERSION_BOOTFS) {
314				error = ENOTSUP;
315				break;
316			}
317
318			/*
319			 * Make sure the vdev config is bootable
320			 */
321			if (!vdev_is_bootable(spa->spa_root_vdev)) {
322				error = ENOTSUP;
323				break;
324			}
325
326			reset_bootfs = 1;
327
328			error = nvpair_value_string(elem, &strval);
329
330			if (!error) {
331				uint64_t compress;
332
333				if (strval == NULL || strval[0] == '\0') {
334					objnum = zpool_prop_default_numeric(
335					    ZPOOL_PROP_BOOTFS);
336					break;
337				}
338
339				if (error = dmu_objset_open(strval, DMU_OST_ZFS,
340				    DS_MODE_USER | DS_MODE_READONLY, &os))
341					break;
342
343				/* We don't support gzip bootable datasets */
344				if ((error = dsl_prop_get_integer(strval,
345				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
346				    &compress, NULL)) == 0 &&
347				    !BOOTFS_COMPRESS_VALID(compress)) {
348					error = ENOTSUP;
349				} else {
350					objnum = dmu_objset_id(os);
351				}
352				dmu_objset_close(os);
353			}
354			break;
355
356		case ZPOOL_PROP_FAILUREMODE:
357			error = nvpair_value_uint64(elem, &intval);
358			if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
359			    intval > ZIO_FAILURE_MODE_PANIC))
360				error = EINVAL;
361
362			/*
363			 * This is a special case which only occurs when
364			 * the pool has completely failed. This allows
365			 * the user to change the in-core failmode property
366			 * without syncing it out to disk (I/Os might
367			 * currently be blocked). We do this by returning
368			 * EIO to the caller (spa_prop_set) to trick it
369			 * into thinking we encountered a property validation
370			 * error.
371			 */
372			if (!error && spa_suspended(spa)) {
373				spa->spa_failmode = intval;
374				error = EIO;
375			}
376			break;
377
378		case ZPOOL_PROP_CACHEFILE:
379			if ((error = nvpair_value_string(elem, &strval)) != 0)
380				break;
381
382			if (strval[0] == '\0')
383				break;
384
385			if (strcmp(strval, "none") == 0)
386				break;
387
388			if (strval[0] != '/') {
389				error = EINVAL;
390				break;
391			}
392
393			slash = strrchr(strval, '/');
394			ASSERT(slash != NULL);
395
396			if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
397			    strcmp(slash, "/..") == 0)
398				error = EINVAL;
399			break;
400		}
401
402		if (error)
403			break;
404	}
405
406	if (!error && reset_bootfs) {
407		error = nvlist_remove(props,
408		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
409
410		if (!error) {
411			error = nvlist_add_uint64(props,
412			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
413		}
414	}
415
416	return (error);
417}
418
419void
420spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
421{
422	char *cachefile;
423	spa_config_dirent_t *dp;
424
425	if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
426	    &cachefile) != 0)
427		return;
428
429	dp = kmem_alloc(sizeof (spa_config_dirent_t),
430	    KM_SLEEP);
431
432	if (cachefile[0] == '\0')
433		dp->scd_path = spa_strdup(spa_config_path);
434	else if (strcmp(cachefile, "none") == 0)
435		dp->scd_path = NULL;
436	else
437		dp->scd_path = spa_strdup(cachefile);
438
439	list_insert_head(&spa->spa_config_list, dp);
440	if (need_sync)
441		spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
442}
443
444int
445spa_prop_set(spa_t *spa, nvlist_t *nvp)
446{
447	int error;
448	nvpair_t *elem;
449	boolean_t need_sync = B_FALSE;
450	zpool_prop_t prop;
451
452	if ((error = spa_prop_validate(spa, nvp)) != 0)
453		return (error);
454
455	elem = NULL;
456	while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
457		if ((prop = zpool_name_to_prop(
458		    nvpair_name(elem))) == ZPROP_INVAL)
459			return (EINVAL);
460
461		if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT)
462			continue;
463
464		need_sync = B_TRUE;
465		break;
466	}
467
468	if (need_sync)
469		return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
470		    spa, nvp, 3));
471	else
472		return (0);
473}
474
475/*
476 * If the bootfs property value is dsobj, clear it.
477 */
478void
479spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
480{
481	if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
482		VERIFY(zap_remove(spa->spa_meta_objset,
483		    spa->spa_pool_props_object,
484		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
485		spa->spa_bootfs = 0;
486	}
487}
488
489/*
490 * ==========================================================================
491 * SPA state manipulation (open/create/destroy/import/export)
492 * ==========================================================================
493 */
494
495static int
496spa_error_entry_compare(const void *a, const void *b)
497{
498	spa_error_entry_t *sa = (spa_error_entry_t *)a;
499	spa_error_entry_t *sb = (spa_error_entry_t *)b;
500	int ret;
501
502	ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
503	    sizeof (zbookmark_t));
504
505	if (ret < 0)
506		return (-1);
507	else if (ret > 0)
508		return (1);
509	else
510		return (0);
511}
512
513/*
514 * Utility function which retrieves copies of the current logs and
515 * re-initializes them in the process.
516 */
517void
518spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
519{
520	ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
521
522	bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
523	bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
524
525	avl_create(&spa->spa_errlist_scrub,
526	    spa_error_entry_compare, sizeof (spa_error_entry_t),
527	    offsetof(spa_error_entry_t, se_avl));
528	avl_create(&spa->spa_errlist_last,
529	    spa_error_entry_compare, sizeof (spa_error_entry_t),
530	    offsetof(spa_error_entry_t, se_avl));
531}
532
533/*
534 * Activate an uninitialized pool.
535 */
536static void
537spa_activate(spa_t *spa, int mode)
538{
539	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
540
541	spa->spa_state = POOL_STATE_ACTIVE;
542	spa->spa_mode = mode;
543
544	spa->spa_normal_class = metaslab_class_create();
545	spa->spa_log_class = metaslab_class_create();
546
547	for (int t = 0; t < ZIO_TYPES; t++) {
548		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
549			spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
550			    zio_taskq_threads[t][q], maxclsyspri, 50,
551			    INT_MAX, TASKQ_PREPOPULATE);
552		}
553	}
554
555	list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
556	    offsetof(vdev_t, vdev_config_dirty_node));
557	list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
558	    offsetof(vdev_t, vdev_state_dirty_node));
559
560	txg_list_create(&spa->spa_vdev_txg_list,
561	    offsetof(struct vdev, vdev_txg_node));
562
563	avl_create(&spa->spa_errlist_scrub,
564	    spa_error_entry_compare, sizeof (spa_error_entry_t),
565	    offsetof(spa_error_entry_t, se_avl));
566	avl_create(&spa->spa_errlist_last,
567	    spa_error_entry_compare, sizeof (spa_error_entry_t),
568	    offsetof(spa_error_entry_t, se_avl));
569}
570
571/*
572 * Opposite of spa_activate().
573 */
574static void
575spa_deactivate(spa_t *spa)
576{
577	ASSERT(spa->spa_sync_on == B_FALSE);
578	ASSERT(spa->spa_dsl_pool == NULL);
579	ASSERT(spa->spa_root_vdev == NULL);
580
581	ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
582
583	txg_list_destroy(&spa->spa_vdev_txg_list);
584
585	list_destroy(&spa->spa_config_dirty_list);
586	list_destroy(&spa->spa_state_dirty_list);
587
588	for (int t = 0; t < ZIO_TYPES; t++) {
589		for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
590			taskq_destroy(spa->spa_zio_taskq[t][q]);
591			spa->spa_zio_taskq[t][q] = NULL;
592		}
593	}
594
595	metaslab_class_destroy(spa->spa_normal_class);
596	spa->spa_normal_class = NULL;
597
598	metaslab_class_destroy(spa->spa_log_class);
599	spa->spa_log_class = NULL;
600
601	/*
602	 * If this was part of an import or the open otherwise failed, we may
603	 * still have errors left in the queues.  Empty them just in case.
604	 */
605	spa_errlog_drain(spa);
606
607	avl_destroy(&spa->spa_errlist_scrub);
608	avl_destroy(&spa->spa_errlist_last);
609
610	spa->spa_state = POOL_STATE_UNINITIALIZED;
611}
612
613/*
614 * Verify a pool configuration, and construct the vdev tree appropriately.  This
615 * will create all the necessary vdevs in the appropriate layout, with each vdev
616 * in the CLOSED state.  This will prep the pool before open/creation/import.
617 * All vdev validation is done by the vdev_alloc() routine.
618 */
619static int
620spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
621    uint_t id, int atype)
622{
623	nvlist_t **child;
624	uint_t c, children;
625	int error;
626
627	if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
628		return (error);
629
630	if ((*vdp)->vdev_ops->vdev_op_leaf)
631		return (0);
632
633	error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
634	    &child, &children);
635
636	if (error == ENOENT)
637		return (0);
638
639	if (error) {
640		vdev_free(*vdp);
641		*vdp = NULL;
642		return (EINVAL);
643	}
644
645	for (c = 0; c < children; c++) {
646		vdev_t *vd;
647		if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
648		    atype)) != 0) {
649			vdev_free(*vdp);
650			*vdp = NULL;
651			return (error);
652		}
653	}
654
655	ASSERT(*vdp != NULL);
656
657	return (0);
658}
659
660/*
661 * Opposite of spa_load().
662 */
663static void
664spa_unload(spa_t *spa)
665{
666	int i;
667
668	ASSERT(MUTEX_HELD(&spa_namespace_lock));
669
670	/*
671	 * Stop async tasks.
672	 */
673	spa_async_suspend(spa);
674
675	/*
676	 * Stop syncing.
677	 */
678	if (spa->spa_sync_on) {
679		txg_sync_stop(spa->spa_dsl_pool);
680		spa->spa_sync_on = B_FALSE;
681	}
682
683	/*
684	 * Wait for any outstanding async I/O to complete.
685	 */
686	if (spa->spa_async_zio_root != NULL) {
687		(void) zio_wait(spa->spa_async_zio_root);
688		spa->spa_async_zio_root = NULL;
689	}
690
691	/*
692	 * Close the dsl pool.
693	 */
694	if (spa->spa_dsl_pool) {
695		dsl_pool_close(spa->spa_dsl_pool);
696		spa->spa_dsl_pool = NULL;
697	}
698
699	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
700
701	/*
702	 * Drop and purge level 2 cache
703	 */
704	spa_l2cache_drop(spa);
705
706	/*
707	 * Close all vdevs.
708	 */
709	if (spa->spa_root_vdev)
710		vdev_free(spa->spa_root_vdev);
711	ASSERT(spa->spa_root_vdev == NULL);
712
713	for (i = 0; i < spa->spa_spares.sav_count; i++)
714		vdev_free(spa->spa_spares.sav_vdevs[i]);
715	if (spa->spa_spares.sav_vdevs) {
716		kmem_free(spa->spa_spares.sav_vdevs,
717		    spa->spa_spares.sav_count * sizeof (void *));
718		spa->spa_spares.sav_vdevs = NULL;
719	}
720	if (spa->spa_spares.sav_config) {
721		nvlist_free(spa->spa_spares.sav_config);
722		spa->spa_spares.sav_config = NULL;
723	}
724	spa->spa_spares.sav_count = 0;
725
726	for (i = 0; i < spa->spa_l2cache.sav_count; i++)
727		vdev_free(spa->spa_l2cache.sav_vdevs[i]);
728	if (spa->spa_l2cache.sav_vdevs) {
729		kmem_free(spa->spa_l2cache.sav_vdevs,
730		    spa->spa_l2cache.sav_count * sizeof (void *));
731		spa->spa_l2cache.sav_vdevs = NULL;
732	}
733	if (spa->spa_l2cache.sav_config) {
734		nvlist_free(spa->spa_l2cache.sav_config);
735		spa->spa_l2cache.sav_config = NULL;
736	}
737	spa->spa_l2cache.sav_count = 0;
738
739	spa->spa_async_suspended = 0;
740
741	spa_config_exit(spa, SCL_ALL, FTAG);
742}
743
744/*
745 * Load (or re-load) the current list of vdevs describing the active spares for
746 * this pool.  When this is called, we have some form of basic information in
747 * 'spa_spares.sav_config'.  We parse this into vdevs, try to open them, and
748 * then re-generate a more complete list including status information.
749 */
750static void
751spa_load_spares(spa_t *spa)
752{
753	nvlist_t **spares;
754	uint_t nspares;
755	int i;
756	vdev_t *vd, *tvd;
757
758	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
759
760	/*
761	 * First, close and free any existing spare vdevs.
762	 */
763	for (i = 0; i < spa->spa_spares.sav_count; i++) {
764		vd = spa->spa_spares.sav_vdevs[i];
765
766		/* Undo the call to spa_activate() below */
767		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
768		    B_FALSE)) != NULL && tvd->vdev_isspare)
769			spa_spare_remove(tvd);
770		vdev_close(vd);
771		vdev_free(vd);
772	}
773
774	if (spa->spa_spares.sav_vdevs)
775		kmem_free(spa->spa_spares.sav_vdevs,
776		    spa->spa_spares.sav_count * sizeof (void *));
777
778	if (spa->spa_spares.sav_config == NULL)
779		nspares = 0;
780	else
781		VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
782		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
783
784	spa->spa_spares.sav_count = (int)nspares;
785	spa->spa_spares.sav_vdevs = NULL;
786
787	if (nspares == 0)
788		return;
789
790	/*
791	 * Construct the array of vdevs, opening them to get status in the
792	 * process.   For each spare, there is potentially two different vdev_t
793	 * structures associated with it: one in the list of spares (used only
794	 * for basic validation purposes) and one in the active vdev
795	 * configuration (if it's spared in).  During this phase we open and
796	 * validate each vdev on the spare list.  If the vdev also exists in the
797	 * active configuration, then we also mark this vdev as an active spare.
798	 */
799	spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
800	    KM_SLEEP);
801	for (i = 0; i < spa->spa_spares.sav_count; i++) {
802		VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
803		    VDEV_ALLOC_SPARE) == 0);
804		ASSERT(vd != NULL);
805
806		spa->spa_spares.sav_vdevs[i] = vd;
807
808		if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
809		    B_FALSE)) != NULL) {
810			if (!tvd->vdev_isspare)
811				spa_spare_add(tvd);
812
813			/*
814			 * We only mark the spare active if we were successfully
815			 * able to load the vdev.  Otherwise, importing a pool
816			 * with a bad active spare would result in strange
817			 * behavior, because multiple pool would think the spare
818			 * is actively in use.
819			 *
820			 * There is a vulnerability here to an equally bizarre
821			 * circumstance, where a dead active spare is later
822			 * brought back to life (onlined or otherwise).  Given
823			 * the rarity of this scenario, and the extra complexity
824			 * it adds, we ignore the possibility.
825			 */
826			if (!vdev_is_dead(tvd))
827				spa_spare_activate(tvd);
828		}
829
830		vd->vdev_top = vd;
831
832		if (vdev_open(vd) != 0)
833			continue;
834
835		if (vdev_validate_aux(vd) == 0)
836			spa_spare_add(vd);
837	}
838
839	/*
840	 * Recompute the stashed list of spares, with status information
841	 * this time.
842	 */
843	VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
844	    DATA_TYPE_NVLIST_ARRAY) == 0);
845
846	spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
847	    KM_SLEEP);
848	for (i = 0; i < spa->spa_spares.sav_count; i++)
849		spares[i] = vdev_config_generate(spa,
850		    spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
851	VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
852	    ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
853	for (i = 0; i < spa->spa_spares.sav_count; i++)
854		nvlist_free(spares[i]);
855	kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
856}
857
858/*
859 * Load (or re-load) the current list of vdevs describing the active l2cache for
860 * this pool.  When this is called, we have some form of basic information in
861 * 'spa_l2cache.sav_config'.  We parse this into vdevs, try to open them, and
862 * then re-generate a more complete list including status information.
863 * Devices which are already active have their details maintained, and are
864 * not re-opened.
865 */
866static void
867spa_load_l2cache(spa_t *spa)
868{
869	nvlist_t **l2cache;
870	uint_t nl2cache;
871	int i, j, oldnvdevs;
872	uint64_t guid, size;
873	vdev_t *vd, **oldvdevs, **newvdevs;
874	spa_aux_vdev_t *sav = &spa->spa_l2cache;
875
876	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
877
878	if (sav->sav_config != NULL) {
879		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
880		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
881		newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
882	} else {
883		nl2cache = 0;
884	}
885
886	oldvdevs = sav->sav_vdevs;
887	oldnvdevs = sav->sav_count;
888	sav->sav_vdevs = NULL;
889	sav->sav_count = 0;
890
891	/*
892	 * Process new nvlist of vdevs.
893	 */
894	for (i = 0; i < nl2cache; i++) {
895		VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
896		    &guid) == 0);
897
898		newvdevs[i] = NULL;
899		for (j = 0; j < oldnvdevs; j++) {
900			vd = oldvdevs[j];
901			if (vd != NULL && guid == vd->vdev_guid) {
902				/*
903				 * Retain previous vdev for add/remove ops.
904				 */
905				newvdevs[i] = vd;
906				oldvdevs[j] = NULL;
907				break;
908			}
909		}
910
911		if (newvdevs[i] == NULL) {
912			/*
913			 * Create new vdev
914			 */
915			VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
916			    VDEV_ALLOC_L2CACHE) == 0);
917			ASSERT(vd != NULL);
918			newvdevs[i] = vd;
919
920			/*
921			 * Commit this vdev as an l2cache device,
922			 * even if it fails to open.
923			 */
924			spa_l2cache_add(vd);
925
926			vd->vdev_top = vd;
927			vd->vdev_aux = sav;
928
929			spa_l2cache_activate(vd);
930
931			if (vdev_open(vd) != 0)
932				continue;
933
934			(void) vdev_validate_aux(vd);
935
936			if (!vdev_is_dead(vd)) {
937				size = vdev_get_rsize(vd);
938				l2arc_add_vdev(spa, vd,
939				    VDEV_LABEL_START_SIZE,
940				    size - VDEV_LABEL_START_SIZE);
941			}
942		}
943	}
944
945	/*
946	 * Purge vdevs that were dropped
947	 */
948	for (i = 0; i < oldnvdevs; i++) {
949		uint64_t pool;
950
951		vd = oldvdevs[i];
952		if (vd != NULL) {
953			if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
954			    pool != 0ULL && l2arc_vdev_present(vd))
955				l2arc_remove_vdev(vd);
956			(void) vdev_close(vd);
957			spa_l2cache_remove(vd);
958		}
959	}
960
961	if (oldvdevs)
962		kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
963
964	if (sav->sav_config == NULL)
965		goto out;
966
967	sav->sav_vdevs = newvdevs;
968	sav->sav_count = (int)nl2cache;
969
970	/*
971	 * Recompute the stashed list of l2cache devices, with status
972	 * information this time.
973	 */
974	VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
975	    DATA_TYPE_NVLIST_ARRAY) == 0);
976
977	l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
978	for (i = 0; i < sav->sav_count; i++)
979		l2cache[i] = vdev_config_generate(spa,
980		    sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
981	VERIFY(nvlist_add_nvlist_array(sav->sav_config,
982	    ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
983out:
984	for (i = 0; i < sav->sav_count; i++)
985		nvlist_free(l2cache[i]);
986	if (sav->sav_count)
987		kmem_free(l2cache, sav->sav_count * sizeof (void *));
988}
989
990static int
991load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
992{
993	dmu_buf_t *db;
994	char *packed = NULL;
995	size_t nvsize = 0;
996	int error;
997	*value = NULL;
998
999	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1000	nvsize = *(uint64_t *)db->db_data;
1001	dmu_buf_rele(db, FTAG);
1002
1003	packed = kmem_alloc(nvsize, KM_SLEEP);
1004	error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
1005	if (error == 0)
1006		error = nvlist_unpack(packed, nvsize, value, 0);
1007	kmem_free(packed, nvsize);
1008
1009	return (error);
1010}
1011
1012/*
1013 * Checks to see if the given vdev could not be opened, in which case we post a
1014 * sysevent to notify the autoreplace code that the device has been removed.
1015 */
1016static void
1017spa_check_removed(vdev_t *vd)
1018{
1019	int c;
1020
1021	for (c = 0; c < vd->vdev_children; c++)
1022		spa_check_removed(vd->vdev_child[c]);
1023
1024	if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1025		zfs_post_autoreplace(vd->vdev_spa, vd);
1026		spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1027	}
1028}
1029
1030/*
1031 * Check for missing log devices
1032 */
1033int
1034spa_check_logs(spa_t *spa)
1035{
1036	switch (spa->spa_log_state) {
1037	case SPA_LOG_MISSING:
1038		/* need to recheck in case slog has been restored */
1039	case SPA_LOG_UNKNOWN:
1040		if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1041		    DS_FIND_CHILDREN)) {
1042			spa->spa_log_state = SPA_LOG_MISSING;
1043			return (1);
1044		}
1045		break;
1046
1047	case SPA_LOG_CLEAR:
1048		(void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
1049		    DS_FIND_CHILDREN);
1050		break;
1051	}
1052	spa->spa_log_state = SPA_LOG_GOOD;
1053	return (0);
1054}
1055
1056/*
1057 * Load an existing storage pool, using the pool's builtin spa_config as a
1058 * source of configuration information.
1059 */
1060static int
1061spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1062{
1063	int error = 0;
1064	nvlist_t *nvroot = NULL;
1065	vdev_t *rvd;
1066	uberblock_t *ub = &spa->spa_uberblock;
1067	uint64_t config_cache_txg = spa->spa_config_txg;
1068	uint64_t pool_guid;
1069	uint64_t version;
1070	uint64_t autoreplace = 0;
1071	int orig_mode = spa->spa_mode;
1072	char *ereport = FM_EREPORT_ZFS_POOL;
1073
1074	/*
1075	 * If this is an untrusted config, access the pool in read-only mode.
1076	 * This prevents things like resilvering recently removed devices.
1077	 */
1078	if (!mosconfig)
1079		spa->spa_mode = FREAD;
1080
1081	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1082
1083	spa->spa_load_state = state;
1084
1085	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1086	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
1087		error = EINVAL;
1088		goto out;
1089	}
1090
1091	/*
1092	 * Versioning wasn't explicitly added to the label until later, so if
1093	 * it's not present treat it as the initial version.
1094	 */
1095	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
1096		version = SPA_VERSION_INITIAL;
1097
1098	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1099	    &spa->spa_config_txg);
1100
1101	if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1102	    spa_guid_exists(pool_guid, 0)) {
1103		error = EEXIST;
1104		goto out;
1105	}
1106
1107	spa->spa_load_guid = pool_guid;
1108
1109	/*
1110	 * Create "The Godfather" zio to hold all async IOs
1111	 */
1112	if (spa->spa_async_zio_root == NULL)
1113		spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1114		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1115		    ZIO_FLAG_GODFATHER);
1116
1117	/*
1118	 * Parse the configuration into a vdev tree.  We explicitly set the
1119	 * value that will be returned by spa_version() since parsing the
1120	 * configuration requires knowing the version number.
1121	 */
1122	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1123	spa->spa_ubsync.ub_version = version;
1124	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
1125	spa_config_exit(spa, SCL_ALL, FTAG);
1126
1127	if (error != 0)
1128		goto out;
1129
1130	ASSERT(spa->spa_root_vdev == rvd);
1131	ASSERT(spa_guid(spa) == pool_guid);
1132
1133	/*
1134	 * Try to open all vdevs, loading each label in the process.
1135	 */
1136	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1137	error = vdev_open(rvd);
1138	spa_config_exit(spa, SCL_ALL, FTAG);
1139	if (error != 0)
1140		goto out;
1141
1142	/*
1143	 * We need to validate the vdev labels against the configuration that
1144	 * we have in hand, which is dependent on the setting of mosconfig. If
1145	 * mosconfig is true then we're validating the vdev labels based on
1146	 * that config. Otherwise, we're validating against the cached config
1147	 * (zpool.cache) that was read when we loaded the zfs module, and then
1148	 * later we will recursively call spa_load() and validate against
1149	 * the vdev config.
1150	 */
1151	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1152	error = vdev_validate(rvd);
1153	spa_config_exit(spa, SCL_ALL, FTAG);
1154	if (error != 0)
1155		goto out;
1156
1157	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1158		error = ENXIO;
1159		goto out;
1160	}
1161
1162	/*
1163	 * Find the best uberblock.
1164	 */
1165	vdev_uberblock_load(NULL, rvd, ub);
1166
1167	/*
1168	 * If we weren't able to find a single valid uberblock, return failure.
1169	 */
1170	if (ub->ub_txg == 0) {
1171		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1172		    VDEV_AUX_CORRUPT_DATA);
1173		error = ENXIO;
1174		goto out;
1175	}
1176
1177	/*
1178	 * If the pool is newer than the code, we can't open it.
1179	 */
1180	if (ub->ub_version > SPA_VERSION) {
1181		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1182		    VDEV_AUX_VERSION_NEWER);
1183		error = ENOTSUP;
1184		goto out;
1185	}
1186
1187	/*
1188	 * If the vdev guid sum doesn't match the uberblock, we have an
1189	 * incomplete configuration.
1190	 */
1191	if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
1192		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1193		    VDEV_AUX_BAD_GUID_SUM);
1194		error = ENXIO;
1195		goto out;
1196	}
1197
1198	/*
1199	 * Initialize internal SPA structures.
1200	 */
1201	spa->spa_state = POOL_STATE_ACTIVE;
1202	spa->spa_ubsync = spa->spa_uberblock;
1203	spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
1204	error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1205	if (error) {
1206		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1207		    VDEV_AUX_CORRUPT_DATA);
1208		goto out;
1209	}
1210	spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1211
1212	if (zap_lookup(spa->spa_meta_objset,
1213	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1214	    sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
1215		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1216		    VDEV_AUX_CORRUPT_DATA);
1217		error = EIO;
1218		goto out;
1219	}
1220
1221	if (!mosconfig) {
1222		nvlist_t *newconfig;
1223		uint64_t hostid;
1224
1225		if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
1226			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1227			    VDEV_AUX_CORRUPT_DATA);
1228			error = EIO;
1229			goto out;
1230		}
1231
1232		if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
1233		    ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1234			char *hostname;
1235			unsigned long myhostid = 0;
1236
1237			VERIFY(nvlist_lookup_string(newconfig,
1238			    ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1239
1240#ifdef	_KERNEL
1241			myhostid = zone_get_hostid(NULL);
1242#else	/* _KERNEL */
1243			/*
1244			 * We're emulating the system's hostid in userland, so
1245			 * we can't use zone_get_hostid().
1246			 */
1247			(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1248#endif	/* _KERNEL */
1249			if (hostid != 0 && myhostid != 0 &&
1250			    hostid != myhostid) {
1251				cmn_err(CE_WARN, "pool '%s' could not be "
1252				    "loaded as it was last accessed by "
1253				    "another system (host: %s hostid: 0x%lx). "
1254				    "See: http://www.sun.com/msg/ZFS-8000-EY",
1255				    spa_name(spa), hostname,
1256				    (unsigned long)hostid);
1257				error = EBADF;
1258				goto out;
1259			}
1260		}
1261
1262		spa_config_set(spa, newconfig);
1263		spa_unload(spa);
1264		spa_deactivate(spa);
1265		spa_activate(spa, orig_mode);
1266
1267		return (spa_load(spa, newconfig, state, B_TRUE));
1268	}
1269
1270	if (zap_lookup(spa->spa_meta_objset,
1271	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1272	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
1273		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1274		    VDEV_AUX_CORRUPT_DATA);
1275		error = EIO;
1276		goto out;
1277	}
1278
1279	/*
1280	 * Load the bit that tells us to use the new accounting function
1281	 * (raid-z deflation).  If we have an older pool, this will not
1282	 * be present.
1283	 */
1284	error = zap_lookup(spa->spa_meta_objset,
1285	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
1286	    sizeof (uint64_t), 1, &spa->spa_deflate);
1287	if (error != 0 && error != ENOENT) {
1288		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1289		    VDEV_AUX_CORRUPT_DATA);
1290		error = EIO;
1291		goto out;
1292	}
1293
1294	/*
1295	 * Load the persistent error log.  If we have an older pool, this will
1296	 * not be present.
1297	 */
1298	error = zap_lookup(spa->spa_meta_objset,
1299	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1300	    sizeof (uint64_t), 1, &spa->spa_errlog_last);
1301	if (error != 0 && error != ENOENT) {
1302		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1303		    VDEV_AUX_CORRUPT_DATA);
1304		error = EIO;
1305		goto out;
1306	}
1307
1308	error = zap_lookup(spa->spa_meta_objset,
1309	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1310	    sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1311	if (error != 0 && error != ENOENT) {
1312		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1313		    VDEV_AUX_CORRUPT_DATA);
1314		error = EIO;
1315		goto out;
1316	}
1317
1318	/*
1319	 * Load the history object.  If we have an older pool, this
1320	 * will not be present.
1321	 */
1322	error = zap_lookup(spa->spa_meta_objset,
1323	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
1324	    sizeof (uint64_t), 1, &spa->spa_history);
1325	if (error != 0 && error != ENOENT) {
1326		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1327		    VDEV_AUX_CORRUPT_DATA);
1328		error = EIO;
1329		goto out;
1330	}
1331
1332	/*
1333	 * Load any hot spares for this pool.
1334	 */
1335	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1336	    DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
1337	if (error != 0 && error != ENOENT) {
1338		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1339		    VDEV_AUX_CORRUPT_DATA);
1340		error = EIO;
1341		goto out;
1342	}
1343	if (error == 0) {
1344		ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
1345		if (load_nvlist(spa, spa->spa_spares.sav_object,
1346		    &spa->spa_spares.sav_config) != 0) {
1347			vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1348			    VDEV_AUX_CORRUPT_DATA);
1349			error = EIO;
1350			goto out;
1351		}
1352
1353		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1354		spa_load_spares(spa);
1355		spa_config_exit(spa, SCL_ALL, FTAG);
1356	}
1357
1358	/*
1359	 * Load any level 2 ARC devices for this pool.
1360	 */
1361	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1362	    DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
1363	    &spa->spa_l2cache.sav_object);
1364	if (error != 0 && error != ENOENT) {
1365		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1366		    VDEV_AUX_CORRUPT_DATA);
1367		error = EIO;
1368		goto out;
1369	}
1370	if (error == 0) {
1371		ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
1372		if (load_nvlist(spa, spa->spa_l2cache.sav_object,
1373		    &spa->spa_l2cache.sav_config) != 0) {
1374			vdev_set_state(rvd, B_TRUE,
1375			    VDEV_STATE_CANT_OPEN,
1376			    VDEV_AUX_CORRUPT_DATA);
1377			error = EIO;
1378			goto out;
1379		}
1380
1381		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1382		spa_load_l2cache(spa);
1383		spa_config_exit(spa, SCL_ALL, FTAG);
1384	}
1385
1386	if (spa_check_logs(spa)) {
1387		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1388		    VDEV_AUX_BAD_LOG);
1389		error = ENXIO;
1390		ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1391		goto out;
1392	}
1393
1394
1395	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1396
1397	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1398	    DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1399
1400	if (error && error != ENOENT) {
1401		vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1402		    VDEV_AUX_CORRUPT_DATA);
1403		error = EIO;
1404		goto out;
1405	}
1406
1407	if (error == 0) {
1408		(void) zap_lookup(spa->spa_meta_objset,
1409		    spa->spa_pool_props_object,
1410		    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1411		    sizeof (uint64_t), 1, &spa->spa_bootfs);
1412		(void) zap_lookup(spa->spa_meta_objset,
1413		    spa->spa_pool_props_object,
1414		    zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
1415		    sizeof (uint64_t), 1, &autoreplace);
1416		(void) zap_lookup(spa->spa_meta_objset,
1417		    spa->spa_pool_props_object,
1418		    zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1419		    sizeof (uint64_t), 1, &spa->spa_delegation);
1420		(void) zap_lookup(spa->spa_meta_objset,
1421		    spa->spa_pool_props_object,
1422		    zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
1423		    sizeof (uint64_t), 1, &spa->spa_failmode);
1424	}
1425
1426	/*
1427	 * If the 'autoreplace' property is set, then post a resource notifying
1428	 * the ZFS DE that it should not issue any faults for unopenable
1429	 * devices.  We also iterate over the vdevs, and post a sysevent for any
1430	 * unopenable vdevs so that the normal autoreplace handler can take
1431	 * over.
1432	 */
1433	if (autoreplace && state != SPA_LOAD_TRYIMPORT)
1434		spa_check_removed(spa->spa_root_vdev);
1435
1436	/*
1437	 * Load the vdev state for all toplevel vdevs.
1438	 */
1439	vdev_load(rvd);
1440
1441	/*
1442	 * Propagate the leaf DTLs we just loaded all the way up the tree.
1443	 */
1444	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1445	vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1446	spa_config_exit(spa, SCL_ALL, FTAG);
1447
1448	/*
1449	 * Check the state of the root vdev.  If it can't be opened, it
1450	 * indicates one or more toplevel vdevs are faulted.
1451	 */
1452	if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1453		error = ENXIO;
1454		goto out;
1455	}
1456
1457	if (spa_writeable(spa)) {
1458		dmu_tx_t *tx;
1459		int need_update = B_FALSE;
1460
1461		ASSERT(state != SPA_LOAD_TRYIMPORT);
1462
1463		/*
1464		 * Claim log blocks that haven't been committed yet.
1465		 * This must all happen in a single txg.
1466		 */
1467		tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1468		    spa_first_txg(spa));
1469		(void) dmu_objset_find(spa_name(spa),
1470		    zil_claim, tx, DS_FIND_CHILDREN);
1471		dmu_tx_commit(tx);
1472
1473		spa->spa_sync_on = B_TRUE;
1474		txg_sync_start(spa->spa_dsl_pool);
1475
1476		/*
1477		 * Wait for all claims to sync.
1478		 */
1479		txg_wait_synced(spa->spa_dsl_pool, 0);
1480
1481		/*
1482		 * If the config cache is stale, or we have uninitialized
1483		 * metaslabs (see spa_vdev_add()), then update the config.
1484		 */
1485		if (config_cache_txg != spa->spa_config_txg ||
1486		    state == SPA_LOAD_IMPORT)
1487			need_update = B_TRUE;
1488
1489		for (int c = 0; c < rvd->vdev_children; c++)
1490			if (rvd->vdev_child[c]->vdev_ms_array == 0)
1491				need_update = B_TRUE;
1492
1493		/*
1494		 * Update the config cache asychronously in case we're the
1495		 * root pool, in which case the config cache isn't writable yet.
1496		 */
1497		if (need_update)
1498			spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1499
1500		/*
1501		 * Check all DTLs to see if anything needs resilvering.
1502		 */
1503		if (vdev_resilver_needed(rvd, NULL, NULL))
1504			spa_async_request(spa, SPA_ASYNC_RESILVER);
1505	}
1506
1507	error = 0;
1508out:
1509	spa->spa_minref = refcount_count(&spa->spa_refcount);
1510	if (error && error != EBADF)
1511		zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1512	spa->spa_load_state = SPA_LOAD_NONE;
1513	spa->spa_ena = 0;
1514
1515	return (error);
1516}
1517
1518/*
1519 * Pool Open/Import
1520 *
1521 * The import case is identical to an open except that the configuration is sent
1522 * down from userland, instead of grabbed from the configuration cache.  For the
1523 * case of an open, the pool configuration will exist in the
1524 * POOL_STATE_UNINITIALIZED state.
1525 *
1526 * The stats information (gen/count/ustats) is used to gather vdev statistics at
1527 * the same time open the pool, without having to keep around the spa_t in some
1528 * ambiguous state.
1529 */
1530static int
1531spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1532{
1533	spa_t *spa;
1534	int error;
1535	int locked = B_FALSE;
1536
1537	*spapp = NULL;
1538
1539	/*
1540	 * As disgusting as this is, we need to support recursive calls to this
1541	 * function because dsl_dir_open() is called during spa_load(), and ends
1542	 * up calling spa_open() again.  The real fix is to figure out how to
1543	 * avoid dsl_dir_open() calling this in the first place.
1544	 */
1545	if (mutex_owner(&spa_namespace_lock) != curthread) {
1546		mutex_enter(&spa_namespace_lock);
1547		locked = B_TRUE;
1548	}
1549
1550	if ((spa = spa_lookup(pool)) == NULL) {
1551		if (locked)
1552			mutex_exit(&spa_namespace_lock);
1553		return (ENOENT);
1554	}
1555	if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1556
1557		spa_activate(spa, spa_mode_global);
1558
1559		error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1560
1561		if (error == EBADF) {
1562			/*
1563			 * If vdev_validate() returns failure (indicated by
1564			 * EBADF), it indicates that one of the vdevs indicates
1565			 * that the pool has been exported or destroyed.  If
1566			 * this is the case, the config cache is out of sync and
1567			 * we should remove the pool from the namespace.
1568			 */
1569			spa_unload(spa);
1570			spa_deactivate(spa);
1571			spa_config_sync(spa, B_TRUE, B_TRUE);
1572			spa_remove(spa);
1573			if (locked)
1574				mutex_exit(&spa_namespace_lock);
1575			return (ENOENT);
1576		}
1577
1578		if (error) {
1579			/*
1580			 * We can't open the pool, but we still have useful
1581			 * information: the state of each vdev after the
1582			 * attempted vdev_open().  Return this to the user.
1583			 */
1584			if (config != NULL && spa->spa_root_vdev != NULL)
1585				*config = spa_config_generate(spa, NULL, -1ULL,
1586				    B_TRUE);
1587			spa_unload(spa);
1588			spa_deactivate(spa);
1589			spa->spa_last_open_failed = B_TRUE;
1590			if (locked)
1591				mutex_exit(&spa_namespace_lock);
1592			*spapp = NULL;
1593			return (error);
1594		} else {
1595			spa->spa_last_open_failed = B_FALSE;
1596		}
1597	}
1598
1599	spa_open_ref(spa, tag);
1600
1601	if (locked)
1602		mutex_exit(&spa_namespace_lock);
1603
1604	*spapp = spa;
1605
1606	if (config != NULL)
1607		*config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1608
1609	return (0);
1610}
1611
1612int
1613spa_open(const char *name, spa_t **spapp, void *tag)
1614{
1615	return (spa_open_common(name, spapp, tag, NULL));
1616}
1617
1618/*
1619 * Lookup the given spa_t, incrementing the inject count in the process,
1620 * preventing it from being exported or destroyed.
1621 */
1622spa_t *
1623spa_inject_addref(char *name)
1624{
1625	spa_t *spa;
1626
1627	mutex_enter(&spa_namespace_lock);
1628	if ((spa = spa_lookup(name)) == NULL) {
1629		mutex_exit(&spa_namespace_lock);
1630		return (NULL);
1631	}
1632	spa->spa_inject_ref++;
1633	mutex_exit(&spa_namespace_lock);
1634
1635	return (spa);
1636}
1637
1638void
1639spa_inject_delref(spa_t *spa)
1640{
1641	mutex_enter(&spa_namespace_lock);
1642	spa->spa_inject_ref--;
1643	mutex_exit(&spa_namespace_lock);
1644}
1645
1646/*
1647 * Add spares device information to the nvlist.
1648 */
1649static void
1650spa_add_spares(spa_t *spa, nvlist_t *config)
1651{
1652	nvlist_t **spares;
1653	uint_t i, nspares;
1654	nvlist_t *nvroot;
1655	uint64_t guid;
1656	vdev_stat_t *vs;
1657	uint_t vsc;
1658	uint64_t pool;
1659
1660	if (spa->spa_spares.sav_count == 0)
1661		return;
1662
1663	VERIFY(nvlist_lookup_nvlist(config,
1664	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1665	VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1666	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1667	if (nspares != 0) {
1668		VERIFY(nvlist_add_nvlist_array(nvroot,
1669		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
1670		VERIFY(nvlist_lookup_nvlist_array(nvroot,
1671		    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1672
1673		/*
1674		 * Go through and find any spares which have since been
1675		 * repurposed as an active spare.  If this is the case, update
1676		 * their status appropriately.
1677		 */
1678		for (i = 0; i < nspares; i++) {
1679			VERIFY(nvlist_lookup_uint64(spares[i],
1680			    ZPOOL_CONFIG_GUID, &guid) == 0);
1681			if (spa_spare_exists(guid, &pool, NULL) &&
1682			    pool != 0ULL) {
1683				VERIFY(nvlist_lookup_uint64_array(
1684				    spares[i], ZPOOL_CONFIG_STATS,
1685				    (uint64_t **)&vs, &vsc) == 0);
1686				vs->vs_state = VDEV_STATE_CANT_OPEN;
1687				vs->vs_aux = VDEV_AUX_SPARED;
1688			}
1689		}
1690	}
1691}
1692
1693/*
1694 * Add l2cache device information to the nvlist, including vdev stats.
1695 */
1696static void
1697spa_add_l2cache(spa_t *spa, nvlist_t *config)
1698{
1699	nvlist_t **l2cache;
1700	uint_t i, j, nl2cache;
1701	nvlist_t *nvroot;
1702	uint64_t guid;
1703	vdev_t *vd;
1704	vdev_stat_t *vs;
1705	uint_t vsc;
1706
1707	if (spa->spa_l2cache.sav_count == 0)
1708		return;
1709
1710	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1711
1712	VERIFY(nvlist_lookup_nvlist(config,
1713	    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1714	VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
1715	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1716	if (nl2cache != 0) {
1717		VERIFY(nvlist_add_nvlist_array(nvroot,
1718		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
1719		VERIFY(nvlist_lookup_nvlist_array(nvroot,
1720		    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1721
1722		/*
1723		 * Update level 2 cache device stats.
1724		 */
1725
1726		for (i = 0; i < nl2cache; i++) {
1727			VERIFY(nvlist_lookup_uint64(l2cache[i],
1728			    ZPOOL_CONFIG_GUID, &guid) == 0);
1729
1730			vd = NULL;
1731			for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
1732				if (guid ==
1733				    spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
1734					vd = spa->spa_l2cache.sav_vdevs[j];
1735					break;
1736				}
1737			}
1738			ASSERT(vd != NULL);
1739
1740			VERIFY(nvlist_lookup_uint64_array(l2cache[i],
1741			    ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
1742			vdev_get_stats(vd, vs);
1743		}
1744	}
1745
1746	spa_config_exit(spa, SCL_CONFIG, FTAG);
1747}
1748
1749int
1750spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1751{
1752	int error;
1753	spa_t *spa;
1754
1755	*config = NULL;
1756	error = spa_open_common(name, &spa, FTAG, config);
1757
1758	if (spa && *config != NULL) {
1759		VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
1760		    spa_get_errlog_size(spa)) == 0);
1761
1762		if (spa_suspended(spa))
1763			VERIFY(nvlist_add_uint64(*config,
1764			    ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
1765
1766		spa_add_spares(spa, *config);
1767		spa_add_l2cache(spa, *config);
1768	}
1769
1770	/*
1771	 * We want to get the alternate root even for faulted pools, so we cheat
1772	 * and call spa_lookup() directly.
1773	 */
1774	if (altroot) {
1775		if (spa == NULL) {
1776			mutex_enter(&spa_namespace_lock);
1777			spa = spa_lookup(name);
1778			if (spa)
1779				spa_altroot(spa, altroot, buflen);
1780			else
1781				altroot[0] = '\0';
1782			spa = NULL;
1783			mutex_exit(&spa_namespace_lock);
1784		} else {
1785			spa_altroot(spa, altroot, buflen);
1786		}
1787	}
1788
1789	if (spa != NULL)
1790		spa_close(spa, FTAG);
1791
1792	return (error);
1793}
1794
1795/*
1796 * Validate that the auxiliary device array is well formed.  We must have an
1797 * array of nvlists, each which describes a valid leaf vdev.  If this is an
1798 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
1799 * specified, as long as they are well-formed.
1800 */
1801static int
1802spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
1803    spa_aux_vdev_t *sav, const char *config, uint64_t version,
1804    vdev_labeltype_t label)
1805{
1806	nvlist_t **dev;
1807	uint_t i, ndev;
1808	vdev_t *vd;
1809	int error;
1810
1811	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1812
1813	/*
1814	 * It's acceptable to have no devs specified.
1815	 */
1816	if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
1817		return (0);
1818
1819	if (ndev == 0)
1820		return (EINVAL);
1821
1822	/*
1823	 * Make sure the pool is formatted with a version that supports this
1824	 * device type.
1825	 */
1826	if (spa_version(spa) < version)
1827		return (ENOTSUP);
1828
1829	/*
1830	 * Set the pending device list so we correctly handle device in-use
1831	 * checking.
1832	 */
1833	sav->sav_pending = dev;
1834	sav->sav_npending = ndev;
1835
1836	for (i = 0; i < ndev; i++) {
1837		if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
1838		    mode)) != 0)
1839			goto out;
1840
1841		if (!vd->vdev_ops->vdev_op_leaf) {
1842			vdev_free(vd);
1843			error = EINVAL;
1844			goto out;
1845		}
1846
1847		/*
1848		 * The L2ARC currently only supports disk devices in
1849		 * kernel context.  For user-level testing, we allow it.
1850		 */
1851#ifdef _KERNEL
1852		if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
1853		    strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
1854			error = ENOTBLK;
1855			goto out;
1856		}
1857#endif
1858		vd->vdev_top = vd;
1859
1860		if ((error = vdev_open(vd)) == 0 &&
1861		    (error = vdev_label_init(vd, crtxg, label)) == 0) {
1862			VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
1863			    vd->vdev_guid) == 0);
1864		}
1865
1866		vdev_free(vd);
1867
1868		if (error &&
1869		    (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
1870			goto out;
1871		else
1872			error = 0;
1873	}
1874
1875out:
1876	sav->sav_pending = NULL;
1877	sav->sav_npending = 0;
1878	return (error);
1879}
1880
1881static int
1882spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
1883{
1884	int error;
1885
1886	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1887
1888	if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1889	    &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
1890	    VDEV_LABEL_SPARE)) != 0) {
1891		return (error);
1892	}
1893
1894	return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1895	    &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
1896	    VDEV_LABEL_L2CACHE));
1897}
1898
1899static void
1900spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
1901    const char *config)
1902{
1903	int i;
1904
1905	if (sav->sav_config != NULL) {
1906		nvlist_t **olddevs;
1907		uint_t oldndevs;
1908		nvlist_t **newdevs;
1909
1910		/*
1911		 * Generate new dev list by concatentating with the
1912		 * current dev list.
1913		 */
1914		VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
1915		    &olddevs, &oldndevs) == 0);
1916
1917		newdevs = kmem_alloc(sizeof (void *) *
1918		    (ndevs + oldndevs), KM_SLEEP);
1919		for (i = 0; i < oldndevs; i++)
1920			VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
1921			    KM_SLEEP) == 0);
1922		for (i = 0; i < ndevs; i++)
1923			VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
1924			    KM_SLEEP) == 0);
1925
1926		VERIFY(nvlist_remove(sav->sav_config, config,
1927		    DATA_TYPE_NVLIST_ARRAY) == 0);
1928
1929		VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1930		    config, newdevs, ndevs + oldndevs) == 0);
1931		for (i = 0; i < oldndevs + ndevs; i++)
1932			nvlist_free(newdevs[i]);
1933		kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
1934	} else {
1935		/*
1936		 * Generate a new dev list.
1937		 */
1938		VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
1939		    KM_SLEEP) == 0);
1940		VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
1941		    devs, ndevs) == 0);
1942	}
1943}
1944
1945/*
1946 * Stop and drop level 2 ARC devices
1947 */
1948void
1949spa_l2cache_drop(spa_t *spa)
1950{
1951	vdev_t *vd;
1952	int i;
1953	spa_aux_vdev_t *sav = &spa->spa_l2cache;
1954
1955	for (i = 0; i < sav->sav_count; i++) {
1956		uint64_t pool;
1957
1958		vd = sav->sav_vdevs[i];
1959		ASSERT(vd != NULL);
1960
1961		if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1962		    pool != 0ULL && l2arc_vdev_present(vd))
1963			l2arc_remove_vdev(vd);
1964		if (vd->vdev_isl2cache)
1965			spa_l2cache_remove(vd);
1966		vdev_clear_stats(vd);
1967		(void) vdev_close(vd);
1968	}
1969}
1970
1971/*
1972 * Pool Creation
1973 */
1974int
1975spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
1976    const char *history_str, nvlist_t *zplprops)
1977{
1978	spa_t *spa;
1979	char *altroot = NULL;
1980	vdev_t *rvd;
1981	dsl_pool_t *dp;
1982	dmu_tx_t *tx;
1983	int c, error = 0;
1984	uint64_t txg = TXG_INITIAL;
1985	nvlist_t **spares, **l2cache;
1986	uint_t nspares, nl2cache;
1987	uint64_t version;
1988
1989	/*
1990	 * If this pool already exists, return failure.
1991	 */
1992	mutex_enter(&spa_namespace_lock);
1993	if (spa_lookup(pool) != NULL) {
1994		mutex_exit(&spa_namespace_lock);
1995		return (EEXIST);
1996	}
1997
1998	/*
1999	 * Allocate a new spa_t structure.
2000	 */
2001	(void) nvlist_lookup_string(props,
2002	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2003	spa = spa_add(pool, altroot);
2004	spa_activate(spa, spa_mode_global);
2005
2006	spa->spa_uberblock.ub_txg = txg - 1;
2007
2008	if (props && (error = spa_prop_validate(spa, props))) {
2009		spa_unload(spa);
2010		spa_deactivate(spa);
2011		spa_remove(spa);
2012		mutex_exit(&spa_namespace_lock);
2013		return (error);
2014	}
2015
2016	if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2017	    &version) != 0)
2018		version = SPA_VERSION;
2019	ASSERT(version <= SPA_VERSION);
2020	spa->spa_uberblock.ub_version = version;
2021	spa->spa_ubsync = spa->spa_uberblock;
2022
2023	/*
2024	 * Create "The Godfather" zio to hold all async IOs
2025	 */
2026	if (spa->spa_async_zio_root == NULL)
2027		spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2028		    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2029		    ZIO_FLAG_GODFATHER);
2030
2031	/*
2032	 * Create the root vdev.
2033	 */
2034	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2035
2036	error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
2037
2038	ASSERT(error != 0 || rvd != NULL);
2039	ASSERT(error != 0 || spa->spa_root_vdev == rvd);
2040
2041	if (error == 0 && !zfs_allocatable_devs(nvroot))
2042		error = EINVAL;
2043
2044	if (error == 0 &&
2045	    (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2046	    (error = spa_validate_aux(spa, nvroot, txg,
2047	    VDEV_ALLOC_ADD)) == 0) {
2048		for (c = 0; c < rvd->vdev_children; c++)
2049			vdev_init(rvd->vdev_child[c], txg);
2050		vdev_config_dirty(rvd);
2051	}
2052
2053	spa_config_exit(spa, SCL_ALL, FTAG);
2054
2055	if (error != 0) {
2056		spa_unload(spa);
2057		spa_deactivate(spa);
2058		spa_remove(spa);
2059		mutex_exit(&spa_namespace_lock);
2060		return (error);
2061	}
2062
2063	/*
2064	 * Get the list of spares, if specified.
2065	 */
2066	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2067	    &spares, &nspares) == 0) {
2068		VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
2069		    KM_SLEEP) == 0);
2070		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2071		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2072		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2073		spa_load_spares(spa);
2074		spa_config_exit(spa, SCL_ALL, FTAG);
2075		spa->spa_spares.sav_sync = B_TRUE;
2076	}
2077
2078	/*
2079	 * Get the list of level 2 cache devices, if specified.
2080	 */
2081	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2082	    &l2cache, &nl2cache) == 0) {
2083		VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2084		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2085		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2086		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2087		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2088		spa_load_l2cache(spa);
2089		spa_config_exit(spa, SCL_ALL, FTAG);
2090		spa->spa_l2cache.sav_sync = B_TRUE;
2091	}
2092
2093	spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2094	spa->spa_meta_objset = dp->dp_meta_objset;
2095
2096	tx = dmu_tx_create_assigned(dp, txg);
2097
2098	/*
2099	 * Create the pool config object.
2100	 */
2101	spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
2102	    DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2103	    DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2104
2105	if (zap_add(spa->spa_meta_objset,
2106	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2107	    sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2108		cmn_err(CE_PANIC, "failed to add pool config");
2109	}
2110
2111	/* Newly created pools with the right version are always deflated. */
2112	if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
2113		spa->spa_deflate = TRUE;
2114		if (zap_add(spa->spa_meta_objset,
2115		    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
2116		    sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
2117			cmn_err(CE_PANIC, "failed to add deflate");
2118		}
2119	}
2120
2121	/*
2122	 * Create the deferred-free bplist object.  Turn off compression
2123	 * because sync-to-convergence takes longer if the blocksize
2124	 * keeps changing.
2125	 */
2126	spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2127	    1 << 14, tx);
2128	dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2129	    ZIO_COMPRESS_OFF, tx);
2130
2131	if (zap_add(spa->spa_meta_objset,
2132	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2133	    sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
2134		cmn_err(CE_PANIC, "failed to add bplist");
2135	}
2136
2137	/*
2138	 * Create the pool's history object.
2139	 */
2140	if (version >= SPA_VERSION_ZPOOL_HISTORY)
2141		spa_history_create_obj(spa, tx);
2142
2143	/*
2144	 * Set pool properties.
2145	 */
2146	spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
2147	spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2148	spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
2149	if (props != NULL) {
2150		spa_configfile_set(spa, props, B_FALSE);
2151		spa_sync_props(spa, props, CRED(), tx);
2152	}
2153
2154	dmu_tx_commit(tx);
2155
2156	spa->spa_sync_on = B_TRUE;
2157	txg_sync_start(spa->spa_dsl_pool);
2158
2159	/*
2160	 * We explicitly wait for the first transaction to complete so that our
2161	 * bean counters are appropriately updated.
2162	 */
2163	txg_wait_synced(spa->spa_dsl_pool, txg);
2164
2165	spa_config_sync(spa, B_FALSE, B_TRUE);
2166
2167	if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
2168		(void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
2169
2170	spa->spa_minref = refcount_count(&spa->spa_refcount);
2171
2172	mutex_exit(&spa_namespace_lock);
2173
2174	return (0);
2175}
2176
2177/*
2178 * Import the given pool into the system.  We set up the necessary spa_t and
2179 * then call spa_load() to do the dirty work.
2180 */
2181static int
2182spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
2183    boolean_t isroot, boolean_t allowfaulted)
2184{
2185	spa_t *spa;
2186	char *altroot = NULL;
2187	int error, loaderr;
2188	nvlist_t *nvroot;
2189	nvlist_t **spares, **l2cache;
2190	uint_t nspares, nl2cache;
2191
2192	/*
2193	 * If a pool with this name exists, return failure.
2194	 */
2195	mutex_enter(&spa_namespace_lock);
2196	if ((spa = spa_lookup(pool)) != NULL) {
2197		if (isroot) {
2198			/*
2199			 * Remove the existing root pool from the
2200			 * namespace so that we can replace it with
2201			 * the correct config we just read in.
2202			 */
2203			ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
2204			spa_remove(spa);
2205		} else {
2206			mutex_exit(&spa_namespace_lock);
2207			return (EEXIST);
2208		}
2209	}
2210
2211	/*
2212	 * Create and initialize the spa structure.
2213	 */
2214	(void) nvlist_lookup_string(props,
2215	    zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2216	spa = spa_add(pool, altroot);
2217	spa_activate(spa, spa_mode_global);
2218
2219	if (allowfaulted)
2220		spa->spa_import_faulted = B_TRUE;
2221	spa->spa_is_root = isroot;
2222
2223	/*
2224	 * Pass off the heavy lifting to spa_load().
2225	 * Pass TRUE for mosconfig (unless this is a root pool) because
2226	 * the user-supplied config is actually the one to trust when
2227	 * doing an import.
2228	 */
2229	loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2230
2231	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2232	/*
2233	 * Toss any existing sparelist, as it doesn't have any validity anymore,
2234	 * and conflicts with spa_has_spare().
2235	 */
2236	if (!isroot && spa->spa_spares.sav_config) {
2237		nvlist_free(spa->spa_spares.sav_config);
2238		spa->spa_spares.sav_config = NULL;
2239		spa_load_spares(spa);
2240	}
2241	if (!isroot && spa->spa_l2cache.sav_config) {
2242		nvlist_free(spa->spa_l2cache.sav_config);
2243		spa->spa_l2cache.sav_config = NULL;
2244		spa_load_l2cache(spa);
2245	}
2246
2247	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2248	    &nvroot) == 0);
2249	if (error == 0)
2250		error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
2251	if (error == 0)
2252		error = spa_validate_aux(spa, nvroot, -1ULL,
2253		    VDEV_ALLOC_L2CACHE);
2254	spa_config_exit(spa, SCL_ALL, FTAG);
2255
2256	if (props != NULL)
2257		spa_configfile_set(spa, props, B_FALSE);
2258
2259	if (error != 0 || (props && spa_writeable(spa) &&
2260	    (error = spa_prop_set(spa, props)))) {
2261		if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
2262			/*
2263			 * If we failed to load the pool, but 'allowfaulted' is
2264			 * set, then manually set the config as if the config
2265			 * passed in was specified in the cache file.
2266			 */
2267			error = 0;
2268			spa->spa_import_faulted = B_FALSE;
2269			if (spa->spa_config == NULL)
2270				spa->spa_config = spa_config_generate(spa,
2271				    NULL, -1ULL, B_TRUE);
2272			spa_unload(spa);
2273			spa_deactivate(spa);
2274			spa_config_sync(spa, B_FALSE, B_TRUE);
2275		} else {
2276			spa_unload(spa);
2277			spa_deactivate(spa);
2278			spa_remove(spa);
2279		}
2280		mutex_exit(&spa_namespace_lock);
2281		return (error);
2282	}
2283
2284	/*
2285	 * Override any spares and level 2 cache devices as specified by
2286	 * the user, as these may have correct device names/devids, etc.
2287	 */
2288	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2289	    &spares, &nspares) == 0) {
2290		if (spa->spa_spares.sav_config)
2291			VERIFY(nvlist_remove(spa->spa_spares.sav_config,
2292			    ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
2293		else
2294			VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
2295			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2296		VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2297		    ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2298		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2299		spa_load_spares(spa);
2300		spa_config_exit(spa, SCL_ALL, FTAG);
2301		spa->spa_spares.sav_sync = B_TRUE;
2302	}
2303	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2304	    &l2cache, &nl2cache) == 0) {
2305		if (spa->spa_l2cache.sav_config)
2306			VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
2307			    ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
2308		else
2309			VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2310			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2311		VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2312		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2313		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2314		spa_load_l2cache(spa);
2315		spa_config_exit(spa, SCL_ALL, FTAG);
2316		spa->spa_l2cache.sav_sync = B_TRUE;
2317	}
2318
2319	if (spa_writeable(spa)) {
2320		/*
2321		 * Update the config cache to include the newly-imported pool.
2322		 */
2323		spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
2324	}
2325
2326	spa->spa_import_faulted = B_FALSE;
2327	mutex_exit(&spa_namespace_lock);
2328
2329	return (0);
2330}
2331
2332#ifdef _KERNEL
2333/*
2334 * Build a "root" vdev for a top level vdev read in from a rootpool
2335 * device label.
2336 */
2337static void
2338spa_build_rootpool_config(nvlist_t *config)
2339{
2340	nvlist_t *nvtop, *nvroot;
2341	uint64_t pgid;
2342
2343	/*
2344	 * Add this top-level vdev to the child array.
2345	 */
2346	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
2347	    == 0);
2348	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
2349	    == 0);
2350
2351	/*
2352	 * Put this pool's top-level vdevs into a root vdev.
2353	 */
2354	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2355	VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
2356	    == 0);
2357	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
2358	VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
2359	VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2360	    &nvtop, 1) == 0);
2361
2362	/*
2363	 * Replace the existing vdev_tree with the new root vdev in
2364	 * this pool's configuration (remove the old, add the new).
2365	 */
2366	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
2367	nvlist_free(nvroot);
2368}
2369
2370/*
2371 * Get the root pool information from the root disk, then import the root pool
2372 * during the system boot up time.
2373 */
2374extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
2375
2376int
2377spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
2378    uint64_t *besttxg)
2379{
2380	nvlist_t *config;
2381	uint64_t txg;
2382	int error;
2383
2384	if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
2385		return (error);
2386
2387	VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
2388
2389	if (bestconf != NULL)
2390		*bestconf = config;
2391	else
2392		nvlist_free(config);
2393	*besttxg = txg;
2394	return (0);
2395}
2396
2397boolean_t
2398spa_rootdev_validate(nvlist_t *nv)
2399{
2400	uint64_t ival;
2401
2402	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2403	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2404	    nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2405		return (B_FALSE);
2406
2407	return (B_TRUE);
2408}
2409
2410
2411/*
2412 * Given the boot device's physical path or devid, check if the device
2413 * is in a valid state.  If so, return the configuration from the vdev
2414 * label.
2415 */
2416int
2417spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
2418{
2419	nvlist_t *conf = NULL;
2420	uint64_t txg = 0;
2421	nvlist_t *nvtop, **child;
2422	char *type;
2423	char *bootpath = NULL;
2424	uint_t children, c;
2425	char *tmp;
2426	int error;
2427
2428	if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
2429		*tmp = '\0';
2430	if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
2431		cmn_err(CE_NOTE, "error reading device label");
2432		return (error);
2433	}
2434	if (txg == 0) {
2435		cmn_err(CE_NOTE, "this device is detached");
2436		nvlist_free(conf);
2437		return (EINVAL);
2438	}
2439
2440	VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
2441	    &nvtop) == 0);
2442	VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
2443
2444	if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2445		if (spa_rootdev_validate(nvtop)) {
2446			goto out;
2447		} else {
2448			nvlist_free(conf);
2449			return (EINVAL);
2450		}
2451	}
2452
2453	ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
2454
2455	VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
2456	    &child, &children) == 0);
2457
2458	/*
2459	 * Go thru vdevs in the mirror to see if the given device
2460	 * has the most recent txg. Only the device with the most
2461	 * recent txg has valid information and should be booted.
2462	 */
2463	for (c = 0; c < children; c++) {
2464		char *cdevid, *cpath;
2465		uint64_t tmptxg;
2466
2467		cpath = NULL;
2468		cdevid = NULL;
2469		if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
2470		    &cpath) != 0 && nvlist_lookup_string(child[c],
2471		    ZPOOL_CONFIG_DEVID, &cdevid) != 0)
2472			return (EINVAL);
2473		if ((spa_check_rootconf(cpath, cdevid, NULL,
2474		    &tmptxg) == 0) && (tmptxg > txg)) {
2475			txg = tmptxg;
2476			VERIFY(nvlist_lookup_string(child[c],
2477			    ZPOOL_CONFIG_PATH, &bootpath) == 0);
2478		}
2479	}
2480
2481	/* Does the best device match the one we've booted from? */
2482	if (bootpath) {
2483		cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
2484		return (EINVAL);
2485	}
2486out:
2487	*bestconf = conf;
2488	return (0);
2489}
2490
2491/*
2492 * Import a root pool.
2493 *
2494 * For x86. devpath_list will consist of devid and/or physpath name of
2495 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
2496 * The GRUB "findroot" command will return the vdev we should boot.
2497 *
2498 * For Sparc, devpath_list consists the physpath name of the booting device
2499 * no matter the rootpool is a single device pool or a mirrored pool.
2500 * e.g.
2501 *	"/pci@1f,0/ide@d/disk@0,0:a"
2502 */
2503int
2504spa_import_rootpool(char *devpath, char *devid)
2505{
2506	nvlist_t *conf = NULL;
2507	char *pname;
2508	int error;
2509
2510	/*
2511	 * Get the vdev pathname and configuation from the most
2512	 * recently updated vdev (highest txg).
2513	 */
2514	if (error = spa_get_rootconf(devpath, devid, &conf))
2515		goto msg_out;
2516
2517	/*
2518	 * Add type "root" vdev to the config.
2519	 */
2520	spa_build_rootpool_config(conf);
2521
2522	VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
2523
2524	/*
2525	 * We specify 'allowfaulted' for this to be treated like spa_open()
2526	 * instead of spa_import().  This prevents us from marking vdevs as
2527	 * persistently unavailable, and generates FMA ereports as if it were a
2528	 * pool open, not import.
2529	 */
2530	error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
2531	ASSERT(error != EEXIST);
2532
2533	nvlist_free(conf);
2534	return (error);
2535
2536msg_out:
2537	cmn_err(CE_NOTE, "\n"
2538	    "  ***************************************************  \n"
2539	    "  *  This device is not bootable!                   *  \n"
2540	    "  *  It is either offlined or detached or faulted.  *  \n"
2541	    "  *  Please try to boot from a different device.    *  \n"
2542	    "  ***************************************************  ");
2543
2544	return (error);
2545}
2546#endif
2547
2548/*
2549 * Import a non-root pool into the system.
2550 */
2551int
2552spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
2553{
2554	return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
2555}
2556
2557int
2558spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
2559{
2560	return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
2561}
2562
2563
2564/*
2565 * This (illegal) pool name is used when temporarily importing a spa_t in order
2566 * to get the vdev stats associated with the imported devices.
2567 */
2568#define	TRYIMPORT_NAME	"$import"
2569
2570nvlist_t *
2571spa_tryimport(nvlist_t *tryconfig)
2572{
2573	nvlist_t *config = NULL;
2574	char *poolname;
2575	spa_t *spa;
2576	uint64_t state;
2577	int error;
2578
2579	if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2580		return (NULL);
2581
2582	if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2583		return (NULL);
2584
2585	/*
2586	 * Create and initialize the spa structure.
2587	 */
2588	mutex_enter(&spa_namespace_lock);
2589	spa = spa_add(TRYIMPORT_NAME, NULL);
2590	spa_activate(spa, FREAD);
2591
2592	/*
2593	 * Pass off the heavy lifting to spa_load().
2594	 * Pass TRUE for mosconfig because the user-supplied config
2595	 * is actually the one to trust when doing an import.
2596	 */
2597	error = spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2598
2599	/*
2600	 * If 'tryconfig' was at least parsable, return the current config.
2601	 */
2602	if (spa->spa_root_vdev != NULL) {
2603		config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2604		VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2605		    poolname) == 0);
2606		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2607		    state) == 0);
2608		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
2609		    spa->spa_uberblock.ub_timestamp) == 0);
2610
2611		/*
2612		 * If the bootfs property exists on this pool then we
2613		 * copy it out so that external consumers can tell which
2614		 * pools are bootable.
2615		 */
2616		if ((!error || error == EEXIST) && spa->spa_bootfs) {
2617			char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2618
2619			/*
2620			 * We have to play games with the name since the
2621			 * pool was opened as TRYIMPORT_NAME.
2622			 */
2623			if (dsl_dsobj_to_dsname(spa_name(spa),
2624			    spa->spa_bootfs, tmpname) == 0) {
2625				char *cp;
2626				char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2627
2628				cp = strchr(tmpname, '/');
2629				if (cp == NULL) {
2630					(void) strlcpy(dsname, tmpname,
2631					    MAXPATHLEN);
2632				} else {
2633					(void) snprintf(dsname, MAXPATHLEN,
2634					    "%s/%s", poolname, ++cp);
2635				}
2636				VERIFY(nvlist_add_string(config,
2637				    ZPOOL_CONFIG_BOOTFS, dsname) == 0);
2638				kmem_free(dsname, MAXPATHLEN);
2639			}
2640			kmem_free(tmpname, MAXPATHLEN);
2641		}
2642
2643		/*
2644		 * Add the list of hot spares and level 2 cache devices.
2645		 */
2646		spa_add_spares(spa, config);
2647		spa_add_l2cache(spa, config);
2648	}
2649
2650	spa_unload(spa);
2651	spa_deactivate(spa);
2652	spa_remove(spa);
2653	mutex_exit(&spa_namespace_lock);
2654
2655	return (config);
2656}
2657
2658/*
2659 * Pool export/destroy
2660 *
2661 * The act of destroying or exporting a pool is very simple.  We make sure there
2662 * is no more pending I/O and any references to the pool are gone.  Then, we
2663 * update the pool state and sync all the labels to disk, removing the
2664 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
2665 * we don't sync the labels or remove the configuration cache.
2666 */
2667static int
2668spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
2669    boolean_t force, boolean_t hardforce)
2670{
2671	spa_t *spa;
2672
2673	if (oldconfig)
2674		*oldconfig = NULL;
2675
2676	if (!(spa_mode_global & FWRITE))
2677		return (EROFS);
2678
2679	mutex_enter(&spa_namespace_lock);
2680	if ((spa = spa_lookup(pool)) == NULL) {
2681		mutex_exit(&spa_namespace_lock);
2682		return (ENOENT);
2683	}
2684
2685	/*
2686	 * Put a hold on the pool, drop the namespace lock, stop async tasks,
2687	 * reacquire the namespace lock, and see if we can export.
2688	 */
2689	spa_open_ref(spa, FTAG);
2690	mutex_exit(&spa_namespace_lock);
2691	spa_async_suspend(spa);
2692	mutex_enter(&spa_namespace_lock);
2693	spa_close(spa, FTAG);
2694
2695	/*
2696	 * The pool will be in core if it's openable,
2697	 * in which case we can modify its state.
2698	 */
2699	if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2700		/*
2701		 * Objsets may be open only because they're dirty, so we
2702		 * have to force it to sync before checking spa_refcnt.
2703		 */
2704		txg_wait_synced(spa->spa_dsl_pool, 0);
2705
2706		/*
2707		 * A pool cannot be exported or destroyed if there are active
2708		 * references.  If we are resetting a pool, allow references by
2709		 * fault injection handlers.
2710		 */
2711		if (!spa_refcount_zero(spa) ||
2712		    (spa->spa_inject_ref != 0 &&
2713		    new_state != POOL_STATE_UNINITIALIZED)) {
2714			spa_async_resume(spa);
2715			mutex_exit(&spa_namespace_lock);
2716			return (EBUSY);
2717		}
2718
2719		/*
2720		 * A pool cannot be exported if it has an active shared spare.
2721		 * This is to prevent other pools stealing the active spare
2722		 * from an exported pool. At user's own will, such pool can
2723		 * be forcedly exported.
2724		 */
2725		if (!force && new_state == POOL_STATE_EXPORTED &&
2726		    spa_has_active_shared_spare(spa)) {
2727			spa_async_resume(spa);
2728			mutex_exit(&spa_namespace_lock);
2729			return (EXDEV);
2730		}
2731
2732		/*
2733		 * We want this to be reflected on every label,
2734		 * so mark them all dirty.  spa_unload() will do the
2735		 * final sync that pushes these changes out.
2736		 */
2737		if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
2738			spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2739			spa->spa_state = new_state;
2740			spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
2741			vdev_config_dirty(spa->spa_root_vdev);
2742			spa_config_exit(spa, SCL_ALL, FTAG);
2743		}
2744	}
2745
2746	spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
2747
2748	if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2749		spa_unload(spa);
2750		spa_deactivate(spa);
2751	}
2752
2753	if (oldconfig && spa->spa_config)
2754		VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
2755
2756	if (new_state != POOL_STATE_UNINITIALIZED) {
2757		if (!hardforce)
2758			spa_config_sync(spa, B_TRUE, B_TRUE);
2759		spa_remove(spa);
2760	}
2761	mutex_exit(&spa_namespace_lock);
2762
2763	return (0);
2764}
2765
2766/*
2767 * Destroy a storage pool.
2768 */
2769int
2770spa_destroy(char *pool)
2771{
2772	return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
2773	    B_FALSE, B_FALSE));
2774}
2775
2776/*
2777 * Export a storage pool.
2778 */
2779int
2780spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
2781    boolean_t hardforce)
2782{
2783	return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
2784	    force, hardforce));
2785}
2786
2787/*
2788 * Similar to spa_export(), this unloads the spa_t without actually removing it
2789 * from the namespace in any way.
2790 */
2791int
2792spa_reset(char *pool)
2793{
2794	return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
2795	    B_FALSE, B_FALSE));
2796}
2797
2798/*
2799 * ==========================================================================
2800 * Device manipulation
2801 * ==========================================================================
2802 */
2803
2804/*
2805 * Add a device to a storage pool.
2806 */
2807int
2808spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2809{
2810	uint64_t txg;
2811	int error;
2812	vdev_t *rvd = spa->spa_root_vdev;
2813	vdev_t *vd, *tvd;
2814	nvlist_t **spares, **l2cache;
2815	uint_t nspares, nl2cache;
2816
2817	txg = spa_vdev_enter(spa);
2818
2819	if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
2820	    VDEV_ALLOC_ADD)) != 0)
2821		return (spa_vdev_exit(spa, NULL, txg, error));
2822
2823	spa->spa_pending_vdev = vd;	/* spa_vdev_exit() will clear this */
2824
2825	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
2826	    &nspares) != 0)
2827		nspares = 0;
2828
2829	if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
2830	    &nl2cache) != 0)
2831		nl2cache = 0;
2832
2833	if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
2834		return (spa_vdev_exit(spa, vd, txg, EINVAL));
2835
2836	if (vd->vdev_children != 0 &&
2837	    (error = vdev_create(vd, txg, B_FALSE)) != 0)
2838		return (spa_vdev_exit(spa, vd, txg, error));
2839
2840	/*
2841	 * We must validate the spares and l2cache devices after checking the
2842	 * children.  Otherwise, vdev_inuse() will blindly overwrite the spare.
2843	 */
2844	if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
2845		return (spa_vdev_exit(spa, vd, txg, error));
2846
2847	/*
2848	 * Transfer each new top-level vdev from vd to rvd.
2849	 */
2850	for (int c = 0; c < vd->vdev_children; c++) {
2851		tvd = vd->vdev_child[c];
2852		vdev_remove_child(vd, tvd);
2853		tvd->vdev_id = rvd->vdev_children;
2854		vdev_add_child(rvd, tvd);
2855		vdev_config_dirty(tvd);
2856	}
2857
2858	if (nspares != 0) {
2859		spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
2860		    ZPOOL_CONFIG_SPARES);
2861		spa_load_spares(spa);
2862		spa->spa_spares.sav_sync = B_TRUE;
2863	}
2864
2865	if (nl2cache != 0) {
2866		spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
2867		    ZPOOL_CONFIG_L2CACHE);
2868		spa_load_l2cache(spa);
2869		spa->spa_l2cache.sav_sync = B_TRUE;
2870	}
2871
2872	/*
2873	 * We have to be careful when adding new vdevs to an existing pool.
2874	 * If other threads start allocating from these vdevs before we
2875	 * sync the config cache, and we lose power, then upon reboot we may
2876	 * fail to open the pool because there are DVAs that the config cache
2877	 * can't translate.  Therefore, we first add the vdevs without
2878	 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
2879	 * and then let spa_config_update() initialize the new metaslabs.
2880	 *
2881	 * spa_load() checks for added-but-not-initialized vdevs, so that
2882	 * if we lose power at any point in this sequence, the remaining
2883	 * steps will be completed the next time we load the pool.
2884	 */
2885	(void) spa_vdev_exit(spa, vd, txg, 0);
2886
2887	mutex_enter(&spa_namespace_lock);
2888	spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
2889	mutex_exit(&spa_namespace_lock);
2890
2891	return (0);
2892}
2893
2894/*
2895 * Attach a device to a mirror.  The arguments are the path to any device
2896 * in the mirror, and the nvroot for the new device.  If the path specifies
2897 * a device that is not mirrored, we automatically insert the mirror vdev.
2898 *
2899 * If 'replacing' is specified, the new device is intended to replace the
2900 * existing device; in this case the two devices are made into their own
2901 * mirror using the 'replacing' vdev, which is functionally identical to
2902 * the mirror vdev (it actually reuses all the same ops) but has a few
2903 * extra rules: you can't attach to it after it's been created, and upon
2904 * completion of resilvering, the first disk (the one being replaced)
2905 * is automatically detached.
2906 */
2907int
2908spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2909{
2910	uint64_t txg, open_txg;
2911	vdev_t *rvd = spa->spa_root_vdev;
2912	vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
2913	vdev_ops_t *pvops;
2914	dmu_tx_t *tx;
2915	char *oldvdpath, *newvdpath;
2916	int newvd_isspare;
2917	int error;
2918
2919	txg = spa_vdev_enter(spa);
2920
2921	oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2922
2923	if (oldvd == NULL)
2924		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2925
2926	if (!oldvd->vdev_ops->vdev_op_leaf)
2927		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2928
2929	pvd = oldvd->vdev_parent;
2930
2931	if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
2932	    VDEV_ALLOC_ADD)) != 0)
2933		return (spa_vdev_exit(spa, NULL, txg, EINVAL));
2934
2935	if (newrootvd->vdev_children != 1)
2936		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2937
2938	newvd = newrootvd->vdev_child[0];
2939
2940	if (!newvd->vdev_ops->vdev_op_leaf)
2941		return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2942
2943	if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2944		return (spa_vdev_exit(spa, newrootvd, txg, error));
2945
2946	/*
2947	 * Spares can't replace logs
2948	 */
2949	if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
2950		return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2951
2952	if (!replacing) {
2953		/*
2954		 * For attach, the only allowable parent is a mirror or the root
2955		 * vdev.
2956		 */
2957		if (pvd->vdev_ops != &vdev_mirror_ops &&
2958		    pvd->vdev_ops != &vdev_root_ops)
2959			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2960
2961		pvops = &vdev_mirror_ops;
2962	} else {
2963		/*
2964		 * Active hot spares can only be replaced by inactive hot
2965		 * spares.
2966		 */
2967		if (pvd->vdev_ops == &vdev_spare_ops &&
2968		    pvd->vdev_child[1] == oldvd &&
2969		    !spa_has_spare(spa, newvd->vdev_guid))
2970			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2971
2972		/*
2973		 * If the source is a hot spare, and the parent isn't already a
2974		 * spare, then we want to create a new hot spare.  Otherwise, we
2975		 * want to create a replacing vdev.  The user is not allowed to
2976		 * attach to a spared vdev child unless the 'isspare' state is
2977		 * the same (spare replaces spare, non-spare replaces
2978		 * non-spare).
2979		 */
2980		if (pvd->vdev_ops == &vdev_replacing_ops)
2981			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2982		else if (pvd->vdev_ops == &vdev_spare_ops &&
2983		    newvd->vdev_isspare != oldvd->vdev_isspare)
2984			return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2985		else if (pvd->vdev_ops != &vdev_spare_ops &&
2986		    newvd->vdev_isspare)
2987			pvops = &vdev_spare_ops;
2988		else
2989			pvops = &vdev_replacing_ops;
2990	}
2991
2992	/*
2993	 * Compare the new device size with the replaceable/attachable
2994	 * device size.
2995	 */
2996	if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2997		return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2998
2999	/*
3000	 * The new device cannot have a higher alignment requirement
3001	 * than the top-level vdev.
3002	 */
3003	if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3004		return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3005
3006	/*
3007	 * If this is an in-place replacement, update oldvd's path and devid
3008	 * to make it distinguishable from newvd, and unopenable from now on.
3009	 */
3010	if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3011		spa_strfree(oldvd->vdev_path);
3012		oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3013		    KM_SLEEP);
3014		(void) sprintf(oldvd->vdev_path, "%s/%s",
3015		    newvd->vdev_path, "old");
3016		if (oldvd->vdev_devid != NULL) {
3017			spa_strfree(oldvd->vdev_devid);
3018			oldvd->vdev_devid = NULL;
3019		}
3020	}
3021
3022	/*
3023	 * If the parent is not a mirror, or if we're replacing, insert the new
3024	 * mirror/replacing/spare vdev above oldvd.
3025	 */
3026	if (pvd->vdev_ops != pvops)
3027		pvd = vdev_add_parent(oldvd, pvops);
3028
3029	ASSERT(pvd->vdev_top->vdev_parent == rvd);
3030	ASSERT(pvd->vdev_ops == pvops);
3031	ASSERT(oldvd->vdev_parent == pvd);
3032
3033	/*
3034	 * Extract the new device from its root and add it to pvd.
3035	 */
3036	vdev_remove_child(newrootvd, newvd);
3037	newvd->vdev_id = pvd->vdev_children;
3038	vdev_add_child(pvd, newvd);
3039
3040	/*
3041	 * If newvd is smaller than oldvd, but larger than its rsize,
3042	 * the addition of newvd may have decreased our parent's asize.
3043	 */
3044	pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
3045
3046	tvd = newvd->vdev_top;
3047	ASSERT(pvd->vdev_top == tvd);
3048	ASSERT(tvd->vdev_parent == rvd);
3049
3050	vdev_config_dirty(tvd);
3051
3052	/*
3053	 * Set newvd's DTL to [TXG_INITIAL, open_txg].  It will propagate
3054	 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
3055	 */
3056	open_txg = txg + TXG_CONCURRENT_STATES - 1;
3057
3058	vdev_dtl_dirty(newvd, DTL_MISSING,
3059	    TXG_INITIAL, open_txg - TXG_INITIAL + 1);
3060
3061	if (newvd->vdev_isspare)
3062		spa_spare_activate(newvd);
3063	oldvdpath = spa_strdup(oldvd->vdev_path);
3064	newvdpath = spa_strdup(newvd->vdev_path);
3065	newvd_isspare = newvd->vdev_isspare;
3066
3067	/*
3068	 * Mark newvd's DTL dirty in this txg.
3069	 */
3070	vdev_dirty(tvd, VDD_DTL, newvd, txg);
3071
3072	(void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
3073
3074	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
3075	if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
3076		spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
3077		    CRED(),  "%s vdev=%s %s vdev=%s",
3078		    replacing && newvd_isspare ? "spare in" :
3079		    replacing ? "replace" : "attach", newvdpath,
3080		    replacing ? "for" : "to", oldvdpath);
3081		dmu_tx_commit(tx);
3082	} else {
3083		dmu_tx_abort(tx);
3084	}
3085
3086	spa_strfree(oldvdpath);
3087	spa_strfree(newvdpath);
3088
3089	/*
3090	 * Kick off a resilver to update newvd.
3091	 */
3092	VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3093
3094	return (0);
3095}
3096
3097/*
3098 * Detach a device from a mirror or replacing vdev.
3099 * If 'replace_done' is specified, only detach if the parent
3100 * is a replacing vdev.
3101 */
3102int
3103spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3104{
3105	uint64_t txg;
3106	int error;
3107	vdev_t *rvd = spa->spa_root_vdev;
3108	vdev_t *vd, *pvd, *cvd, *tvd;
3109	boolean_t unspare = B_FALSE;
3110	uint64_t unspare_guid;
3111	size_t len;
3112
3113	txg = spa_vdev_enter(spa);
3114
3115	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3116
3117	if (vd == NULL)
3118		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3119
3120	if (!vd->vdev_ops->vdev_op_leaf)
3121		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3122
3123	pvd = vd->vdev_parent;
3124
3125	/*
3126	 * If the parent/child relationship is not as expected, don't do it.
3127	 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
3128	 * vdev that's replacing B with C.  The user's intent in replacing
3129	 * is to go from M(A,B) to M(A,C).  If the user decides to cancel
3130	 * the replace by detaching C, the expected behavior is to end up
3131	 * M(A,B).  But suppose that right after deciding to detach C,
3132	 * the replacement of B completes.  We would have M(A,C), and then
3133	 * ask to detach C, which would leave us with just A -- not what
3134	 * the user wanted.  To prevent this, we make sure that the
3135	 * parent/child relationship hasn't changed -- in this example,
3136	 * that C's parent is still the replacing vdev R.
3137	 */
3138	if (pvd->vdev_guid != pguid && pguid != 0)
3139		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3140
3141	/*
3142	 * If replace_done is specified, only remove this device if it's
3143	 * the first child of a replacing vdev.  For the 'spare' vdev, either
3144	 * disk can be removed.
3145	 */
3146	if (replace_done) {
3147		if (pvd->vdev_ops == &vdev_replacing_ops) {
3148			if (vd->vdev_id != 0)
3149				return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3150		} else if (pvd->vdev_ops != &vdev_spare_ops) {
3151			return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3152		}
3153	}
3154
3155	ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
3156	    spa_version(spa) >= SPA_VERSION_SPARES);
3157
3158	/*
3159	 * Only mirror, replacing, and spare vdevs support detach.
3160	 */
3161	if (pvd->vdev_ops != &vdev_replacing_ops &&
3162	    pvd->vdev_ops != &vdev_mirror_ops &&
3163	    pvd->vdev_ops != &vdev_spare_ops)
3164		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3165
3166	/*
3167	 * If this device has the only valid copy of some data,
3168	 * we cannot safely detach it.
3169	 */
3170	if (vdev_dtl_required(vd))
3171		return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3172
3173	ASSERT(pvd->vdev_children >= 2);
3174
3175	/*
3176	 * If we are detaching the second disk from a replacing vdev, then
3177	 * check to see if we changed the original vdev's path to have "/old"
3178	 * at the end in spa_vdev_attach().  If so, undo that change now.
3179	 */
3180	if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
3181	    pvd->vdev_child[0]->vdev_path != NULL &&
3182	    pvd->vdev_child[1]->vdev_path != NULL) {
3183		ASSERT(pvd->vdev_child[1] == vd);
3184		cvd = pvd->vdev_child[0];
3185		len = strlen(vd->vdev_path);
3186		if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
3187		    strcmp(cvd->vdev_path + len, "/old") == 0) {
3188			spa_strfree(cvd->vdev_path);
3189			cvd->vdev_path = spa_strdup(vd->vdev_path);
3190		}
3191	}
3192
3193	/*
3194	 * If we are detaching the original disk from a spare, then it implies
3195	 * that the spare should become a real disk, and be removed from the
3196	 * active spare list for the pool.
3197	 */
3198	if (pvd->vdev_ops == &vdev_spare_ops &&
3199	    vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
3200		unspare = B_TRUE;
3201
3202	/*
3203	 * Erase the disk labels so the disk can be used for other things.
3204	 * This must be done after all other error cases are handled,
3205	 * but before we disembowel vd (so we can still do I/O to it).
3206	 * But if we can't do it, don't treat the error as fatal --
3207	 * it may be that the unwritability of the disk is the reason
3208	 * it's being detached!
3209	 */
3210	error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3211
3212	/*
3213	 * Remove vd from its parent and compact the parent's children.
3214	 */
3215	vdev_remove_child(pvd, vd);
3216	vdev_compact_children(pvd);
3217
3218	/*
3219	 * Remember one of the remaining children so we can get tvd below.
3220	 */
3221	cvd = pvd->vdev_child[0];
3222
3223	/*
3224	 * If we need to remove the remaining child from the list of hot spares,
3225	 * do it now, marking the vdev as no longer a spare in the process.
3226	 * We must do this before vdev_remove_parent(), because that can
3227	 * change the GUID if it creates a new toplevel GUID.  For a similar
3228	 * reason, we must remove the spare now, in the same txg as the detach;
3229	 * otherwise someone could attach a new sibling, change the GUID, and
3230	 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
3231	 */
3232	if (unspare) {
3233		ASSERT(cvd->vdev_isspare);
3234		spa_spare_remove(cvd);
3235		unspare_guid = cvd->vdev_guid;
3236		(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3237	}
3238
3239	/*
3240	 * If the parent mirror/replacing vdev only has one child,
3241	 * the parent is no longer needed.  Remove it from the tree.
3242	 */
3243	if (pvd->vdev_children == 1)
3244		vdev_remove_parent(cvd);
3245
3246	/*
3247	 * We don't set tvd until now because the parent we just removed
3248	 * may have been the previous top-level vdev.
3249	 */
3250	tvd = cvd->vdev_top;
3251	ASSERT(tvd->vdev_parent == rvd);
3252
3253	/*
3254	 * Reevaluate the parent vdev state.
3255	 */
3256	vdev_propagate_state(cvd);
3257
3258	/*
3259	 * If the device we just detached was smaller than the others, it may be
3260	 * possible to add metaslabs (i.e. grow the pool).  vdev_metaslab_init()
3261	 * can't fail because the existing metaslabs are already in core, so
3262	 * there's nothing to read from disk.
3263	 */
3264	VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3265
3266	vdev_config_dirty(tvd);
3267
3268	/*
3269	 * Mark vd's DTL as dirty in this txg.  vdev_dtl_sync() will see that
3270	 * vd->vdev_detached is set and free vd's DTL object in syncing context.
3271	 * But first make sure we're not on any *other* txg's DTL list, to
3272	 * prevent vd from being accessed after it's freed.
3273	 */
3274	for (int t = 0; t < TXG_SIZE; t++)
3275		(void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
3276	vd->vdev_detached = B_TRUE;
3277	vdev_dirty(tvd, VDD_DTL, vd, txg);
3278
3279	spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
3280
3281	error = spa_vdev_exit(spa, vd, txg, 0);
3282
3283	/*
3284	 * If this was the removal of the original device in a hot spare vdev,
3285	 * then we want to go through and remove the device from the hot spare
3286	 * list of every other pool.
3287	 */
3288	if (unspare) {
3289		spa_t *myspa = spa;
3290		spa = NULL;
3291		mutex_enter(&spa_namespace_lock);
3292		while ((spa = spa_next(spa)) != NULL) {
3293			if (spa->spa_state != POOL_STATE_ACTIVE)
3294				continue;
3295			if (spa == myspa)
3296				continue;
3297			spa_open_ref(spa, FTAG);
3298			mutex_exit(&spa_namespace_lock);
3299			(void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3300			mutex_enter(&spa_namespace_lock);
3301			spa_close(spa, FTAG);
3302		}
3303		mutex_exit(&spa_namespace_lock);
3304	}
3305
3306	return (error);
3307}
3308
3309static nvlist_t *
3310spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
3311{
3312	for (int i = 0; i < count; i++) {
3313		uint64_t guid;
3314
3315		VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
3316		    &guid) == 0);
3317
3318		if (guid == target_guid)
3319			return (nvpp[i]);
3320	}
3321
3322	return (NULL);
3323}
3324
3325static void
3326spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
3327	nvlist_t *dev_to_remove)
3328{
3329	nvlist_t **newdev = NULL;
3330
3331	if (count > 1)
3332		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
3333
3334	for (int i = 0, j = 0; i < count; i++) {
3335		if (dev[i] == dev_to_remove)
3336			continue;
3337		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
3338	}
3339
3340	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
3341	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
3342
3343	for (int i = 0; i < count - 1; i++)
3344		nvlist_free(newdev[i]);
3345
3346	if (count > 1)
3347		kmem_free(newdev, (count - 1) * sizeof (void *));
3348}
3349
3350/*
3351 * Remove a device from the pool.  Currently, this supports removing only hot
3352 * spares and level 2 ARC devices.
3353 */
3354int
3355spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
3356{
3357	vdev_t *vd;
3358	nvlist_t **spares, **l2cache, *nv;
3359	uint_t nspares, nl2cache;
3360	uint64_t txg = 0;
3361	int error = 0;
3362	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
3363
3364	if (!locked)
3365		txg = spa_vdev_enter(spa);
3366
3367	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3368
3369	if (spa->spa_spares.sav_vdevs != NULL &&
3370	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3371	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
3372	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
3373		/*
3374		 * Only remove the hot spare if it's not currently in use
3375		 * in this pool.
3376		 */
3377		if (vd == NULL || unspare) {
3378			spa_vdev_remove_aux(spa->spa_spares.sav_config,
3379			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
3380			spa_load_spares(spa);
3381			spa->spa_spares.sav_sync = B_TRUE;
3382		} else {
3383			error = EBUSY;
3384		}
3385	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
3386	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3387	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
3388	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
3389		/*
3390		 * Cache devices can always be removed.
3391		 */
3392		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
3393		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
3394		spa_load_l2cache(spa);
3395		spa->spa_l2cache.sav_sync = B_TRUE;
3396	} else if (vd != NULL) {
3397		/*
3398		 * Normal vdevs cannot be removed (yet).
3399		 */
3400		error = ENOTSUP;
3401	} else {
3402		/*
3403		 * There is no vdev of any kind with the specified guid.
3404		 */
3405		error = ENOENT;
3406	}
3407
3408	if (!locked)
3409		return (spa_vdev_exit(spa, NULL, txg, error));
3410
3411	return (error);
3412}
3413
3414/*
3415 * Find any device that's done replacing, or a vdev marked 'unspare' that's
3416 * current spared, so we can detach it.
3417 */
3418static vdev_t *
3419spa_vdev_resilver_done_hunt(vdev_t *vd)
3420{
3421	vdev_t *newvd, *oldvd;
3422	int c;
3423
3424	for (c = 0; c < vd->vdev_children; c++) {
3425		oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
3426		if (oldvd != NULL)
3427			return (oldvd);
3428	}
3429
3430	/*
3431	 * Check for a completed replacement.
3432	 */
3433	if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
3434		oldvd = vd->vdev_child[0];
3435		newvd = vd->vdev_child[1];
3436
3437		if (vdev_dtl_empty(newvd, DTL_MISSING) &&
3438		    !vdev_dtl_required(oldvd))
3439			return (oldvd);
3440	}
3441
3442	/*
3443	 * Check for a completed resilver with the 'unspare' flag set.
3444	 */
3445	if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
3446		newvd = vd->vdev_child[0];
3447		oldvd = vd->vdev_child[1];
3448
3449		if (newvd->vdev_unspare &&
3450		    vdev_dtl_empty(newvd, DTL_MISSING) &&
3451		    !vdev_dtl_required(oldvd)) {
3452			newvd->vdev_unspare = 0;
3453			return (oldvd);
3454		}
3455	}
3456
3457	return (NULL);
3458}
3459
3460static void
3461spa_vdev_resilver_done(spa_t *spa)
3462{
3463	vdev_t *vd, *pvd, *ppvd;
3464	uint64_t guid, sguid, pguid, ppguid;
3465
3466	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3467
3468	while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
3469		pvd = vd->vdev_parent;
3470		ppvd = pvd->vdev_parent;
3471		guid = vd->vdev_guid;
3472		pguid = pvd->vdev_guid;
3473		ppguid = ppvd->vdev_guid;
3474		sguid = 0;
3475		/*
3476		 * If we have just finished replacing a hot spared device, then
3477		 * we need to detach the parent's first child (the original hot
3478		 * spare) as well.
3479		 */
3480		if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
3481			ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
3482			ASSERT(ppvd->vdev_children == 2);
3483			sguid = ppvd->vdev_child[1]->vdev_guid;
3484		}
3485		spa_config_exit(spa, SCL_ALL, FTAG);
3486		if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
3487			return;
3488		if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
3489			return;
3490		spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3491	}
3492
3493	spa_config_exit(spa, SCL_ALL, FTAG);
3494}
3495
3496/*
3497 * Update the stored path for this vdev.  Dirty the vdev configuration, relying
3498 * on spa_vdev_enter/exit() to synchronize the labels and cache.
3499 */
3500int
3501spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
3502{
3503	vdev_t *vd;
3504	uint64_t txg;
3505
3506	txg = spa_vdev_enter(spa);
3507
3508	if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
3509		/*
3510		 * Determine if this is a reference to a hot spare device.  If
3511		 * it is, update the path manually as there is no associated
3512		 * vdev_t that can be synced to disk.
3513		 */
3514		nvlist_t **spares;
3515		uint_t i, nspares;
3516
3517		if (spa->spa_spares.sav_config != NULL) {
3518			VERIFY(nvlist_lookup_nvlist_array(
3519			    spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
3520			    &spares, &nspares) == 0);
3521			for (i = 0; i < nspares; i++) {
3522				uint64_t theguid;
3523				VERIFY(nvlist_lookup_uint64(spares[i],
3524				    ZPOOL_CONFIG_GUID, &theguid) == 0);
3525				if (theguid == guid) {
3526					VERIFY(nvlist_add_string(spares[i],
3527					    ZPOOL_CONFIG_PATH, newpath) == 0);
3528					spa_load_spares(spa);
3529					spa->spa_spares.sav_sync = B_TRUE;
3530					return (spa_vdev_exit(spa, NULL, txg,
3531					    0));
3532				}
3533			}
3534		}
3535
3536		return (spa_vdev_exit(spa, NULL, txg, ENOENT));
3537	}
3538
3539	if (!vd->vdev_ops->vdev_op_leaf)
3540		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3541
3542	spa_strfree(vd->vdev_path);
3543	vd->vdev_path = spa_strdup(newpath);
3544
3545	vdev_config_dirty(vd->vdev_top);
3546
3547	return (spa_vdev_exit(spa, NULL, txg, 0));
3548}
3549
3550/*
3551 * ==========================================================================
3552 * SPA Scrubbing
3553 * ==========================================================================
3554 */
3555
3556int
3557spa_scrub(spa_t *spa, pool_scrub_type_t type)
3558{
3559	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
3560
3561	if ((uint_t)type >= POOL_SCRUB_TYPES)
3562		return (ENOTSUP);
3563
3564	/*
3565	 * If a resilver was requested, but there is no DTL on a
3566	 * writeable leaf device, we have nothing to do.
3567	 */
3568	if (type == POOL_SCRUB_RESILVER &&
3569	    !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
3570		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3571		return (0);
3572	}
3573
3574	if (type == POOL_SCRUB_EVERYTHING &&
3575	    spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
3576	    spa->spa_dsl_pool->dp_scrub_isresilver)
3577		return (EBUSY);
3578
3579	if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
3580		return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
3581	} else if (type == POOL_SCRUB_NONE) {
3582		return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
3583	} else {
3584		return (EINVAL);
3585	}
3586}
3587
3588/*
3589 * ==========================================================================
3590 * SPA async task processing
3591 * ==========================================================================
3592 */
3593
3594static void
3595spa_async_remove(spa_t *spa, vdev_t *vd)
3596{
3597	if (vd->vdev_remove_wanted) {
3598		vd->vdev_remove_wanted = 0;
3599		vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
3600		vdev_clear(spa, vd);
3601		vdev_state_dirty(vd->vdev_top);
3602	}
3603
3604	for (int c = 0; c < vd->vdev_children; c++)
3605		spa_async_remove(spa, vd->vdev_child[c]);
3606}
3607
3608static void
3609spa_async_probe(spa_t *spa, vdev_t *vd)
3610{
3611	if (vd->vdev_probe_wanted) {
3612		vd->vdev_probe_wanted = 0;
3613		vdev_reopen(vd);	/* vdev_open() does the actual probe */
3614	}
3615
3616	for (int c = 0; c < vd->vdev_children; c++)
3617		spa_async_probe(spa, vd->vdev_child[c]);
3618}
3619
3620static void
3621spa_async_thread(spa_t *spa)
3622{
3623	int tasks;
3624
3625	ASSERT(spa->spa_sync_on);
3626
3627	mutex_enter(&spa->spa_async_lock);
3628	tasks = spa->spa_async_tasks;
3629	spa->spa_async_tasks = 0;
3630	mutex_exit(&spa->spa_async_lock);
3631
3632	/*
3633	 * See if the config needs to be updated.
3634	 */
3635	if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
3636		mutex_enter(&spa_namespace_lock);
3637		spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3638		mutex_exit(&spa_namespace_lock);
3639	}
3640
3641	/*
3642	 * See if any devices need to be marked REMOVED.
3643	 */
3644	if (tasks & SPA_ASYNC_REMOVE) {
3645		spa_vdev_state_enter(spa);
3646		spa_async_remove(spa, spa->spa_root_vdev);
3647		for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
3648			spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
3649		for (int i = 0; i < spa->spa_spares.sav_count; i++)
3650			spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
3651		(void) spa_vdev_state_exit(spa, NULL, 0);
3652	}
3653
3654	/*
3655	 * See if any devices need to be probed.
3656	 */
3657	if (tasks & SPA_ASYNC_PROBE) {
3658		spa_vdev_state_enter(spa);
3659		spa_async_probe(spa, spa->spa_root_vdev);
3660		(void) spa_vdev_state_exit(spa, NULL, 0);
3661	}
3662
3663	/*
3664	 * If any devices are done replacing, detach them.
3665	 */
3666	if (tasks & SPA_ASYNC_RESILVER_DONE)
3667		spa_vdev_resilver_done(spa);
3668
3669	/*
3670	 * Kick off a resilver.
3671	 */
3672	if (tasks & SPA_ASYNC_RESILVER)
3673		VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
3674
3675	/*
3676	 * Let the world know that we're done.
3677	 */
3678	mutex_enter(&spa->spa_async_lock);
3679	spa->spa_async_thread = NULL;
3680	cv_broadcast(&spa->spa_async_cv);
3681	mutex_exit(&spa->spa_async_lock);
3682	thread_exit();
3683}
3684
3685void
3686spa_async_suspend(spa_t *spa)
3687{
3688	mutex_enter(&spa->spa_async_lock);
3689	spa->spa_async_suspended++;
3690	while (spa->spa_async_thread != NULL)
3691		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
3692	mutex_exit(&spa->spa_async_lock);
3693}
3694
3695void
3696spa_async_resume(spa_t *spa)
3697{
3698	mutex_enter(&spa->spa_async_lock);
3699	ASSERT(spa->spa_async_suspended != 0);
3700	spa->spa_async_suspended--;
3701	mutex_exit(&spa->spa_async_lock);
3702}
3703
3704static void
3705spa_async_dispatch(spa_t *spa)
3706{
3707	mutex_enter(&spa->spa_async_lock);
3708	if (spa->spa_async_tasks && !spa->spa_async_suspended &&
3709	    spa->spa_async_thread == NULL &&
3710	    rootdir != NULL && !vn_is_readonly(rootdir))
3711		spa->spa_async_thread = thread_create(NULL, 0,
3712		    spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
3713	mutex_exit(&spa->spa_async_lock);
3714}
3715
3716void
3717spa_async_request(spa_t *spa, int task)
3718{
3719	mutex_enter(&spa->spa_async_lock);
3720	spa->spa_async_tasks |= task;
3721	mutex_exit(&spa->spa_async_lock);
3722}
3723
3724/*
3725 * ==========================================================================
3726 * SPA syncing routines
3727 * ==========================================================================
3728 */
3729
3730static void
3731spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3732{
3733	bplist_t *bpl = &spa->spa_sync_bplist;
3734	dmu_tx_t *tx;
3735	blkptr_t blk;
3736	uint64_t itor = 0;
3737	zio_t *zio;
3738	int error;
3739	uint8_t c = 1;
3740
3741	zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
3742
3743	while (bplist_iterate(bpl, &itor, &blk) == 0) {
3744		ASSERT(blk.blk_birth < txg);
3745		zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
3746		    ZIO_FLAG_MUSTSUCCEED));
3747	}
3748
3749	error = zio_wait(zio);
3750	ASSERT3U(error, ==, 0);
3751
3752	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3753	bplist_vacate(bpl, tx);
3754
3755	/*
3756	 * Pre-dirty the first block so we sync to convergence faster.
3757	 * (Usually only the first block is needed.)
3758	 */
3759	dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3760	dmu_tx_commit(tx);
3761}
3762
3763static void
3764spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
3765{
3766	char *packed = NULL;
3767	size_t bufsize;
3768	size_t nvsize = 0;
3769	dmu_buf_t *db;
3770
3771	VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
3772
3773	/*
3774	 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
3775	 * information.  This avoids the dbuf_will_dirty() path and
3776	 * saves us a pre-read to get data we don't actually care about.
3777	 */
3778	bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
3779	packed = kmem_alloc(bufsize, KM_SLEEP);
3780
3781	VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
3782	    KM_SLEEP) == 0);
3783	bzero(packed + nvsize, bufsize - nvsize);
3784
3785	dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
3786
3787	kmem_free(packed, bufsize);
3788
3789	VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
3790	dmu_buf_will_dirty(db, tx);
3791	*(uint64_t *)db->db_data = nvsize;
3792	dmu_buf_rele(db, FTAG);
3793}
3794
3795static void
3796spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
3797    const char *config, const char *entry)
3798{
3799	nvlist_t *nvroot;
3800	nvlist_t **list;
3801	int i;
3802
3803	if (!sav->sav_sync)
3804		return;
3805
3806	/*
3807	 * Update the MOS nvlist describing the list of available devices.
3808	 * spa_validate_aux() will have already made sure this nvlist is
3809	 * valid and the vdevs are labeled appropriately.
3810	 */
3811	if (sav->sav_object == 0) {
3812		sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
3813		    DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
3814		    sizeof (uint64_t), tx);
3815		VERIFY(zap_update(spa->spa_meta_objset,
3816		    DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
3817		    &sav->sav_object, tx) == 0);
3818	}
3819
3820	VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3821	if (sav->sav_count == 0) {
3822		VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
3823	} else {
3824		list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
3825		for (i = 0; i < sav->sav_count; i++)
3826			list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
3827			    B_FALSE, B_FALSE, B_TRUE);
3828		VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
3829		    sav->sav_count) == 0);
3830		for (i = 0; i < sav->sav_count; i++)
3831			nvlist_free(list[i]);
3832		kmem_free(list, sav->sav_count * sizeof (void *));
3833	}
3834
3835	spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
3836	nvlist_free(nvroot);
3837
3838	sav->sav_sync = B_FALSE;
3839}
3840
3841static void
3842spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3843{
3844	nvlist_t *config;
3845
3846	if (list_is_empty(&spa->spa_config_dirty_list))
3847		return;
3848
3849	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3850
3851	config = spa_config_generate(spa, spa->spa_root_vdev,
3852	    dmu_tx_get_txg(tx), B_FALSE);
3853
3854	spa_config_exit(spa, SCL_STATE, FTAG);
3855
3856	if (spa->spa_config_syncing)
3857		nvlist_free(spa->spa_config_syncing);
3858	spa->spa_config_syncing = config;
3859
3860	spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3861}
3862
3863/*
3864 * Set zpool properties.
3865 */
3866static void
3867spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3868{
3869	spa_t *spa = arg1;
3870	objset_t *mos = spa->spa_meta_objset;
3871	nvlist_t *nvp = arg2;
3872	nvpair_t *elem;
3873	uint64_t intval;
3874	char *strval;
3875	zpool_prop_t prop;
3876	const char *propname;
3877	zprop_type_t proptype;
3878
3879	mutex_enter(&spa->spa_props_lock);
3880
3881	elem = NULL;
3882	while ((elem = nvlist_next_nvpair(nvp, elem))) {
3883		switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3884		case ZPOOL_PROP_VERSION:
3885			/*
3886			 * Only set version for non-zpool-creation cases
3887			 * (set/import). spa_create() needs special care
3888			 * for version setting.
3889			 */
3890			if (tx->tx_txg != TXG_INITIAL) {
3891				VERIFY(nvpair_value_uint64(elem,
3892				    &intval) == 0);
3893				ASSERT(intval <= SPA_VERSION);
3894				ASSERT(intval >= spa_version(spa));
3895				spa->spa_uberblock.ub_version = intval;
3896				vdev_config_dirty(spa->spa_root_vdev);
3897			}
3898			break;
3899
3900		case ZPOOL_PROP_ALTROOT:
3901			/*
3902			 * 'altroot' is a non-persistent property. It should
3903			 * have been set temporarily at creation or import time.
3904			 */
3905			ASSERT(spa->spa_root != NULL);
3906			break;
3907
3908		case ZPOOL_PROP_CACHEFILE:
3909			/*
3910			 * 'cachefile' is also a non-persisitent property.
3911			 */
3912			break;
3913		default:
3914			/*
3915			 * Set pool property values in the poolprops mos object.
3916			 */
3917			if (spa->spa_pool_props_object == 0) {
3918				objset_t *mos = spa->spa_meta_objset;
3919
3920				VERIFY((spa->spa_pool_props_object =
3921				    zap_create(mos, DMU_OT_POOL_PROPS,
3922				    DMU_OT_NONE, 0, tx)) > 0);
3923
3924				VERIFY(zap_update(mos,
3925				    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3926				    8, 1, &spa->spa_pool_props_object, tx)
3927				    == 0);
3928			}
3929
3930			/* normalize the property name */
3931			propname = zpool_prop_to_name(prop);
3932			proptype = zpool_prop_get_type(prop);
3933
3934			if (nvpair_type(elem) == DATA_TYPE_STRING) {
3935				ASSERT(proptype == PROP_TYPE_STRING);
3936				VERIFY(nvpair_value_string(elem, &strval) == 0);
3937				VERIFY(zap_update(mos,
3938				    spa->spa_pool_props_object, propname,
3939				    1, strlen(strval) + 1, strval, tx) == 0);
3940
3941			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3942				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3943
3944				if (proptype == PROP_TYPE_INDEX) {
3945					const char *unused;
3946					VERIFY(zpool_prop_index_to_string(
3947					    prop, intval, &unused) == 0);
3948				}
3949				VERIFY(zap_update(mos,
3950				    spa->spa_pool_props_object, propname,
3951				    8, 1, &intval, tx) == 0);
3952			} else {
3953				ASSERT(0); /* not allowed */
3954			}
3955
3956			switch (prop) {
3957			case ZPOOL_PROP_DELEGATION:
3958				spa->spa_delegation = intval;
3959				break;
3960			case ZPOOL_PROP_BOOTFS:
3961				spa->spa_bootfs = intval;
3962				break;
3963			case ZPOOL_PROP_FAILUREMODE:
3964				spa->spa_failmode = intval;
3965				break;
3966			default:
3967				break;
3968			}
3969		}
3970
3971		/* log internal history if this is not a zpool create */
3972		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
3973		    tx->tx_txg != TXG_INITIAL) {
3974			spa_history_internal_log(LOG_POOL_PROPSET,
3975			    spa, tx, cr, "%s %lld %s",
3976			    nvpair_name(elem), intval, spa_name(spa));
3977		}
3978	}
3979
3980	mutex_exit(&spa->spa_props_lock);
3981}
3982
3983/*
3984 * Sync the specified transaction group.  New blocks may be dirtied as
3985 * part of the process, so we iterate until it converges.
3986 */
3987void
3988spa_sync(spa_t *spa, uint64_t txg)
3989{
3990	dsl_pool_t *dp = spa->spa_dsl_pool;
3991	objset_t *mos = spa->spa_meta_objset;
3992	bplist_t *bpl = &spa->spa_sync_bplist;
3993	vdev_t *rvd = spa->spa_root_vdev;
3994	vdev_t *vd;
3995	dmu_tx_t *tx;
3996	int dirty_vdevs;
3997	int error;
3998
3999	/*
4000	 * Lock out configuration changes.
4001	 */
4002	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4003
4004	spa->spa_syncing_txg = txg;
4005	spa->spa_sync_pass = 0;
4006
4007	/*
4008	 * If there are any pending vdev state changes, convert them
4009	 * into config changes that go out with this transaction group.
4010	 */
4011	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4012	while (list_head(&spa->spa_state_dirty_list) != NULL) {
4013		/*
4014		 * We need the write lock here because, for aux vdevs,
4015		 * calling vdev_config_dirty() modifies sav_config.
4016		 * This is ugly and will become unnecessary when we
4017		 * eliminate the aux vdev wart by integrating all vdevs
4018		 * into the root vdev tree.
4019		 */
4020		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4021		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
4022		while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
4023			vdev_state_clean(vd);
4024			vdev_config_dirty(vd);
4025		}
4026		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4027		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
4028	}
4029	spa_config_exit(spa, SCL_STATE, FTAG);
4030
4031	VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
4032
4033	tx = dmu_tx_create_assigned(dp, txg);
4034
4035	/*
4036	 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
4037	 * set spa_deflate if we have no raid-z vdevs.
4038	 */
4039	if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
4040	    spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
4041		int i;
4042
4043		for (i = 0; i < rvd->vdev_children; i++) {
4044			vd = rvd->vdev_child[i];
4045			if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
4046				break;
4047		}
4048		if (i == rvd->vdev_children) {
4049			spa->spa_deflate = TRUE;
4050			VERIFY(0 == zap_add(spa->spa_meta_objset,
4051			    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
4052			    sizeof (uint64_t), 1, &spa->spa_deflate, tx));
4053		}
4054	}
4055
4056	if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
4057	    spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
4058		dsl_pool_create_origin(dp, tx);
4059
4060		/* Keeping the origin open increases spa_minref */
4061		spa->spa_minref += 3;
4062	}
4063
4064	if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
4065	    spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
4066		dsl_pool_upgrade_clones(dp, tx);
4067	}
4068
4069	/*
4070	 * If anything has changed in this txg, push the deferred frees
4071	 * from the previous txg.  If not, leave them alone so that we
4072	 * don't generate work on an otherwise idle system.
4073	 */
4074	if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
4075	    !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
4076	    !txg_list_empty(&dp->dp_sync_tasks, txg))
4077		spa_sync_deferred_frees(spa, txg);
4078
4079	/*
4080	 * Iterate to convergence.
4081	 */
4082	do {
4083		spa->spa_sync_pass++;
4084
4085		spa_sync_config_object(spa, tx);
4086		spa_sync_aux_dev(spa, &spa->spa_spares, tx,
4087		    ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
4088		spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
4089		    ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
4090		spa_errlog_sync(spa, txg);
4091		dsl_pool_sync(dp, txg);
4092
4093		dirty_vdevs = 0;
4094		while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4095			vdev_sync(vd, txg);
4096			dirty_vdevs++;
4097		}
4098
4099		bplist_sync(bpl, tx);
4100	} while (dirty_vdevs);
4101
4102	bplist_close(bpl);
4103
4104	dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4105
4106	/*
4107	 * Rewrite the vdev configuration (which includes the uberblock)
4108	 * to commit the transaction group.
4109	 *
4110	 * If there are no dirty vdevs, we sync the uberblock to a few
4111	 * random top-level vdevs that are known to be visible in the
4112	 * config cache (see spa_vdev_add() for a complete description).
4113	 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4114	 */
4115	for (;;) {
4116		/*
4117		 * We hold SCL_STATE to prevent vdev open/close/etc.
4118		 * while we're attempting to write the vdev labels.
4119		 */
4120		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4121
4122		if (list_is_empty(&spa->spa_config_dirty_list)) {
4123			vdev_t *svd[SPA_DVAS_PER_BP];
4124			int svdcount = 0;
4125			int children = rvd->vdev_children;
4126			int c0 = spa_get_random(children);
4127			int c;
4128
4129			for (c = 0; c < children; c++) {
4130				vd = rvd->vdev_child[(c0 + c) % children];
4131				if (vd->vdev_ms_array == 0 || vd->vdev_islog)
4132					continue;
4133				svd[svdcount++] = vd;
4134				if (svdcount == SPA_DVAS_PER_BP)
4135					break;
4136			}
4137			error = vdev_config_sync(svd, svdcount, txg);
4138		} else {
4139			error = vdev_config_sync(rvd->vdev_child,
4140			    rvd->vdev_children, txg);
4141		}
4142
4143		spa_config_exit(spa, SCL_STATE, FTAG);
4144
4145		if (error == 0)
4146			break;
4147		zio_suspend(spa, NULL);
4148		zio_resume_wait(spa);
4149	}
4150	dmu_tx_commit(tx);
4151
4152	/*
4153	 * Clear the dirty config list.
4154	 */
4155	while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
4156		vdev_config_clean(vd);
4157
4158	/*
4159	 * Now that the new config has synced transactionally,
4160	 * let it become visible to the config cache.
4161	 */
4162	if (spa->spa_config_syncing != NULL) {
4163		spa_config_set(spa, spa->spa_config_syncing);
4164		spa->spa_config_txg = txg;
4165		spa->spa_config_syncing = NULL;
4166	}
4167
4168	spa->spa_ubsync = spa->spa_uberblock;
4169
4170	/*
4171	 * Clean up the ZIL records for the synced txg.
4172	 */
4173	dsl_pool_zil_clean(dp);
4174
4175	/*
4176	 * Update usable space statistics.
4177	 */
4178	while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4179		vdev_sync_done(vd, txg);
4180
4181	/*
4182	 * It had better be the case that we didn't dirty anything
4183	 * since vdev_config_sync().
4184	 */
4185	ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4186	ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4187	ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4188	ASSERT(bpl->bpl_queue == NULL);
4189
4190	spa_config_exit(spa, SCL_CONFIG, FTAG);
4191
4192	/*
4193	 * If any async tasks have been requested, kick them off.
4194	 */
4195	spa_async_dispatch(spa);
4196}
4197
4198/*
4199 * Sync all pools.  We don't want to hold the namespace lock across these
4200 * operations, so we take a reference on the spa_t and drop the lock during the
4201 * sync.
4202 */
4203void
4204spa_sync_allpools(void)
4205{
4206	spa_t *spa = NULL;
4207	mutex_enter(&spa_namespace_lock);
4208	while ((spa = spa_next(spa)) != NULL) {
4209		if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4210			continue;
4211		spa_open_ref(spa, FTAG);
4212		mutex_exit(&spa_namespace_lock);
4213		txg_wait_synced(spa_get_dsl(spa), 0);
4214		mutex_enter(&spa_namespace_lock);
4215		spa_close(spa, FTAG);
4216	}
4217	mutex_exit(&spa_namespace_lock);
4218}
4219
4220/*
4221 * ==========================================================================
4222 * Miscellaneous routines
4223 * ==========================================================================
4224 */
4225
4226/*
4227 * Remove all pools in the system.
4228 */
4229void
4230spa_evict_all(void)
4231{
4232	spa_t *spa;
4233
4234	/*
4235	 * Remove all cached state.  All pools should be closed now,
4236	 * so every spa in the AVL tree should be unreferenced.
4237	 */
4238	mutex_enter(&spa_namespace_lock);
4239	while ((spa = spa_next(NULL)) != NULL) {
4240		/*
4241		 * Stop async tasks.  The async thread may need to detach
4242		 * a device that's been replaced, which requires grabbing
4243		 * spa_namespace_lock, so we must drop it here.
4244		 */
4245		spa_open_ref(spa, FTAG);
4246		mutex_exit(&spa_namespace_lock);
4247		spa_async_suspend(spa);
4248		mutex_enter(&spa_namespace_lock);
4249		spa_close(spa, FTAG);
4250
4251		if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4252			spa_unload(spa);
4253			spa_deactivate(spa);
4254		}
4255		spa_remove(spa);
4256	}
4257	mutex_exit(&spa_namespace_lock);
4258}
4259
4260vdev_t *
4261spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
4262{
4263	vdev_t *vd;
4264	int i;
4265
4266	if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
4267		return (vd);
4268
4269	if (l2cache) {
4270		for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
4271			vd = spa->spa_l2cache.sav_vdevs[i];
4272			if (vd->vdev_guid == guid)
4273				return (vd);
4274		}
4275	}
4276
4277	return (NULL);
4278}
4279
4280void
4281spa_upgrade(spa_t *spa, uint64_t version)
4282{
4283	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4284
4285	/*
4286	 * This should only be called for a non-faulted pool, and since a
4287	 * future version would result in an unopenable pool, this shouldn't be
4288	 * possible.
4289	 */
4290	ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
4291	ASSERT(version >= spa->spa_uberblock.ub_version);
4292
4293	spa->spa_uberblock.ub_version = version;
4294	vdev_config_dirty(spa->spa_root_vdev);
4295
4296	spa_config_exit(spa, SCL_ALL, FTAG);
4297
4298	txg_wait_synced(spa_get_dsl(spa), 0);
4299}
4300
4301boolean_t
4302spa_has_spare(spa_t *spa, uint64_t guid)
4303{
4304	int i;
4305	uint64_t spareguid;
4306	spa_aux_vdev_t *sav = &spa->spa_spares;
4307
4308	for (i = 0; i < sav->sav_count; i++)
4309		if (sav->sav_vdevs[i]->vdev_guid == guid)
4310			return (B_TRUE);
4311
4312	for (i = 0; i < sav->sav_npending; i++) {
4313		if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
4314		    &spareguid) == 0 && spareguid == guid)
4315			return (B_TRUE);
4316	}
4317
4318	return (B_FALSE);
4319}
4320
4321/*
4322 * Check if a pool has an active shared spare device.
4323 * Note: reference count of an active spare is 2, as a spare and as a replace
4324 */
4325static boolean_t
4326spa_has_active_shared_spare(spa_t *spa)
4327{
4328	int i, refcnt;
4329	uint64_t pool;
4330	spa_aux_vdev_t *sav = &spa->spa_spares;
4331
4332	for (i = 0; i < sav->sav_count; i++) {
4333		if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
4334		    &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
4335		    refcnt > 2)
4336			return (B_TRUE);
4337	}
4338
4339	return (B_FALSE);
4340}
4341
4342/*
4343 * Post a sysevent corresponding to the given event.  The 'name' must be one of
4344 * the event definitions in sys/sysevent/eventdefs.h.  The payload will be
4345 * filled in from the spa and (optionally) the vdev.  This doesn't do anything
4346 * in the userland libzpool, as we don't want consumers to misinterpret ztest
4347 * or zdb as real changes.
4348 */
4349void
4350spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
4351{
4352#ifdef _KERNEL
4353	sysevent_t		*ev;
4354	sysevent_attr_list_t	*attr = NULL;
4355	sysevent_value_t	value;
4356	sysevent_id_t		eid;
4357
4358	ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
4359	    SE_SLEEP);
4360
4361	value.value_type = SE_DATA_TYPE_STRING;
4362	value.value.sv_string = spa_name(spa);
4363	if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
4364		goto done;
4365
4366	value.value_type = SE_DATA_TYPE_UINT64;
4367	value.value.sv_uint64 = spa_guid(spa);
4368	if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
4369		goto done;
4370
4371	if (vd) {
4372		value.value_type = SE_DATA_TYPE_UINT64;
4373		value.value.sv_uint64 = vd->vdev_guid;
4374		if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
4375		    SE_SLEEP) != 0)
4376			goto done;
4377
4378		if (vd->vdev_path) {
4379			value.value_type = SE_DATA_TYPE_STRING;
4380			value.value.sv_string = vd->vdev_path;
4381			if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
4382			    &value, SE_SLEEP) != 0)
4383				goto done;
4384		}
4385	}
4386
4387	if (sysevent_attach_attributes(ev, attr) != 0)
4388		goto done;
4389	attr = NULL;
4390
4391	(void) log_sysevent(ev, SE_SLEEP, &eid);
4392
4393done:
4394	if (attr)
4395		sysevent_free_attr(attr);
4396	sysevent_free(ev);
4397#endif
4398}
4399