xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 12949:b521d551715f)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
2312296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/spa.h>
27789Sahrens #include <sys/spa_impl.h>
28789Sahrens #include <sys/nvpair.h>
29789Sahrens #include <sys/uio.h>
30789Sahrens #include <sys/fs/zfs.h>
31789Sahrens #include <sys/vdev_impl.h>
32789Sahrens #include <sys/zfs_ioctl.h>
333975Sek110237 #include <sys/utsname.h>
343975Sek110237 #include <sys/systeminfo.h>
353975Sek110237 #include <sys/sunddi.h>
361544Seschrock #ifdef _KERNEL
371544Seschrock #include <sys/kobj.h>
388662SJordan.Vaughan@Sun.com #include <sys/zone.h>
391544Seschrock #endif
401544Seschrock 
41789Sahrens /*
42789Sahrens  * Pool configuration repository.
43789Sahrens  *
445363Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
455363Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
465363Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
475363Seschrock  * property set that allows them to be stored in an alternate location until
485363Seschrock  * the control of external software.
49789Sahrens  *
505363Seschrock  * For each cache file, we have a single nvlist which holds all the
515363Seschrock  * configuration information.  When the module loads, we read this information
525363Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
535363Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
545363Seschrock  * the configuration of a pool is changed, we call spa_config_sync(), which
555363Seschrock  * walks through all the active pools and writes the configuration to disk.
56789Sahrens  */
57789Sahrens 
58789Sahrens static uint64_t spa_config_generation = 1;
59789Sahrens 
60789Sahrens /*
61789Sahrens  * This can be overridden in userland to preserve an alternate namespace for
62789Sahrens  * userland pools when doing testing.
63789Sahrens  */
646643Seschrock const char *spa_config_path = ZPOOL_CACHE;
65789Sahrens 
66789Sahrens /*
67789Sahrens  * Called when the module is first loaded, this routine loads the configuration
68789Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
69789Sahrens  * only populates the namespace.
70789Sahrens  */
71789Sahrens void
spa_config_load(void)72789Sahrens spa_config_load(void)
73789Sahrens {
74789Sahrens 	void *buf = NULL;
75789Sahrens 	nvlist_t *nvlist, *child;
76789Sahrens 	nvpair_t *nvpair;
777754SJeff.Bonwick@Sun.COM 	char *pathname;
781544Seschrock 	struct _buf *file;
793912Slling 	uint64_t fsize;
80789Sahrens 
81789Sahrens 	/*
82789Sahrens 	 * Open the configuration file.
83789Sahrens 	 */
847754SJeff.Bonwick@Sun.COM 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
857754SJeff.Bonwick@Sun.COM 
867754SJeff.Bonwick@Sun.COM 	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
876643Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
881544Seschrock 
891544Seschrock 	file = kobj_open_file(pathname);
907754SJeff.Bonwick@Sun.COM 
917754SJeff.Bonwick@Sun.COM 	kmem_free(pathname, MAXPATHLEN);
927754SJeff.Bonwick@Sun.COM 
931544Seschrock 	if (file == (struct _buf *)-1)
94789Sahrens 		return;
95789Sahrens 
963912Slling 	if (kobj_get_filesize(file, &fsize) != 0)
971544Seschrock 		goto out;
981544Seschrock 
993912Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
1001544Seschrock 
101789Sahrens 	/*
102789Sahrens 	 * Read the nvlist from the file.
103789Sahrens 	 */
1043912Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
105789Sahrens 		goto out;
106789Sahrens 
107789Sahrens 	/*
108789Sahrens 	 * Unpack the nvlist.
109789Sahrens 	 */
1103912Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
111789Sahrens 		goto out;
112789Sahrens 
113789Sahrens 	/*
114789Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
115789Sahrens 	 * each one with the specified configuration.
116789Sahrens 	 */
117789Sahrens 	mutex_enter(&spa_namespace_lock);
118789Sahrens 	nvpair = NULL;
119789Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
120789Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
121789Sahrens 			continue;
122789Sahrens 
123789Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
124789Sahrens 
125789Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
126789Sahrens 			continue;
12710921STim.Haley@Sun.COM 		(void) spa_add(nvpair_name(nvpair), child, NULL);
128789Sahrens 	}
129789Sahrens 	mutex_exit(&spa_namespace_lock);
130789Sahrens 
131789Sahrens 	nvlist_free(nvlist);
132789Sahrens 
133789Sahrens out:
134789Sahrens 	if (buf != NULL)
1353912Slling 		kmem_free(buf, fsize);
136789Sahrens 
1371544Seschrock 	kobj_close_file(file);
138789Sahrens }
139789Sahrens 
1406643Seschrock static void
spa_config_write(spa_config_dirent_t * dp,nvlist_t * nvl)1416643Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1425363Seschrock {
143789Sahrens 	size_t buflen;
144789Sahrens 	char *buf;
145789Sahrens 	vnode_t *vp;
146789Sahrens 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
1477754SJeff.Bonwick@Sun.COM 	char *temp;
1486643Seschrock 
1496643Seschrock 	/*
1506643Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
1516643Seschrock 	 */
1526643Seschrock 	if (nvl == NULL) {
1536643Seschrock 		(void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
1546643Seschrock 		return;
1556643Seschrock 	}
156789Sahrens 
157789Sahrens 	/*
158789Sahrens 	 * Pack the configuration into a buffer.
159789Sahrens 	 */
1606643Seschrock 	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
161789Sahrens 
162789Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
1637754SJeff.Bonwick@Sun.COM 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
164789Sahrens 
1656643Seschrock 	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
1661544Seschrock 	    KM_SLEEP) == 0);
167789Sahrens 
168789Sahrens 	/*
169789Sahrens 	 * Write the configuration to disk.  We need to do the traditional
170789Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
171789Sahrens 	 * always have a consistent view of the data.
172789Sahrens 	 */
1737754SJeff.Bonwick@Sun.COM 	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
174789Sahrens 
1757754SJeff.Bonwick@Sun.COM 	if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) {
1767754SJeff.Bonwick@Sun.COM 		if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
1777754SJeff.Bonwick@Sun.COM 		    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
1787754SJeff.Bonwick@Sun.COM 		    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
1797754SJeff.Bonwick@Sun.COM 			(void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
1807754SJeff.Bonwick@Sun.COM 		}
1817754SJeff.Bonwick@Sun.COM 		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
1827754SJeff.Bonwick@Sun.COM 		VN_RELE(vp);
183789Sahrens 	}
184789Sahrens 
1857754SJeff.Bonwick@Sun.COM 	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
186789Sahrens 
1875363Seschrock 	kmem_free(buf, buflen);
1887754SJeff.Bonwick@Sun.COM 	kmem_free(temp, MAXPATHLEN);
1895363Seschrock }
1905363Seschrock 
1915363Seschrock /*
1926643Seschrock  * Synchronize pool configuration to disk.  This must be called with the
1936643Seschrock  * namespace lock held.
1945363Seschrock  */
1955363Seschrock void
spa_config_sync(spa_t * target,boolean_t removing,boolean_t postsysevent)1966643Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
1975363Seschrock {
1986643Seschrock 	spa_config_dirent_t *dp, *tdp;
1996643Seschrock 	nvlist_t *nvl;
2005363Seschrock 
2015363Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
202789Sahrens 
20310000SVictor.Latushkin@Sun.COM 	if (rootdir == NULL || !(spa_mode_global & FWRITE))
2048225SGeorge.Wilson@Sun.COM 		return;
2058225SGeorge.Wilson@Sun.COM 
2066643Seschrock 	/*
2076643Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
2086643Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
2096643Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
2106643Seschrock 	 */
2116643Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
2126643Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
2137754SJeff.Bonwick@Sun.COM 		spa_t *spa = NULL;
2146643Seschrock 		if (dp->scd_path == NULL)
2156643Seschrock 			continue;
2166643Seschrock 
2176643Seschrock 		/*
2186643Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
2196643Seschrock 		 */
2206643Seschrock 		nvl = NULL;
2216643Seschrock 		while ((spa = spa_next(spa)) != NULL) {
2226643Seschrock 			if (spa == target && removing)
2236643Seschrock 				continue;
2246643Seschrock 
2257754SJeff.Bonwick@Sun.COM 			mutex_enter(&spa->spa_props_lock);
2266643Seschrock 			tdp = list_head(&spa->spa_config_list);
2277754SJeff.Bonwick@Sun.COM 			if (spa->spa_config == NULL ||
2287754SJeff.Bonwick@Sun.COM 			    tdp->scd_path == NULL ||
2297754SJeff.Bonwick@Sun.COM 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
2307754SJeff.Bonwick@Sun.COM 				mutex_exit(&spa->spa_props_lock);
2316643Seschrock 				continue;
2327754SJeff.Bonwick@Sun.COM 			}
2336643Seschrock 
2346643Seschrock 			if (nvl == NULL)
2356643Seschrock 				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
2366643Seschrock 				    KM_SLEEP) == 0);
2376643Seschrock 
2386643Seschrock 			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
2396643Seschrock 			    spa->spa_config) == 0);
2407754SJeff.Bonwick@Sun.COM 			mutex_exit(&spa->spa_props_lock);
2416643Seschrock 		}
2426643Seschrock 
2436643Seschrock 		spa_config_write(dp, nvl);
2446643Seschrock 		nvlist_free(nvl);
2456643Seschrock 	}
2465363Seschrock 
2475363Seschrock 	/*
2486643Seschrock 	 * Remove any config entries older than the current one.
2495363Seschrock 	 */
2506643Seschrock 	dp = list_head(&target->spa_config_list);
2516643Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
2526643Seschrock 		list_remove(&target->spa_config_list, tdp);
2536643Seschrock 		if (tdp->scd_path != NULL)
2546643Seschrock 			spa_strfree(tdp->scd_path);
2556643Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
2565363Seschrock 	}
2575363Seschrock 
2585363Seschrock 	spa_config_generation++;
2596643Seschrock 
2606643Seschrock 	if (postsysevent)
2616643Seschrock 		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
262789Sahrens }
263789Sahrens 
264789Sahrens /*
2651635Sbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
266789Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
267789Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
268789Sahrens  * information for all pool visible within the zone.
269789Sahrens  */
270789Sahrens nvlist_t *
spa_all_configs(uint64_t * generation)271789Sahrens spa_all_configs(uint64_t *generation)
272789Sahrens {
273789Sahrens 	nvlist_t *pools;
2747754SJeff.Bonwick@Sun.COM 	spa_t *spa = NULL;
275789Sahrens 
276789Sahrens 	if (*generation == spa_config_generation)
277789Sahrens 		return (NULL);
278789Sahrens 
2791544Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
280789Sahrens 
281789Sahrens 	mutex_enter(&spa_namespace_lock);
282789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
283789Sahrens 		if (INGLOBALZONE(curproc) ||
284789Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
2857754SJeff.Bonwick@Sun.COM 			mutex_enter(&spa->spa_props_lock);
286789Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
287789Sahrens 			    spa->spa_config) == 0);
2887754SJeff.Bonwick@Sun.COM 			mutex_exit(&spa->spa_props_lock);
289789Sahrens 		}
290789Sahrens 	}
2917754SJeff.Bonwick@Sun.COM 	*generation = spa_config_generation;
292789Sahrens 	mutex_exit(&spa_namespace_lock);
293789Sahrens 
294789Sahrens 	return (pools);
295789Sahrens }
296789Sahrens 
297789Sahrens void
spa_config_set(spa_t * spa,nvlist_t * config)298789Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
299789Sahrens {
3007754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
301789Sahrens 	if (spa->spa_config != NULL)
302789Sahrens 		nvlist_free(spa->spa_config);
303789Sahrens 	spa->spa_config = config;
3047754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
305789Sahrens }
306789Sahrens 
307789Sahrens /*
308789Sahrens  * Generate the pool's configuration based on the current in-core state.
309789Sahrens  * We infer whether to generate a complete config or just one top-level config
310789Sahrens  * based on whether vd is the root vdev.
311789Sahrens  */
312789Sahrens nvlist_t *
spa_config_generate(spa_t * spa,vdev_t * vd,uint64_t txg,int getstats)313789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
314789Sahrens {
315789Sahrens 	nvlist_t *config, *nvroot;
316789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3173975Sek110237 	unsigned long hostid = 0;
3187754SJeff.Bonwick@Sun.COM 	boolean_t locked = B_FALSE;
31911422SMark.Musante@Sun.COM 	uint64_t split_guid;
320789Sahrens 
3217754SJeff.Bonwick@Sun.COM 	if (vd == NULL) {
3227754SJeff.Bonwick@Sun.COM 		vd = rvd;
3237754SJeff.Bonwick@Sun.COM 		locked = B_TRUE;
3247754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
3257754SJeff.Bonwick@Sun.COM 	}
3261635Sbonwick 
3277754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
3287754SJeff.Bonwick@Sun.COM 	    (SCL_CONFIG | SCL_STATE));
329789Sahrens 
330789Sahrens 	/*
331789Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
332789Sahrens 	 */
333789Sahrens 	if (txg == -1ULL)
334789Sahrens 		txg = spa->spa_config_txg;
335789Sahrens 
3361544Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
337789Sahrens 
338789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
3392082Seschrock 	    spa_version(spa)) == 0);
340789Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
341789Sahrens 	    spa_name(spa)) == 0);
342789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
343789Sahrens 	    spa_state(spa)) == 0);
344789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
345789Sahrens 	    txg) == 0);
346789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
347789Sahrens 	    spa_guid(spa)) == 0);
3488662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
3498662SJordan.Vaughan@Sun.com 	hostid = zone_get_hostid(NULL);
3508662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
3518662SJordan.Vaughan@Sun.com 	/*
3528662SJordan.Vaughan@Sun.com 	 * We're emulating the system's hostid in userland, so we can't use
3538662SJordan.Vaughan@Sun.com 	 * zone_get_hostid().
3548662SJordan.Vaughan@Sun.com 	 */
3553975Sek110237 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
3568662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
3574178Slling 	if (hostid != 0) {
3584178Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3594527Sperrin 		    hostid) == 0);
3604178Slling 	}
3613975Sek110237 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
3623975Sek110237 	    utsname.nodename) == 0);
363789Sahrens 
364789Sahrens 	if (vd != rvd) {
365789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
366789Sahrens 		    vd->vdev_top->vdev_guid) == 0);
367789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
368789Sahrens 		    vd->vdev_guid) == 0);
3692082Seschrock 		if (vd->vdev_isspare)
3702082Seschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
3712082Seschrock 			    1ULL) == 0);
3724527Sperrin 		if (vd->vdev_islog)
3734527Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3744527Sperrin 			    1ULL) == 0);
375789Sahrens 		vd = vd->vdev_top;		/* label contains top config */
37611422SMark.Musante@Sun.COM 	} else {
37711422SMark.Musante@Sun.COM 		/*
37811422SMark.Musante@Sun.COM 		 * Only add the (potentially large) split information
37911422SMark.Musante@Sun.COM 		 * in the mos config, and not in the vdev labels
38011422SMark.Musante@Sun.COM 		 */
38111422SMark.Musante@Sun.COM 		if (spa->spa_config_splitting != NULL)
38211422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
38311422SMark.Musante@Sun.COM 			    spa->spa_config_splitting) == 0);
384789Sahrens 	}
385789Sahrens 
38610594SGeorge.Wilson@Sun.COM 	/*
38710594SGeorge.Wilson@Sun.COM 	 * Add the top-level config.  We even add this on pools which
388*12949SGeorge.Wilson@Sun.COM 	 * don't support holes in the namespace.
38910594SGeorge.Wilson@Sun.COM 	 */
39010594SGeorge.Wilson@Sun.COM 	vdev_top_config_generate(spa, config);
39110594SGeorge.Wilson@Sun.COM 
39211422SMark.Musante@Sun.COM 	/*
39311422SMark.Musante@Sun.COM 	 * If we're splitting, record the original pool's guid.
39411422SMark.Musante@Sun.COM 	 */
39511422SMark.Musante@Sun.COM 	if (spa->spa_config_splitting != NULL &&
39611422SMark.Musante@Sun.COM 	    nvlist_lookup_uint64(spa->spa_config_splitting,
39711422SMark.Musante@Sun.COM 	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
39811422SMark.Musante@Sun.COM 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
39911422SMark.Musante@Sun.COM 		    split_guid) == 0);
40011422SMark.Musante@Sun.COM 	}
40111422SMark.Musante@Sun.COM 
40212296SLin.Ling@Sun.COM 	nvroot = vdev_config_generate(spa, vd, getstats, 0);
403789Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
404789Sahrens 	nvlist_free(nvroot);
405789Sahrens 
40611149SGeorge.Wilson@Sun.COM 	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
40711149SGeorge.Wilson@Sun.COM 		ddt_histogram_t *ddh;
40811149SGeorge.Wilson@Sun.COM 		ddt_stat_t *dds;
40911149SGeorge.Wilson@Sun.COM 		ddt_object_t *ddo;
41011149SGeorge.Wilson@Sun.COM 
41111149SGeorge.Wilson@Sun.COM 		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
41211149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_histogram(spa, ddh);
41311149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
41411149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_HISTOGRAM,
41511149SGeorge.Wilson@Sun.COM 		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
41611149SGeorge.Wilson@Sun.COM 		kmem_free(ddh, sizeof (ddt_histogram_t));
41711149SGeorge.Wilson@Sun.COM 
41811149SGeorge.Wilson@Sun.COM 		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
41911149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_object_stats(spa, ddo);
42011149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
42111149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_OBJ_STATS,
42211149SGeorge.Wilson@Sun.COM 		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
42311149SGeorge.Wilson@Sun.COM 		kmem_free(ddo, sizeof (ddt_object_t));
42411149SGeorge.Wilson@Sun.COM 
42511149SGeorge.Wilson@Sun.COM 		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
42611149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_stats(spa, dds);
42711149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
42811149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_STATS,
42911149SGeorge.Wilson@Sun.COM 		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
43011149SGeorge.Wilson@Sun.COM 		kmem_free(dds, sizeof (ddt_stat_t));
43111149SGeorge.Wilson@Sun.COM 	}
43211149SGeorge.Wilson@Sun.COM 
4337754SJeff.Bonwick@Sun.COM 	if (locked)
4347754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4357754SJeff.Bonwick@Sun.COM 
436789Sahrens 	return (config);
437789Sahrens }
4381635Sbonwick 
4391635Sbonwick /*
4406423Sgw25295  * Update all disk labels, generate a fresh config based on the current
4416423Sgw25295  * in-core state, and sync the global config cache (do not sync the config
4426423Sgw25295  * cache if this is a booting rootpool).
4436423Sgw25295  */
4446423Sgw25295 void
spa_config_update(spa_t * spa,int what)44510100SLin.Ling@Sun.COM spa_config_update(spa_t *spa, int what)
4466423Sgw25295 {
4471635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4481635Sbonwick 	uint64_t txg;
4491635Sbonwick 	int c;
4501635Sbonwick 
4511635Sbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4521635Sbonwick 
4537754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4541635Sbonwick 	txg = spa_last_synced_txg(spa) + 1;
4551635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
4561635Sbonwick 		vdev_config_dirty(rvd);
4571635Sbonwick 	} else {
4581635Sbonwick 		/*
4591635Sbonwick 		 * If we have top-level vdevs that were added but have
4601635Sbonwick 		 * not yet been prepared for allocation, do that now.
4611635Sbonwick 		 * (It's safe now because the config cache is up to date,
4621635Sbonwick 		 * so it will be able to translate the new DVAs.)
4631635Sbonwick 		 * See comments in spa_vdev_add() for full details.
4641635Sbonwick 		 */
4651635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
4661635Sbonwick 			vdev_t *tvd = rvd->vdev_child[c];
4679816SGeorge.Wilson@Sun.COM 			if (tvd->vdev_ms_array == 0)
4689816SGeorge.Wilson@Sun.COM 				vdev_metaslab_set_size(tvd);
4699816SGeorge.Wilson@Sun.COM 			vdev_expand(tvd, txg);
4701635Sbonwick 		}
4711635Sbonwick 	}
4727754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4731635Sbonwick 
4741635Sbonwick 	/*
4751635Sbonwick 	 * Wait for the mosconfig to be regenerated and synced.
4761635Sbonwick 	 */
4771635Sbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
4781635Sbonwick 
4791635Sbonwick 	/*
4801635Sbonwick 	 * Update the global config cache to reflect the new mosconfig.
4811635Sbonwick 	 */
48210100SLin.Ling@Sun.COM 	if (!spa->spa_is_root)
4836643Seschrock 		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
4841635Sbonwick 
4851635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
48610100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
4871635Sbonwick }
488