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