1145516Sdarrenr/*
2145516Sdarrenr * CDDL HEADER START
3145516Sdarrenr *
4255332Scy * The contents of this file are subject to the terms of the
5145516Sdarrenr * Common Development and Distribution License (the "License").
6145516Sdarrenr * You may not use this file except in compliance with the License.
7145516Sdarrenr *
8145516Sdarrenr * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9145516Sdarrenr * or http://www.opensolaris.org/os/licensing.
10255332Scy * See the License for the specific language governing permissions
11145516Sdarrenr * and limitations under the License.
12145516Sdarrenr *
13145516Sdarrenr * When distributing Covered Code, include this CDDL HEADER in each
14145516Sdarrenr * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15145516Sdarrenr * If applicable, add the following below this CDDL HEADER, with the
16145516Sdarrenr * fields enclosed by brackets "[]" replaced with your own identifying
17145516Sdarrenr * information: Portions Copyright [yyyy] [name of copyright owner]
18145516Sdarrenr *
19145516Sdarrenr * CDDL HEADER END
20145516Sdarrenr */
21145516Sdarrenr
22145516Sdarrenr/*
23145516Sdarrenr * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24145516Sdarrenr * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25145516Sdarrenr * Copyright (c) 2013 by Delphix. All rights reserved.
26145516Sdarrenr */
27145516Sdarrenr
28145516Sdarrenr#include <sys/zfs_context.h>
29145516Sdarrenr#include <sys/spa.h>
30145516Sdarrenr#include <sys/fm/fs/zfs.h>
31145516Sdarrenr#include <sys/spa_impl.h>
32145516Sdarrenr#include <sys/nvpair.h>
33145516Sdarrenr#include <sys/uio.h>
34145516Sdarrenr#include <sys/fs/zfs.h>
35145516Sdarrenr#include <sys/vdev_impl.h>
36255332Scy#include <sys/zfs_ioctl.h>
37255332Scy#include <sys/utsname.h>
38145516Sdarrenr#include <sys/sunddi.h>
39145516Sdarrenr#include <sys/zfeature.h>
40145516Sdarrenr#ifdef _KERNEL
41145516Sdarrenr#include <sys/kobj.h>
42170268Sdarrenr#include <sys/zone.h>
43195699Srwatson#endif
44145516Sdarrenr
45145516Sdarrenr/*
46145516Sdarrenr * Pool configuration repository.
47170268Sdarrenr *
48145516Sdarrenr * Pool configuration is stored as a packed nvlist on the filesystem.  By
49145516Sdarrenr * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
50145516Sdarrenr * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
51145516Sdarrenr * property set that allows them to be stored in an alternate location until
52145516Sdarrenr * the control of external software.
53145516Sdarrenr *
54145516Sdarrenr * For each cache file, we have a single nvlist which holds all the
55145516Sdarrenr * configuration information.  When the module loads, we read this information
56145516Sdarrenr * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
57145516Sdarrenr * maintained independently in spa.c.  Whenever the namespace is modified, or
58145516Sdarrenr * the configuration of a pool is changed, we call spa_config_sync(), which
59145516Sdarrenr * walks through all the active pools and writes the configuration to disk.
60145516Sdarrenr */
61145516Sdarrenr
62145516Sdarrenrstatic uint64_t spa_config_generation = 1;
63145516Sdarrenr
64145516Sdarrenr/*
65145516Sdarrenr * This can be overridden in userland to preserve an alternate namespace for
66145516Sdarrenr * userland pools when doing testing.
67145516Sdarrenr */
68145516Sdarrenrconst char *spa_config_path = ZPOOL_CACHE;
69145516Sdarrenr
70145516Sdarrenr/*
71145516Sdarrenr * Called when the module is first loaded, this routine loads the configuration
72255332Scy * file into the SPA namespace.  It does not actually open or load the pools; it
73255332Scy * only populates the namespace.
74145516Sdarrenr */
75145516Sdarrenrvoid
76145516Sdarrenrspa_config_load(void)
77145516Sdarrenr{
78145516Sdarrenr	void *buf = NULL;
79145516Sdarrenr	nvlist_t *nvlist, *child;
80145516Sdarrenr	nvpair_t *nvpair;
81145516Sdarrenr	char *pathname;
82145516Sdarrenr	struct _buf *file;
83145516Sdarrenr	uint64_t fsize;
84145516Sdarrenr
85145516Sdarrenr	/*
86145516Sdarrenr	 * Open the configuration file.
87151897Srwatson	 */
88145516Sdarrenr	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
89145516Sdarrenr
90145516Sdarrenr	(void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path);
91255332Scy
92255332Scy	file = kobj_open_file(pathname);
93255332Scy
94255332Scy	kmem_free(pathname, MAXPATHLEN);
95145516Sdarrenr
96145516Sdarrenr	if (file == (struct _buf *)-1)
97255332Scy		return;
98145516Sdarrenr
99145516Sdarrenr	if (kobj_get_filesize(file, &fsize) != 0)
100145516Sdarrenr		goto out;
101145516Sdarrenr
102255332Scy	buf = kmem_alloc(fsize, KM_SLEEP);
103145516Sdarrenr
104255332Scy	/*
105145516Sdarrenr	 * Read the nvlist from the file.
106255332Scy	 */
107145516Sdarrenr	if (kobj_read_file(file, buf, fsize, 0) < 0)
108145516Sdarrenr		goto out;
109153876Sguido
110153876Sguido	/*
111153876Sguido	 * Unpack the nvlist.
112153876Sguido	 */
113153876Sguido	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
114255332Scy		goto out;
115153876Sguido
116255332Scy	/*
117153876Sguido	 * Iterate over all elements in the nvlist, creating a new spa_t for
118153876Sguido	 * each one with the specified configuration.
119153876Sguido	 */
120145516Sdarrenr	mutex_enter(&spa_namespace_lock);
121145516Sdarrenr	nvpair = NULL;
122255332Scy	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
123145516Sdarrenr		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
124145516Sdarrenr			continue;
125255332Scy
126255332Scy		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
127255332Scy
128255332Scy		if (spa_lookup(nvpair_name(nvpair)) != NULL)
129255332Scy			continue;
130255332Scy		(void) spa_add(nvpair_name(nvpair), child, NULL);
131255332Scy	}
132255332Scy	mutex_exit(&spa_namespace_lock);
133255332Scy
134255332Scy	nvlist_free(nvlist);
135255332Scy
136255332Scyout:
137255332Scy	if (buf != NULL)
138255332Scy		kmem_free(buf, fsize);
139255332Scy
140255332Scy	kobj_close_file(file);
141255332Scy}
142255332Scy
143255332Scystatic int
144145516Sdarrenrspa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
145145516Sdarrenr{
146145516Sdarrenr	size_t buflen;
147145516Sdarrenr	char *buf;
148145516Sdarrenr	vnode_t *vp;
149145516Sdarrenr	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
150255332Scy	char *temp;
151145516Sdarrenr	int err;
152255332Scy
153255332Scy	/*
154145516Sdarrenr	 * If the nvlist is empty (NULL), then remove the old cachefile.
155145516Sdarrenr	 */
156145516Sdarrenr	if (nvl == NULL) {
157255332Scy		err = vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
158255332Scy		return (err);
159145516Sdarrenr	}
160145516Sdarrenr
161145516Sdarrenr	/*
162145516Sdarrenr	 * Pack the configuration into a buffer.
163145516Sdarrenr	 */
164145516Sdarrenr	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
165145516Sdarrenr
166145516Sdarrenr	buf = kmem_alloc(buflen, KM_SLEEP);
167255332Scy	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
168255332Scy
169255332Scy	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
170145516Sdarrenr	    KM_SLEEP) == 0);
171255332Scy
172255332Scy	/*
173255332Scy	 * Write the configuration to disk.  We need to do the traditional
174255332Scy	 * 'write to temporary file, sync, move over original' to make sure we
175255332Scy	 * always have a consistent view of the data.
176255332Scy	 */
177255332Scy	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
178255332Scy
179255332Scy	err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
180255332Scy	if (err == 0) {
181255332Scy		err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
182255332Scy		    0, RLIM64_INFINITY, kcred, NULL);
183255332Scy		if (err == 0)
184255332Scy			err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
185255332Scy		if (err == 0)
186255332Scy			err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
187255332Scy		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
188255332Scy	}
189255332Scy
190255332Scy	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
191255332Scy
192255332Scy	kmem_free(buf, buflen);
193255332Scy	kmem_free(temp, MAXPATHLEN);
194255332Scy	return (err);
195255332Scy}
196145516Sdarrenr
197145516Sdarrenr/*
198145516Sdarrenr * Synchronize pool configuration to disk.  This must be called with the
199145516Sdarrenr * namespace lock held. Synchronizing the pool cache is typically done after
200145516Sdarrenr * the configuration has been synced to the MOS. This exposes a window where
201255332Scy * the MOS config will have been updated but the cache file has not. If
202145516Sdarrenr * the system were to crash at that instant then the cached config may not
203145516Sdarrenr * contain the correct information to open the pool and an explicity import
204145516Sdarrenr * would be required.
205145516Sdarrenr */
206255332Scyvoid
207145516Sdarrenrspa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
208145516Sdarrenr{
209145516Sdarrenr	spa_config_dirent_t *dp, *tdp;
210145516Sdarrenr	nvlist_t *nvl;
211145516Sdarrenr	boolean_t ccw_failure;
212255332Scy	int error;
213255332Scy
214255332Scy	ASSERT(MUTEX_HELD(&spa_namespace_lock));
215145516Sdarrenr
216145516Sdarrenr	if (rootdir == NULL || !(spa_mode_global & FWRITE))
217255332Scy		return;
218255332Scy
219145516Sdarrenr	/*
220255332Scy	 * Iterate over all cachefiles for the pool, past or present.  When the
221181803Sbz	 * cachefile is changed, the new one is pushed onto this list, allowing
222145516Sdarrenr	 * us to update previous cachefiles that no longer contain this pool.
223255332Scy	 */
224255332Scy	ccw_failure = B_FALSE;
225145516Sdarrenr	for (dp = list_head(&target->spa_config_list); dp != NULL;
226255332Scy	    dp = list_next(&target->spa_config_list, dp)) {
227255332Scy		spa_t *spa = NULL;
228145516Sdarrenr		if (dp->scd_path == NULL)
229145516Sdarrenr			continue;
230145516Sdarrenr
231145516Sdarrenr		/*
232145516Sdarrenr		 * Iterate over all pools, adding any matching pools to 'nvl'.
233145516Sdarrenr		 */
234145516Sdarrenr		nvl = NULL;
235145516Sdarrenr		while ((spa = spa_next(spa)) != NULL) {
236255332Scy			/*
237255332Scy			 * Skip over our own pool if we're about to remove
238255332Scy			 * ourselves from the spa namespace or any pool that
239145516Sdarrenr			 * is readonly. Since we cannot guarantee that a
240145516Sdarrenr			 * readonly pool would successfully import upon reboot,
241145516Sdarrenr			 * we don't allow them to be written to the cache file.
242145516Sdarrenr			 */
243255332Scy			if ((spa == target && removing) ||
244255332Scy			    !spa_writeable(spa))
245181803Sbz				continue;
246145516Sdarrenr
247145516Sdarrenr			mutex_enter(&spa->spa_props_lock);
248145516Sdarrenr			tdp = list_head(&spa->spa_config_list);
249255332Scy			if (spa->spa_config == NULL ||
250255332Scy			    tdp->scd_path == NULL ||
251255332Scy			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
252145516Sdarrenr				mutex_exit(&spa->spa_props_lock);
253145516Sdarrenr				continue;
254255332Scy			}
255255332Scy
256255332Scy			if (nvl == NULL)
257145516Sdarrenr				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
258145516Sdarrenr				    KM_SLEEP) == 0);
259255332Scy
260145516Sdarrenr			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
261255332Scy			    spa->spa_config) == 0);
262145516Sdarrenr			mutex_exit(&spa->spa_props_lock);
263145516Sdarrenr		}
264145516Sdarrenr
265145516Sdarrenr		error = spa_config_write(dp, nvl);
266145516Sdarrenr		if (error != 0)
267145516Sdarrenr			ccw_failure = B_TRUE;
268145516Sdarrenr		nvlist_free(nvl);
269145516Sdarrenr	}
270145516Sdarrenr
271145516Sdarrenr	if (ccw_failure) {
272255332Scy		/*
273255332Scy		 * Keep trying so that configuration data is
274145516Sdarrenr		 * written if/when any temporary filesystem
275255332Scy		 * resource issues are resolved.
276192895Sjamie		 */
277170268Sdarrenr		if (target->spa_ccw_fail_time == 0) {
278255332Scy			zfs_ereport_post(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
279255332Scy			    target, NULL, NULL, 0, 0);
280255332Scy		}
281255332Scy		target->spa_ccw_fail_time = gethrtime();
282145516Sdarrenr		spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
283170268Sdarrenr	} else {
284170268Sdarrenr		/*
285145516Sdarrenr		 * Do not rate limit future attempts to update
286255332Scy		 * the config cache.
287255332Scy		 */
288255332Scy		target->spa_ccw_fail_time = 0;
289255332Scy	}
290145516Sdarrenr
291255332Scy	/*
292145516Sdarrenr	 * Remove any config entries older than the current one.
293145516Sdarrenr	 */
294145516Sdarrenr	dp = list_head(&target->spa_config_list);
295255332Scy	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
296255332Scy		list_remove(&target->spa_config_list, tdp);
297145516Sdarrenr		if (tdp->scd_path != NULL)
298255332Scy			spa_strfree(tdp->scd_path);
299145516Sdarrenr		kmem_free(tdp, sizeof (spa_config_dirent_t));
300255332Scy	}
301255332Scy
302255332Scy	spa_config_generation++;
303145516Sdarrenr
304255332Scy	if (postsysevent)
305145516Sdarrenr		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
306173181Sdarrenr}
307255332Scy
308255332Scy/*
309255332Scy * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
310145516Sdarrenr * and we don't want to allow the local zone to see all the pools anyway.
311255332Scy * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
312145516Sdarrenr * information for all pool visible within the zone.
313145516Sdarrenr */
314145516Sdarrenrnvlist_t *
315145516Sdarrenrspa_all_configs(uint64_t *generation)
316255332Scy{
317145516Sdarrenr	nvlist_t *pools;
318145516Sdarrenr	spa_t *spa = NULL;
319145516Sdarrenr
320145516Sdarrenr	if (*generation == spa_config_generation)
321145516Sdarrenr		return (NULL);
322145516Sdarrenr
323161356Sguido	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
324145516Sdarrenr
325145516Sdarrenr	mutex_enter(&spa_namespace_lock);
326145516Sdarrenr	while ((spa = spa_next(spa)) != NULL) {
327145516Sdarrenr		if (INGLOBALZONE(curthread) ||
328145516Sdarrenr		    zone_dataset_visible(spa_name(spa), NULL)) {
329255332Scy			mutex_enter(&spa->spa_props_lock);
330145516Sdarrenr			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
331145516Sdarrenr			    spa->spa_config) == 0);
332255332Scy			mutex_exit(&spa->spa_props_lock);
333255332Scy		}
334255332Scy	}
335145516Sdarrenr	*generation = spa_config_generation;
336145516Sdarrenr	mutex_exit(&spa_namespace_lock);
337145516Sdarrenr
338145516Sdarrenr	return (pools);
339145516Sdarrenr}
340145516Sdarrenr
341145516Sdarrenrvoid
342145516Sdarrenrspa_config_set(spa_t *spa, nvlist_t *config)
343145516Sdarrenr{
344145516Sdarrenr	mutex_enter(&spa->spa_props_lock);
345145516Sdarrenr	if (spa->spa_config != NULL)
346145516Sdarrenr		nvlist_free(spa->spa_config);
347145516Sdarrenr	spa->spa_config = config;
348255332Scy	mutex_exit(&spa->spa_props_lock);
349145516Sdarrenr}
350145516Sdarrenr
351145516Sdarrenr/*
352145516Sdarrenr * Generate the pool's configuration based on the current in-core state.
353145516Sdarrenr *
354145516Sdarrenr * We infer whether to generate a complete config or just one top-level config
355145516Sdarrenr * based on whether vd is the root vdev.
356145516Sdarrenr */
357145516Sdarrenrnvlist_t *
358145516Sdarrenrspa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
359145516Sdarrenr{
360145516Sdarrenr	nvlist_t *config, *nvroot;
361145516Sdarrenr	vdev_t *rvd = spa->spa_root_vdev;
362145516Sdarrenr	unsigned long hostid = 0;
363145516Sdarrenr	boolean_t locked = B_FALSE;
364145516Sdarrenr	uint64_t split_guid;
365145516Sdarrenr
366145516Sdarrenr	if (vd == NULL) {
367145516Sdarrenr		vd = rvd;
368145516Sdarrenr		locked = B_TRUE;
369145516Sdarrenr		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
370145516Sdarrenr	}
371145516Sdarrenr
372145516Sdarrenr	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
373145516Sdarrenr	    (SCL_CONFIG | SCL_STATE));
374145516Sdarrenr
375145516Sdarrenr	/*
376145516Sdarrenr	 * If txg is -1, report the current value of spa->spa_config_txg.
377145516Sdarrenr	 */
378145516Sdarrenr	if (txg == -1ULL)
379145516Sdarrenr		txg = spa->spa_config_txg;
380145516Sdarrenr
381145516Sdarrenr	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
382145516Sdarrenr
383145516Sdarrenr	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
384145516Sdarrenr	    spa_version(spa)) == 0);
385145516Sdarrenr	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
386145516Sdarrenr	    spa_name(spa)) == 0);
387145516Sdarrenr	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
388145516Sdarrenr	    spa_state(spa)) == 0);
389145516Sdarrenr	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
390145516Sdarrenr	    txg) == 0);
391145516Sdarrenr	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
392145516Sdarrenr	    spa_guid(spa)) == 0);
393145516Sdarrenr	VERIFY(spa->spa_comment == NULL || nvlist_add_string(config,
394145516Sdarrenr	    ZPOOL_CONFIG_COMMENT, spa->spa_comment) == 0);
395145516Sdarrenr
396145516Sdarrenr
397145516Sdarrenr#ifdef	_KERNEL
398145516Sdarrenr	hostid = zone_get_hostid(NULL);
399145516Sdarrenr#else	/* _KERNEL */
400145516Sdarrenr	/*
401145516Sdarrenr	 * We're emulating the system's hostid in userland, so we can't use
402145516Sdarrenr	 * zone_get_hostid().
403145516Sdarrenr	 */
404145516Sdarrenr	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
405145516Sdarrenr#endif	/* _KERNEL */
406145516Sdarrenr	if (hostid != 0) {
407145516Sdarrenr		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
408145516Sdarrenr		    hostid) == 0);
409145516Sdarrenr	}
410145516Sdarrenr	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
411145516Sdarrenr	    utsname.nodename) == 0);
412145516Sdarrenr
413255332Scy	if (vd != rvd) {
414255332Scy		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
415145516Sdarrenr		    vd->vdev_top->vdev_guid) == 0);
416145516Sdarrenr		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
417255332Scy		    vd->vdev_guid) == 0);
418145516Sdarrenr		if (vd->vdev_isspare)
419145516Sdarrenr			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
420145516Sdarrenr			    1ULL) == 0);
421145516Sdarrenr		if (vd->vdev_islog)
422145516Sdarrenr			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
423145516Sdarrenr			    1ULL) == 0);
424145516Sdarrenr		vd = vd->vdev_top;		/* label contains top config */
425255332Scy	} else {
426255332Scy		/*
427145516Sdarrenr		 * Only add the (potentially large) split information
428145516Sdarrenr		 * in the mos config, and not in the vdev labels
429145516Sdarrenr		 */
430255332Scy		if (spa->spa_config_splitting != NULL)
431255332Scy			VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
432255332Scy			    spa->spa_config_splitting) == 0);
433255332Scy	}
434255332Scy
435255332Scy	/*
436255332Scy	 * Add the top-level config.  We even add this on pools which
437145516Sdarrenr	 * don't support holes in the namespace.
438145516Sdarrenr	 */
439145516Sdarrenr	vdev_top_config_generate(spa, config);
440145516Sdarrenr
441145516Sdarrenr	/*
442145516Sdarrenr	 * If we're splitting, record the original pool's guid.
443145516Sdarrenr	 */
444255332Scy	if (spa->spa_config_splitting != NULL &&
445145516Sdarrenr	    nvlist_lookup_uint64(spa->spa_config_splitting,
446145516Sdarrenr	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
447145516Sdarrenr		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
448145516Sdarrenr		    split_guid) == 0);
449145516Sdarrenr	}
450255332Scy
451255332Scy	nvroot = vdev_config_generate(spa, vd, getstats, 0);
452145516Sdarrenr	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
453255332Scy	nvlist_free(nvroot);
454255332Scy
455145516Sdarrenr	/*
456145516Sdarrenr	 * Store what's necessary for reading the MOS in the label.
457145516Sdarrenr	 */
458255332Scy	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
459255332Scy	    spa->spa_label_features) == 0);
460145516Sdarrenr
461145516Sdarrenr	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
462145516Sdarrenr		ddt_histogram_t *ddh;
463181803Sbz		ddt_stat_t *dds;
464145516Sdarrenr		ddt_object_t *ddo;
465145516Sdarrenr
466145516Sdarrenr		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
467145516Sdarrenr		ddt_get_dedup_histogram(spa, ddh);
468145516Sdarrenr		VERIFY(nvlist_add_uint64_array(config,
469145516Sdarrenr		    ZPOOL_CONFIG_DDT_HISTOGRAM,
470145516Sdarrenr		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
471145516Sdarrenr		kmem_free(ddh, sizeof (ddt_histogram_t));
472145516Sdarrenr
473145516Sdarrenr		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
474255332Scy		ddt_get_dedup_object_stats(spa, ddo);
475255332Scy		VERIFY(nvlist_add_uint64_array(config,
476145516Sdarrenr		    ZPOOL_CONFIG_DDT_OBJ_STATS,
477255332Scy		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
478145516Sdarrenr		kmem_free(ddo, sizeof (ddt_object_t));
479145516Sdarrenr
480145516Sdarrenr		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
481145516Sdarrenr		ddt_get_dedup_stats(spa, dds);
482145516Sdarrenr		VERIFY(nvlist_add_uint64_array(config,
483145516Sdarrenr		    ZPOOL_CONFIG_DDT_STATS,
484145516Sdarrenr		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
485145516Sdarrenr		kmem_free(dds, sizeof (ddt_stat_t));
486145516Sdarrenr	}
487145516Sdarrenr
488145516Sdarrenr	if (locked)
489145516Sdarrenr		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
490145516Sdarrenr
491145516Sdarrenr	return (config);
492255332Scy}
493145516Sdarrenr
494145516Sdarrenr/*
495255332Scy * Update all disk labels, generate a fresh config based on the current
496145516Sdarrenr * in-core state, and sync the global config cache (do not sync the config
497255332Scy * cache if this is a booting rootpool).
498145516Sdarrenr */
499145516Sdarrenrvoid
500145516Sdarrenrspa_config_update(spa_t *spa, int what)
501255332Scy{
502255332Scy	vdev_t *rvd = spa->spa_root_vdev;
503255332Scy	uint64_t txg;
504255332Scy	int c;
505255332Scy
506145516Sdarrenr	ASSERT(MUTEX_HELD(&spa_namespace_lock));
507145516Sdarrenr
508145516Sdarrenr	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
509145516Sdarrenr	txg = spa_last_synced_txg(spa) + 1;
510145516Sdarrenr	if (what == SPA_CONFIG_UPDATE_POOL) {
511255332Scy		vdev_config_dirty(rvd);
512145516Sdarrenr	} else {
513145516Sdarrenr		/*
514145516Sdarrenr		 * If we have top-level vdevs that were added but have
515145516Sdarrenr		 * not yet been prepared for allocation, do that now.
516145516Sdarrenr		 * (It's safe now because the config cache is up to date,
517145516Sdarrenr		 * so it will be able to translate the new DVAs.)
518172776Sdarrenr		 * See comments in spa_vdev_add() for full details.
519145516Sdarrenr		 */
520145516Sdarrenr		for (c = 0; c < rvd->vdev_children; c++) {
521145516Sdarrenr			vdev_t *tvd = rvd->vdev_child[c];
522145516Sdarrenr			if (tvd->vdev_ms_array == 0) {
523255332Scy				vdev_ashift_optimize(tvd);
524255332Scy				vdev_metaslab_set_size(tvd);
525255332Scy			}
526255332Scy			vdev_expand(tvd, txg);
527255332Scy		}
528255332Scy	}
529255332Scy	spa_config_exit(spa, SCL_ALL, FTAG);
530145516Sdarrenr
531145516Sdarrenr	/*
532145516Sdarrenr	 * Wait for the mosconfig to be regenerated and synced.
533255332Scy	 */
534145516Sdarrenr	txg_wait_synced(spa->spa_dsl_pool, txg);
535145516Sdarrenr
536145516Sdarrenr	/*
537145516Sdarrenr	 * Update the global config cache to reflect the new mosconfig.
538145516Sdarrenr	 */
539145516Sdarrenr	if (!spa->spa_is_root)
540145516Sdarrenr		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
541145516Sdarrenr
542145516Sdarrenr	if (what == SPA_CONFIG_UPDATE_POOL)
543145516Sdarrenr		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
544145516Sdarrenr}
545145516Sdarrenr