1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
2199653d4eschrock
22fa9e406ahrens/*
233f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
248704186Dan McDonald * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2511f6a96Serapheim Dimitropoulos * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26ce1577bDave Eddy * Copyright 2017 Joyent, Inc.
27fa9e406ahrens */
28fa9e406ahrens
29fa9e406ahrens#include <sys/spa.h>
303cb69f7Will Andrews#include <sys/fm/fs/zfs.h>
31fa9e406ahrens#include <sys/spa_impl.h>
32fa9e406ahrens#include <sys/nvpair.h>
33fa9e406ahrens#include <sys/uio.h>
34fa9e406ahrens#include <sys/fs/zfs.h>
35fa9e406ahrens#include <sys/vdev_impl.h>
36fa9e406ahrens#include <sys/zfs_ioctl.h>
379517395ek#include <sys/utsname.h>
389517395ek#include <sys/systeminfo.h>
399517395ek#include <sys/sunddi.h>
40ad135b5Christopher Siden#include <sys/zfeature.h>
41ea8dc4beschrock#ifdef _KERNEL
42ea8dc4beschrock#include <sys/kobj.h>
435679c89jv#include <sys/zone.h>
44ea8dc4beschrock#endif
45ea8dc4beschrock
46fa9e406ahrens/*
47fa9e406ahrens * Pool configuration repository.
48fa9e406ahrens *
492f8aaabeschrock * Pool configuration is stored as a packed nvlist on the filesystem.  By
502f8aaabeschrock * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
512f8aaabeschrock * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
522f8aaabeschrock * property set that allows them to be stored in an alternate location until
532f8aaabeschrock * the control of external software.
54fa9e406ahrens *
552f8aaabeschrock * For each cache file, we have a single nvlist which holds all the
562f8aaabeschrock * configuration information.  When the module loads, we read this information
572f8aaabeschrock * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
582f8aaabeschrock * maintained independently in spa.c.  Whenever the namespace is modified, or
595cabbc6Prashanth Sreenivasa * the configuration of a pool is changed, we call spa_write_cachefile(), which
602f8aaabeschrock * walks through all the active pools and writes the configuration to disk.
61fa9e406ahrens */
62fa9e406ahrens
63fa9e406ahrensstatic uint64_t spa_config_generation = 1;
64fa9e406ahrens
65fa9e406ahrens/*
66fa9e406ahrens * This can be overridden in userland to preserve an alternate namespace for
67fa9e406ahrens * userland pools when doing testing.
68fa9e406ahrens */
69c5904d1eschrockconst char *spa_config_path = ZPOOL_CACHE;
70fa9e406ahrens
71fa9e406ahrens/*
72fa9e406ahrens * Called when the module is first loaded, this routine loads the configuration
73fa9e406ahrens * file into the SPA namespace.  It does not actually open or load the pools; it
74fa9e406ahrens * only populates the namespace.
75fa9e406ahrens */
76fa9e406ahrensvoid
77fa9e406ahrensspa_config_load(void)
78fa9e406ahrens{
79fa9e406ahrens	void *buf = NULL;
80fa9e406ahrens	nvlist_t *nvlist, *child;
81fa9e406ahrens	nvpair_t *nvpair;
82e14bb32Jeff Bonwick	char *pathname;
83ea8dc4beschrock	struct _buf *file;
84b1b8ab3lling	uint64_t fsize;
85fa9e406ahrens
86fa9e406ahrens	/*
87fa9e406ahrens	 * Open the configuration file.
88fa9e406ahrens	 */
89e14bb32Jeff Bonwick	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
90e14bb32Jeff Bonwick
91e14bb32Jeff Bonwick	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
92c5904d1eschrock	    (rootdir != NULL) ? "./" : "", spa_config_path);
93ea8dc4beschrock
94ea8dc4beschrock	file = kobj_open_file(pathname);
95e14bb32Jeff Bonwick
96e14bb32Jeff Bonwick	kmem_free(pathname, MAXPATHLEN);
97e14bb32Jeff Bonwick
98ea8dc4beschrock	if (file == (struct _buf *)-1)
99fa9e406ahrens		return;
100fa9e406ahrens
101b1b8ab3lling	if (kobj_get_filesize(file, &fsize) != 0)
102fa9e406ahrens		goto out;
103fa9e406ahrens
104b1b8ab3lling	buf = kmem_alloc(fsize, KM_SLEEP);
105fa9e406ahrens
106ea8dc4beschrock	/*
107ea8dc4beschrock	 * Read the nvlist from the file.
108ea8dc4beschrock	 */
109b1b8ab3lling	if (kobj_read_file(file, buf, fsize, 0) < 0)
110fa9e406ahrens		goto out;
111fa9e406ahrens
112fa9e406ahrens	/*
113fa9e406ahrens	 * Unpack the nvlist.
114fa9e406ahrens	 */
115b1b8ab3lling	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
116fa9e406ahrens		goto out;
117fa9e406ahrens
118fa9e406ahrens	/*
119fa9e406ahrens	 * Iterate over all elements in the nvlist, creating a new spa_t for
120fa9e406ahrens	 * each one with the specified configuration.
121fa9e406ahrens	 */
122fa9e406ahrens	mutex_enter(&spa_namespace_lock);
123fa9e406ahrens	nvpair = NULL;
124fa9e406ahrens	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
125fa9e406ahrens		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
126fa9e406ahrens			continue;
127fa9e406ahrens
128215198aJoe Stein		child = fnvpair_value_nvlist(nvpair);
129fa9e406ahrens
130fa9e406ahrens		if (spa_lookup(nvpair_name(nvpair)) != NULL)
131fa9e406ahrens			continue;
132468c413Tim Haley		(void) spa_add(nvpair_name(nvpair), child, NULL);
133fa9e406ahrens	}
134fa9e406ahrens	mutex_exit(&spa_namespace_lock);
135fa9e406ahrens
136fa9e406ahrens	nvlist_free(nvlist);
137fa9e406ahrens
138fa9e406ahrensout:
139fa9e406ahrens	if (buf != NULL)
140b1b8ab3lling		kmem_free(buf, fsize);
141fa9e406ahrens
142ea8dc4beschrock	kobj_close_file(file);
143fa9e406ahrens}
144fa9e406ahrens
1453cb69f7Will Andrewsstatic int
146c5904d1eschrockspa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1472f8aaabeschrock{
1482f8aaabeschrock	size_t buflen;
1492f8aaabeschrock	char *buf;
1502f8aaabeschrock	vnode_t *vp;
1512f8aaabeschrock	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
152e14bb32Jeff Bonwick	char *temp;
1533cb69f7Will Andrews	int err;
154c5904d1eschrock
155c5904d1eschrock	/*
156c5904d1eschrock	 * If the nvlist is empty (NULL), then remove the old cachefile.
157c5904d1eschrock	 */
158c5904d1eschrock	if (nvl == NULL) {
1593cb69f7Will Andrews		err = vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
1603cb69f7Will Andrews		return (err);
161c5904d1eschrock	}
1622f8aaabeschrock
163fa9e406ahrens	/*
164fa9e406ahrens	 * Pack the configuration into a buffer.
165fa9e406ahrens	 */
166215198aJoe Stein	buf = fnvlist_pack(nvl, &buflen);
167e14bb32Jeff Bonwick	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
168fa9e406ahrens
169fa9e406ahrens	/*
170fa9e406ahrens	 * Write the configuration to disk.  We need to do the traditional
171fa9e406ahrens	 * 'write to temporary file, sync, move over original' to make sure we
172fa9e406ahrens	 * always have a consistent view of the data.
173fa9e406ahrens	 */
174e14bb32Jeff Bonwick	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
175fa9e406ahrens
1763cb69f7Will Andrews	err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
1773cb69f7Will Andrews	if (err == 0) {
1783cb69f7Will Andrews		err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
1793cb69f7Will Andrews		    0, RLIM64_INFINITY, kcred, NULL);
1803cb69f7Will Andrews		if (err == 0)
1813cb69f7Will Andrews			err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
1823cb69f7Will Andrews		if (err == 0)
1833cb69f7Will Andrews			err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
184e14bb32Jeff Bonwick		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
185e14bb32Jeff Bonwick		VN_RELE(vp);
186fa9e406ahrens	}
187fa9e406ahrens
188e14bb32Jeff Bonwick	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
189fa9e406ahrens
190215198aJoe Stein	fnvlist_pack_free(buf, buflen);
191e14bb32Jeff Bonwick	kmem_free(temp, MAXPATHLEN);
1923cb69f7Will Andrews	return (err);
1932f8aaabeschrock}
1942f8aaabeschrock
1952f8aaabeschrock/*
196c5904d1eschrock * Synchronize pool configuration to disk.  This must be called with the
197b4952e1George Wilson * namespace lock held. Synchronizing the pool cache is typically done after
198b4952e1George Wilson * the configuration has been synced to the MOS. This exposes a window where
199b4952e1George Wilson * the MOS config will have been updated but the cache file has not. If
200b4952e1George Wilson * the system were to crash at that instant then the cached config may not
2015cabbc6Prashanth Sreenivasa * contain the correct information to open the pool and an explicit import
202b4952e1George Wilson * would be required.
2032f8aaabeschrock */
2042f8aaabeschrockvoid
2055cabbc6Prashanth Sreenivasaspa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent)
2062f8aaabeschrock{
207c5904d1eschrock	spa_config_dirent_t *dp, *tdp;
208c5904d1eschrock	nvlist_t *nvl;
2093cb69f7Will Andrews	boolean_t ccw_failure;
2103cb69f7Will Andrews	int error;
21104e5635Andriy Gapon	char *pool_name;
2122f8aaabeschrock
2132f8aaabeschrock	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2142f8aaabeschrock
2154f0f5e5Victor Latushkin	if (rootdir == NULL || !(spa_mode_global & FWRITE))
2169fddc96George Wilson		return;
2179fddc96George Wilson
218c5904d1eschrock	/*
219c5904d1eschrock	 * Iterate over all cachefiles for the pool, past or present.  When the
220c5904d1eschrock	 * cachefile is changed, the new one is pushed onto this list, allowing
221c5904d1eschrock	 * us to update previous cachefiles that no longer contain this pool.
222c5904d1eschrock	 */
2233cb69f7Will Andrews	ccw_failure = B_FALSE;
224c5904d1eschrock	for (dp = list_head(&target->spa_config_list); dp != NULL;
225c5904d1eschrock	    dp = list_next(&target->spa_config_list, dp)) {
226e14bb32Jeff Bonwick		spa_t *spa = NULL;
227c5904d1eschrock		if (dp->scd_path == NULL)
228c5904d1eschrock			continue;
229c5904d1eschrock
230c5904d1eschrock		/*
231c5904d1eschrock		 * Iterate over all pools, adding any matching pools to 'nvl'.
232c5904d1eschrock		 */
233c5904d1eschrock		nvl = NULL;
234c5904d1eschrock		while ((spa = spa_next(spa)) != NULL) {
235fb02ae0George Wilson			/*
236fb02ae0George Wilson			 * Skip over our own pool if we're about to remove
237fb02ae0George Wilson			 * ourselves from the spa namespace or any pool that
238fb02ae0George Wilson			 * is readonly. Since we cannot guarantee that a
239fb02ae0George Wilson			 * readonly pool would successfully import upon reboot,
240fb02ae0George Wilson			 * we don't allow them to be written to the cache file.
241fb02ae0George Wilson			 */
242fb02ae0George Wilson			if ((spa == target && removing) ||
243fb02ae0George Wilson			    !spa_writeable(spa))
244c5904d1eschrock				continue;
245c5904d1eschrock
246e14bb32Jeff Bonwick			mutex_enter(&spa->spa_props_lock);
247c5904d1eschrock			tdp = list_head(&spa->spa_config_list);
248e14bb32Jeff Bonwick			if (spa->spa_config == NULL ||
249e14bb32Jeff Bonwick			    tdp->scd_path == NULL ||
250e14bb32Jeff Bonwick			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
251e14bb32Jeff Bonwick				mutex_exit(&spa->spa_props_lock);
252c5904d1eschrock				continue;
253e14bb32Jeff Bonwick			}
254c5904d1eschrock
255c5904d1eschrock			if (nvl == NULL)
256215198aJoe Stein				nvl = fnvlist_alloc();
257c5904d1eschrock
25804e5635Andriy Gapon			if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
25904e5635Andriy Gapon				pool_name = fnvlist_lookup_string(
26004e5635Andriy Gapon				    spa->spa_config, ZPOOL_CONFIG_POOL_NAME);
26104e5635Andriy Gapon			} else {
26204e5635Andriy Gapon				pool_name = spa_name(spa);
26304e5635Andriy Gapon			}
26404e5635Andriy Gapon
26504e5635Andriy Gapon			fnvlist_add_nvlist(nvl, pool_name,
266215198aJoe Stein			    spa->spa_config);
267e14bb32Jeff Bonwick			mutex_exit(&spa->spa_props_lock);
268c5904d1eschrock		}
269c5904d1eschrock
2703cb69f7Will Andrews		error = spa_config_write(dp, nvl);
2713cb69f7Will Andrews		if (error != 0)
2723cb69f7Will Andrews			ccw_failure = B_TRUE;
273c5904d1eschrock		nvlist_free(nvl);
274c5904d1eschrock	}
2752f8aaabeschrock
2763cb69f7Will Andrews	if (ccw_failure) {
2773cb69f7Will Andrews		/*
2783cb69f7Will Andrews		 * Keep trying so that configuration data is
2793cb69f7Will Andrews		 * written if/when any temporary filesystem
2803cb69f7Will Andrews		 * resource issues are resolved.
2813cb69f7Will Andrews		 */
2823cb69f7Will Andrews		if (target->spa_ccw_fail_time == 0) {
2833cb69f7Will Andrews			zfs_ereport_post(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
284eb63303Tom Caputi			    target, NULL, NULL, NULL, 0, 0);
2853cb69f7Will Andrews		}
2863cb69f7Will Andrews		target->spa_ccw_fail_time = gethrtime();
2873cb69f7Will Andrews		spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
2883cb69f7Will Andrews	} else {
2893cb69f7Will Andrews		/*
2903cb69f7Will Andrews		 * Do not rate limit future attempts to update
2913cb69f7Will Andrews		 * the config cache.
2923cb69f7Will Andrews		 */
2933cb69f7Will Andrews		target->spa_ccw_fail_time = 0;
2943cb69f7Will Andrews	}
2953cb69f7Will Andrews
2962f8aaabeschrock	/*
297c5904d1eschrock	 * Remove any config entries older than the current one.
2982f8aaabeschrock	 */
299c5904d1eschrock	dp = list_head(&target->spa_config_list);
300c5904d1eschrock	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
301c5904d1eschrock		list_remove(&target->spa_config_list, tdp);
302c5904d1eschrock		if (tdp->scd_path != NULL)
303c5904d1eschrock			spa_strfree(tdp->scd_path);
304c5904d1eschrock		kmem_free(tdp, sizeof (spa_config_dirent_t));
3052f8aaabeschrock	}
3062f8aaabeschrock
3072f8aaabeschrock	spa_config_generation++;
308c5904d1eschrock
309c5904d1eschrock	if (postsysevent)
310ce1577bDave Eddy		spa_event_notify(target, NULL, NULL, ESC_ZFS_CONFIG_SYNC);
311fa9e406ahrens}
312fa9e406ahrens
313fa9e406ahrens/*
3140373e76bonwick * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
315fa9e406ahrens * and we don't want to allow the local zone to see all the pools anyway.
316fa9e406ahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
317fa9e406ahrens * information for all pool visible within the zone.
318fa9e406ahrens */
319fa9e406ahrensnvlist_t *
320fa9e406ahrensspa_all_configs(uint64_t *generation)
321fa9e406ahrens{
322fa9e406ahrens	nvlist_t *pools;
323e14bb32Jeff Bonwick	spa_t *spa = NULL;
324fa9e406ahrens
325fa9e406ahrens	if (*generation == spa_config_generation)
326fa9e406ahrens		return (NULL);
327fa9e406ahrens
328215198aJoe Stein	pools = fnvlist_alloc();
329fa9e406ahrens
330fa9e406ahrens	mutex_enter(&spa_namespace_lock);
331fa9e406ahrens	while ((spa = spa_next(spa)) != NULL) {
332fa9e406ahrens		if (INGLOBALZONE(curproc) ||
333fa9e406ahrens		    zone_dataset_visible(spa_name(spa), NULL)) {
334e14bb32Jeff Bonwick			mutex_enter(&spa->spa_props_lock);
335215198aJoe Stein			fnvlist_add_nvlist(pools, spa_name(spa),
336215198aJoe Stein			    spa->spa_config);
337e14bb32Jeff Bonwick			mutex_exit(&spa->spa_props_lock);
338fa9e406ahrens		}
339fa9e406ahrens	}
340fa9e406ahrens	*generation = spa_config_generation;
341e14bb32Jeff Bonwick	mutex_exit(&spa_namespace_lock);
342fa9e406ahrens
343fa9e406ahrens	return (pools);
344fa9e406ahrens}
345fa9e406ahrens
346fa9e406ahrensvoid
347fa9e406ahrensspa_config_set(spa_t *spa, nvlist_t *config)
348fa9e406ahrens{
349e14bb32Jeff Bonwick	mutex_enter(&spa->spa_props_lock);
3506f79381Pavel Zakharov	if (spa->spa_config != NULL && spa->spa_config != config)
3516f79381Pavel Zakharov		nvlist_free(spa->spa_config);
352fa9e406ahrens	spa->spa_config = config;
353e14bb32Jeff Bonwick	mutex_exit(&spa->spa_props_lock);
354fa9e406ahrens}
355fa9e406ahrens
356fa9e406ahrens/*
357fa9e406ahrens * Generate the pool's configuration based on the current in-core state.
358f717074Will Andrews *
359fa9e406ahrens * We infer whether to generate a complete config or just one top-level config
360fa9e406ahrens * based on whether vd is the root vdev.
361fa9e406ahrens */
362fa9e406ahrensnvlist_t *
363fa9e406ahrensspa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
364fa9e406ahrens{
365fa9e406ahrens	nvlist_t *config, *nvroot;
366fa9e406ahrens	vdev_t *rvd = spa->spa_root_vdev;
3679517395ek	unsigned long hostid = 0;
368e14bb32Jeff Bonwick	boolean_t locked = B_FALSE;
3691195e68Mark J Musante	uint64_t split_guid;
37004e5635Andriy Gapon	char *pool_name;
371fa9e406ahrens
372e14bb32Jeff Bonwick	if (vd == NULL) {
373fa9e406ahrens		vd = rvd;
374e14bb32Jeff Bonwick		locked = B_TRUE;
375e14bb32Jeff Bonwick		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
376e14bb32Jeff Bonwick	}
377e14bb32Jeff Bonwick
378e14bb32Jeff Bonwick	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
379e14bb32Jeff Bonwick	    (SCL_CONFIG | SCL_STATE));
380fa9e406ahrens
381fa9e406ahrens	/*
382fa9e406ahrens	 * If txg is -1, report the current value of spa->spa_config_txg.
383fa9e406ahrens	 */
384fa9e406ahrens	if (txg == -1ULL)
385fa9e406ahrens		txg = spa->spa_config_txg;
386fa9e406ahrens
38704e5635Andriy Gapon	/*
38804e5635Andriy Gapon	 * Originally, users had to handle spa namespace collisions by either
38904e5635Andriy Gapon	 * exporting the already imported pool or by specifying a new name for
39004e5635Andriy Gapon	 * the pool with a conflicting name. In the case of root pools from
39104e5635Andriy Gapon	 * virtual guests, neither approach to collision resolution is
39204e5635Andriy Gapon	 * reasonable. This is addressed by extending the new name syntax with
39304e5635Andriy Gapon	 * an option to specify that the new name is temporary. When specified,
39404e5635Andriy Gapon	 * ZFS_IMPORT_TEMP_NAME will be set in spa->spa_import_flags to tell us
39504e5635Andriy Gapon	 * to use the previous name, which we do below.
39604e5635Andriy Gapon	 */
39704e5635Andriy Gapon	if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
39804e5635Andriy Gapon		pool_name = fnvlist_lookup_string(spa->spa_config,
39904e5635Andriy Gapon		    ZPOOL_CONFIG_POOL_NAME);
40004e5635Andriy Gapon	} else {
40104e5635Andriy Gapon		pool_name = spa_name(spa);
40204e5635Andriy Gapon	}
40304e5635Andriy Gapon
404215198aJoe Stein	config = fnvlist_alloc();
4058704186Dan McDonald
406215198aJoe Stein	fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
40704e5635Andriy Gapon	fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, pool_name);
408215198aJoe Stein	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, spa_state(spa));
409215198aJoe Stein	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, txg);
410215198aJoe Stein	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
411eb63303Tom Caputi	fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, spa->spa_errata);
412215198aJoe Stein	if (spa->spa_comment != NULL) {
413215198aJoe Stein		fnvlist_add_string(config, ZPOOL_CONFIG_COMMENT,
414215198aJoe Stein		    spa->spa_comment);
415215198aJoe Stein	}
4168704186Dan McDonald
417e0f1c0aOlaf Faaland	hostid = spa_get_hostid();
41817194a5lling	if (hostid != 0) {
419215198aJoe Stein		fnvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, hostid);
42017194a5lling	}
421215198aJoe Stein	fnvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, utsname.nodename);
422fa9e406ahrens
423215198aJoe Stein	int config_gen_flags = 0;
424fa9e406ahrens	if (vd != rvd) {
425215198aJoe Stein		fnvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
426215198aJoe Stein		    vd->vdev_top->vdev_guid);
427215198aJoe Stein		fnvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
428215198aJoe Stein		    vd->vdev_guid);
429215198aJoe Stein		if (vd->vdev_isspare) {
430215198aJoe Stein			fnvlist_add_uint64(config,
431215198aJoe Stein			    ZPOOL_CONFIG_IS_SPARE, 1ULL);
432215198aJoe Stein		}
433215198aJoe Stein		if (vd->vdev_islog) {
434215198aJoe Stein			fnvlist_add_uint64(config,
435215198aJoe Stein			    ZPOOL_CONFIG_IS_LOG, 1ULL);
436215198aJoe Stein		}
437fa9e406ahrens		vd = vd->vdev_top;		/* label contains top config */
4381195e68Mark J Musante	} else {
4391195e68Mark J Musante		/*
4401195e68Mark J Musante		 * Only add the (potentially large) split information
4411195e68Mark J Musante		 * in the mos config, and not in the vdev labels
4421195e68Mark J Musante		 */
4431195e68Mark J Musante		if (spa->spa_config_splitting != NULL)
444215198aJoe Stein			fnvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
445215198aJoe Stein			    spa->spa_config_splitting);
446215198aJoe Stein		fnvlist_add_boolean(config,
447215198aJoe Stein		    ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS);
448215198aJoe Stein
449215198aJoe Stein		config_gen_flags |= VDEV_CONFIG_MOS;
450fa9e406ahrens	}
451fa9e406ahrens
45288ecc94George Wilson	/*
45388ecc94George Wilson	 * Add the top-level config.  We even add this on pools which
4544b964adGeorge Wilson	 * don't support holes in the namespace.
45588ecc94George Wilson	 */
45688ecc94George Wilson	vdev_top_config_generate(spa, config);
45788ecc94George Wilson
4581195e68Mark J Musante	/*
4591195e68Mark J Musante	 * If we're splitting, record the original pool's guid.
4601195e68Mark J Musante	 */
4611195e68Mark J Musante	if (spa->spa_config_splitting != NULL &&
4621195e68Mark J Musante	    nvlist_lookup_uint64(spa->spa_config_splitting,
4631195e68Mark J Musante	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
464215198aJoe Stein		fnvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
465215198aJoe Stein		    split_guid);
4661195e68Mark J Musante	}
4671195e68Mark J Musante
468215198aJoe Stein	nvroot = vdev_config_generate(spa, vd, getstats, config_gen_flags);
469215198aJoe Stein	fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot);
470fa9e406ahrens	nvlist_free(nvroot);
471fa9e406ahrens
472ad135b5Christopher Siden	/*
473ad135b5Christopher Siden	 * Store what's necessary for reading the MOS in the label.
474ad135b5Christopher Siden	 */
475215198aJoe Stein	fnvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
476215198aJoe Stein	    spa->spa_label_features);
477ad135b5Christopher Siden
4789eb19f4George Wilson	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
4799eb19f4George Wilson		ddt_histogram_t *ddh;
4809eb19f4George Wilson		ddt_stat_t *dds;
4819eb19f4George Wilson		ddt_object_t *ddo;
4829eb19f4George Wilson
4839eb19f4George Wilson		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
4849eb19f4George Wilson		ddt_get_dedup_histogram(spa, ddh);
485215198aJoe Stein		fnvlist_add_uint64_array(config,
4869eb19f4George Wilson		    ZPOOL_CONFIG_DDT_HISTOGRAM,
487215198aJoe Stein		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t));
4889eb19f4George Wilson		kmem_free(ddh, sizeof (ddt_histogram_t));
4899eb19f4George Wilson
4909eb19f4George Wilson		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
4919eb19f4George Wilson		ddt_get_dedup_object_stats(spa, ddo);
492215198aJoe Stein		fnvlist_add_uint64_array(config,
4939eb19f4George Wilson		    ZPOOL_CONFIG_DDT_OBJ_STATS,
494215198aJoe Stein		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t));
4959eb19f4George Wilson		kmem_free(ddo, sizeof (ddt_object_t));
4969eb19f4George Wilson
4979eb19f4George Wilson		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
4989eb19f4George Wilson		ddt_get_dedup_stats(spa, dds);
499215198aJoe Stein		fnvlist_add_uint64_array(config,
5009eb19f4George Wilson		    ZPOOL_CONFIG_DDT_STATS,
501215198aJoe Stein		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t));
5029eb19f4George Wilson		kmem_free(dds, sizeof (ddt_stat_t));
5039eb19f4George Wilson	}
5049eb19f4George Wilson
505e14bb32Jeff Bonwick	if (locked)
506e14bb32Jeff Bonwick		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
507e14bb32Jeff Bonwick
508fa9e406ahrens	return (config);
509fa9e406ahrens}
5100373e76bonwick
5110373e76bonwick/*
512e7cbe64gw * Update all disk labels, generate a fresh config based on the current
513e7cbe64gw * in-core state, and sync the global config cache (do not sync the config
514e7cbe64gw * cache if this is a booting rootpool).
515e7cbe64gw */
516e7cbe64gwvoid
517bc75843Lin Lingspa_config_update(spa_t *spa, int what)
518e7cbe64gw{
5190373e76bonwick	vdev_t *rvd = spa->spa_root_vdev;
5200373e76bonwick	uint64_t txg;
5210373e76bonwick	int c;
5220373e76bonwick
5230373e76bonwick	ASSERT(MUTEX_HELD(&spa_namespace_lock));
5240373e76bonwick
525e14bb32Jeff Bonwick	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5260373e76bonwick	txg = spa_last_synced_txg(spa) + 1;
5270373e76bonwick	if (what == SPA_CONFIG_UPDATE_POOL) {
5280373e76bonwick		vdev_config_dirty(rvd);
5290373e76bonwick	} else {
5300373e76bonwick		/*
5310373e76bonwick		 * If we have top-level vdevs that were added but have
5320373e76bonwick		 * not yet been prepared for allocation, do that now.
5330373e76bonwick		 * (It's safe now because the config cache is up to date,
5340373e76bonwick		 * so it will be able to translate the new DVAs.)
5350373e76bonwick		 * See comments in spa_vdev_add() for full details.
5360373e76bonwick		 */
5370373e76bonwick		for (c = 0; c < rvd->vdev_children; c++) {
5380373e76bonwick			vdev_t *tvd = rvd->vdev_child[c];
53911f6a96Serapheim Dimitropoulos
54011f6a96Serapheim Dimitropoulos			/*
54111f6a96Serapheim Dimitropoulos			 * Explicitly skip vdevs that are indirect or
54211f6a96Serapheim Dimitropoulos			 * log vdevs that are being removed. The reason
54311f6a96Serapheim Dimitropoulos			 * is that both of those can have vdev_ms_array
54411f6a96Serapheim Dimitropoulos			 * set to 0 and we wouldn't want to change their
54511f6a96Serapheim Dimitropoulos			 * metaslab size nor call vdev_expand() on them.
54611f6a96Serapheim Dimitropoulos			 */
54711f6a96Serapheim Dimitropoulos			if (!vdev_is_concrete(tvd) ||
54811f6a96Serapheim Dimitropoulos			    (tvd->vdev_islog && tvd->vdev_removing))
54911f6a96Serapheim Dimitropoulos				continue;
55011f6a96Serapheim Dimitropoulos
551573ca77George Wilson			if (tvd->vdev_ms_array == 0)
552573ca77George Wilson				vdev_metaslab_set_size(tvd);
553573ca77George Wilson			vdev_expand(tvd, txg);
5540373e76bonwick		}
5550373e76bonwick	}
556e14bb32Jeff Bonwick	spa_config_exit(spa, SCL_ALL, FTAG);
5570373e76bonwick
5580373e76bonwick	/*
5590373e76bonwick	 * Wait for the mosconfig to be regenerated and synced.
5600373e76bonwick	 */
5610373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, txg);
5620373e76bonwick
5630373e76bonwick	/*
5640373e76bonwick	 * Update the global config cache to reflect the new mosconfig.
5650373e76bonwick	 */
5665cabbc6Prashanth Sreenivasa	if (!spa->spa_is_root) {
5675cabbc6Prashanth Sreenivasa		spa_write_cachefile(spa, B_FALSE,
5685cabbc6Prashanth Sreenivasa		    what != SPA_CONFIG_UPDATE_POOL);
5695cabbc6Prashanth Sreenivasa	}
5700373e76bonwick
5710373e76bonwick	if (what == SPA_CONFIG_UPDATE_POOL)
572bc75843Lin Ling		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
5730373e76bonwick}
574