xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 11149:8bad7424e2c2)
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 /*
238662SJordan.Vaughan@Sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #include <sys/spa.h>
28789Sahrens #include <sys/spa_impl.h>
29789Sahrens #include <sys/nvpair.h>
30789Sahrens #include <sys/uio.h>
31789Sahrens #include <sys/fs/zfs.h>
32789Sahrens #include <sys/vdev_impl.h>
33789Sahrens #include <sys/zfs_ioctl.h>
343975Sek110237 #include <sys/utsname.h>
353975Sek110237 #include <sys/systeminfo.h>
363975Sek110237 #include <sys/sunddi.h>
371544Seschrock #ifdef _KERNEL
381544Seschrock #include <sys/kobj.h>
398662SJordan.Vaughan@Sun.com #include <sys/zone.h>
401544Seschrock #endif
411544Seschrock 
42789Sahrens /*
43789Sahrens  * Pool configuration repository.
44789Sahrens  *
455363Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
465363Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
475363Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
485363Seschrock  * property set that allows them to be stored in an alternate location until
495363Seschrock  * the control of external software.
50789Sahrens  *
515363Seschrock  * For each cache file, we have a single nvlist which holds all the
525363Seschrock  * configuration information.  When the module loads, we read this information
535363Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
545363Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
555363Seschrock  * the configuration of a pool is changed, we call spa_config_sync(), which
565363Seschrock  * walks through all the active pools and writes the configuration to disk.
57789Sahrens  */
58789Sahrens 
59789Sahrens static uint64_t spa_config_generation = 1;
60789Sahrens 
61789Sahrens /*
62789Sahrens  * This can be overridden in userland to preserve an alternate namespace for
63789Sahrens  * userland pools when doing testing.
64789Sahrens  */
656643Seschrock const char *spa_config_path = ZPOOL_CACHE;
66789Sahrens 
67789Sahrens /*
68789Sahrens  * Called when the module is first loaded, this routine loads the configuration
69789Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
70789Sahrens  * only populates the namespace.
71789Sahrens  */
72789Sahrens void
73789Sahrens spa_config_load(void)
74789Sahrens {
75789Sahrens 	void *buf = NULL;
76789Sahrens 	nvlist_t *nvlist, *child;
77789Sahrens 	nvpair_t *nvpair;
787754SJeff.Bonwick@Sun.COM 	char *pathname;
791544Seschrock 	struct _buf *file;
803912Slling 	uint64_t fsize;
81789Sahrens 
82789Sahrens 	/*
83789Sahrens 	 * Open the configuration file.
84789Sahrens 	 */
857754SJeff.Bonwick@Sun.COM 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
867754SJeff.Bonwick@Sun.COM 
877754SJeff.Bonwick@Sun.COM 	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
886643Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
891544Seschrock 
901544Seschrock 	file = kobj_open_file(pathname);
917754SJeff.Bonwick@Sun.COM 
927754SJeff.Bonwick@Sun.COM 	kmem_free(pathname, MAXPATHLEN);
937754SJeff.Bonwick@Sun.COM 
941544Seschrock 	if (file == (struct _buf *)-1)
95789Sahrens 		return;
96789Sahrens 
973912Slling 	if (kobj_get_filesize(file, &fsize) != 0)
981544Seschrock 		goto out;
991544Seschrock 
1003912Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
1011544Seschrock 
102789Sahrens 	/*
103789Sahrens 	 * Read the nvlist from the file.
104789Sahrens 	 */
1053912Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
106789Sahrens 		goto out;
107789Sahrens 
108789Sahrens 	/*
109789Sahrens 	 * Unpack the nvlist.
110789Sahrens 	 */
1113912Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
112789Sahrens 		goto out;
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
116789Sahrens 	 * each one with the specified configuration.
117789Sahrens 	 */
118789Sahrens 	mutex_enter(&spa_namespace_lock);
119789Sahrens 	nvpair = NULL;
120789Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
121789Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
122789Sahrens 			continue;
123789Sahrens 
124789Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
125789Sahrens 
126789Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
127789Sahrens 			continue;
12810921STim.Haley@Sun.COM 		(void) spa_add(nvpair_name(nvpair), child, NULL);
129789Sahrens 	}
130789Sahrens 	mutex_exit(&spa_namespace_lock);
131789Sahrens 
132789Sahrens 	nvlist_free(nvlist);
133789Sahrens 
134789Sahrens out:
135789Sahrens 	if (buf != NULL)
1363912Slling 		kmem_free(buf, fsize);
137789Sahrens 
1381544Seschrock 	kobj_close_file(file);
139789Sahrens }
140789Sahrens 
1416643Seschrock static void
1426643Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1435363Seschrock {
144789Sahrens 	size_t buflen;
145789Sahrens 	char *buf;
146789Sahrens 	vnode_t *vp;
147789Sahrens 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
1487754SJeff.Bonwick@Sun.COM 	char *temp;
1496643Seschrock 
1506643Seschrock 	/*
1516643Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
1526643Seschrock 	 */
1536643Seschrock 	if (nvl == NULL) {
1546643Seschrock 		(void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
1556643Seschrock 		return;
1566643Seschrock 	}
157789Sahrens 
158789Sahrens 	/*
159789Sahrens 	 * Pack the configuration into a buffer.
160789Sahrens 	 */
1616643Seschrock 	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
162789Sahrens 
163789Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
1647754SJeff.Bonwick@Sun.COM 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
165789Sahrens 
1666643Seschrock 	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
1671544Seschrock 	    KM_SLEEP) == 0);
168789Sahrens 
169789Sahrens 	/*
170789Sahrens 	 * Write the configuration to disk.  We need to do the traditional
171789Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
172789Sahrens 	 * always have a consistent view of the data.
173789Sahrens 	 */
1747754SJeff.Bonwick@Sun.COM 	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
175789Sahrens 
1767754SJeff.Bonwick@Sun.COM 	if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) {
1777754SJeff.Bonwick@Sun.COM 		if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
1787754SJeff.Bonwick@Sun.COM 		    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
1797754SJeff.Bonwick@Sun.COM 		    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
1807754SJeff.Bonwick@Sun.COM 			(void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
1817754SJeff.Bonwick@Sun.COM 		}
1827754SJeff.Bonwick@Sun.COM 		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
1837754SJeff.Bonwick@Sun.COM 		VN_RELE(vp);
184789Sahrens 	}
185789Sahrens 
1867754SJeff.Bonwick@Sun.COM 	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
187789Sahrens 
1885363Seschrock 	kmem_free(buf, buflen);
1897754SJeff.Bonwick@Sun.COM 	kmem_free(temp, MAXPATHLEN);
1905363Seschrock }
1915363Seschrock 
1925363Seschrock /*
1936643Seschrock  * Synchronize pool configuration to disk.  This must be called with the
1946643Seschrock  * namespace lock held.
1955363Seschrock  */
1965363Seschrock void
1976643Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
1985363Seschrock {
1996643Seschrock 	spa_config_dirent_t *dp, *tdp;
2006643Seschrock 	nvlist_t *nvl;
2015363Seschrock 
2025363Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
203789Sahrens 
20410000SVictor.Latushkin@Sun.COM 	if (rootdir == NULL || !(spa_mode_global & FWRITE))
2058225SGeorge.Wilson@Sun.COM 		return;
2068225SGeorge.Wilson@Sun.COM 
2076643Seschrock 	/*
2086643Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
2096643Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
2106643Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
2116643Seschrock 	 */
2126643Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
2136643Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
2147754SJeff.Bonwick@Sun.COM 		spa_t *spa = NULL;
2156643Seschrock 		if (dp->scd_path == NULL)
2166643Seschrock 			continue;
2176643Seschrock 
2186643Seschrock 		/*
2196643Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
2206643Seschrock 		 */
2216643Seschrock 		nvl = NULL;
2226643Seschrock 		while ((spa = spa_next(spa)) != NULL) {
2236643Seschrock 			if (spa == target && removing)
2246643Seschrock 				continue;
2256643Seschrock 
2267754SJeff.Bonwick@Sun.COM 			mutex_enter(&spa->spa_props_lock);
2276643Seschrock 			tdp = list_head(&spa->spa_config_list);
2287754SJeff.Bonwick@Sun.COM 			if (spa->spa_config == NULL ||
2297754SJeff.Bonwick@Sun.COM 			    tdp->scd_path == NULL ||
2307754SJeff.Bonwick@Sun.COM 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
2317754SJeff.Bonwick@Sun.COM 				mutex_exit(&spa->spa_props_lock);
2326643Seschrock 				continue;
2337754SJeff.Bonwick@Sun.COM 			}
2346643Seschrock 
2356643Seschrock 			if (nvl == NULL)
2366643Seschrock 				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
2376643Seschrock 				    KM_SLEEP) == 0);
2386643Seschrock 
2396643Seschrock 			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
2406643Seschrock 			    spa->spa_config) == 0);
2417754SJeff.Bonwick@Sun.COM 			mutex_exit(&spa->spa_props_lock);
2426643Seschrock 		}
2436643Seschrock 
2446643Seschrock 		spa_config_write(dp, nvl);
2456643Seschrock 		nvlist_free(nvl);
2466643Seschrock 	}
2475363Seschrock 
2485363Seschrock 	/*
2496643Seschrock 	 * Remove any config entries older than the current one.
2505363Seschrock 	 */
2516643Seschrock 	dp = list_head(&target->spa_config_list);
2526643Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
2536643Seschrock 		list_remove(&target->spa_config_list, tdp);
2546643Seschrock 		if (tdp->scd_path != NULL)
2556643Seschrock 			spa_strfree(tdp->scd_path);
2566643Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
2575363Seschrock 	}
2585363Seschrock 
2595363Seschrock 	spa_config_generation++;
2606643Seschrock 
2616643Seschrock 	if (postsysevent)
2626643Seschrock 		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
263789Sahrens }
264789Sahrens 
265789Sahrens /*
2661635Sbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
267789Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
268789Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
269789Sahrens  * information for all pool visible within the zone.
270789Sahrens  */
271789Sahrens nvlist_t *
272789Sahrens spa_all_configs(uint64_t *generation)
273789Sahrens {
274789Sahrens 	nvlist_t *pools;
2757754SJeff.Bonwick@Sun.COM 	spa_t *spa = NULL;
276789Sahrens 
277789Sahrens 	if (*generation == spa_config_generation)
278789Sahrens 		return (NULL);
279789Sahrens 
2801544Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
281789Sahrens 
282789Sahrens 	mutex_enter(&spa_namespace_lock);
283789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
284789Sahrens 		if (INGLOBALZONE(curproc) ||
285789Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
2867754SJeff.Bonwick@Sun.COM 			mutex_enter(&spa->spa_props_lock);
287789Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
288789Sahrens 			    spa->spa_config) == 0);
2897754SJeff.Bonwick@Sun.COM 			mutex_exit(&spa->spa_props_lock);
290789Sahrens 		}
291789Sahrens 	}
2927754SJeff.Bonwick@Sun.COM 	*generation = spa_config_generation;
293789Sahrens 	mutex_exit(&spa_namespace_lock);
294789Sahrens 
295789Sahrens 	return (pools);
296789Sahrens }
297789Sahrens 
298789Sahrens void
299789Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
300789Sahrens {
3017754SJeff.Bonwick@Sun.COM 	mutex_enter(&spa->spa_props_lock);
302789Sahrens 	if (spa->spa_config != NULL)
303789Sahrens 		nvlist_free(spa->spa_config);
304789Sahrens 	spa->spa_config = config;
3057754SJeff.Bonwick@Sun.COM 	mutex_exit(&spa->spa_props_lock);
306789Sahrens }
307789Sahrens 
30810921STim.Haley@Sun.COM /* Add discovered rewind info, if any to the provided nvlist */
30910921STim.Haley@Sun.COM void
31010921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa_t *spa, nvlist_t *tonvl)
31110921STim.Haley@Sun.COM {
31210921STim.Haley@Sun.COM 	int64_t loss = 0;
31310921STim.Haley@Sun.COM 
31410921STim.Haley@Sun.COM 	if (tonvl == NULL || spa->spa_load_txg == 0)
31510921STim.Haley@Sun.COM 		return;
31610921STim.Haley@Sun.COM 
31710921STim.Haley@Sun.COM 	VERIFY(nvlist_add_uint64(tonvl, ZPOOL_CONFIG_LOAD_TIME,
31810921STim.Haley@Sun.COM 	    spa->spa_load_txg_ts) == 0);
31910921STim.Haley@Sun.COM 	if (spa->spa_last_ubsync_txg)
32010921STim.Haley@Sun.COM 		loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
32110921STim.Haley@Sun.COM 	VERIFY(nvlist_add_int64(tonvl, ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
32210921STim.Haley@Sun.COM 	VERIFY(nvlist_add_uint64(tonvl, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
32310921STim.Haley@Sun.COM 	    spa->spa_load_data_errors) == 0);
32410921STim.Haley@Sun.COM }
32510921STim.Haley@Sun.COM 
326789Sahrens /*
327789Sahrens  * Generate the pool's configuration based on the current in-core state.
328789Sahrens  * We infer whether to generate a complete config or just one top-level config
329789Sahrens  * based on whether vd is the root vdev.
330789Sahrens  */
331789Sahrens nvlist_t *
332789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
333789Sahrens {
334789Sahrens 	nvlist_t *config, *nvroot;
335789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3363975Sek110237 	unsigned long hostid = 0;
3377754SJeff.Bonwick@Sun.COM 	boolean_t locked = B_FALSE;
338789Sahrens 
3397754SJeff.Bonwick@Sun.COM 	if (vd == NULL) {
3407754SJeff.Bonwick@Sun.COM 		vd = rvd;
3417754SJeff.Bonwick@Sun.COM 		locked = B_TRUE;
3427754SJeff.Bonwick@Sun.COM 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
3437754SJeff.Bonwick@Sun.COM 	}
3441635Sbonwick 
3457754SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
3467754SJeff.Bonwick@Sun.COM 	    (SCL_CONFIG | SCL_STATE));
347789Sahrens 
348789Sahrens 	/*
349789Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
350789Sahrens 	 */
351789Sahrens 	if (txg == -1ULL)
352789Sahrens 		txg = spa->spa_config_txg;
353789Sahrens 
3541544Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
355789Sahrens 
356789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
3572082Seschrock 	    spa_version(spa)) == 0);
358789Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
359789Sahrens 	    spa_name(spa)) == 0);
360789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
361789Sahrens 	    spa_state(spa)) == 0);
362789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
363789Sahrens 	    txg) == 0);
364789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
365789Sahrens 	    spa_guid(spa)) == 0);
3668662SJordan.Vaughan@Sun.com #ifdef	_KERNEL
3678662SJordan.Vaughan@Sun.com 	hostid = zone_get_hostid(NULL);
3688662SJordan.Vaughan@Sun.com #else	/* _KERNEL */
3698662SJordan.Vaughan@Sun.com 	/*
3708662SJordan.Vaughan@Sun.com 	 * We're emulating the system's hostid in userland, so we can't use
3718662SJordan.Vaughan@Sun.com 	 * zone_get_hostid().
3728662SJordan.Vaughan@Sun.com 	 */
3733975Sek110237 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
3748662SJordan.Vaughan@Sun.com #endif	/* _KERNEL */
3754178Slling 	if (hostid != 0) {
3764178Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3774527Sperrin 		    hostid) == 0);
3784178Slling 	}
3793975Sek110237 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
3803975Sek110237 	    utsname.nodename) == 0);
381789Sahrens 
382789Sahrens 	if (vd != rvd) {
383789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
384789Sahrens 		    vd->vdev_top->vdev_guid) == 0);
385789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
386789Sahrens 		    vd->vdev_guid) == 0);
3872082Seschrock 		if (vd->vdev_isspare)
3882082Seschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
3892082Seschrock 			    1ULL) == 0);
3904527Sperrin 		if (vd->vdev_islog)
3914527Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3924527Sperrin 			    1ULL) == 0);
393789Sahrens 		vd = vd->vdev_top;		/* label contains top config */
394789Sahrens 	}
395789Sahrens 
39610594SGeorge.Wilson@Sun.COM 	/*
39710594SGeorge.Wilson@Sun.COM 	 * Add the top-level config.  We even add this on pools which
39810594SGeorge.Wilson@Sun.COM 	 * don't support holes in the namespace as older pools will
39910594SGeorge.Wilson@Sun.COM 	 * just ignore it.
40010594SGeorge.Wilson@Sun.COM 	 */
40110594SGeorge.Wilson@Sun.COM 	vdev_top_config_generate(spa, config);
40210594SGeorge.Wilson@Sun.COM 
4035450Sbrendan 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE);
404789Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
405789Sahrens 	nvlist_free(nvroot);
406789Sahrens 
407*11149SGeorge.Wilson@Sun.COM 	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
408*11149SGeorge.Wilson@Sun.COM 		ddt_histogram_t *ddh;
409*11149SGeorge.Wilson@Sun.COM 		ddt_stat_t *dds;
410*11149SGeorge.Wilson@Sun.COM 		ddt_object_t *ddo;
411*11149SGeorge.Wilson@Sun.COM 
412*11149SGeorge.Wilson@Sun.COM 		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
413*11149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_histogram(spa, ddh);
414*11149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
415*11149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_HISTOGRAM,
416*11149SGeorge.Wilson@Sun.COM 		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
417*11149SGeorge.Wilson@Sun.COM 		kmem_free(ddh, sizeof (ddt_histogram_t));
418*11149SGeorge.Wilson@Sun.COM 
419*11149SGeorge.Wilson@Sun.COM 		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
420*11149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_object_stats(spa, ddo);
421*11149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
422*11149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_OBJ_STATS,
423*11149SGeorge.Wilson@Sun.COM 		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
424*11149SGeorge.Wilson@Sun.COM 		kmem_free(ddo, sizeof (ddt_object_t));
425*11149SGeorge.Wilson@Sun.COM 
426*11149SGeorge.Wilson@Sun.COM 		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
427*11149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_stats(spa, dds);
428*11149SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64_array(config,
429*11149SGeorge.Wilson@Sun.COM 		    ZPOOL_CONFIG_DDT_STATS,
430*11149SGeorge.Wilson@Sun.COM 		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
431*11149SGeorge.Wilson@Sun.COM 		kmem_free(dds, sizeof (ddt_stat_t));
432*11149SGeorge.Wilson@Sun.COM 	}
433*11149SGeorge.Wilson@Sun.COM 
43410921STim.Haley@Sun.COM 	spa_rewind_data_to_nvlist(spa, config);
43510921STim.Haley@Sun.COM 
4367754SJeff.Bonwick@Sun.COM 	if (locked)
4377754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
4387754SJeff.Bonwick@Sun.COM 
439789Sahrens 	return (config);
440789Sahrens }
4411635Sbonwick 
4421635Sbonwick /*
4436423Sgw25295  * Update all disk labels, generate a fresh config based on the current
4446423Sgw25295  * in-core state, and sync the global config cache (do not sync the config
4456423Sgw25295  * cache if this is a booting rootpool).
4466423Sgw25295  */
4476423Sgw25295 void
44810100SLin.Ling@Sun.COM spa_config_update(spa_t *spa, int what)
4496423Sgw25295 {
4501635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4511635Sbonwick 	uint64_t txg;
4521635Sbonwick 	int c;
4531635Sbonwick 
4541635Sbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4551635Sbonwick 
4567754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4571635Sbonwick 	txg = spa_last_synced_txg(spa) + 1;
4581635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
4591635Sbonwick 		vdev_config_dirty(rvd);
4601635Sbonwick 	} else {
4611635Sbonwick 		/*
4621635Sbonwick 		 * If we have top-level vdevs that were added but have
4631635Sbonwick 		 * not yet been prepared for allocation, do that now.
4641635Sbonwick 		 * (It's safe now because the config cache is up to date,
4651635Sbonwick 		 * so it will be able to translate the new DVAs.)
4661635Sbonwick 		 * See comments in spa_vdev_add() for full details.
4671635Sbonwick 		 */
4681635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
4691635Sbonwick 			vdev_t *tvd = rvd->vdev_child[c];
4709816SGeorge.Wilson@Sun.COM 			if (tvd->vdev_ms_array == 0)
4719816SGeorge.Wilson@Sun.COM 				vdev_metaslab_set_size(tvd);
4729816SGeorge.Wilson@Sun.COM 			vdev_expand(tvd, txg);
4731635Sbonwick 		}
4741635Sbonwick 	}
4757754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_ALL, FTAG);
4761635Sbonwick 
4771635Sbonwick 	/*
4781635Sbonwick 	 * Wait for the mosconfig to be regenerated and synced.
4791635Sbonwick 	 */
4801635Sbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
4811635Sbonwick 
4821635Sbonwick 	/*
4831635Sbonwick 	 * Update the global config cache to reflect the new mosconfig.
4841635Sbonwick 	 */
48510100SLin.Ling@Sun.COM 	if (!spa->spa_is_root)
4866643Seschrock 		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
4871635Sbonwick 
4881635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
48910100SLin.Ling@Sun.COM 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
4901635Sbonwick }
491