1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd
22168404Spjd/*
23219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24228103Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25254112Sdelphij * Copyright (c) 2013 by Delphix. All rights reserved.
26168404Spjd */
27168404Spjd
28168404Spjd#include <sys/zfs_context.h>
29168404Spjd#include <sys/spa.h>
30251636Sdelphij#include <sys/fm/fs/zfs.h>
31168404Spjd#include <sys/spa_impl.h>
32168404Spjd#include <sys/nvpair.h>
33168404Spjd#include <sys/uio.h>
34168404Spjd#include <sys/fs/zfs.h>
35168404Spjd#include <sys/vdev_impl.h>
36168404Spjd#include <sys/zfs_ioctl.h>
37168498Spjd#include <sys/utsname.h>
38168962Spjd#include <sys/sunddi.h>
39236884Smm#include <sys/zfeature.h>
40168404Spjd#ifdef _KERNEL
41168404Spjd#include <sys/kobj.h>
42219089Spjd#include <sys/zone.h>
43168404Spjd#endif
44168404Spjd
45168404Spjd/*
46168404Spjd * Pool configuration repository.
47168404Spjd *
48185029Spjd * Pool configuration is stored as a packed nvlist on the filesystem.  By
49185029Spjd * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
50185029Spjd * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
51185029Spjd * property set that allows them to be stored in an alternate location until
52185029Spjd * the control of external software.
53168404Spjd *
54185029Spjd * For each cache file, we have a single nvlist which holds all the
55185029Spjd * configuration information.  When the module loads, we read this information
56185029Spjd * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
57185029Spjd * maintained independently in spa.c.  Whenever the namespace is modified, or
58185029Spjd * the configuration of a pool is changed, we call spa_config_sync(), which
59185029Spjd * walks through all the active pools and writes the configuration to disk.
60168404Spjd */
61168404Spjd
62168404Spjdstatic uint64_t spa_config_generation = 1;
63168404Spjd
64168404Spjd/*
65168404Spjd * This can be overridden in userland to preserve an alternate namespace for
66168404Spjd * userland pools when doing testing.
67168404Spjd */
68185029Spjdconst char *spa_config_path = ZPOOL_CACHE;
69168404Spjd
70168404Spjd/*
71168404Spjd * Called when the module is first loaded, this routine loads the configuration
72168404Spjd * file into the SPA namespace.  It does not actually open or load the pools; it
73168404Spjd * only populates the namespace.
74168404Spjd */
75168404Spjdvoid
76168404Spjdspa_config_load(void)
77168404Spjd{
78168404Spjd	void *buf = NULL;
79168404Spjd	nvlist_t *nvlist, *child;
80168404Spjd	nvpair_t *nvpair;
81185029Spjd	char *pathname;
82168404Spjd	struct _buf *file;
83168404Spjd	uint64_t fsize;
84168404Spjd
85168404Spjd	/*
86168404Spjd	 * Open the configuration file.
87168404Spjd	 */
88185029Spjd	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
89168404Spjd
90185029Spjd	(void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path);
91185029Spjd
92168404Spjd	file = kobj_open_file(pathname);
93185029Spjd
94219089Spjd	kmem_free(pathname, MAXPATHLEN);
95168404Spjd
96219089Spjd	if (file == (struct _buf *)-1)
97219089Spjd		return;
98219089Spjd
99219089Spjd	if (kobj_get_filesize(file, &fsize) != 0)
100168404Spjd		goto out;
101168404Spjd
102168404Spjd	buf = kmem_alloc(fsize, KM_SLEEP);
103168404Spjd
104168404Spjd	/*
105168404Spjd	 * Read the nvlist from the file.
106168404Spjd	 */
107219089Spjd	if (kobj_read_file(file, buf, fsize, 0) < 0)
108168404Spjd		goto out;
109168404Spjd
110168404Spjd	/*
111168404Spjd	 * Unpack the nvlist.
112168404Spjd	 */
113168404Spjd	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
114168404Spjd		goto out;
115168404Spjd
116168404Spjd	/*
117168404Spjd	 * Iterate over all elements in the nvlist, creating a new spa_t for
118168404Spjd	 * each one with the specified configuration.
119168404Spjd	 */
120168404Spjd	mutex_enter(&spa_namespace_lock);
121168404Spjd	nvpair = NULL;
122168404Spjd	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
123168404Spjd		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
124168404Spjd			continue;
125168404Spjd
126168404Spjd		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
127168404Spjd
128168404Spjd		if (spa_lookup(nvpair_name(nvpair)) != NULL)
129168404Spjd			continue;
130219089Spjd		(void) spa_add(nvpair_name(nvpair), child, NULL);
131168404Spjd	}
132168404Spjd	mutex_exit(&spa_namespace_lock);
133168404Spjd
134168404Spjd	nvlist_free(nvlist);
135168404Spjd
136168404Spjdout:
137168404Spjd	if (buf != NULL)
138168404Spjd		kmem_free(buf, fsize);
139219089Spjd
140219089Spjd	kobj_close_file(file);
141168404Spjd}
142168404Spjd
143251636Sdelphijstatic int
144185029Spjdspa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
145168404Spjd{
146168404Spjd	size_t buflen;
147219089Spjd	char *buf;
148168404Spjd	vnode_t *vp;
149219089Spjd	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
150219089Spjd	char *temp;
151251636Sdelphij	int err;
152168404Spjd
153168404Spjd	/*
154185029Spjd	 * If the nvlist is empty (NULL), then remove the old cachefile.
155168404Spjd	 */
156185029Spjd	if (nvl == NULL) {
157251636Sdelphij		err = vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
158251636Sdelphij		return (err);
159168404Spjd	}
160168404Spjd
161168404Spjd	/*
162168404Spjd	 * Pack the configuration into a buffer.
163168404Spjd	 */
164185029Spjd	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
165168404Spjd
166168404Spjd	buf = kmem_alloc(buflen, KM_SLEEP);
167185029Spjd	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
168168404Spjd
169185029Spjd	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
170168404Spjd	    KM_SLEEP) == 0);
171168404Spjd
172168404Spjd	/*
173168404Spjd	 * Write the configuration to disk.  We need to do the traditional
174168404Spjd	 * 'write to temporary file, sync, move over original' to make sure we
175168404Spjd	 * always have a consistent view of the data.
176168404Spjd	 */
177185029Spjd	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
178168404Spjd
179251636Sdelphij	err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
180251636Sdelphij	if (err == 0) {
181251636Sdelphij		err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
182251636Sdelphij		    0, RLIM64_INFINITY, kcred, NULL);
183251636Sdelphij		if (err == 0)
184251636Sdelphij			err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
185251636Sdelphij		if (err == 0)
186251636Sdelphij			err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
187185029Spjd		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
188185029Spjd	}
189168404Spjd
190185029Spjd	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
191185029Spjd
192185029Spjd	kmem_free(buf, buflen);
193185029Spjd	kmem_free(temp, MAXPATHLEN);
194251636Sdelphij	return (err);
195185029Spjd}
196185029Spjd
197185029Spjd/*
198185029Spjd * Synchronize pool configuration to disk.  This must be called with the
199254112Sdelphij * namespace lock held. Synchronizing the pool cache is typically done after
200254112Sdelphij * the configuration has been synced to the MOS. This exposes a window where
201254112Sdelphij * the MOS config will have been updated but the cache file has not. If
202254112Sdelphij * the system were to crash at that instant then the cached config may not
203254112Sdelphij * contain the correct information to open the pool and an explicity import
204254112Sdelphij * would be required.
205185029Spjd */
206185029Spjdvoid
207185029Spjdspa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
208185029Spjd{
209185029Spjd	spa_config_dirent_t *dp, *tdp;
210185029Spjd	nvlist_t *nvl;
211251636Sdelphij	boolean_t ccw_failure;
212251636Sdelphij	int error;
213185029Spjd
214185029Spjd	ASSERT(MUTEX_HELD(&spa_namespace_lock));
215185029Spjd
216209962Smm	if (rootdir == NULL || !(spa_mode_global & FWRITE))
217209962Smm		return;
218209962Smm
219185029Spjd	/*
220185029Spjd	 * Iterate over all cachefiles for the pool, past or present.  When the
221185029Spjd	 * cachefile is changed, the new one is pushed onto this list, allowing
222185029Spjd	 * us to update previous cachefiles that no longer contain this pool.
223185029Spjd	 */
224251636Sdelphij	ccw_failure = B_FALSE;
225185029Spjd	for (dp = list_head(&target->spa_config_list); dp != NULL;
226185029Spjd	    dp = list_next(&target->spa_config_list, dp)) {
227185029Spjd		spa_t *spa = NULL;
228185029Spjd		if (dp->scd_path == NULL)
229185029Spjd			continue;
230185029Spjd
231185029Spjd		/*
232185029Spjd		 * Iterate over all pools, adding any matching pools to 'nvl'.
233185029Spjd		 */
234185029Spjd		nvl = NULL;
235185029Spjd		while ((spa = spa_next(spa)) != NULL) {
236249209Smm			/*
237249209Smm			 * Skip over our own pool if we're about to remove
238249209Smm			 * ourselves from the spa namespace or any pool that
239249209Smm			 * is readonly. Since we cannot guarantee that a
240249209Smm			 * readonly pool would successfully import upon reboot,
241249209Smm			 * we don't allow them to be written to the cache file.
242249209Smm			 */
243249209Smm			if ((spa == target && removing) ||
244249209Smm			    !spa_writeable(spa))
245185029Spjd				continue;
246185029Spjd
247185029Spjd			mutex_enter(&spa->spa_props_lock);
248185029Spjd			tdp = list_head(&spa->spa_config_list);
249185029Spjd			if (spa->spa_config == NULL ||
250185029Spjd			    tdp->scd_path == NULL ||
251185029Spjd			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
252185029Spjd				mutex_exit(&spa->spa_props_lock);
253185029Spjd				continue;
254185029Spjd			}
255185029Spjd
256185029Spjd			if (nvl == NULL)
257185029Spjd				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
258185029Spjd				    KM_SLEEP) == 0);
259185029Spjd
260185029Spjd			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
261185029Spjd			    spa->spa_config) == 0);
262185029Spjd			mutex_exit(&spa->spa_props_lock);
263185029Spjd		}
264185029Spjd
265251636Sdelphij		error = spa_config_write(dp, nvl);
266251636Sdelphij		if (error != 0)
267251636Sdelphij			ccw_failure = B_TRUE;
268185029Spjd		nvlist_free(nvl);
269168404Spjd	}
270168404Spjd
271251636Sdelphij	if (ccw_failure) {
272251636Sdelphij		/*
273251636Sdelphij		 * Keep trying so that configuration data is
274251636Sdelphij		 * written if/when any temporary filesystem
275251636Sdelphij		 * resource issues are resolved.
276251636Sdelphij		 */
277251636Sdelphij		if (target->spa_ccw_fail_time == 0) {
278251636Sdelphij			zfs_ereport_post(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
279251636Sdelphij			    target, NULL, NULL, 0, 0);
280251636Sdelphij		}
281251636Sdelphij		target->spa_ccw_fail_time = gethrtime();
282251636Sdelphij		spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
283251636Sdelphij	} else {
284251636Sdelphij		/*
285251636Sdelphij		 * Do not rate limit future attempts to update
286251636Sdelphij		 * the config cache.
287251636Sdelphij		 */
288251636Sdelphij		target->spa_ccw_fail_time = 0;
289251636Sdelphij	}
290251636Sdelphij
291185029Spjd	/*
292185029Spjd	 * Remove any config entries older than the current one.
293185029Spjd	 */
294185029Spjd	dp = list_head(&target->spa_config_list);
295185029Spjd	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
296185029Spjd		list_remove(&target->spa_config_list, tdp);
297185029Spjd		if (tdp->scd_path != NULL)
298185029Spjd			spa_strfree(tdp->scd_path);
299185029Spjd		kmem_free(tdp, sizeof (spa_config_dirent_t));
300185029Spjd	}
301168404Spjd
302168404Spjd	spa_config_generation++;
303168404Spjd
304185029Spjd	if (postsysevent)
305185029Spjd		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
306168404Spjd}
307168404Spjd
308168404Spjd/*
309168404Spjd * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
310168404Spjd * and we don't want to allow the local zone to see all the pools anyway.
311168404Spjd * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
312168404Spjd * information for all pool visible within the zone.
313168404Spjd */
314168404Spjdnvlist_t *
315168404Spjdspa_all_configs(uint64_t *generation)
316168404Spjd{
317168404Spjd	nvlist_t *pools;
318185029Spjd	spa_t *spa = NULL;
319168404Spjd
320168404Spjd	if (*generation == spa_config_generation)
321168404Spjd		return (NULL);
322168404Spjd
323168404Spjd	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
324168404Spjd
325168404Spjd	mutex_enter(&spa_namespace_lock);
326168404Spjd	while ((spa = spa_next(spa)) != NULL) {
327185029Spjd		if (INGLOBALZONE(curthread) ||
328168404Spjd		    zone_dataset_visible(spa_name(spa), NULL)) {
329185029Spjd			mutex_enter(&spa->spa_props_lock);
330168404Spjd			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
331168404Spjd			    spa->spa_config) == 0);
332185029Spjd			mutex_exit(&spa->spa_props_lock);
333168404Spjd		}
334168404Spjd	}
335185029Spjd	*generation = spa_config_generation;
336168404Spjd	mutex_exit(&spa_namespace_lock);
337168404Spjd
338168404Spjd	return (pools);
339168404Spjd}
340168404Spjd
341168404Spjdvoid
342168404Spjdspa_config_set(spa_t *spa, nvlist_t *config)
343168404Spjd{
344185029Spjd	mutex_enter(&spa->spa_props_lock);
345168404Spjd	if (spa->spa_config != NULL)
346168404Spjd		nvlist_free(spa->spa_config);
347168404Spjd	spa->spa_config = config;
348185029Spjd	mutex_exit(&spa->spa_props_lock);
349168404Spjd}
350168404Spjd
351168404Spjd/*
352168404Spjd * Generate the pool's configuration based on the current in-core state.
353251631Sdelphij *
354168404Spjd * We infer whether to generate a complete config or just one top-level config
355168404Spjd * based on whether vd is the root vdev.
356168404Spjd */
357168404Spjdnvlist_t *
358168404Spjdspa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
359168404Spjd{
360168404Spjd	nvlist_t *config, *nvroot;
361168404Spjd	vdev_t *rvd = spa->spa_root_vdev;
362168498Spjd	unsigned long hostid = 0;
363185029Spjd	boolean_t locked = B_FALSE;
364219089Spjd	uint64_t split_guid;
365168404Spjd
366185029Spjd	if (vd == NULL) {
367168404Spjd		vd = rvd;
368185029Spjd		locked = B_TRUE;
369185029Spjd		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
370185029Spjd	}
371168404Spjd
372185029Spjd	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
373185029Spjd	    (SCL_CONFIG | SCL_STATE));
374185029Spjd
375168404Spjd	/*
376168404Spjd	 * If txg is -1, report the current value of spa->spa_config_txg.
377168404Spjd	 */
378168404Spjd	if (txg == -1ULL)
379168404Spjd		txg = spa->spa_config_txg;
380168404Spjd
381168404Spjd	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
382168404Spjd
383168404Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
384168404Spjd	    spa_version(spa)) == 0);
385168404Spjd	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
386168404Spjd	    spa_name(spa)) == 0);
387168404Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
388168404Spjd	    spa_state(spa)) == 0);
389168404Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
390168404Spjd	    txg) == 0);
391168404Spjd	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
392168404Spjd	    spa_guid(spa)) == 0);
393228103Smm	VERIFY(spa->spa_comment == NULL || nvlist_add_string(config,
394228103Smm	    ZPOOL_CONFIG_COMMENT, spa->spa_comment) == 0);
395228103Smm
396228103Smm
397219089Spjd#ifdef	_KERNEL
398219089Spjd	hostid = zone_get_hostid(NULL);
399219089Spjd#else	/* _KERNEL */
400219089Spjd	/*
401219089Spjd	 * We're emulating the system's hostid in userland, so we can't use
402219089Spjd	 * zone_get_hostid().
403219089Spjd	 */
404168498Spjd	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
405219089Spjd#endif	/* _KERNEL */
406185029Spjd	if (hostid != 0) {
407185029Spjd		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
408185029Spjd		    hostid) == 0);
409185029Spjd	}
410168498Spjd	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
411168498Spjd	    utsname.nodename) == 0);
412168404Spjd
413168404Spjd	if (vd != rvd) {
414168404Spjd		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
415168404Spjd		    vd->vdev_top->vdev_guid) == 0);
416168404Spjd		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
417168404Spjd		    vd->vdev_guid) == 0);
418168404Spjd		if (vd->vdev_isspare)
419168404Spjd			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
420168404Spjd			    1ULL) == 0);
421185029Spjd		if (vd->vdev_islog)
422185029Spjd			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
423185029Spjd			    1ULL) == 0);
424168404Spjd		vd = vd->vdev_top;		/* label contains top config */
425219089Spjd	} else {
426219089Spjd		/*
427219089Spjd		 * Only add the (potentially large) split information
428219089Spjd		 * in the mos config, and not in the vdev labels
429219089Spjd		 */
430219089Spjd		if (spa->spa_config_splitting != NULL)
431219089Spjd			VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
432219089Spjd			    spa->spa_config_splitting) == 0);
433168404Spjd	}
434168404Spjd
435219089Spjd	/*
436219089Spjd	 * Add the top-level config.  We even add this on pools which
437219089Spjd	 * don't support holes in the namespace.
438219089Spjd	 */
439219089Spjd	vdev_top_config_generate(spa, config);
440219089Spjd
441219089Spjd	/*
442219089Spjd	 * If we're splitting, record the original pool's guid.
443219089Spjd	 */
444219089Spjd	if (spa->spa_config_splitting != NULL &&
445219089Spjd	    nvlist_lookup_uint64(spa->spa_config_splitting,
446219089Spjd	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
447219089Spjd		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
448219089Spjd		    split_guid) == 0);
449219089Spjd	}
450219089Spjd
451219089Spjd	nvroot = vdev_config_generate(spa, vd, getstats, 0);
452168404Spjd	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
453168404Spjd	nvlist_free(nvroot);
454168404Spjd
455236884Smm	/*
456236884Smm	 * Store what's necessary for reading the MOS in the label.
457236884Smm	 */
458236884Smm	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
459236884Smm	    spa->spa_label_features) == 0);
460236884Smm
461219089Spjd	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
462219089Spjd		ddt_histogram_t *ddh;
463219089Spjd		ddt_stat_t *dds;
464219089Spjd		ddt_object_t *ddo;
465219089Spjd
466219089Spjd		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
467219089Spjd		ddt_get_dedup_histogram(spa, ddh);
468219089Spjd		VERIFY(nvlist_add_uint64_array(config,
469219089Spjd		    ZPOOL_CONFIG_DDT_HISTOGRAM,
470219089Spjd		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
471219089Spjd		kmem_free(ddh, sizeof (ddt_histogram_t));
472219089Spjd
473219089Spjd		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
474219089Spjd		ddt_get_dedup_object_stats(spa, ddo);
475219089Spjd		VERIFY(nvlist_add_uint64_array(config,
476219089Spjd		    ZPOOL_CONFIG_DDT_OBJ_STATS,
477219089Spjd		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
478219089Spjd		kmem_free(ddo, sizeof (ddt_object_t));
479219089Spjd
480219089Spjd		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
481219089Spjd		ddt_get_dedup_stats(spa, dds);
482219089Spjd		VERIFY(nvlist_add_uint64_array(config,
483219089Spjd		    ZPOOL_CONFIG_DDT_STATS,
484219089Spjd		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
485219089Spjd		kmem_free(dds, sizeof (ddt_stat_t));
486219089Spjd	}
487219089Spjd
488185029Spjd	if (locked)
489185029Spjd		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
490185029Spjd
491168404Spjd	return (config);
492168404Spjd}
493168404Spjd
494168404Spjd/*
495185029Spjd * Update all disk labels, generate a fresh config based on the current
496185029Spjd * in-core state, and sync the global config cache (do not sync the config
497185029Spjd * cache if this is a booting rootpool).
498185029Spjd */
499185029Spjdvoid
500209962Smmspa_config_update(spa_t *spa, int what)
501185029Spjd{
502168404Spjd	vdev_t *rvd = spa->spa_root_vdev;
503168404Spjd	uint64_t txg;
504168404Spjd	int c;
505168404Spjd
506168404Spjd	ASSERT(MUTEX_HELD(&spa_namespace_lock));
507168404Spjd
508185029Spjd	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
509168404Spjd	txg = spa_last_synced_txg(spa) + 1;
510168404Spjd	if (what == SPA_CONFIG_UPDATE_POOL) {
511168404Spjd		vdev_config_dirty(rvd);
512168404Spjd	} else {
513168404Spjd		/*
514168404Spjd		 * If we have top-level vdevs that were added but have
515168404Spjd		 * not yet been prepared for allocation, do that now.
516168404Spjd		 * (It's safe now because the config cache is up to date,
517168404Spjd		 * so it will be able to translate the new DVAs.)
518168404Spjd		 * See comments in spa_vdev_add() for full details.
519168404Spjd		 */
520168404Spjd		for (c = 0; c < rvd->vdev_children; c++) {
521168404Spjd			vdev_t *tvd = rvd->vdev_child[c];
522254591Sgibbs			if (tvd->vdev_ms_array == 0) {
523254591Sgibbs				vdev_ashift_optimize(tvd);
524219089Spjd				vdev_metaslab_set_size(tvd);
525254591Sgibbs			}
526219089Spjd			vdev_expand(tvd, txg);
527168404Spjd		}
528168404Spjd	}
529185029Spjd	spa_config_exit(spa, SCL_ALL, FTAG);
530168404Spjd
531168404Spjd	/*
532168404Spjd	 * Wait for the mosconfig to be regenerated and synced.
533168404Spjd	 */
534168404Spjd	txg_wait_synced(spa->spa_dsl_pool, txg);
535168404Spjd
536168404Spjd	/*
537168404Spjd	 * Update the global config cache to reflect the new mosconfig.
538168404Spjd	 */
539209962Smm	if (!spa->spa_is_root)
540185029Spjd		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
541168404Spjd
542168404Spjd	if (what == SPA_CONFIG_UPDATE_POOL)
543209962Smm		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
544168404Spjd}
545