xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 9b088140)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2199653d4eSeschrock 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
248704186eSDan McDonald  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2511f6a968SSerapheim Dimitropoulos  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26ce1577b0SDave Eddy  * Copyright 2017 Joyent, Inc.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/spa.h>
303cb69f73SWill Andrews #include <sys/fm/fs/zfs.h>
31fa9e4066Sahrens #include <sys/spa_impl.h>
32fa9e4066Sahrens #include <sys/nvpair.h>
33fa9e4066Sahrens #include <sys/uio.h>
34fa9e4066Sahrens #include <sys/fs/zfs.h>
35fa9e4066Sahrens #include <sys/vdev_impl.h>
36fa9e4066Sahrens #include <sys/zfs_ioctl.h>
3795173954Sek #include <sys/utsname.h>
3895173954Sek #include <sys/systeminfo.h>
3995173954Sek #include <sys/sunddi.h>
40ad135b5dSChristopher Siden #include <sys/zfeature.h>
41ea8dc4b6Seschrock #ifdef _KERNEL
42ea8dc4b6Seschrock #include <sys/kobj.h>
435679c89fSjv #include <sys/zone.h>
44ea8dc4b6Seschrock #endif
45ea8dc4b6Seschrock 
46fa9e4066Sahrens /*
47fa9e4066Sahrens  * Pool configuration repository.
48fa9e4066Sahrens  *
492f8aaab3Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
502f8aaab3Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
512f8aaab3Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
522f8aaab3Seschrock  * property set that allows them to be stored in an alternate location until
532f8aaab3Seschrock  * the control of external software.
54fa9e4066Sahrens  *
552f8aaab3Seschrock  * For each cache file, we have a single nvlist which holds all the
562f8aaab3Seschrock  * configuration information.  When the module loads, we read this information
572f8aaab3Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
582f8aaab3Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
595cabbc6bSPrashanth Sreenivasa  * the configuration of a pool is changed, we call spa_write_cachefile(), which
602f8aaab3Seschrock  * walks through all the active pools and writes the configuration to disk.
61fa9e4066Sahrens  */
62fa9e4066Sahrens 
63fa9e4066Sahrens static uint64_t spa_config_generation = 1;
64fa9e4066Sahrens 
65fa9e4066Sahrens /*
66fa9e4066Sahrens  * This can be overridden in userland to preserve an alternate namespace for
67fa9e4066Sahrens  * userland pools when doing testing.
68fa9e4066Sahrens  */
69c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE;
70fa9e4066Sahrens 
71fa9e4066Sahrens /*
72fa9e4066Sahrens  * Called when the module is first loaded, this routine loads the configuration
73fa9e4066Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
74fa9e4066Sahrens  * only populates the namespace.
75fa9e4066Sahrens  */
76fa9e4066Sahrens void
spa_config_load(void)77fa9e4066Sahrens spa_config_load(void)
78fa9e4066Sahrens {
79fa9e4066Sahrens 	void *buf = NULL;
80fa9e4066Sahrens 	nvlist_t *nvlist, *child;
81fa9e4066Sahrens 	nvpair_t *nvpair;
82e14bb325SJeff Bonwick 	char *pathname;
83ea8dc4b6Seschrock 	struct _buf *file;
84b1b8ab34Slling 	uint64_t fsize;
85fa9e4066Sahrens 
86fa9e4066Sahrens 	/*
87fa9e4066Sahrens 	 * Open the configuration file.
88fa9e4066Sahrens 	 */
89e14bb325SJeff Bonwick 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
90e14bb325SJeff Bonwick 
91e14bb325SJeff Bonwick 	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
92c5904d13Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
93ea8dc4b6Seschrock 
94ea8dc4b6Seschrock 	file = kobj_open_file(pathname);
95e14bb325SJeff Bonwick 
96e14bb325SJeff Bonwick 	kmem_free(pathname, MAXPATHLEN);
97e14bb325SJeff Bonwick 
98ea8dc4b6Seschrock 	if (file == (struct _buf *)-1)
99fa9e4066Sahrens 		return;
100fa9e4066Sahrens 
101b1b8ab34Slling 	if (kobj_get_filesize(file, &fsize) != 0)
102fa9e4066Sahrens 		goto out;
103fa9e4066Sahrens 
104b1b8ab34Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
105fa9e4066Sahrens 
106ea8dc4b6Seschrock 	/*
107ea8dc4b6Seschrock 	 * Read the nvlist from the file.
108ea8dc4b6Seschrock 	 */
109b1b8ab34Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
110fa9e4066Sahrens 		goto out;
111fa9e4066Sahrens 
112fa9e4066Sahrens 	/*
113fa9e4066Sahrens 	 * Unpack the nvlist.
114fa9e4066Sahrens 	 */
115b1b8ab34Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
116fa9e4066Sahrens 		goto out;
117fa9e4066Sahrens 
118fa9e4066Sahrens 	/*
119fa9e4066Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
120fa9e4066Sahrens 	 * each one with the specified configuration.
121fa9e4066Sahrens 	 */
122fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
123fa9e4066Sahrens 	nvpair = NULL;
124fa9e4066Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
125fa9e4066Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
126fa9e4066Sahrens 			continue;
127fa9e4066Sahrens 
128215198a6SJoe Stein 		child = fnvpair_value_nvlist(nvpair);
129fa9e4066Sahrens 
130fa9e4066Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
131fa9e4066Sahrens 			continue;
132468c413aSTim Haley 		(void) spa_add(nvpair_name(nvpair), child, NULL);
133fa9e4066Sahrens 	}
134fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
135fa9e4066Sahrens 
136fa9e4066Sahrens 	nvlist_free(nvlist);
137fa9e4066Sahrens 
138fa9e4066Sahrens out:
139fa9e4066Sahrens 	if (buf != NULL)
140b1b8ab34Slling 		kmem_free(buf, fsize);
141fa9e4066Sahrens 
142ea8dc4b6Seschrock 	kobj_close_file(file);
143fa9e4066Sahrens }
144fa9e4066Sahrens 
1453cb69f73SWill Andrews static int
spa_config_write(spa_config_dirent_t * dp,nvlist_t * nvl)146c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1472f8aaab3Seschrock {
1482f8aaab3Seschrock 	size_t buflen;
1492f8aaab3Seschrock 	char *buf;
1502f8aaab3Seschrock 	vnode_t *vp;
1512f8aaab3Seschrock 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
152e14bb325SJeff Bonwick 	char *temp;
1533cb69f73SWill Andrews 	int err;
154c5904d13Seschrock 
155c5904d13Seschrock 	/*
156c5904d13Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
157c5904d13Seschrock 	 */
158c5904d13Seschrock 	if (nvl == NULL) {
1593cb69f73SWill Andrews 		err = vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
1603cb69f73SWill Andrews 		return (err);
161c5904d13Seschrock 	}
1622f8aaab3Seschrock 
163fa9e4066Sahrens 	/*
164fa9e4066Sahrens 	 * Pack the configuration into a buffer.
165fa9e4066Sahrens 	 */
166215198a6SJoe Stein 	buf = fnvlist_pack(nvl, &buflen);
167e14bb325SJeff Bonwick 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
168fa9e4066Sahrens 
169fa9e4066Sahrens 	/*
170fa9e4066Sahrens 	 * Write the configuration to disk.  We need to do the traditional
171fa9e4066Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
172fa9e4066Sahrens 	 * always have a consistent view of the data.
173fa9e4066Sahrens 	 */
174e14bb325SJeff Bonwick 	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
175fa9e4066Sahrens 
1763cb69f73SWill Andrews 	err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
1773cb69f73SWill Andrews 	if (err == 0) {
1783cb69f73SWill Andrews 		err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
1793cb69f73SWill Andrews 		    0, RLIM64_INFINITY, kcred, NULL);
1803cb69f73SWill Andrews 		if (err == 0)
1813cb69f73SWill Andrews 			err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
1823cb69f73SWill Andrews 		if (err == 0)
1833cb69f73SWill Andrews 			err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
184e14bb325SJeff Bonwick 		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
185e14bb325SJeff Bonwick 		VN_RELE(vp);
186fa9e4066Sahrens 	}
187fa9e4066Sahrens 
188e14bb325SJeff Bonwick 	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
189fa9e4066Sahrens 
190215198a6SJoe Stein 	fnvlist_pack_free(buf, buflen);
191e14bb325SJeff Bonwick 	kmem_free(temp, MAXPATHLEN);
1923cb69f73SWill Andrews 	return (err);
1932f8aaab3Seschrock }
1942f8aaab3Seschrock 
1952f8aaab3Seschrock /*
196c5904d13Seschrock  * Synchronize pool configuration to disk.  This must be called with the
197b4952e17SGeorge Wilson  * namespace lock held. Synchronizing the pool cache is typically done after
198b4952e17SGeorge Wilson  * the configuration has been synced to the MOS. This exposes a window where
199b4952e17SGeorge Wilson  * the MOS config will have been updated but the cache file has not. If
200b4952e17SGeorge Wilson  * the system were to crash at that instant then the cached config may not
2015cabbc6bSPrashanth Sreenivasa  * contain the correct information to open the pool and an explicit import
202b4952e17SGeorge Wilson  * would be required.
2032f8aaab3Seschrock  */
2042f8aaab3Seschrock void
spa_write_cachefile(spa_t * target,boolean_t removing,boolean_t postsysevent)2055cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent)
2062f8aaab3Seschrock {
207c5904d13Seschrock 	spa_config_dirent_t *dp, *tdp;
208c5904d13Seschrock 	nvlist_t *nvl;
2093cb69f73SWill Andrews 	boolean_t ccw_failure;
2103cb69f73SWill Andrews 	int error;
21104e56356SAndriy Gapon 	char *pool_name;
2122f8aaab3Seschrock 
2132f8aaab3Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2142f8aaab3Seschrock 
2154f0f5e5bSVictor Latushkin 	if (rootdir == NULL || !(spa_mode_global & FWRITE))
2169fddc96aSGeorge Wilson 		return;
2179fddc96aSGeorge Wilson 
218c5904d13Seschrock 	/*
219c5904d13Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
220c5904d13Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
221c5904d13Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
222c5904d13Seschrock 	 */
2233cb69f73SWill Andrews 	ccw_failure = B_FALSE;
224c5904d13Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
225c5904d13Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
226e14bb325SJeff Bonwick 		spa_t *spa = NULL;
227c5904d13Seschrock 		if (dp->scd_path == NULL)
228c5904d13Seschrock 			continue;
229c5904d13Seschrock 
230c5904d13Seschrock 		/*
231c5904d13Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
232c5904d13Seschrock 		 */
233c5904d13Seschrock 		nvl = NULL;
234c5904d13Seschrock 		while ((spa = spa_next(spa)) != NULL) {
235fb02ae02SGeorge Wilson 			/*
236fb02ae02SGeorge Wilson 			 * Skip over our own pool if we're about to remove
237fb02ae02SGeorge Wilson 			 * ourselves from the spa namespace or any pool that
238fb02ae02SGeorge Wilson 			 * is readonly. Since we cannot guarantee that a
239fb02ae02SGeorge Wilson 			 * readonly pool would successfully import upon reboot,
240fb02ae02SGeorge Wilson 			 * we don't allow them to be written to the cache file.
241fb02ae02SGeorge Wilson 			 */
242fb02ae02SGeorge Wilson 			if ((spa == target && removing) ||
243fb02ae02SGeorge Wilson 			    !spa_writeable(spa))
244c5904d13Seschrock 				continue;
245c5904d13Seschrock 
246e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
247c5904d13Seschrock 			tdp = list_head(&spa->spa_config_list);
248e14bb325SJeff Bonwick 			if (spa->spa_config == NULL ||
249e14bb325SJeff Bonwick 			    tdp->scd_path == NULL ||
250e14bb325SJeff Bonwick 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
251e14bb325SJeff Bonwick 				mutex_exit(&spa->spa_props_lock);
252c5904d13Seschrock 				continue;
253e14bb325SJeff Bonwick 			}
254c5904d13Seschrock 
255c5904d13Seschrock 			if (nvl == NULL)
256215198a6SJoe Stein 				nvl = fnvlist_alloc();
257c5904d13Seschrock 
25804e56356SAndriy Gapon 			if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
25904e56356SAndriy Gapon 				pool_name = fnvlist_lookup_string(
26004e56356SAndriy Gapon 				    spa->spa_config, ZPOOL_CONFIG_POOL_NAME);
26104e56356SAndriy Gapon 			} else {
26204e56356SAndriy Gapon 				pool_name = spa_name(spa);
26304e56356SAndriy Gapon 			}
26404e56356SAndriy Gapon 
26504e56356SAndriy Gapon 			fnvlist_add_nvlist(nvl, pool_name,
266215198a6SJoe Stein 			    spa->spa_config);
267e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
268c5904d13Seschrock 		}
269c5904d13Seschrock 
2703cb69f73SWill Andrews 		error = spa_config_write(dp, nvl);
2713cb69f73SWill Andrews 		if (error != 0)
2723cb69f73SWill Andrews 			ccw_failure = B_TRUE;
273c5904d13Seschrock 		nvlist_free(nvl);
274c5904d13Seschrock 	}
2752f8aaab3Seschrock 
2763cb69f73SWill Andrews 	if (ccw_failure) {
2773cb69f73SWill Andrews 		/*
2783cb69f73SWill Andrews 		 * Keep trying so that configuration data is
2793cb69f73SWill Andrews 		 * written if/when any temporary filesystem
2803cb69f73SWill Andrews 		 * resource issues are resolved.
2813cb69f73SWill Andrews 		 */
2823cb69f73SWill Andrews 		if (target->spa_ccw_fail_time == 0) {
283*9b088140SToomas Soome 			(void) zfs_ereport_post(
284*9b088140SToomas Soome 			    FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
285eb633035STom Caputi 			    target, NULL, NULL, NULL, 0, 0);
2863cb69f73SWill Andrews 		}
2873cb69f73SWill Andrews 		target->spa_ccw_fail_time = gethrtime();
2883cb69f73SWill Andrews 		spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
2893cb69f73SWill Andrews 	} else {
2903cb69f73SWill Andrews 		/*
2913cb69f73SWill Andrews 		 * Do not rate limit future attempts to update
2923cb69f73SWill Andrews 		 * the config cache.
2933cb69f73SWill Andrews 		 */
2943cb69f73SWill Andrews 		target->spa_ccw_fail_time = 0;
2953cb69f73SWill Andrews 	}
2963cb69f73SWill Andrews 
2972f8aaab3Seschrock 	/*
298c5904d13Seschrock 	 * Remove any config entries older than the current one.
2992f8aaab3Seschrock 	 */
300c5904d13Seschrock 	dp = list_head(&target->spa_config_list);
301c5904d13Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
302c5904d13Seschrock 		list_remove(&target->spa_config_list, tdp);
303c5904d13Seschrock 		if (tdp->scd_path != NULL)
304c5904d13Seschrock 			spa_strfree(tdp->scd_path);
305c5904d13Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
3062f8aaab3Seschrock 	}
3072f8aaab3Seschrock 
3082f8aaab3Seschrock 	spa_config_generation++;
309c5904d13Seschrock 
310c5904d13Seschrock 	if (postsysevent)
311ce1577b0SDave Eddy 		spa_event_notify(target, NULL, NULL, ESC_ZFS_CONFIG_SYNC);
312fa9e4066Sahrens }
313fa9e4066Sahrens 
314fa9e4066Sahrens /*
3150373e76bSbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
316fa9e4066Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
317fa9e4066Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
318fa9e4066Sahrens  * information for all pool visible within the zone.
319fa9e4066Sahrens  */
320fa9e4066Sahrens nvlist_t *
spa_all_configs(uint64_t * generation)321fa9e4066Sahrens spa_all_configs(uint64_t *generation)
322fa9e4066Sahrens {
323fa9e4066Sahrens 	nvlist_t *pools;
324e14bb325SJeff Bonwick 	spa_t *spa = NULL;
325fa9e4066Sahrens 
326fa9e4066Sahrens 	if (*generation == spa_config_generation)
327fa9e4066Sahrens 		return (NULL);
328fa9e4066Sahrens 
329215198a6SJoe Stein 	pools = fnvlist_alloc();
330fa9e4066Sahrens 
331fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
332fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
333fa9e4066Sahrens 		if (INGLOBALZONE(curproc) ||
334fa9e4066Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
335e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
336215198a6SJoe Stein 			fnvlist_add_nvlist(pools, spa_name(spa),
337215198a6SJoe Stein 			    spa->spa_config);
338e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
339fa9e4066Sahrens 		}
340fa9e4066Sahrens 	}
341fa9e4066Sahrens 	*generation = spa_config_generation;
342e14bb325SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	return (pools);
345fa9e4066Sahrens }
346fa9e4066Sahrens 
347fa9e4066Sahrens void
spa_config_set(spa_t * spa,nvlist_t * config)348fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
349fa9e4066Sahrens {
350e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
3516f793812SPavel Zakharov 	if (spa->spa_config != NULL && spa->spa_config != config)
3526f793812SPavel Zakharov 		nvlist_free(spa->spa_config);
353fa9e4066Sahrens 	spa->spa_config = config;
354e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
355fa9e4066Sahrens }
356fa9e4066Sahrens 
357fa9e4066Sahrens /*
358fa9e4066Sahrens  * Generate the pool's configuration based on the current in-core state.
359f7170741SWill Andrews  *
360fa9e4066Sahrens  * We infer whether to generate a complete config or just one top-level config
361fa9e4066Sahrens  * based on whether vd is the root vdev.
362fa9e4066Sahrens  */
363fa9e4066Sahrens nvlist_t *
spa_config_generate(spa_t * spa,vdev_t * vd,uint64_t txg,int getstats)364fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
365fa9e4066Sahrens {
366fa9e4066Sahrens 	nvlist_t *config, *nvroot;
367fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
36895173954Sek 	unsigned long hostid = 0;
369e14bb325SJeff Bonwick 	boolean_t locked = B_FALSE;
3701195e687SMark J Musante 	uint64_t split_guid;
37104e56356SAndriy Gapon 	char *pool_name;
372fa9e4066Sahrens 
373e14bb325SJeff Bonwick 	if (vd == NULL) {
374fa9e4066Sahrens 		vd = rvd;
375e14bb325SJeff Bonwick 		locked = B_TRUE;
376e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
377e14bb325SJeff Bonwick 	}
378e14bb325SJeff Bonwick 
379e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
380e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE));
381fa9e4066Sahrens 
382fa9e4066Sahrens 	/*
383fa9e4066Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
384fa9e4066Sahrens 	 */
385fa9e4066Sahrens 	if (txg == -1ULL)
386fa9e4066Sahrens 		txg = spa->spa_config_txg;
387fa9e4066Sahrens 
38804e56356SAndriy Gapon 	/*
38904e56356SAndriy Gapon 	 * Originally, users had to handle spa namespace collisions by either
39004e56356SAndriy Gapon 	 * exporting the already imported pool or by specifying a new name for
39104e56356SAndriy Gapon 	 * the pool with a conflicting name. In the case of root pools from
39204e56356SAndriy Gapon 	 * virtual guests, neither approach to collision resolution is
39304e56356SAndriy Gapon 	 * reasonable. This is addressed by extending the new name syntax with
39404e56356SAndriy Gapon 	 * an option to specify that the new name is temporary. When specified,
39504e56356SAndriy Gapon 	 * ZFS_IMPORT_TEMP_NAME will be set in spa->spa_import_flags to tell us
39604e56356SAndriy Gapon 	 * to use the previous name, which we do below.
39704e56356SAndriy Gapon 	 */
39804e56356SAndriy Gapon 	if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
39904e56356SAndriy Gapon 		pool_name = fnvlist_lookup_string(spa->spa_config,
40004e56356SAndriy Gapon 		    ZPOOL_CONFIG_POOL_NAME);
40104e56356SAndriy Gapon 	} else {
40204e56356SAndriy Gapon 		pool_name = spa_name(spa);
40304e56356SAndriy Gapon 	}
40404e56356SAndriy Gapon 
405215198a6SJoe Stein 	config = fnvlist_alloc();
4068704186eSDan McDonald 
407215198a6SJoe Stein 	fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
40804e56356SAndriy Gapon 	fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, pool_name);
409215198a6SJoe Stein 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, spa_state(spa));
410215198a6SJoe Stein 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, txg);
411215198a6SJoe Stein 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
412eb633035STom Caputi 	fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, spa->spa_errata);
413215198a6SJoe Stein 	if (spa->spa_comment != NULL) {
414215198a6SJoe Stein 		fnvlist_add_string(config, ZPOOL_CONFIG_COMMENT,
415215198a6SJoe Stein 		    spa->spa_comment);
416215198a6SJoe Stein 	}
4178704186eSDan McDonald 
418e0f1c0afSOlaf Faaland 	hostid = spa_get_hostid();
41917194a52Slling 	if (hostid != 0) {
420215198a6SJoe Stein 		fnvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, hostid);
42117194a52Slling 	}
422215198a6SJoe Stein 	fnvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, utsname.nodename);
423fa9e4066Sahrens 
424215198a6SJoe Stein 	int config_gen_flags = 0;
425fa9e4066Sahrens 	if (vd != rvd) {
426215198a6SJoe Stein 		fnvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
427215198a6SJoe Stein 		    vd->vdev_top->vdev_guid);
428215198a6SJoe Stein 		fnvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
429215198a6SJoe Stein 		    vd->vdev_guid);
430215198a6SJoe Stein 		if (vd->vdev_isspare) {
431215198a6SJoe Stein 			fnvlist_add_uint64(config,
432215198a6SJoe Stein 			    ZPOOL_CONFIG_IS_SPARE, 1ULL);
433215198a6SJoe Stein 		}
434215198a6SJoe Stein 		if (vd->vdev_islog) {
435215198a6SJoe Stein 			fnvlist_add_uint64(config,
436215198a6SJoe Stein 			    ZPOOL_CONFIG_IS_LOG, 1ULL);
437215198a6SJoe Stein 		}
438fa9e4066Sahrens 		vd = vd->vdev_top;		/* label contains top config */
4391195e687SMark J Musante 	} else {
4401195e687SMark J Musante 		/*
4411195e687SMark J Musante 		 * Only add the (potentially large) split information
4421195e687SMark J Musante 		 * in the mos config, and not in the vdev labels
4431195e687SMark J Musante 		 */
4441195e687SMark J Musante 		if (spa->spa_config_splitting != NULL)
445215198a6SJoe Stein 			fnvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
446215198a6SJoe Stein 			    spa->spa_config_splitting);
447215198a6SJoe Stein 		fnvlist_add_boolean(config,
448215198a6SJoe Stein 		    ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS);
449215198a6SJoe Stein 
450215198a6SJoe Stein 		config_gen_flags |= VDEV_CONFIG_MOS;
451fa9e4066Sahrens 	}
452fa9e4066Sahrens 
45388ecc943SGeorge Wilson 	/*
45488ecc943SGeorge Wilson 	 * Add the top-level config.  We even add this on pools which
4554b964adaSGeorge Wilson 	 * don't support holes in the namespace.
45688ecc943SGeorge Wilson 	 */
45788ecc943SGeorge Wilson 	vdev_top_config_generate(spa, config);
45888ecc943SGeorge Wilson 
4591195e687SMark J Musante 	/*
4601195e687SMark J Musante 	 * If we're splitting, record the original pool's guid.
4611195e687SMark J Musante 	 */
4621195e687SMark J Musante 	if (spa->spa_config_splitting != NULL &&
4631195e687SMark J Musante 	    nvlist_lookup_uint64(spa->spa_config_splitting,
4641195e687SMark J Musante 	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
465215198a6SJoe Stein 		fnvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
466215198a6SJoe Stein 		    split_guid);
4671195e687SMark J Musante 	}
4681195e687SMark J Musante 
469215198a6SJoe Stein 	nvroot = vdev_config_generate(spa, vd, getstats, config_gen_flags);
470215198a6SJoe Stein 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot);
471fa9e4066Sahrens 	nvlist_free(nvroot);
472fa9e4066Sahrens 
473ad135b5dSChristopher Siden 	/*
474ad135b5dSChristopher Siden 	 * Store what's necessary for reading the MOS in the label.
475ad135b5dSChristopher Siden 	 */
476215198a6SJoe Stein 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
477215198a6SJoe Stein 	    spa->spa_label_features);
478ad135b5dSChristopher Siden 
4799eb19f4dSGeorge Wilson 	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
4809eb19f4dSGeorge Wilson 		ddt_histogram_t *ddh;
4819eb19f4dSGeorge Wilson 		ddt_stat_t *dds;
4829eb19f4dSGeorge Wilson 		ddt_object_t *ddo;
4839eb19f4dSGeorge Wilson 
4849eb19f4dSGeorge Wilson 		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
4859eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, ddh);
486215198a6SJoe Stein 		fnvlist_add_uint64_array(config,
4879eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_HISTOGRAM,
488215198a6SJoe Stein 		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t));
4899eb19f4dSGeorge Wilson 		kmem_free(ddh, sizeof (ddt_histogram_t));
4909eb19f4dSGeorge Wilson 
4919eb19f4dSGeorge Wilson 		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
4929eb19f4dSGeorge Wilson 		ddt_get_dedup_object_stats(spa, ddo);
493215198a6SJoe Stein 		fnvlist_add_uint64_array(config,
4949eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_OBJ_STATS,
495215198a6SJoe Stein 		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t));
4969eb19f4dSGeorge Wilson 		kmem_free(ddo, sizeof (ddt_object_t));
4979eb19f4dSGeorge Wilson 
4989eb19f4dSGeorge Wilson 		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
4999eb19f4dSGeorge Wilson 		ddt_get_dedup_stats(spa, dds);
500215198a6SJoe Stein 		fnvlist_add_uint64_array(config,
5019eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_STATS,
502215198a6SJoe Stein 		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t));
5039eb19f4dSGeorge Wilson 		kmem_free(dds, sizeof (ddt_stat_t));
5049eb19f4dSGeorge Wilson 	}
5059eb19f4dSGeorge Wilson 
506e14bb325SJeff Bonwick 	if (locked)
507e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
508e14bb325SJeff Bonwick 
509fa9e4066Sahrens 	return (config);
510fa9e4066Sahrens }
5110373e76bSbonwick 
512e7cbe64fSgw /*
513e7cbe64fSgw  * Update all disk labels, generate a fresh config based on the current
514e7cbe64fSgw  * in-core state, and sync the global config cache (do not sync the config
515e7cbe64fSgw  * cache if this is a booting rootpool).
516e7cbe64fSgw  */
517e7cbe64fSgw void
spa_config_update(spa_t * spa,int what)518bc758434SLin Ling spa_config_update(spa_t *spa, int what)
5190373e76bSbonwick {
5200373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
5210373e76bSbonwick 	uint64_t txg;
5220373e76bSbonwick 	int c;
5230373e76bSbonwick 
5240373e76bSbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5250373e76bSbonwick 
526e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5270373e76bSbonwick 	txg = spa_last_synced_txg(spa) + 1;
5280373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
5290373e76bSbonwick 		vdev_config_dirty(rvd);
5300373e76bSbonwick 	} else {
5310373e76bSbonwick 		/*
5320373e76bSbonwick 		 * If we have top-level vdevs that were added but have
5330373e76bSbonwick 		 * not yet been prepared for allocation, do that now.
5340373e76bSbonwick 		 * (It's safe now because the config cache is up to date,
5350373e76bSbonwick 		 * so it will be able to translate the new DVAs.)
5360373e76bSbonwick 		 * See comments in spa_vdev_add() for full details.
5370373e76bSbonwick 		 */
5380373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
5390373e76bSbonwick 			vdev_t *tvd = rvd->vdev_child[c];
54011f6a968SSerapheim Dimitropoulos 
54111f6a968SSerapheim Dimitropoulos 			/*
54211f6a968SSerapheim Dimitropoulos 			 * Explicitly skip vdevs that are indirect or
54311f6a968SSerapheim Dimitropoulos 			 * log vdevs that are being removed. The reason
54411f6a968SSerapheim Dimitropoulos 			 * is that both of those can have vdev_ms_array
54511f6a968SSerapheim Dimitropoulos 			 * set to 0 and we wouldn't want to change their
54611f6a968SSerapheim Dimitropoulos 			 * metaslab size nor call vdev_expand() on them.
54711f6a968SSerapheim Dimitropoulos 			 */
54811f6a968SSerapheim Dimitropoulos 			if (!vdev_is_concrete(tvd) ||
54911f6a968SSerapheim Dimitropoulos 			    (tvd->vdev_islog && tvd->vdev_removing))
55011f6a968SSerapheim Dimitropoulos 				continue;
55111f6a968SSerapheim Dimitropoulos 
552573ca77eSGeorge Wilson 			if (tvd->vdev_ms_array == 0)
553573ca77eSGeorge Wilson 				vdev_metaslab_set_size(tvd);
554573ca77eSGeorge Wilson 			vdev_expand(tvd, txg);
5550373e76bSbonwick 		}
5560373e76bSbonwick 	}
557e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
5580373e76bSbonwick 
5590373e76bSbonwick 	/*
5600373e76bSbonwick 	 * Wait for the mosconfig to be regenerated and synced.
5610373e76bSbonwick 	 */
5620373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
5630373e76bSbonwick 
5640373e76bSbonwick 	/*
5650373e76bSbonwick 	 * Update the global config cache to reflect the new mosconfig.
5660373e76bSbonwick 	 */
5675cabbc6bSPrashanth Sreenivasa 	if (!spa->spa_is_root) {
5685cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE,
5695cabbc6bSPrashanth Sreenivasa 		    what != SPA_CONFIG_UPDATE_POOL);
5705cabbc6bSPrashanth Sreenivasa 	}
5710373e76bSbonwick 
5720373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
573bc758434SLin Ling 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
5740373e76bSbonwick }
575