xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 4b964ada391d44b89d97e7e930e6a9a136e0a2f4)
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.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/spa.h>
27fa9e4066Sahrens #include <sys/spa_impl.h>
28fa9e4066Sahrens #include <sys/nvpair.h>
29fa9e4066Sahrens #include <sys/uio.h>
30fa9e4066Sahrens #include <sys/fs/zfs.h>
31fa9e4066Sahrens #include <sys/vdev_impl.h>
32fa9e4066Sahrens #include <sys/zfs_ioctl.h>
3395173954Sek #include <sys/utsname.h>
3495173954Sek #include <sys/systeminfo.h>
3595173954Sek #include <sys/sunddi.h>
36ea8dc4b6Seschrock #ifdef _KERNEL
37ea8dc4b6Seschrock #include <sys/kobj.h>
385679c89fSjv #include <sys/zone.h>
39ea8dc4b6Seschrock #endif
40ea8dc4b6Seschrock 
41fa9e4066Sahrens /*
42fa9e4066Sahrens  * Pool configuration repository.
43fa9e4066Sahrens  *
442f8aaab3Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
452f8aaab3Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
462f8aaab3Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
472f8aaab3Seschrock  * property set that allows them to be stored in an alternate location until
482f8aaab3Seschrock  * the control of external software.
49fa9e4066Sahrens  *
502f8aaab3Seschrock  * For each cache file, we have a single nvlist which holds all the
512f8aaab3Seschrock  * configuration information.  When the module loads, we read this information
522f8aaab3Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
532f8aaab3Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
542f8aaab3Seschrock  * the configuration of a pool is changed, we call spa_config_sync(), which
552f8aaab3Seschrock  * walks through all the active pools and writes the configuration to disk.
56fa9e4066Sahrens  */
57fa9e4066Sahrens 
58fa9e4066Sahrens static uint64_t spa_config_generation = 1;
59fa9e4066Sahrens 
60fa9e4066Sahrens /*
61fa9e4066Sahrens  * This can be overridden in userland to preserve an alternate namespace for
62fa9e4066Sahrens  * userland pools when doing testing.
63fa9e4066Sahrens  */
64c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE;
65fa9e4066Sahrens 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Called when the module is first loaded, this routine loads the configuration
68fa9e4066Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
69fa9e4066Sahrens  * only populates the namespace.
70fa9e4066Sahrens  */
71fa9e4066Sahrens void
72fa9e4066Sahrens spa_config_load(void)
73fa9e4066Sahrens {
74fa9e4066Sahrens 	void *buf = NULL;
75fa9e4066Sahrens 	nvlist_t *nvlist, *child;
76fa9e4066Sahrens 	nvpair_t *nvpair;
77e14bb325SJeff Bonwick 	char *pathname;
78ea8dc4b6Seschrock 	struct _buf *file;
79b1b8ab34Slling 	uint64_t fsize;
80fa9e4066Sahrens 
81fa9e4066Sahrens 	/*
82fa9e4066Sahrens 	 * Open the configuration file.
83fa9e4066Sahrens 	 */
84e14bb325SJeff Bonwick 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
85e14bb325SJeff Bonwick 
86e14bb325SJeff Bonwick 	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
87c5904d13Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
88ea8dc4b6Seschrock 
89ea8dc4b6Seschrock 	file = kobj_open_file(pathname);
90e14bb325SJeff Bonwick 
91e14bb325SJeff Bonwick 	kmem_free(pathname, MAXPATHLEN);
92e14bb325SJeff Bonwick 
93ea8dc4b6Seschrock 	if (file == (struct _buf *)-1)
94fa9e4066Sahrens 		return;
95fa9e4066Sahrens 
96b1b8ab34Slling 	if (kobj_get_filesize(file, &fsize) != 0)
97fa9e4066Sahrens 		goto out;
98fa9e4066Sahrens 
99b1b8ab34Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
100fa9e4066Sahrens 
101ea8dc4b6Seschrock 	/*
102ea8dc4b6Seschrock 	 * Read the nvlist from the file.
103ea8dc4b6Seschrock 	 */
104b1b8ab34Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
105fa9e4066Sahrens 		goto out;
106fa9e4066Sahrens 
107fa9e4066Sahrens 	/*
108fa9e4066Sahrens 	 * Unpack the nvlist.
109fa9e4066Sahrens 	 */
110b1b8ab34Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
111fa9e4066Sahrens 		goto out;
112fa9e4066Sahrens 
113fa9e4066Sahrens 	/*
114fa9e4066Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
115fa9e4066Sahrens 	 * each one with the specified configuration.
116fa9e4066Sahrens 	 */
117fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
118fa9e4066Sahrens 	nvpair = NULL;
119fa9e4066Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
120fa9e4066Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
121fa9e4066Sahrens 			continue;
122fa9e4066Sahrens 
123fa9e4066Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
124fa9e4066Sahrens 
125fa9e4066Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
126fa9e4066Sahrens 			continue;
127468c413aSTim Haley 		(void) spa_add(nvpair_name(nvpair), child, NULL);
128fa9e4066Sahrens 	}
129fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
130fa9e4066Sahrens 
131fa9e4066Sahrens 	nvlist_free(nvlist);
132fa9e4066Sahrens 
133fa9e4066Sahrens out:
134fa9e4066Sahrens 	if (buf != NULL)
135b1b8ab34Slling 		kmem_free(buf, fsize);
136fa9e4066Sahrens 
137ea8dc4b6Seschrock 	kobj_close_file(file);
138fa9e4066Sahrens }
139fa9e4066Sahrens 
1402f8aaab3Seschrock static void
141c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1422f8aaab3Seschrock {
1432f8aaab3Seschrock 	size_t buflen;
1442f8aaab3Seschrock 	char *buf;
1452f8aaab3Seschrock 	vnode_t *vp;
1462f8aaab3Seschrock 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
147e14bb325SJeff Bonwick 	char *temp;
148c5904d13Seschrock 
149c5904d13Seschrock 	/*
150c5904d13Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
151c5904d13Seschrock 	 */
152c5904d13Seschrock 	if (nvl == NULL) {
153c5904d13Seschrock 		(void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
154c5904d13Seschrock 		return;
155c5904d13Seschrock 	}
1562f8aaab3Seschrock 
157fa9e4066Sahrens 	/*
158fa9e4066Sahrens 	 * Pack the configuration into a buffer.
159fa9e4066Sahrens 	 */
160c5904d13Seschrock 	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
161fa9e4066Sahrens 
162fa9e4066Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
163e14bb325SJeff Bonwick 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
164fa9e4066Sahrens 
165c5904d13Seschrock 	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
166ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
167fa9e4066Sahrens 
168fa9e4066Sahrens 	/*
169fa9e4066Sahrens 	 * Write the configuration to disk.  We need to do the traditional
170fa9e4066Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
171fa9e4066Sahrens 	 * always have a consistent view of the data.
172fa9e4066Sahrens 	 */
173e14bb325SJeff Bonwick 	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
174fa9e4066Sahrens 
175e14bb325SJeff Bonwick 	if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) {
176e14bb325SJeff Bonwick 		if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
177e14bb325SJeff Bonwick 		    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
178e14bb325SJeff Bonwick 		    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
179e14bb325SJeff Bonwick 			(void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
180e14bb325SJeff Bonwick 		}
181e14bb325SJeff Bonwick 		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
182e14bb325SJeff Bonwick 		VN_RELE(vp);
183fa9e4066Sahrens 	}
184fa9e4066Sahrens 
185e14bb325SJeff Bonwick 	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
186fa9e4066Sahrens 
187fa9e4066Sahrens 	kmem_free(buf, buflen);
188e14bb325SJeff Bonwick 	kmem_free(temp, MAXPATHLEN);
1892f8aaab3Seschrock }
1902f8aaab3Seschrock 
1912f8aaab3Seschrock /*
192c5904d13Seschrock  * Synchronize pool configuration to disk.  This must be called with the
193c5904d13Seschrock  * namespace lock held.
1942f8aaab3Seschrock  */
1952f8aaab3Seschrock void
196c5904d13Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
1972f8aaab3Seschrock {
198c5904d13Seschrock 	spa_config_dirent_t *dp, *tdp;
199c5904d13Seschrock 	nvlist_t *nvl;
2002f8aaab3Seschrock 
2012f8aaab3Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2022f8aaab3Seschrock 
2034f0f5e5bSVictor Latushkin 	if (rootdir == NULL || !(spa_mode_global & FWRITE))
2049fddc96aSGeorge Wilson 		return;
2059fddc96aSGeorge Wilson 
206c5904d13Seschrock 	/*
207c5904d13Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
208c5904d13Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
209c5904d13Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
210c5904d13Seschrock 	 */
211c5904d13Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
212c5904d13Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
213e14bb325SJeff Bonwick 		spa_t *spa = NULL;
214c5904d13Seschrock 		if (dp->scd_path == NULL)
215c5904d13Seschrock 			continue;
216c5904d13Seschrock 
217c5904d13Seschrock 		/*
218c5904d13Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
219c5904d13Seschrock 		 */
220c5904d13Seschrock 		nvl = NULL;
221c5904d13Seschrock 		while ((spa = spa_next(spa)) != NULL) {
222c5904d13Seschrock 			if (spa == target && removing)
223c5904d13Seschrock 				continue;
224c5904d13Seschrock 
225e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
226c5904d13Seschrock 			tdp = list_head(&spa->spa_config_list);
227e14bb325SJeff Bonwick 			if (spa->spa_config == NULL ||
228e14bb325SJeff Bonwick 			    tdp->scd_path == NULL ||
229e14bb325SJeff Bonwick 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
230e14bb325SJeff Bonwick 				mutex_exit(&spa->spa_props_lock);
231c5904d13Seschrock 				continue;
232e14bb325SJeff Bonwick 			}
233c5904d13Seschrock 
234c5904d13Seschrock 			if (nvl == NULL)
235c5904d13Seschrock 				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
236c5904d13Seschrock 				    KM_SLEEP) == 0);
237c5904d13Seschrock 
238c5904d13Seschrock 			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
239c5904d13Seschrock 			    spa->spa_config) == 0);
240e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
241c5904d13Seschrock 		}
242c5904d13Seschrock 
243c5904d13Seschrock 		spa_config_write(dp, nvl);
244c5904d13Seschrock 		nvlist_free(nvl);
245c5904d13Seschrock 	}
2462f8aaab3Seschrock 
2472f8aaab3Seschrock 	/*
248c5904d13Seschrock 	 * Remove any config entries older than the current one.
2492f8aaab3Seschrock 	 */
250c5904d13Seschrock 	dp = list_head(&target->spa_config_list);
251c5904d13Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
252c5904d13Seschrock 		list_remove(&target->spa_config_list, tdp);
253c5904d13Seschrock 		if (tdp->scd_path != NULL)
254c5904d13Seschrock 			spa_strfree(tdp->scd_path);
255c5904d13Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
2562f8aaab3Seschrock 	}
2572f8aaab3Seschrock 
2582f8aaab3Seschrock 	spa_config_generation++;
259c5904d13Seschrock 
260c5904d13Seschrock 	if (postsysevent)
261c5904d13Seschrock 		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
262fa9e4066Sahrens }
263fa9e4066Sahrens 
264fa9e4066Sahrens /*
2650373e76bSbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
266fa9e4066Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
267fa9e4066Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
268fa9e4066Sahrens  * information for all pool visible within the zone.
269fa9e4066Sahrens  */
270fa9e4066Sahrens nvlist_t *
271fa9e4066Sahrens spa_all_configs(uint64_t *generation)
272fa9e4066Sahrens {
273fa9e4066Sahrens 	nvlist_t *pools;
274e14bb325SJeff Bonwick 	spa_t *spa = NULL;
275fa9e4066Sahrens 
276fa9e4066Sahrens 	if (*generation == spa_config_generation)
277fa9e4066Sahrens 		return (NULL);
278fa9e4066Sahrens 
279ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
280fa9e4066Sahrens 
281fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
282fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
283fa9e4066Sahrens 		if (INGLOBALZONE(curproc) ||
284fa9e4066Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
285e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
286fa9e4066Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
287fa9e4066Sahrens 			    spa->spa_config) == 0);
288e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
289fa9e4066Sahrens 		}
290fa9e4066Sahrens 	}
291fa9e4066Sahrens 	*generation = spa_config_generation;
292e14bb325SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
293fa9e4066Sahrens 
294fa9e4066Sahrens 	return (pools);
295fa9e4066Sahrens }
296fa9e4066Sahrens 
297fa9e4066Sahrens void
298fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
299fa9e4066Sahrens {
300e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
301fa9e4066Sahrens 	if (spa->spa_config != NULL)
302fa9e4066Sahrens 		nvlist_free(spa->spa_config);
303fa9e4066Sahrens 	spa->spa_config = config;
304e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307fa9e4066Sahrens /*
308fa9e4066Sahrens  * Generate the pool's configuration based on the current in-core state.
309fa9e4066Sahrens  * We infer whether to generate a complete config or just one top-level config
310fa9e4066Sahrens  * based on whether vd is the root vdev.
311fa9e4066Sahrens  */
312fa9e4066Sahrens nvlist_t *
313fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
314fa9e4066Sahrens {
315fa9e4066Sahrens 	nvlist_t *config, *nvroot;
316fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
31795173954Sek 	unsigned long hostid = 0;
318e14bb325SJeff Bonwick 	boolean_t locked = B_FALSE;
3191195e687SMark J Musante 	uint64_t split_guid;
320fa9e4066Sahrens 
321e14bb325SJeff Bonwick 	if (vd == NULL) {
322fa9e4066Sahrens 		vd = rvd;
323e14bb325SJeff Bonwick 		locked = B_TRUE;
324e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
325e14bb325SJeff Bonwick 	}
326e14bb325SJeff Bonwick 
327e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
328e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE));
329fa9e4066Sahrens 
330fa9e4066Sahrens 	/*
331fa9e4066Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
332fa9e4066Sahrens 	 */
333fa9e4066Sahrens 	if (txg == -1ULL)
334fa9e4066Sahrens 		txg = spa->spa_config_txg;
335fa9e4066Sahrens 
336ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
337fa9e4066Sahrens 
338fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
33999653d4eSeschrock 	    spa_version(spa)) == 0);
340fa9e4066Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
341fa9e4066Sahrens 	    spa_name(spa)) == 0);
342fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
343fa9e4066Sahrens 	    spa_state(spa)) == 0);
344fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
345fa9e4066Sahrens 	    txg) == 0);
346fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
347fa9e4066Sahrens 	    spa_guid(spa)) == 0);
3485679c89fSjv #ifdef	_KERNEL
3495679c89fSjv 	hostid = zone_get_hostid(NULL);
3505679c89fSjv #else	/* _KERNEL */
3515679c89fSjv 	/*
3525679c89fSjv 	 * We're emulating the system's hostid in userland, so we can't use
3535679c89fSjv 	 * zone_get_hostid().
3545679c89fSjv 	 */
35595173954Sek 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
3565679c89fSjv #endif	/* _KERNEL */
35717194a52Slling 	if (hostid != 0) {
35817194a52Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3598654d025Sperrin 		    hostid) == 0);
36017194a52Slling 	}
36195173954Sek 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
36295173954Sek 	    utsname.nodename) == 0);
363fa9e4066Sahrens 
364fa9e4066Sahrens 	if (vd != rvd) {
365fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
366fa9e4066Sahrens 		    vd->vdev_top->vdev_guid) == 0);
367fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
368fa9e4066Sahrens 		    vd->vdev_guid) == 0);
36999653d4eSeschrock 		if (vd->vdev_isspare)
37099653d4eSeschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
37199653d4eSeschrock 			    1ULL) == 0);
3728654d025Sperrin 		if (vd->vdev_islog)
3738654d025Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3748654d025Sperrin 			    1ULL) == 0);
375fa9e4066Sahrens 		vd = vd->vdev_top;		/* label contains top config */
3761195e687SMark J Musante 	} else {
3771195e687SMark J Musante 		/*
3781195e687SMark J Musante 		 * Only add the (potentially large) split information
3791195e687SMark J Musante 		 * in the mos config, and not in the vdev labels
3801195e687SMark J Musante 		 */
3811195e687SMark J Musante 		if (spa->spa_config_splitting != NULL)
3821195e687SMark J Musante 			VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
3831195e687SMark J Musante 			    spa->spa_config_splitting) == 0);
384fa9e4066Sahrens 	}
385fa9e4066Sahrens 
38688ecc943SGeorge Wilson 	/*
38788ecc943SGeorge Wilson 	 * Add the top-level config.  We even add this on pools which
388*4b964adaSGeorge Wilson 	 * don't support holes in the namespace.
38988ecc943SGeorge Wilson 	 */
39088ecc943SGeorge Wilson 	vdev_top_config_generate(spa, config);
39188ecc943SGeorge Wilson 
3921195e687SMark J Musante 	/*
3931195e687SMark J Musante 	 * If we're splitting, record the original pool's guid.
3941195e687SMark J Musante 	 */
3951195e687SMark J Musante 	if (spa->spa_config_splitting != NULL &&
3961195e687SMark J Musante 	    nvlist_lookup_uint64(spa->spa_config_splitting,
3971195e687SMark J Musante 	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
3981195e687SMark J Musante 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
3991195e687SMark J Musante 		    split_guid) == 0);
4001195e687SMark J Musante 	}
4011195e687SMark J Musante 
4023f9d6ad7SLin Ling 	nvroot = vdev_config_generate(spa, vd, getstats, 0);
403fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
404fa9e4066Sahrens 	nvlist_free(nvroot);
405fa9e4066Sahrens 
4069eb19f4dSGeorge Wilson 	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
4079eb19f4dSGeorge Wilson 		ddt_histogram_t *ddh;
4089eb19f4dSGeorge Wilson 		ddt_stat_t *dds;
4099eb19f4dSGeorge Wilson 		ddt_object_t *ddo;
4109eb19f4dSGeorge Wilson 
4119eb19f4dSGeorge Wilson 		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
4129eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, ddh);
4139eb19f4dSGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config,
4149eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_HISTOGRAM,
4159eb19f4dSGeorge Wilson 		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
4169eb19f4dSGeorge Wilson 		kmem_free(ddh, sizeof (ddt_histogram_t));
4179eb19f4dSGeorge Wilson 
4189eb19f4dSGeorge Wilson 		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
4199eb19f4dSGeorge Wilson 		ddt_get_dedup_object_stats(spa, ddo);
4209eb19f4dSGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config,
4219eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_OBJ_STATS,
4229eb19f4dSGeorge Wilson 		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
4239eb19f4dSGeorge Wilson 		kmem_free(ddo, sizeof (ddt_object_t));
4249eb19f4dSGeorge Wilson 
4259eb19f4dSGeorge Wilson 		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
4269eb19f4dSGeorge Wilson 		ddt_get_dedup_stats(spa, dds);
4279eb19f4dSGeorge Wilson 		VERIFY(nvlist_add_uint64_array(config,
4289eb19f4dSGeorge Wilson 		    ZPOOL_CONFIG_DDT_STATS,
4299eb19f4dSGeorge Wilson 		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
4309eb19f4dSGeorge Wilson 		kmem_free(dds, sizeof (ddt_stat_t));
4319eb19f4dSGeorge Wilson 	}
4329eb19f4dSGeorge Wilson 
433e14bb325SJeff Bonwick 	if (locked)
434e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
435e14bb325SJeff Bonwick 
436fa9e4066Sahrens 	return (config);
437fa9e4066Sahrens }
4380373e76bSbonwick 
439e7cbe64fSgw /*
440e7cbe64fSgw  * Update all disk labels, generate a fresh config based on the current
441e7cbe64fSgw  * in-core state, and sync the global config cache (do not sync the config
442e7cbe64fSgw  * cache if this is a booting rootpool).
443e7cbe64fSgw  */
444e7cbe64fSgw void
445bc758434SLin Ling spa_config_update(spa_t *spa, int what)
4460373e76bSbonwick {
4470373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4480373e76bSbonwick 	uint64_t txg;
4490373e76bSbonwick 	int c;
4500373e76bSbonwick 
4510373e76bSbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4520373e76bSbonwick 
453e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4540373e76bSbonwick 	txg = spa_last_synced_txg(spa) + 1;
4550373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
4560373e76bSbonwick 		vdev_config_dirty(rvd);
4570373e76bSbonwick 	} else {
4580373e76bSbonwick 		/*
4590373e76bSbonwick 		 * If we have top-level vdevs that were added but have
4600373e76bSbonwick 		 * not yet been prepared for allocation, do that now.
4610373e76bSbonwick 		 * (It's safe now because the config cache is up to date,
4620373e76bSbonwick 		 * so it will be able to translate the new DVAs.)
4630373e76bSbonwick 		 * See comments in spa_vdev_add() for full details.
4640373e76bSbonwick 		 */
4650373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
4660373e76bSbonwick 			vdev_t *tvd = rvd->vdev_child[c];
467573ca77eSGeorge Wilson 			if (tvd->vdev_ms_array == 0)
468573ca77eSGeorge Wilson 				vdev_metaslab_set_size(tvd);
469573ca77eSGeorge Wilson 			vdev_expand(tvd, txg);
4700373e76bSbonwick 		}
4710373e76bSbonwick 	}
472e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
4730373e76bSbonwick 
4740373e76bSbonwick 	/*
4750373e76bSbonwick 	 * Wait for the mosconfig to be regenerated and synced.
4760373e76bSbonwick 	 */
4770373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
4780373e76bSbonwick 
4790373e76bSbonwick 	/*
4800373e76bSbonwick 	 * Update the global config cache to reflect the new mosconfig.
4810373e76bSbonwick 	 */
482bc758434SLin Ling 	if (!spa->spa_is_root)
483c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
4840373e76bSbonwick 
4850373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
486bc758434SLin Ling 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
4870373e76bSbonwick }
488